From e34f577a4f692c9e5320e7f9507183a2b66889f9 Mon Sep 17 00:00:00 2001 From: Mike Long Date: Tue, 26 Apr 2011 17:20:48 +0100 Subject: [PATCH] New feature: no longer need to specify argument count in the declaration --- fakegen.rb | 54 ++++++++++++++++++++++++++++++++++++++++++- fff.h | 41 +++++++++++++++++++++++++------- test/fff_test_c.c | 11 +++++---- test/fff_test_cpp.cpp | 6 ++--- 4 files changed, 95 insertions(+), 17 deletions(-) diff --git a/fakegen.rb b/fakegen.rb index e140cb3..7185689 100644 --- a/fakegen.rb +++ b/fakegen.rb @@ -223,9 +223,60 @@ def include_guard puts "#endif // FAKE_FUNCTIONS" end +def output_macro_counting_shortcuts + puts <<-MACRO_COUNTING + +#define PP_NARG_MINUS2(...) \ + PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()) + +#define PP_NARG_MINUS2_(...) \ + PP_ARG_MINUS2_N(__VA_ARGS__) + +#define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N + +#define PP_RSEQ_N_MINUS2() \ + 9,8,7,6,5,4,3,2,1,0 + + +#define FAKE_VALUE_FUNC(...) \ + FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) + +#define FUNC_VALUE_(N,...) \ + FUNC_VALUE_N(N,__VA_ARGS__) + +#define FUNC_VALUE_N(N,...) \ + FAKE_VALUE_FUNC ## N(__VA_ARGS__) + + + +#define PP_NARG_MINUS1(...) \ + PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()) + +#define PP_NARG_MINUS1_(...) \ + PP_ARG_MINUS1_N(__VA_ARGS__) + +#define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N + +#define PP_RSEQ_N_MINUS1() \ + 9,8,7,6,5,4,3,2,1,0 + +#define FAKE_VOID_FUNC(...) \ + FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) + +#define FUNC_VOID_(N,...) \ + FUNC_VOID_N(N,__VA_ARGS__) + +#define FUNC_VOID_N(N,...) \ + FAKE_VOID_FUNC ## N(__VA_ARGS__) + + MACRO_COUNTING +end + def output_c_and_cpp include_guard { + output_constants + puts "#ifdef __cplusplus" $cpp_output = true yield @@ -235,12 +286,13 @@ def output_c_and_cpp $cpp_output = false yield puts "#endif /* cpp/ansi c */" + + output_macro_counting_shortcuts } end # lets generate!! output_c_and_cpp{ - output_constants define_reset_fake define_call_history define_return_sequence diff --git a/fff.h b/fff.h index 9e7e7d9..ef44fbf 100644 --- a/fff.h +++ b/fff.h @@ -1,7 +1,6 @@ #ifndef FAKE_FUNCTIONS #define FAKE_FUNCTIONS -#ifdef __cplusplus #define FFF_MAX_ARGS (10u) #ifndef FFF_ARG_HISTORY_LEN #define FFF_ARG_HISTORY_LEN (50u) @@ -9,6 +8,7 @@ #ifndef FFF_CALL_HISTORY_LEN #define FFF_CALL_HISTORY_LEN (50u) #endif +#ifdef __cplusplus /* Defining a function to reset a fake function */ #define RESET_FAKE(FUNCNAME) { \ @@ -1320,13 +1320,6 @@ extern "C"{ \ STATIC_INIT(FUNCNAME) \ #else /* ansi c */ -#define FFF_MAX_ARGS (10u) -#ifndef FFF_ARG_HISTORY_LEN - #define FFF_ARG_HISTORY_LEN (50u) -#endif -#ifndef FFF_CALL_HISTORY_LEN - #define FFF_CALL_HISTORY_LEN (50u) -#endif /* Defining a function to reset a fake function */ #define RESET_FAKE(FUNCNAME) { \ @@ -2555,4 +2548,36 @@ static void RESET_HISTORY() { #endif /* cpp/ansi c */ +#define PP_NARG_MINUS2(...) PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()) + +#define PP_NARG_MINUS2_(...) PP_ARG_MINUS2_N(__VA_ARGS__) + +#define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N + +#define PP_RSEQ_N_MINUS2() 9,8,7,6,5,4,3,2,1,0 + + +#define FAKE_VALUE_FUNC(...) FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) + +#define FUNC_VALUE_(N,...) FUNC_VALUE_N(N,__VA_ARGS__) + +#define FUNC_VALUE_N(N,...) FAKE_VALUE_FUNC ## N(__VA_ARGS__) + + + +#define PP_NARG_MINUS1(...) PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()) + +#define PP_NARG_MINUS1_(...) PP_ARG_MINUS1_N(__VA_ARGS__) + +#define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N + +#define PP_RSEQ_N_MINUS1() 9,8,7,6,5,4,3,2,1,0 + +#define FAKE_VOID_FUNC(...) FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) + +#define FUNC_VOID_(N,...) FUNC_VOID_N(N,__VA_ARGS__) + +#define FUNC_VOID_N(N,...) FAKE_VOID_FUNC ## N(__VA_ARGS__) + + #endif // FAKE_FUNCTIONS diff --git a/test/fff_test_c.c b/test/fff_test_c.c index 0519d27..97076e3 100644 --- a/test/fff_test_c.c +++ b/test/fff_test_c.c @@ -25,11 +25,11 @@ struct MyStruct { int y; }; -FAKE_VOID_FUNC1(voidfunc1, int); -FAKE_VOID_FUNC2(voidfunc2, char, char); -FAKE_VALUE_FUNC0(long, longfunc0); -FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0); -FAKE_VALUE_FUNC0(struct MyStruct, structfunc0); +FAKE_VOID_FUNC(voidfunc1, int); +FAKE_VOID_FUNC(voidfunc2, char, char); +FAKE_VALUE_FUNC(long, longfunc0); +FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0); +FAKE_VALUE_FUNC(struct MyStruct, structfunc0); void setup() @@ -254,6 +254,7 @@ TEST_F(FFFTestSuite, default_constants_can_be_overridden) ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_arg_history_len); } + int main() { setbuf(stdout, NULL); diff --git a/test/fff_test_cpp.cpp b/test/fff_test_cpp.cpp index f4ced16..c086bbb 100644 --- a/test/fff_test_cpp.cpp +++ b/test/fff_test_cpp.cpp @@ -15,9 +15,9 @@ #include "../fff.h" #include -FAKE_VOID_FUNC1(voidfunc1, int); -FAKE_VOID_FUNC2(voidfunc2, char, char); -FAKE_VALUE_FUNC0(long, longfunc0); +FAKE_VOID_FUNC(voidfunc1, int); +FAKE_VOID_FUNC(voidfunc2, char, char); +FAKE_VALUE_FUNC(long, longfunc0); class FFFTestSuite: public testing::Test {