From bbc5edfb340da3a682ba1ded9de74ac9790b30a6 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Tue, 17 Nov 2015 17:49:27 +0100 Subject: [PATCH] Refactored the implementation of theory macros in internal/ --- include/criterion/internal/theories.h | 71 +++++++++++++++++++++++++++ include/criterion/theories.h | 68 ++++--------------------- 2 files changed, 81 insertions(+), 58 deletions(-) create mode 100644 include/criterion/internal/theories.h diff --git a/include/criterion/internal/theories.h b/include/criterion/internal/theories.h new file mode 100644 index 0000000..a0176c8 --- /dev/null +++ b/include/criterion/internal/theories.h @@ -0,0 +1,71 @@ +#ifndef CRITERION_INTERNAL_THEORIES_H_ +# define CRITERION_INTERNAL_THEORIES_H_ + +# include "test.h" + +# ifdef __cplusplus +# include +using std::size_t; +# else +# include +# endif + +# ifdef __cplusplus +template +constexpr size_t criterion_va_num__(const T &...) { + return sizeof...(T); +} +# endif + +struct criterion_datapoints { + size_t size; + size_t len; + const char *name; + void *arr; +}; + +CR_BEGIN_C_API + +CR_API void cr_theory_main(struct criterion_datapoints *dps, size_t datapoints, void (*fnptr)(void)); + +CR_END_C_API + +# ifdef __cplusplus +# define CR_TH_VA_NUM(Type, ...) criterion_va_num__(__VA_ARGS__) +# define CR_TH_TEMP_ARRAY(Type, ...) []() { static Type arr[] = { __VA_ARGS__ }; return &arr; }() +# else +# define CR_TH_VA_NUM(Type, ...) sizeof ((Type[]) { __VA_ARGS__ }) / sizeof (Type) +# define CR_TH_TEMP_ARRAY(Type, ...) &(Type[]) { __VA_ARGS__ } +# endif + +# define CR_TH_INTERNAL_TDPS(Category, Name) \ + static struct criterion_datapoints CR_IDENTIFIER_(Category, Name, dps)[] + +# define CR_TH_INTERNAL_TDP(Category, Name) \ + (CR_IDENTIFIER_(Category, Name, dps)) + +# define CR_TH_INTERNAL_DP(Type, ...) { \ + sizeof (Type), \ + CR_EXPAND(CR_TH_VA_NUM(Type, __VA_ARGS__)), \ + #Type, \ + CR_EXPAND(CR_TH_TEMP_ARRAY(Type, __VA_ARGS__)), \ + } + +# define CR_NB_DATAPOINTS(Var) \ + (sizeof (Var) / sizeof (struct criterion_datapoints)) + +# define CR_VAARG_ID(Suffix, Category, Name, ...) \ + CR_IDENTIFIER_(Category, Name, Suffix) + +# define CR_THEORY_BASE(Args, ...) \ + void CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,))Args; \ + CR_EXPAND(CR_TEST_BASE(__VA_ARGS__, .sentinel_ = 0)) { \ + cr_theory_main( \ + CR_EXPAND(CR_VAARG_ID(dps, __VA_ARGS__,)), \ + CR_NB_DATAPOINTS(CR_EXPAND(CR_VAARG_ID(dps, __VA_ARGS__,))), \ + (void(*)(void)) CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,)) \ + ); \ + } \ + void CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,))Args + +#endif /* !CRITERION_INTERNAL_THEORIES_H_ */ diff --git a/include/criterion/theories.h b/include/criterion/theories.h index 7472760..711e7b1 100644 --- a/include/criterion/theories.h +++ b/include/criterion/theories.h @@ -24,62 +24,30 @@ #ifndef CRITERION_THEORIES_H_ # define CRITERION_THEORIES_H_ -# ifdef __cplusplus -# include -using std::size_t; -# else -# include -# endif - # include "criterion.h" - -# ifdef __cplusplus -template -constexpr size_t criterion_va_num__(const T &...) { - return sizeof...(T); -} -# endif +# include "internal/theories.h" CR_BEGIN_C_API CR_API void cr_theory_abort(void); -# define TheoryDataPoints(Category, Name) \ - static struct criterion_datapoints CR_IDENTIFIER_(Category, Name, dps)[] +CR_END_C_API -# define TheoryDataPoint(Category, Name) \ - (CR_IDENTIFIER_(Category, Name, dps)) +// Theory and datapoint macros -# ifdef __cplusplus -# define CR_TH_VA_NUM(Type, ...) criterion_va_num__(__VA_ARGS__) -# define CR_TH_TEMP_ARRAY(Type, ...) []() { static Type arr[] = { __VA_ARGS__ }; return &arr; }() -# else -# define CR_TH_VA_NUM(Type, ...) sizeof ((Type[]) { __VA_ARGS__ }) / sizeof (Type) -# define CR_TH_TEMP_ARRAY(Type, ...) &(Type[]) { __VA_ARGS__ } -# endif +# define Theory(Args, ...) CR_EXPAND(CR_THEORY_BASE(Args, __VA_ARGS__)) -# define DataPoints(Type, ...) { \ - sizeof (Type), \ - CR_EXPAND(CR_TH_VA_NUM(Type, __VA_ARGS__)), \ - #Type, \ - CR_EXPAND(CR_TH_TEMP_ARRAY(Type, __VA_ARGS__)), \ - } +# define TheoryDataPoints(Category, Name) CR_TH_INTERNAL_TDPS(Category, Name) +# define TheoryDataPoint(Category, Name) CR_TH_INTERNAL_TDP(Category, Name) +# define DataPoints(Type, ...) CR_EXPAND(CR_TH_INTERNAL_DP(Type, __VA_ARGS__)) -struct criterion_datapoints { - size_t size; - size_t len; - const char *name; - void *arr; -}; - -# define CR_NB_DATAPOINTS(Var) \ - (sizeof (Var) / sizeof (struct criterion_datapoints)) +// Theory invariants # define cr_assume(Condition) \ do { \ if (!(Condition)) \ cr_theory_abort(); \ - } while (0); + } while (0) # define cr_assume_not(Condition) cr_assume(!(Condition)) @@ -115,23 +83,7 @@ struct criterion_datapoints { # define cr_assume_arr_eq(Actual, Expected, Size) cr_assume(!memcmp((A), (B), (Size))) # define cr_assume_arr_neq(Actual, Expected, Size) cr_assume(memcmp((A), (B), (Size))) -CR_API void cr_theory_main(struct criterion_datapoints *dps, size_t datapoints, void (*fnptr)(void)); - -# define CR_VAARG_ID(Suffix, Category, Name, ...) \ - CR_IDENTIFIER_(Category, Name, Suffix) - -# define Theory(Args, ...) \ - void CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,))Args; \ - CR_EXPAND(CR_TEST_BASE(__VA_ARGS__, .sentinel_ = 0)) { \ - cr_theory_main( \ - CR_EXPAND(CR_VAARG_ID(dps, __VA_ARGS__,)), \ - CR_NB_DATAPOINTS(CR_EXPAND(CR_VAARG_ID(dps, __VA_ARGS__,))), \ - (void(*)(void)) CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,)) \ - ); \ - } \ - void CR_EXPAND(CR_VAARG_ID(theory, __VA_ARGS__,))Args - -CR_END_C_API +// Deprecated # ifndef CRITERION_NO_COMPAT # define cr_assume_strings_eq(...) CRITERION_ASSERT_DEPRECATED_B(cr_assume_strings_eq, cr_assume_str_eq) cr_assume_str_eq(__VA_ARGS__)