summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-21 19:57:11 +0100
committerTyge Løvset <[email protected]>2023-01-21 19:57:11 +0100
commit0197ade3ef8a2e89a466f5a1c3e512363752879b (patch)
tree7b8d46696cabda723cb07557ec7027adcf270a42
parent798f3a637818147eaf231f438c7b104387de70e5 (diff)
downloadSTC-modified-0197ade3ef8a2e89a466f5a1c3e512363752879b.tar.gz
STC-modified-0197ade3ef8a2e89a466f5a1c3e512363752879b.zip
GoogleTest-ified ctest.h (more compatible). Added original ctest example file mytest.c.txt
-rw-r--r--misc/tests/ctest.h59
-rw-r--r--misc/tests/mytests.c.txt237
2 files changed, 260 insertions, 36 deletions
diff --git a/misc/tests/ctest.h b/misc/tests/ctest.h
index 13344428..29eb151e 100644
--- a/misc/tests/ctest.h
+++ b/misc/tests/ctest.h
@@ -178,16 +178,16 @@ void CTEST_ERR(const char* fmt, ...) CTEST_IMPL_FORMAT_PRINTF(1, 2); // doesn't
void assert_str(const char* cmp, const char* exp, const char* real, const char* caller, int line);
-#define ASSERT_STR(exp, real) assert_str("==", exp, real, __FILE__, __LINE__)
-#define ASSERT_NOT_STR(exp, real) assert_str("!=", exp, real, __FILE__, __LINE__)
-#define ASSERT_STRSTR(str, substr) assert_str("=~", str, substr, __FILE__, __LINE__)
-#define ASSERT_NOT_STRSTR(str, substr) assert_str("!~", str, substr, __FILE__, __LINE__)
+#define ASSERT_STREQ(exp, real) assert_str("==", exp, real, __FILE__, __LINE__)
+#define ASSERT_STRNE(exp, real) assert_str("!=", exp, real, __FILE__, __LINE__)
+#define ASSERT_SUBSTR(str, substr) assert_str("=~", str, substr, __FILE__, __LINE__)
+#define ASSERT_NOTSUBSTR(str, substr) assert_str("!~", str, substr, __FILE__, __LINE__)
void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const char* caller, int line);
-#define ASSERT_WSTR(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__)
-#define ASSERT_NOT_WSTR(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__)
-#define ASSERT_WSTRSTR(str, substr) assert_wstr("=~", str, substr, __FILE__, __LINE__)
-#define ASSERT_NOT_WSTRSTR(str, substr) assert_wstr("!~", str, substr, __FILE__, __LINE__)
+#define ASSERT_WSTREQ(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__)
+#define ASSERT_WSTRNE(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__)
+#define ASSERT_WSUBSTR(str, substr) assert_wstr("=~", str, substr, __FILE__, __LINE__)
+#define ASSERT_NOTWSUBSTR(str, substr) assert_wstr("!~", str, substr, __FILE__, __LINE__)
void assert_data(const unsigned char* exp, size_t expsize,
const unsigned char* real, size_t realsize,
@@ -199,28 +199,21 @@ void assert_data(const unsigned char* exp, size_t expsize,
#define CTEST_DBL_EPSILON 1e-12
void assert_compare(const char* cmp, intmax_t exp, intmax_t real, const char* caller, int line);
-#define ASSERT_EQUAL(exp, real) assert_compare("==", exp, real, __FILE__, __LINE__)
-#define ASSERT_NOT_EQUAL(exp, real) assert_compare("!=", exp, real, __FILE__, __LINE__)
-
-#define ASSERT_EQ ASSERT_EQUAL
-#define ASSERT_NE ASSERT_NOT_EQUAL
+#define ASSERT_EQ(v1, v2) assert_compare("==", v1, v2, __FILE__, __LINE__)
+#define ASSERT_NE(v1, v2) assert_compare("!=", v1, v2, __FILE__, __LINE__)
#define ASSERT_LT(v1, v2) assert_compare("<", v1, v2, __FILE__, __LINE__)
#define ASSERT_LE(v1, v2) assert_compare("<=", v1, v2, __FILE__, __LINE__)
#define ASSERT_GT(v1, v2) assert_compare(">", v1, v2, __FILE__, __LINE__)
#define ASSERT_GE(v1, v2) assert_compare(">=", v1, v2, __FILE__, __LINE__)
-void assert_compare_u(const char* cmp, uintmax_t exp, uintmax_t real, const char* caller, int line);
-#define ASSERT_EQUAL_U(exp, real) assert_compare_u("==", exp, real, __FILE__, __LINE__)
-#define ASSERT_NOT_EQUAL_U(exp, real) assert_compare_u("!=", exp, real, __FILE__, __LINE__)
-
void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line);
-#define ASSERT_INTERVAL(exp1, exp2, real) assert_interval(exp1, exp2, real, __FILE__, __LINE__)
+#define ASSERT_INRANGE(exp1, exp2, real) assert_interval(exp1, exp2, real, __FILE__, __LINE__)
void assert_null(void* real, const char* caller, int line);
#define ASSERT_NULL(real) assert_null((void*)real, __FILE__, __LINE__)
void assert_not_null(const void* real, const char* caller, int line);
-#define ASSERT_NOT_NULL(real) assert_not_null(real, __FILE__, __LINE__)
+#define ASSERT_NOTNULL(real) assert_not_null(real, __FILE__, __LINE__)
void assert_true(int real, const char* caller, int line);
#define ASSERT_TRUE(real) assert_true(real, __FILE__, __LINE__)
@@ -232,15 +225,17 @@ void assert_fail(const char* caller, int line);
#define ASSERT_FAIL() assert_fail(__FILE__, __LINE__)
void assert_dbl_compare(const char* cmp, double exp, double real, double tol, const char* caller, int line);
-#define ASSERT_DBL_NEAR(exp, real) assert_dbl_compare("==", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__)
-#define ASSERT_DBL_NEAR_TOL(exp, real, tol) assert_dbl_compare("==", exp, real, tol, __FILE__, __LINE__)
-#define ASSERT_DBL_FAR(exp, real) assert_dbl_compare("!=", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__)
-#define ASSERT_DBL_FAR_TOL(exp, real, tol) assert_dbl_compare("!=", exp, real, tol, __FILE__, __LINE__)
-
-#define ASSERT_FLT_NEAR(v1, v2) assert_dbl_compare("==", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__)
-#define ASSERT_FLT_FAR(v1, v2) assert_dbl_compare("!=", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__)
-#define ASSERT_DBL_LT(v1, v2) assert_dbl_compare("<", v1, v2, 0.0, __FILE__, __LINE__)
-#define ASSERT_DBL_GT(v1, v2) assert_dbl_compare(">", v1, v2, 0.0, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_NEAR(exp, real) assert_dbl_compare("==", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_NEAR_TOL(exp, real, tol) assert_dbl_compare("==", exp, real, tol, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_NOTNEAR(exp, real) assert_dbl_compare("!=", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_NOTNEAR_TOL(exp, real, tol) assert_dbl_compare("!=", exp, real, tol, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_LT(v1, v2) assert_dbl_compare("<", v1, v2, 0.0, __FILE__, __LINE__)
+#define ASSERT_DOUBLE_GT(v1, v2) assert_dbl_compare(">", v1, v2, 0.0, __FILE__, __LINE__)
+
+#define ASSERT_FLOAT_NEAR(v1, v2) assert_dbl_compare("==", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__)
+#define ASSERT_FLOAT_NOTNEAR(v1, v2) assert_dbl_compare("!=", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__)
+#define ASSERT_FLOAT_LT(v1, v2) assert_dbl_compare("<", (float)(v1), (float)(v2), 0.0, __FILE__, __LINE__)
+#define ASSERT_FLOAT_GT(v1, v2) assert_dbl_compare(">", (float)(v1), (float)(v2), 0.0, __FILE__, __LINE__)
#ifdef CTEST_MAIN
@@ -404,14 +399,6 @@ void assert_compare(const char* cmp, intmax_t exp, intmax_t real, const char* ca
}
}
-void assert_compare_u(const char* cmp, uintmax_t exp, uintmax_t real, const char* caller, int line) {
- int c3 = (real < exp) - (exp < real);
-
- if (!get_compare_result(cmp, c3, c3 == 0)) {
- CTEST_ERR("%s:%d assertion failed, %" PRIuMAX " %s %" PRIuMAX, caller, line, exp, cmp, real);
- }
-}
-
void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) {
if (real < exp1 || real > exp2) {
CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real);
diff --git a/misc/tests/mytests.c.txt b/misc/tests/mytests.c.txt
new file mode 100644
index 00000000..71824d14
--- /dev/null
+++ b/misc/tests/mytests.c.txt
@@ -0,0 +1,237 @@
+/* Copyright 2011-2023 Bas van den Berg
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include "ctest.h"
+
+// basic test without setup/teardown
+CTEST(suite1, test1) {
+}
+
+// there are many different ASSERT macro's (see ctest.h)
+CTEST(suite1, test2) {
+ ASSERT_EQ(1,2);
+}
+
+CTEST(suite2, test1) {
+ ASSERT_STREQ("foo", "bar");
+}
+
+CTEST(suite3, test3) {
+}
+
+
+// A test suite with a setup/teardown function
+// This is converted into a struct that's automatically passed to all tests in the suite
+CTEST_DATA(memtest) {
+ unsigned char* buffer;
+};
+
+// Optional setup function for suite, called before every test in suite
+CTEST_SETUP(memtest) {
+ CTEST_LOG("%s() data=%p buffer=%p", __func__, (void*)data, (void*)data->buffer);
+ data->buffer = (unsigned char*)malloc(1024);
+}
+
+// Optional teardown function for suite, called after every test in suite
+CTEST_TEARDOWN(memtest) {
+ CTEST_LOG("%s() data=%p buffer=%p", __func__, (void*)data, (void*)data->buffer);
+ if (data->buffer) free(data->buffer);
+}
+
+// These tests are called with the struct* (named data) as argument
+CTEST2(memtest, test1) {
+ CTEST_LOG("%s() data=%p buffer=%p", __func__, (void*)data, (void*)data->buffer);
+}
+
+CTEST2_SKIP(memtest, test3) {
+ (void)data;
+ ASSERT_FAIL();
+}
+
+CTEST2(memtest, test2) {
+ CTEST_LOG("%s() data=%p buffer=%p", __func__, (void*)data, (void*)data->buffer);
+ ASSERT_FAIL();
+}
+
+
+CTEST_DATA(fail) {
+ int unused;
+};
+
+// Asserts can also be used in setup/teardown functions
+CTEST_SETUP(fail) {
+ (void)data;
+ ASSERT_FAIL();
+}
+
+CTEST2(fail, test1) {
+ (void)data;
+}
+
+
+
+CTEST_DATA(weaklinkage) {
+ int number;
+};
+
+// This suite has data, but no setup/teardown
+CTEST2(weaklinkage, test1) {
+ (void)data;
+ CTEST_LOG("%s()", __func__);
+}
+
+CTEST2(weaklinkage, test2) {
+ (void)data;
+ CTEST_LOG("%s()", __func__);
+}
+
+
+CTEST_DATA(nosetup) {
+ int value;
+};
+
+CTEST_TEARDOWN(nosetup) {
+ (void)data;
+ CTEST_LOG("%s()", __func__);
+}
+
+CTEST2(nosetup, test1) {
+ (void)data;
+ CTEST_LOG("%s()", __func__);
+}
+
+
+// more ASSERT examples
+CTEST(ctest, test_assert_str) {
+ ASSERT_STREQ("foo", "foo");
+ ASSERT_STREQ("foo", "bar");
+}
+
+CTEST(ctest, test_assert_equal) {
+ ASSERT_EQ(123, 123);
+ ASSERT_EQ(123, 456);
+}
+
+CTEST(ctest, test_assert_not_equal) {
+ ASSERT_NE(123, 456);
+ ASSERT_NE(123, 123);
+}
+
+CTEST(ctest, test_assert_interval) {
+ ASSERT_INRANGE(10, 20, 15);
+ ASSERT_INRANGE(1000, 2000, 3000);
+}
+
+CTEST(ctest, test_assert_null) {
+ ASSERT_NULL(NULL);
+ ASSERT_NULL((void*)0xdeadbeef);
+}
+
+CTEST(ctest, test_assert_not_null_const) {
+ ASSERT_NOTNULL((const char*)"hallo");
+}
+
+CTEST(ctest, test_assert_not_null) {
+ ASSERT_NOTNULL((void*)0xdeadbeef);
+ ASSERT_NOTNULL(NULL);
+}
+
+CTEST(ctest, test_assert_true) {
+ ASSERT_TRUE(1);
+ ASSERT_TRUE(0);
+}
+
+CTEST(ctest, test_assert_false) {
+ ASSERT_FALSE(0);
+ ASSERT_FALSE(1);
+}
+
+CTEST_SKIP(ctest, test_skip) {
+ ASSERT_FAIL();
+}
+
+CTEST(ctest, test_assert_fail) {
+ ASSERT_FAIL();
+}
+
+/* Test that NULL-strings won't result in segv */
+CTEST(ctest, test_null_null) {
+ ASSERT_STREQ(NULL, NULL);
+}
+
+CTEST(ctest, test_null_string) {
+ ASSERT_STREQ(NULL, "shouldfail");
+}
+
+CTEST(ctest, test_string_null) {
+ ASSERT_STREQ("shouldfail", NULL);
+}
+
+CTEST(ctest, test_string_diff_ptrs) {
+ const char *str = "abc\0abc";
+ ASSERT_STREQ(str, str+4);
+}
+
+CTEST(ctest, test_large_numbers) {
+ unsigned long exp = 4200000000u;
+ ASSERT_EQ(exp, 4200000000u);
+ ASSERT_NE(exp, 1200000000u);
+}
+
+CTEST(ctest, test_ctest_err) {
+ CTEST_ERR("error log");
+}
+
+CTEST(ctest, test_dbl_near) {
+ double a = 0.000111;
+ ASSERT_DOUBLE_NEAR(0.0001, a);
+}
+
+CTEST(ctest, test_dbl_near_tol) {
+ double a = 0.000111;
+ ASSERT_DOUBLE_NEAR_TOL(0.0001, a, 1e-5); /* will fail */
+}
+
+CTEST(ctest, test_dbl_far) {
+ double a = 1.1;
+ ASSERT_DOUBLE_NOTNEAR(1., a);
+ ASSERT_DOUBLE_NOTNEAR_TOL(1., a, 0.01);
+}
+
+CTEST(ctest, test_assert_compare) {
+ ASSERT_LT(123, 456);
+ ASSERT_GE(123, 123);
+ ASSERT_GT(99, 100);
+}
+
+CTEST(ctest, test_dbl_near2) {
+ float a = 0.000001000003f;
+ ASSERT_FLOAT_NEAR(0.000001f, a); /* ok, uses float epsilon -1e-5 */
+ ASSERT_DOUBLE_NEAR_TOL(0.000001, a, -1e-5); /* ok, tol < 0 = relative err (epsilon) */
+ ASSERT_DOUBLE_NEAR(0.000001, a); /* fail, tol = -1e-12 (epsilon) */
+}
+
+CTEST(ctest, test_dbl_compare) {
+ float a = 0.000001000003f;
+ ASSERT_DOUBLE_LT(0.000001, a);
+ ASSERT_DOUBLE_GT(0.000001, a); /* fail */
+}
+
+CTEST(ctest, test_str_contains) {
+ ASSERT_STRNE("Hello", "World");
+ ASSERT_SUBSTR("Hello", "ello");
+ ASSERT_NOTSUBSTR("Hello", "ello");
+}