Skip to content

Commit

Permalink
Tests for TRANSFORM macro.
Browse files Browse the repository at this point in the history
Require MSVC to use its conforming preprocessor. Change `tdb_new` macro to work with it.
  • Loading branch information
eric-hughes-tiledb committed Oct 8, 2023
1 parent fd4b343 commit 99a9413
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ set(TILEDB_EP_INSTALL_PREFIX "${TILEDB_EP_BASE}/install")
if (MSVC)
# Turn on standards-conformance mode
add_compile_options("/permissive-")
# Enable the conforming preprocessor instead of the traditional (non-conforming) preprocessor
add_compile_options("/Zc:preprocessor")
# /EH: Enables standard C++ stack unwinding.
# s: Catches only standard C++ exceptions when you use catch(...) syntax.
# c: The compiler assumes that functions declared as extern "C" never throw a C++ exception
Expand Down
13 changes: 10 additions & 3 deletions tiledb/api/c_api_support/exception_wrapper/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
/*
* Transform
*/
#define SELECT_BY_ARITY(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, f, ...) f
#define SELECT_BY_ARITY(x1, x2, x3, x4, x5, x6, x7, x8, x9, ff, ...) ff

#define SELECT_F(f,...) SELECT_BY_ARITY(__VA_ARGS__ __VA_OPT__(,) TR9, TR8, TR7, TR6, TR5, TR4, TR3, TR2, TR1, TR0)

#define SELECT_F_TEXT(f, ...) (__VA_ARGS__ __VA_OPT__(,) TR9, TR8, TR7, TR6, TR5, TR4, TR3, TR2, TR1, TR0)

// concatentate without separator
#define TR0(f)
Expand All @@ -38,9 +42,11 @@
#define TS8(f, s, x, ...) f(x)s TS7(f, s, __VA_ARGS__)
#define TS9(f, s, x, ...) f(x)s TS8(f, s, __VA_ARGS__)


#define TRANSFORM(f,...) \
SELECT_BY_ARITY(nil,__VA_ARGS__,TR9,TR8,TR7,TR6,TR5,TR4,TR3,TR2,TR1,TR0) \
(f,__VA_ARGS__)

// SELECT_BY_ARITY(nil,__VA_ARGS__,TR9,TR8,TR7,TR6,TR5,TR4,TR3,TR2,TR1,TR0) \
// (f,__VA_ARGS__)
#define TRANSFORM_S(f,s,...) \
SELECT_BY_ARITY(nil,__VA_ARGS__,TS9,TS8,TS7,TS6,TS5,TS4,TS3,TS2,TS1,TS0) \
(f,__VA_ARGS__)
Expand Down Expand Up @@ -83,6 +89,7 @@
r tiledb_##f(da) noexcept {return api_entry_##w<tiledb::api::tiledb_##f>(ca);}

#define id(x) x
#define id2(x, y) x, y
#define select_second(a, b) b
#define to_call(x) select_second x

Expand Down
122 changes: 95 additions & 27 deletions tiledb/api/c_api_support/exception_wrapper/test/unit_capi_hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,80 @@ using namespace tiledb::api::detail;
* matching parenthesis, however. So we invoke the argument with an "extra" pair
* of parentheses and then remove them to initialize test vector entries.
*/
#define dquote(x) #x
#define quote(x) dquote(x)
constexpr std::string remove_parentheses(std::string s) {
#define dquote_(x) #x
#define quote(x) dquote_(x)
constexpr std::string unparen(std::string s) {
return s.substr(1, s.size() - 2);
}

//-------------------------------------------------------
// TRANSFORM
//-------------------------------------------------------
struct TransformTestVector {
std::string name;
std::string expression;
std::string expected;
};

#define plusone(x) x+1

std::vector<TransformTestVector> transform_test_vectors {
#define e_ id(x)
{ "framework 0", "x", unparen(quote((e_)))},
#undef e_
#define e_ id2(x, y)
{ "framework 1", "x, y", unparen(quote((e_)))},
#undef e_
#define e_ SELECT_BY_ARITY(TR9, TR8, TR7, TR6, TR5, TR4, TR3, TR2, TR1, TR0)
{ "sba-0", "TR0", quote(e_)},
#undef e_
#define e_ SELECT_BY_ARITY(0, TR9, TR8,TR7,TR6,TR5,TR4,TR3,TR2,TR1,TR0)
{ "sba-1", "TR1", quote(e_)},
#undef e_
#define e_ SELECT_BY_ARITY(0, 1, TR9, TR8,TR7,TR6,TR5,TR4,TR3,TR2,TR1,TR0)
{ "sba-2", "TR2", quote(e_)},
#undef e_
#define e_ SELECT_F(id)
{ "trf-0", "TR0", quote(e_)},
#undef e_
#define e_ SELECT_F(id, 0)
{ "trf-1", "TR1", quote(e_)},
#undef e_
#define e_ SELECT_F(id, 0, 1)
{ "trf-2", "TR2", quote(e_)},
#undef e_
#define e_ SELECT_F_TEXT(id, 0, 1)
{ "trf-2a", "TR2", quote(e_)},
#undef e_
#define e_ TRANSFORM(id, 0)
{ "", "0", unparen(quote((e_)))},
#undef e_
#define e_ TRANSFORM(id, 0, 1)
{ "", "0, 1", unparen(quote((e_)))},
#undef e_
#define e_ TRANSFORM(id, 0, 1, 2)
{ "", "0, 1, 2", unparen(quote((e_)))},
#undef e_
#define e_ TRANSFORM(plusone, 0)
{ "", "0", unparen(quote((e_)))},
#undef e_
#define e_ TRANSFORM(plusone, 0, 1)
{ "", "0, 1", unparen(quote((e_)))},
#undef e_
#define e_ TRANSFORM(plusone, 0, 1, 2)
{ "", "0, 1, 2", unparen(quote((e_)))},
#undef e_
};

TEST_CASE("Transform 0", "[transform]") {
for (const auto& x : transform_test_vectors) {
DYNAMIC_SECTION(x.name)
{
CHECK(x.expression == x.expected);
}
}
}

//-------------------------------------------------------
// Test vectors for macro testing
//-------------------------------------------------------
Expand Down Expand Up @@ -136,14 +204,14 @@ MacroTestVectorsT macro_test_vectors{
"noexcept {"
"return api_entry_plain<tiledb::api::tiledb_as_built_dump>(out);"
"}"},
.final_macro{remove_parentheses(quote((m0_)))},
.final_macro_q{remove_parentheses(quote((m1_)))},
.last_step_macro{remove_parentheses(quote((m2_)))},
.args_macroargvalue{remove_parentheses(quote((fma_)))},
.args_decl_macro{remove_parentheses(quote((mda_)))},
.args_call_macro{remove_parentheses(quote((mca_)))},
.da_value{remove_parentheses(quote((fda_)))},
.ca_value{remove_parentheses(quote((fca_)))},
.final_macro{unparen(quote((m0_)))},
.final_macro_q{unparen(quote((m1_)))},
.last_step_macro{unparen(quote((m2_)))},
.args_macroargvalue{unparen(quote((fma_)))},
.args_decl_macro{unparen(quote((mda_)))},
.args_call_macro{unparen(quote((mca_)))},
.da_value{unparen(quote((fda_)))},
.ca_value{unparen(quote((fca_)))},
},
#undef rn_
#undef fma_
Expand All @@ -165,14 +233,14 @@ MacroTestVectorsT macro_test_vectors{
"void tiledb_config_free(tiledb_config_t** config) noexcept "
"{return "
"api_entry_void<tiledb::api::tiledb_config_free>(config);}"},
.final_macro{remove_parentheses(quote((m0_)))},
.final_macro_q{remove_parentheses(quote((m1_)))},
.last_step_macro{remove_parentheses(quote((m2_)))},
.args_macroargvalue{remove_parentheses(quote((fma_)))},
.args_decl_macro{remove_parentheses(quote((mda_)))},
.args_call_macro{remove_parentheses(quote((mca_)))},
.da_value{remove_parentheses(quote((fda_)))},
.ca_value{remove_parentheses(quote((fca_)))},
.final_macro{unparen(quote((m0_)))},
.final_macro_q{unparen(quote((m1_)))},
.last_step_macro{unparen(quote((m2_)))},
.args_macroargvalue{unparen(quote((fma_)))},
.args_decl_macro{unparen(quote((mda_)))},
.args_call_macro{unparen(quote((mca_)))},
.da_value{unparen(quote((fda_)))},
.ca_value{unparen(quote((fca_)))},
},
#undef rn_
#undef fma_
Expand All @@ -198,14 +266,14 @@ MacroTestVectorsT macro_test_vectors{
"(tiledb_config_t** config, tiledb_error_t** error) noexcept "
"{return api_entry_error<tiledb::api::tiledb_config_alloc>"
"(error, config);}"},
.final_macro{remove_parentheses(quote((m0_)))},
.final_macro_q{remove_parentheses(quote((m1_)))},
.last_step_macro{remove_parentheses(quote((m2_)))},
.args_macroargvalue{remove_parentheses(quote((fma_)))},
.args_decl_macro{remove_parentheses(quote((mda_)))},
.args_call_macro{remove_parentheses(quote((mca_)))},
.da_value{remove_parentheses(quote((fda_)))},
.ca_value{remove_parentheses(quote((fca_)))},
.final_macro{unparen(quote((m0_)))},
.final_macro_q{unparen(quote((m1_)))},
.last_step_macro{unparen(quote((m2_)))},
.args_macroargvalue{unparen(quote((fma_)))},
.args_decl_macro{unparen(quote((mda_)))},
.args_call_macro{unparen(quote((mca_)))},
.da_value{unparen(quote((fda_)))},
.ca_value{unparen(quote((fca_)))},
}
#undef rn_
#undef fma_
Expand Down
2 changes: 1 addition & 1 deletion tiledb/common/heap_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ using tiledb_unique_ptr = std::unique_ptr<T, TileDBUniquePtrDeleter<T>>;

#ifdef _MSC_VER
#define tdb_new(T, ...) \
tiledb::common::tiledb_new<T>(TILEDB_HEAP_MEM_LABEL, __VA_ARGS__)
tiledb::common::tiledb_new<T>(TILEDB_HEAP_MEM_LABEL __VA_OPT__(,) __VA_ARGS__)
#else
#define tdb_new(T, ...) \
tiledb::common::tiledb_new<T>(TILEDB_HEAP_MEM_LABEL, ##__VA_ARGS__)
Expand Down

0 comments on commit 99a9413

Please sign in to comment.