From da8be854b81fda3250ec851db4f2a620955e9b31 Mon Sep 17 00:00:00 2001
From: "Jayesh Badwaik (FZ Juelich)" <j.badwaik@fz-juelich.de>
Date: Wed, 8 Feb 2023 23:26:44 +0100
Subject: [PATCH] + mol: add compile test infrastructure

---
 bs/setup.cmake                                |  2 +-
 src/cpp/include/mol/compile_test.hpp          | 22 +++++++++++++
 src/cpp/include/mol/dependent_false.hpp       | 23 +++++++++++++
 src/cpp/include/mol/detail/compile_test.hpp   | 32 +++++++++++++++++++
 .../include/mol/detail/dependent_false.hpp    | 13 ++++++++
 src/cpp/include/mol/detail/test_name.hpp      | 18 +++++++++++
 test/CMakeLists.txt                           | 11 +++++--
 test/compile/CMakeLists.txt                   |  6 ++++
 test/compile/cpp/CMakeLists.txt               |  6 ++++
 test/compile/cpp/mol/false_false.t.cpp        | 27 ++++++++++++++++
 test/compile/cpp/mol/false_true.t.cpp         | 27 ++++++++++++++++
 test/compile/cpp/mol/true_false.fail.t.cpp    | 26 +++++++++++++++
 test/compile/cpp/mol/true_true.t.cpp          | 26 +++++++++++++++
 13 files changed, 236 insertions(+), 3 deletions(-)
 create mode 100644 src/cpp/include/mol/compile_test.hpp
 create mode 100644 src/cpp/include/mol/dependent_false.hpp
 create mode 100644 src/cpp/include/mol/detail/compile_test.hpp
 create mode 100644 src/cpp/include/mol/detail/dependent_false.hpp
 create mode 100644 src/cpp/include/mol/detail/test_name.hpp
 create mode 100644 test/compile/CMakeLists.txt
 create mode 100644 test/compile/cpp/CMakeLists.txt
 create mode 100644 test/compile/cpp/mol/false_false.t.cpp
 create mode 100644 test/compile/cpp/mol/false_true.t.cpp
 create mode 100644 test/compile/cpp/mol/true_false.fail.t.cpp
 create mode 100644 test/compile/cpp/mol/true_true.t.cpp

diff --git a/bs/setup.cmake b/bs/setup.cmake
index 064791f..0e84361 100644
--- a/bs/setup.cmake
+++ b/bs/setup.cmake
@@ -110,7 +110,7 @@ endif()
 # Dev : Setup Targets for Linting
 # --------------------------------------------------------------------------------------------------
 if(DIAGNOSTIC_LINTING)
-  set(TORA_CLANG_TIDY_PASSTHROUGH "-DZELL_RUN_CLANG_TIDY")
+  set(TORA_CLANG_TIDY_PASSTHROUGH "-DMOL_COMPILE_TEST_DISABLE")
   find_package(tora REQUIRED COMPONENTS clang-tidy)
   add_custom_target(diagnostic.linting)
   add_dependencies(diagnostic.linting tora.clang-tidy)
diff --git a/src/cpp/include/mol/compile_test.hpp b/src/cpp/include/mol/compile_test.hpp
new file mode 100644
index 0000000..fc9fd4b
--- /dev/null
+++ b/src/cpp/include/mol/compile_test.hpp
@@ -0,0 +1,22 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#ifndef MOL_COMPILE_TEST_HPP
+#define MOL_COMPILE_TEST_HPP
+
+#include <mol/detail/compile_test.hpp>
+#include <mol/detail/test_name.hpp>
+#include <type_traits>
+
+#ifndef MOL_COMPILE_TEST_DISABLE
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_COMPILE_TEST(condition)                                                                \
+  MOL_DETAIL_COMPILE_TEST_IMPL(MOL_DETAIL_TEST_NAME(__COUNTER__), condition)
+#else
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_COMPILE_TEST(condition)                                                                \
+  MOL_DETAIL_COMPILE_TEST_DUMMY_IMPL(MOL_DETAIL_TEST_NAME(__COUNTER__), condition)
+#endif
+#endif // MOL_COMPILE_TEST_HPP
diff --git a/src/cpp/include/mol/dependent_false.hpp b/src/cpp/include/mol/dependent_false.hpp
new file mode 100644
index 0000000..098f86f
--- /dev/null
+++ b/src/cpp/include/mol/dependent_false.hpp
@@ -0,0 +1,23 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#ifndef MOL_DEPENDENT_FALSE_HPP
+#define MOL_DEPENDENT_FALSE_HPP
+
+#include <mol/detail/dependent_false.hpp>
+#include <utility>
+
+namespace mol {
+template <class T>
+struct dependent_false : std::false_type {};
+
+template <>
+struct dependent_false<detail::dependent_false_dummy_class> : std::true_type {};
+
+template <typename T>
+constexpr inline auto dependent_false_v = dependent_false<T>::value;
+} // namespace mol
+
+#endif // MOL_DEPENDENT_FALSE_HPP
diff --git a/src/cpp/include/mol/detail/compile_test.hpp b/src/cpp/include/mol/detail/compile_test.hpp
new file mode 100644
index 0000000..06db49d
--- /dev/null
+++ b/src/cpp/include/mol/detail/compile_test.hpp
@@ -0,0 +1,32 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#ifndef MOL_DETAIL_COMPILE_TEST_HPP
+#define MOL_DETAIL_COMPILE_TEST_HPP
+
+#include <mol/detail/test_name.hpp>
+
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_COMPILE_TEST_IMPL(test_name, condition)                                         \
+  template <typename T, bool b, typename std::enable_if_t<!b>* = nullptr>                          \
+  void test_name()                                                                                 \
+  {                                                                                                \
+  }                                                                                                \
+  template <typename T, bool b, typename std::enable_if_t<b>* = nullptr>                           \
+  void test_name();                                                                                \
+  void test_name_##instantiate();                                                                  \
+  void test_name_##instantiate()                                                                   \
+  {                                                                                                \
+    MOL_DETAIL_TEST_ID(test_name)<int, condition>();                                               \
+  }                                                                                                \
+  template <typename T, bool b, typename std::enable_if_t<b>*>                                     \
+  void test_name()
+
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_COMPILE_TEST_DUMMY_IMPL(test_name, condition)                                   \
+  template <typename T, bool b, typename std::enable_if_t<b>* = nullptr>                           \
+  void test_name()
+
+#endif // MOL_DETAIL_COMPILE_TEST_HPP
diff --git a/src/cpp/include/mol/detail/dependent_false.hpp b/src/cpp/include/mol/detail/dependent_false.hpp
new file mode 100644
index 0000000..952211f
--- /dev/null
+++ b/src/cpp/include/mol/detail/dependent_false.hpp
@@ -0,0 +1,13 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#ifndef MOL_DETAIL_DEPENDENT_FALSE_HPP
+#define MOL_DETAIL_DEPENDENT_FALSE_HPP
+
+namespace mol::detail {
+class dependent_false_dummy_class {};
+} // namespace mol::detail
+
+#endif // MOL_DETAIL_DEPENDENT_FALSE_HPP
diff --git a/src/cpp/include/mol/detail/test_name.hpp b/src/cpp/include/mol/detail/test_name.hpp
new file mode 100644
index 0000000..f9e0ed0
--- /dev/null
+++ b/src/cpp/include/mol/detail/test_name.hpp
@@ -0,0 +1,18 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#ifndef MOL_DETAIL_TEST_NAME_HPP
+#define MOL_DETAIL_TEST_NAME_HPP
+
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_TEST_ID(s) s
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_TEST_NAME_IMPL(s) testzell_compile_test_##s
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_TEST_NAME_PREMACRO(s) MOL_DETAIL_TEST_NAME_IMPL(s)
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define MOL_DETAIL_TEST_NAME(s) MOL_DETAIL_TEST_NAME_PREMACRO(__COUNTER__)
+
+#endif // MOL_DETAIL_TEST_NAME_HPP
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 2fe1912..b8e321c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -13,15 +13,22 @@ target_link_libraries(libtestzell PUBLIC zell::libzell)
 add_subdirectory(common)
 
 # ------------------------------------------------------------------------------
-# Adding Tests
+# Add Compile Tests
 # ------------------------------------------------------------------------------
-
 tora_surrogate_check(libtestzell)
 
 if(BUILD_SOURCE)
   tora_surrogate_check(libzell)
 endif()
 
+add_subdirectory(compile)
+
+
+
+# ------------------------------------------------------------------------------
+# Adding Runtime Tests
+# ------------------------------------------------------------------------------
+
 add_subdirectory(smoke)
 add_subdirectory(unit)
 add_subdirectory(integration)
diff --git a/test/compile/CMakeLists.txt b/test/compile/CMakeLists.txt
new file mode 100644
index 0000000..ec3565a
--- /dev/null
+++ b/test/compile/CMakeLists.txt
@@ -0,0 +1,6 @@
+# --------------------------------------------------------------------------------------------------
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+# --------------------------------------------------------------------------------------------------
+
+add_subdirectory(cpp)
diff --git a/test/compile/cpp/CMakeLists.txt b/test/compile/cpp/CMakeLists.txt
new file mode 100644
index 0000000..e0a6b57
--- /dev/null
+++ b/test/compile/cpp/CMakeLists.txt
@@ -0,0 +1,6 @@
+# --------------------------------------------------------------------------------------------------
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+# --------------------------------------------------------------------------------------------------
+
+add_compile_test_directory(mol libtestzell cpp unit_test)
diff --git a/test/compile/cpp/mol/false_false.t.cpp b/test/compile/cpp/mol/false_false.t.cpp
new file mode 100644
index 0000000..4913ea5
--- /dev/null
+++ b/test/compile/cpp/mol/false_false.t.cpp
@@ -0,0 +1,27 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2023 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#include <mol/compile_test.hpp>
+#include <mol/dependent_false.hpp>
+
+template <typename T, bool b, typename std::enable_if<b>::type* = nullptr>
+void foo()
+{
+}
+
+template <typename T, bool b, typename std::enable_if<!b>::type* = nullptr>
+void foo()
+{
+  static_assert(mol::dependent_false_v<T>, "Not integral");
+}
+
+MOL_COMPILE_TEST(false)
+{
+  foo<int, false>();
+}
+
+auto main() -> int
+{
+}
diff --git a/test/compile/cpp/mol/false_true.t.cpp b/test/compile/cpp/mol/false_true.t.cpp
new file mode 100644
index 0000000..93b4ee9
--- /dev/null
+++ b/test/compile/cpp/mol/false_true.t.cpp
@@ -0,0 +1,27 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2023 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+
+#include <mol/compile_test.hpp>
+#include <mol/dependent_false.hpp>
+
+template <typename T, bool b, typename std::enable_if<b>::type* = nullptr>
+void foo()
+{
+}
+
+template <typename T, bool b, typename std::enable_if<!b>::type* = nullptr>
+void foo()
+{
+  static_assert(mol::dependent_false_v<T>, "Not integral");
+}
+
+MOL_COMPILE_TEST(false)
+{
+  foo<int, true>();
+}
+
+auto main() -> int
+{
+}
diff --git a/test/compile/cpp/mol/true_false.fail.t.cpp b/test/compile/cpp/mol/true_false.fail.t.cpp
new file mode 100644
index 0000000..3ad9030
--- /dev/null
+++ b/test/compile/cpp/mol/true_false.fail.t.cpp
@@ -0,0 +1,26 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2023 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+#include <mol/compile_test.hpp>
+#include <mol/dependent_false.hpp>
+
+template <typename T, bool b, typename std::enable_if<b>::type* = nullptr>
+void foo()
+{
+}
+
+template <typename T, bool b, typename std::enable_if<!b>::type* = nullptr>
+void foo()
+{
+  static_assert(mol::dependent_false_v<T>, "Not integral");
+}
+
+MOL_COMPILE_TEST(true)
+{
+  foo<int, false>();
+}
+
+auto main() -> int
+{
+}
diff --git a/test/compile/cpp/mol/true_true.t.cpp b/test/compile/cpp/mol/true_true.t.cpp
new file mode 100644
index 0000000..3019401
--- /dev/null
+++ b/test/compile/cpp/mol/true_true.t.cpp
@@ -0,0 +1,26 @@
+// -------------------------------------------------------------------------------------------------
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: (C) 2023 Jayesh Badwaik <j.badwaik@fz-juelich.de>
+// -------------------------------------------------------------------------------------------------
+#include <mol/compile_test.hpp>
+#include <mol/dependent_false.hpp>
+
+template <typename T, bool b, typename std::enable_if<b>::type* = nullptr>
+void foo()
+{
+}
+
+template <typename T, bool b, typename std::enable_if<!b>::type* = nullptr>
+void foo()
+{
+  static_assert(mol::dependent_false_v<T>, "Not integral");
+}
+
+MOL_COMPILE_TEST(true)
+{
+  foo<int, true>();
+}
+
+auto main() -> int
+{
+}
-- 
GitLab