summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-03-17 13:45:09 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-03-17 13:56:05 +0000
commit890e4b355c8863f736a0fcb16fcdad23307dc34b (patch)
treeec7a7c40df88345862b6a6a78ed0923baf298518 /vespalib
parent9041c2e4664d86ba5458d21ca91f697e7d19c8a1 (diff)
test REQUIRE and REQUIRE_EQ and make sure they can be constexpr
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/require/CMakeLists.txt9
-rw-r--r--vespalib/src/tests/require/require_test.cpp89
-rw-r--r--vespalib/src/vespa/vespalib/util/require.h2
4 files changed, 100 insertions, 1 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt
index fb255c86b21..5c0574aad8a 100644
--- a/vespalib/CMakeLists.txt
+++ b/vespalib/CMakeLists.txt
@@ -93,6 +93,7 @@ vespa_define_module(
src/tests/referencecounter
src/tests/regex
src/tests/rendezvous
+ src/tests/require
src/tests/runnable_pair
src/tests/sha1
src/tests/shared_string_repo
diff --git a/vespalib/src/tests/require/CMakeLists.txt b/vespalib/src/tests/require/CMakeLists.txt
new file mode 100644
index 00000000000..8853d4fac26
--- /dev/null
+++ b/vespalib/src/tests/require/CMakeLists.txt
@@ -0,0 +1,9 @@
+# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_require_test_app TEST
+ SOURCES
+ require_test.cpp
+ DEPENDS
+ vespalib
+ GTest::GTest
+)
+vespa_add_test(NAME vespalib_require_test_app COMMAND vespalib_require_test_app)
diff --git a/vespalib/src/tests/require/require_test.cpp b/vespalib/src/tests/require/require_test.cpp
new file mode 100644
index 00000000000..d1060cfe474
--- /dev/null
+++ b/vespalib/src/tests/require/require_test.cpp
@@ -0,0 +1,89 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/util/require.h>
+#include <vespa/vespalib/gtest/gtest.h>
+#include <stdexcept>
+
+//-----------------------------------------------------------------------------
+
+void pass_require() {
+ bool this_is_true = true;
+ REQUIRE(this_is_true);
+}
+
+void pass_require_eq() {
+ int a = 3;
+ int b = 3;
+ REQUIRE_EQ(a, b);
+}
+
+TEST(RequireTest, require_can_pass) {
+ EXPECT_NO_THROW(pass_require());
+}
+
+TEST(RequireTest, require_eq_can_pass) {
+ EXPECT_NO_THROW(pass_require_eq());
+}
+
+//-----------------------------------------------------------------------------
+
+void fail_require() {
+ bool this_is_false = false;
+ REQUIRE(this_is_false);
+}
+
+void fail_require_eq() {
+ int a = 3;
+ int b = 5;
+ REQUIRE_EQ(a, b);
+}
+
+TEST(RequireTest, require_can_fail) {
+ using E = std::invalid_argument;
+ EXPECT_THROW(
+ {
+ try { fail_require(); }
+ catch(const E &e) {
+ fprintf(stderr, "e.what() is >>>%s<<<\n", e.what());
+ throw;
+ }
+ }, E);
+}
+
+TEST(RequireTest, require_eq_can_fail) {
+ using E = std::invalid_argument;
+ EXPECT_THROW(
+ {
+ try { fail_require_eq(); }
+ catch(const E &e) {
+ fprintf(stderr, "e.what() is >>>%s<<<\n", e.what());
+ throw;
+ }
+ }, E);
+}
+
+//-----------------------------------------------------------------------------
+
+constexpr bool foo(bool flag) {
+ REQUIRE(flag);
+ return flag;
+}
+
+constexpr int foo(int a, int b) {
+ REQUIRE_EQ(a, b);
+ return (a + b);
+}
+
+TEST(RequireTest, require_can_be_constexpr) {
+ constexpr bool flag = foo(true);
+ EXPECT_TRUE(flag);
+}
+
+TEST(RequireTest, require_eq_can_be_constexpr) {
+ constexpr int value = foo(2, 2);
+ EXPECT_EQ(value, 4);
+}
+
+//-----------------------------------------------------------------------------
+
+GTEST_MAIN_RUN_ALL_TESTS()
diff --git a/vespalib/src/vespa/vespalib/util/require.h b/vespalib/src/vespa/vespalib/util/require.h
index dc9312cb85a..9de58142926 100644
--- a/vespalib/src/vespa/vespalib/util/require.h
+++ b/vespalib/src/vespa/vespalib/util/require.h
@@ -6,7 +6,7 @@
namespace vespalib {
-inline void handle_require_success() {}
+constexpr void handle_require_success() {}
void handle_require_failure [[noreturn]] (const char *description, const char *file, uint32_t line);
template<typename A, typename B>
void handle_require_eq_failure [[noreturn]] (const A& a, const B& b,