mirror of
https://github.com/meekrosoft/fff
synced 2026-01-23 08:25:59 +01:00
Moved global variables into struct so they don't pollute the global namespace
This commit is contained in:
@@ -1,10 +1,3 @@
|
||||
/*
|
||||
* UI.c
|
||||
*
|
||||
* Created on: Dec 17, 2010
|
||||
* Author: mlong
|
||||
*/
|
||||
|
||||
#include "UI.h"
|
||||
#include "DISPLAY.h"
|
||||
#include "SYSTEM.h"
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
/*
|
||||
* UI.h
|
||||
*
|
||||
* Created on: Dec 17, 2010
|
||||
* Author: mlong
|
||||
*/
|
||||
|
||||
#ifndef UI_H_
|
||||
#define UI_H_
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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");
|
||||
|
||||
40
fakegen.rb
40
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
|
||||
|
||||
|
||||
|
||||
24
fff3.h
24
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 \
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ void setup()
|
||||
RESET_FAKE(enumfunc0);
|
||||
RESET_FAKE(structfunc0);
|
||||
|
||||
RESET_HISTORY();
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
RESET_FAKE(voidfunc1);
|
||||
RESET_FAKE(voidfunc2);
|
||||
RESET_FAKE(longfunc0);
|
||||
RESET_HISTORY();
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user