From 3d6cfd218b54158ba79a4e31639677c751fbe3d3 Mon Sep 17 00:00:00 2001 From: Mike Long Date: Tue, 29 May 2012 07:46:21 +0800 Subject: [PATCH] Moved global variables into struct so they don't pollute the global namespace --- examples/UI.c | 7 ------ examples/UI.h | 7 ------ examples/UI_test_ansic.c | 8 +++---- examples/UI_test_cpp.cpp | 8 +++---- examples/test_suite_template.c | 10 ++------- fakegen.rb | 40 ++++++++++++++++++++++++---------- fff3.h | 24 +++++++++++--------- test/fff_test_c.c | 4 ++-- test/fff_test_cpp.cpp | 4 ++-- test/fff_test_global_c.c | 2 +- test/fff_test_global_cpp.cpp | 2 +- test/test_cases.include | 18 +++++++-------- 12 files changed, 68 insertions(+), 66 deletions(-) diff --git a/examples/UI.c b/examples/UI.c index 32b3478..8ce996e 100644 --- a/examples/UI.c +++ b/examples/UI.c @@ -1,10 +1,3 @@ -/* - * UI.c - * - * Created on: Dec 17, 2010 - * Author: mlong - */ - #include "UI.h" #include "DISPLAY.h" #include "SYSTEM.h" diff --git a/examples/UI.h b/examples/UI.h index 6d16805..8a3fb5c 100644 --- a/examples/UI.h +++ b/examples/UI.h @@ -1,10 +1,3 @@ -/* - * UI.h - * - * Created on: Dec 17, 2010 - * Author: mlong - */ - #ifndef UI_H_ #define UI_H_ diff --git a/examples/UI_test_ansic.c b/examples/UI_test_ansic.c index c9916e2..b49b378 100644 --- a/examples/UI_test_ansic.c +++ b/examples/UI_test_ansic.c @@ -40,7 +40,7 @@ void setup() RESET_FAKE(button_press_cbk); - RESET_HISTORY(); + FFF_RESET_HISTORY(); DISPLAY_get_line_capacity_fake.return_val = 2; } @@ -105,9 +105,9 @@ TEST_F(UITests, when_no_empty_lines_write_line_clears_screen_and_outputs_lines_t assert(DISPLAY_output_fake.call_count == 1); // Check the order of the calls: Don't care about the first two: // DISPLAY_get_line_capacity and DISPLAY_get_line_insert_index - assert(call_history_idx == 4); - assert(call_history[2] == (void *) DISPLAY_clear); - assert(call_history[3] == (void *) DISPLAY_output); + assert(fff.call_history_idx == 4); + assert(fff.call_history[2] == (void *) DISPLAY_clear); + assert(fff.call_history[3] == (void *) DISPLAY_output); } TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen) diff --git a/examples/UI_test_cpp.cpp b/examples/UI_test_cpp.cpp index 847552b..e8994f0 100644 --- a/examples/UI_test_cpp.cpp +++ b/examples/UI_test_cpp.cpp @@ -38,7 +38,7 @@ public: RESET_FAKE(button_press_cbk); - RESET_HISTORY(); + FFF_RESET_HISTORY(); // non default init DISPLAY_get_line_capacity_fake.return_val = 2; } @@ -106,9 +106,9 @@ TEST_F(UITests, when_no_empty_lines_write_line_clears_screen_and_outputs_lines_t ASSERT_EQ(DISPLAY_output_fake.call_count, 1); // Check the order of the calls: Don't care about the first two: // DISPLAY_get_line_capacity and DISPLAY_get_line_insert_index - ASSERT_EQ(call_history_idx, 4); - ASSERT_EQ(call_history[2], (void *) DISPLAY_clear); - ASSERT_EQ(call_history[3], (void *) DISPLAY_output); + ASSERT_EQ(fff.call_history_idx, 4); + ASSERT_EQ(fff.call_history[2], (void *) DISPLAY_clear); + ASSERT_EQ(fff.call_history[3], (void *) DISPLAY_output); } TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen) diff --git a/examples/test_suite_template.c b/examples/test_suite_template.c index 2c469da..19600af 100644 --- a/examples/test_suite_template.c +++ b/examples/test_suite_template.c @@ -1,10 +1,4 @@ -#include -#include - -void setup(); -#define TEST_F(SUITE, NAME) void NAME() -#define RUN_TEST(TESTNAME) printf(" Running %s: ", #TESTNAME); fflush(0); setup(); TESTNAME(); printf(" SUCCESS\n"); - +#include "../test/c_test_framework.h" /* Initialializers called for every test */ void setup() @@ -29,7 +23,7 @@ int main() fflush(0); /* Run tests */ - RUN_TEST(hello_world); + RUN_TEST(GreeterTests, hello_world); printf("\n-------------\n"); diff --git a/fakegen.rb b/fakegen.rb index c4d50a9..cadc12a 100644 --- a/fakegen.rb +++ b/fakegen.rb @@ -67,7 +67,6 @@ end def define_declare_all_func_common_helper putd "" - # todo remove funcname putd "#define DECLARE_ALL_FUNC_COMMON \\" putd " unsigned int call_count; \\" putd " unsigned int arg_history_len;\\" @@ -332,19 +331,38 @@ end def define_fff_globals - putd "extern void * call_history[FFF_CALL_HISTORY_LEN];" - putd "extern unsigned int call_history_idx;" - putd "void RESET_HISTORY();" + + + putd "typedef struct { " + putd " void * call_history[FFF_CALL_HISTORY_LEN];" + putd " unsigned int call_history_idx;" + putd "} fff_globals_t;" + putd "" + putd "EXTERN_C \\" + putd "extern fff_globals_t fff;" + putd "END_EXTERN_C \\" putd "" putd "#define DEFINE_FFF_GLOBALS \\" - putd " void * call_history[FFF_CALL_HISTORY_LEN]; \\" - putd " unsigned int call_history_idx; \\" - putd " void RESET_HISTORY() { \\" - putd " call_history_idx = 0; \\" - putd " } \\" - putd "" + putd " EXTERN_C \\" + putd " fff_globals_t fff; \\" + putd " END_EXTERN_C" + putd "#define FFF_RESET_HISTORY() fff.call_history_idx = 0;" + + +# putd "extern void * call_history[FFF_CALL_HISTORY_LEN];" +# putd "extern unsigned int call_history_idx;" +# putd "void RESET_HISTORY();" +# putd "" +# putd "#define DEFINE_FFF_GLOBALS \\" +# putd " void * call_history[FFF_CALL_HISTORY_LEN]; \\" +# putd " unsigned int call_history_idx; \\" +# putd " void RESET_HISTORY() { \\" +# putd " call_history_idx = 0; \\" +# putd " } \\" +# putd "" putd "#define REGISTER_CALL(function) \\" - putd " if(call_history_idx < FFF_CALL_HISTORY_LEN) call_history[call_history_idx++] = (void *)function;" + putd " if(fff.call_history_idx < FFF_CALL_HISTORY_LEN) \\" + putd " fff.call_history[fff.call_history_idx++] = (void *)function;" end diff --git a/fff3.h b/fff3.h index e20512c..75aed04 100644 --- a/fff3.h +++ b/fff3.h @@ -67,19 +67,23 @@ #endif /* cpp/ansi c */ /* -- END INTERNAL HELPER MACROS -- */ -extern void * call_history[FFF_CALL_HISTORY_LEN]; -extern unsigned int call_history_idx; -void RESET_HISTORY(); +typedef struct { + void * call_history[FFF_CALL_HISTORY_LEN]; + unsigned int call_history_idx; +} fff_globals_t; + +EXTERN_C \ +extern fff_globals_t fff; +END_EXTERN_C \ #define DEFINE_FFF_GLOBALS \ - void * call_history[FFF_CALL_HISTORY_LEN]; \ - unsigned int call_history_idx; \ - void RESET_HISTORY() { \ - call_history_idx = 0; \ - } \ - + EXTERN_C \ + fff_globals_t fff; \ + END_EXTERN_C +#define FFF_RESET_HISTORY() fff.call_history_idx = 0; #define REGISTER_CALL(function) \ - if(call_history_idx < FFF_CALL_HISTORY_LEN) call_history[call_history_idx++] = (void *)function; + if(fff.call_history_idx < FFF_CALL_HISTORY_LEN) \ + fff.call_history[fff.call_history_idx++] = (void *)function; #define DECLARE_FAKE_VOID_FUNC0(FUNCNAME) \ EXTERN_C \ diff --git a/test/fff_test_c.c b/test/fff_test_c.c index fbaeba8..1f08023 100644 --- a/test/fff_test_c.c +++ b/test/fff_test_c.c @@ -36,7 +36,7 @@ void setup() RESET_FAKE(longfunc0); RESET_FAKE(enumfunc0); RESET_FAKE(structfunc0); - RESET_HISTORY(); + FFF_RESET_HISTORY(); } @@ -44,7 +44,7 @@ void setup() TEST_F(FFFTestSuite, default_constants_can_be_overridden) { - unsigned sizeCallHistory = (sizeof call_history) / (sizeof call_history[0]); + unsigned sizeCallHistory = (sizeof fff.call_history) / (sizeof fff.call_history[0]); ASSERT_EQ(OVERRIDE_CALL_HIST_LEN, sizeCallHistory); ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_fake.arg_history_len); } diff --git a/test/fff_test_cpp.cpp b/test/fff_test_cpp.cpp index 50d6207..b5a4a23 100644 --- a/test/fff_test_cpp.cpp +++ b/test/fff_test_cpp.cpp @@ -29,7 +29,7 @@ public: RESET_FAKE(voidfunc1); RESET_FAKE(voidfunc2); RESET_FAKE(longfunc0); - RESET_HISTORY(); + FFF_RESET_HISTORY(); } }; @@ -37,7 +37,7 @@ public: TEST_F(FFFTestSuite, default_constants_can_be_overridden) { - unsigned sizeCallHistory = (sizeof call_history) / (sizeof call_history[0]); + unsigned sizeCallHistory = (sizeof fff.call_history) / (sizeof fff.call_history[0]); ASSERT_EQ(OVERRIDE_CALL_HIST_LEN, sizeCallHistory); ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_fake.arg_history_len); } diff --git a/test/fff_test_global_c.c b/test/fff_test_global_c.c index 23e4c95..4c0e252 100644 --- a/test/fff_test_global_c.c +++ b/test/fff_test_global_c.c @@ -14,7 +14,7 @@ void setup() RESET_FAKE(enumfunc0); RESET_FAKE(structfunc0); - RESET_HISTORY(); + FFF_RESET_HISTORY(); } diff --git a/test/fff_test_global_cpp.cpp b/test/fff_test_global_cpp.cpp index fc08506..dfe1e88 100644 --- a/test/fff_test_global_cpp.cpp +++ b/test/fff_test_global_cpp.cpp @@ -14,7 +14,7 @@ public: RESET_FAKE(voidfunc1); RESET_FAKE(voidfunc2); RESET_FAKE(longfunc0); - RESET_HISTORY(); + FFF_RESET_HISTORY(); } }; diff --git a/test/test_cases.include b/test/test_cases.include index 0d4cdcd..4943859 100644 --- a/test/test_cases.include +++ b/test/test_cases.include @@ -135,7 +135,7 @@ TEST_F(FFFTestSuite, value_func_will_return_zero_after_reset) TEST_F(FFFTestSuite, register_call_macro_registers_one_call) { REGISTER_CALL(longfunc0); - ASSERT_EQ(call_history[0], (void *)longfunc0); + ASSERT_EQ(fff.call_history[0], (void *)longfunc0); } TEST_F(FFFTestSuite, register_call_macro_registers_two_calls) @@ -143,18 +143,18 @@ TEST_F(FFFTestSuite, register_call_macro_registers_two_calls) REGISTER_CALL(longfunc0); REGISTER_CALL(voidfunc2); - ASSERT_EQ(call_history[0], (void *)longfunc0); - ASSERT_EQ(call_history[1], (void *)voidfunc2); + ASSERT_EQ(fff.call_history[0], (void *)longfunc0); + ASSERT_EQ(fff.call_history[1], (void *)voidfunc2); } TEST_F(FFFTestSuite, reset_call_history_resets_call_history) { REGISTER_CALL(longfunc0); - RESET_HISTORY(); + FFF_RESET_HISTORY(); REGISTER_CALL(voidfunc2); - ASSERT_EQ(1u, call_history_idx); - ASSERT_EQ(call_history[0], (void *)voidfunc2); + ASSERT_EQ(1u, fff.call_history_idx); + ASSERT_EQ(fff.call_history[0], (void *)voidfunc2); } TEST_F(FFFTestSuite, call_history_will_not_write_past_array_bounds) @@ -163,14 +163,14 @@ TEST_F(FFFTestSuite, call_history_will_not_write_past_array_bounds) { REGISTER_CALL(longfunc0); } - ASSERT_EQ(FFF_CALL_HISTORY_LEN, call_history_idx); + ASSERT_EQ(FFF_CALL_HISTORY_LEN, fff.call_history_idx); } TEST_F(FFFTestSuite, calling_fake_registers_one_call) { longfunc0(); - ASSERT_EQ(call_history_idx, 1u); - ASSERT_EQ(call_history[0], (void *)longfunc0); + ASSERT_EQ(fff.call_history_idx, 1u); + ASSERT_EQ(fff.call_history[0], (void *)longfunc0); } TEST_F(FFFTestSuite, return_value_sequences_not_exhausted)