KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm a C++ developer and when it comes to testing, it's easy to test a class by injecting dependencies, overriding member functions, and so on, so that you can test edge cases easily. However, in C, you can't use those wonderful features. I'm finding it hard to add unit tests to code because of some of the 'standard' ways that C code is written. What are the best ways to tackle the following: Passing around a large 'context' struct pointer: void some_func( global_context_t *ctx, .... ) { /* lots of code, depending on the state of context */ } No easy way to test failure on dependent functions: void some_func( .... ) { if (!get_network_state() && !some_other_func()) { do_something_func(); .... } ... } Functions with lots of parameters: void some_func( global_context_t *, int i, int j, other_struct_t *t, out_param_t **out, ...) { /* hundreds and hundreds of lines of code */ } Static or hidden functions: static void foo( ... ) { /* some code */ } void some_public_func( ... } { /* call static functions */ foo( ... ); }
Tags (comma-separated)
Save Edits
Cancel