From bdd1c657b378061e4b5661002339bade0432a696 Mon Sep 17 00:00:00 2001 From: Siddharth Priya Date: Sun, 7 Jan 2024 21:01:34 -0500 Subject: [PATCH] feat: improve times api builder call instead of passing cardinality in the the times call, pass cardinality into the predicate for better aesthetics --- examples/seahorn/ipc/vmock/mock_env.cc | 8 +-- .../seahorn/ipc/vmock_invokefn/mock_env.cc | 4 +- src/include/seamock.hh | 53 +++++++++---------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/examples/seahorn/ipc/vmock/mock_env.cc b/examples/seahorn/ipc/vmock/mock_env.cc index a3d188d..648f280 100644 --- a/examples/seahorn/ipc/vmock/mock_env.cc +++ b/examples/seahorn/ipc/vmock/mock_env.cc @@ -41,14 +41,14 @@ static constexpr auto set_pointer_fn_read_msg = [](char *msg) { sea_reset_modified(msg); }; -constexpr auto get_msg_expectations = ExpectationBuilder() - .times<1>(Eq) +constexpr auto get_msg_expectations = seamock::ExpectationBuilder() + .times(seamock::Eq<1>()) .returnFn(nd_int) .captureArgAndInvoke<1>(set_pointer_fn_get_msg) .build(); -constexpr auto read_msg_expectations = ExpectationBuilder() - .times<2>(Lt) +constexpr auto read_msg_expectations = seamock::ExpectationBuilder() + .times(seamock::Lt<2>()) .returnFn(MOCK_UTIL_WRAP_VAL(g_msg_size)) .captureArgAndInvoke<1>(set_pointer_fn_read_msg) .build(); diff --git a/examples/seahorn/ipc/vmock_invokefn/mock_env.cc b/examples/seahorn/ipc/vmock_invokefn/mock_env.cc index 509438f..0e35385 100644 --- a/examples/seahorn/ipc/vmock_invokefn/mock_env.cc +++ b/examples/seahorn/ipc/vmock_invokefn/mock_env.cc @@ -47,10 +47,10 @@ constexpr auto invoke_read_msg = }; constexpr auto get_msg_expectations = - ExpectationBuilder().invokeFn(invoke_get_msg).times<1>(Eq).build(); + seamock::ExpectationBuilder().invokeFn(invoke_get_msg).times(seamock::Eq<1>()).build(); constexpr auto read_msg_expectations = - ExpectationBuilder().invokeFn(invoke_read_msg).times<2>(Lt).build(); + seamock::ExpectationBuilder().invokeFn(invoke_read_msg).times(seamock::Lt<2>()).build(); // *** End: define args for mock functions *** // *** Begin: mock definition *** diff --git a/src/include/seamock.hh b/src/include/seamock.hh index 0841821..9862a75 100644 --- a/src/include/seamock.hh +++ b/src/include/seamock.hh @@ -38,17 +38,6 @@ extern bool nd_bool(void); #define SEQ_COUNTER_MAXVAL 10 static size_t g_sequence_counter; -namespace seamock { -namespace util { - -static std::array SeqArray{ - "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", - "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", - "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF"}; - -} // namespace util -} // namespace seamock - // Define the check to run on a given function // NOTE: Current impl does not support querying the return function since // that is set local to the function. @@ -88,11 +77,24 @@ auto Times = [](auto times_fn_val, auto expectations_map) { return hana::insert(tmp, hana::make_pair(TIMES_FN, times_fn_val)); }; -auto Eq = [](auto val) { return hana::equal.to(val); }; +namespace seamock { +template +constexpr auto Eq() { + return hana::equal.to(hana::int_c); +} + + +template +constexpr auto Lt() { + return hana::less.than(hana::int_c); +} -auto Lt = [](auto val) { return hana::less.than(val); }; +template +constexpr auto Gt() { + return hana::greater.than(hana::int_c); +} -auto Gt = [](auto val) { return hana::greater.than(val); }; +} // namespace seamock // TODO: untested auto And = [](auto vals...) { return hana::demux(hana::and_, vals); }; @@ -110,12 +112,6 @@ BOOST_HANA_CONSTEXPR_LAMBDA auto Capture = [](auto capture_map, return hana::insert(tmp, hana::make_pair(CAPTURE_ARGS_MAPS, capture_map)); }; -// BOOST_HANA_CONSTEXPR_LAMBDA auto After = [](auto predecessor_tup, -// auto expectations_map) { -// auto tmp = hana::erase_key(expectations_map, AFTER); -// return hana::insert(tmp, hana::make_pair(AFTER, predecessor_tup)); -// }; - BOOST_HANA_CONSTEXPR_LAMBDA auto InvokeFn = [](auto invokeFn, auto expectations_map) { auto tmp = hana::erase_key(expectations_map, INVOKE_FN); @@ -133,6 +129,7 @@ BOOST_HANA_CONSTEXPR_LAMBDA auto MakeExpectation = [](auto func) { BOOST_HANA_CONSTEXPR_LAMBDA auto AND = hana::infix([](auto fn_x, auto fn_y) { return hana::compose(fn_x, fn_y); }); + #define UNPACK_TRANSFORM_TUPLE(func, tuple) \ BOOST_PP_TUPLE_ENUM(BOOST_PP_SEQ_TO_TUPLE( \ BOOST_PP_SEQ_FOR_EACH_I(func, DONT_CARE, BOOST_PP_TUPLE_TO_SEQ(tuple)))) @@ -223,6 +220,8 @@ BOOST_HANA_CONSTEXPR_LAMBDA auto AND = }); \ } +#define MOCK_UTIL_WRAP_VAL(x) []() -> decltype(x) { return x; } + // --------------------------------------------- // Generic mock fn // --------------------------------------------- @@ -242,7 +241,7 @@ static auto skeletal = [](auto &&expectations_map, auto &&args_tuple) { auto fnName = hana::at_key(expectations_map, CALL_FN_NAME); static_assert(fnName != -1_c); // NOTE: record call in Sequence - seamock::util::SeqArray[g_sequence_counter] = fnName.c_str(); + // seamock::util::SeqArray[g_sequence_counter] = fnName.c_str(); // seamock::util::SetTupleAtIdx(seamock::util::SeqTuple, // g_sequence_counter, // fnName); @@ -325,8 +324,8 @@ static auto skeletal = [](auto &&expectations_map, auto &&args_tuple) { }); }; +namespace seamock { -#define MOCK_UTIL_WRAP_VAL(x) []() -> decltype(x) { return x; } template class ExpectationBuilder { private: @@ -347,11 +346,11 @@ public: return ExpectationBuilder(updatedMap); } - template + template constexpr auto times(TimesFnType f) const { - constexpr auto cardConst = hana::int_c; - constexpr auto fn = f(cardConst); - auto updatedMap = Times(fn, expectationsMap); + // constexpr auto cardConst = hana::int_c; + // constexpr auto fn = f(cardConst); + auto updatedMap = Times(f, expectationsMap); return ExpectationBuilder(updatedMap); } @@ -376,5 +375,5 @@ public: } }; - +} // namespace seamock #endif // SEAMOCK_H_