summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/stctest.h (renamed from include/stc/c11ut.h)102
1 files changed, 51 insertions, 51 deletions
diff --git a/include/stc/c11ut.h b/include/stc/stctest.h
index 070fe47d..fe56d4b3 100644
--- a/include/stc/c11ut.h
+++ b/include/stc/stctest.h
@@ -21,7 +21,7 @@
* SOFTWARE.
*/
-/* ctest: A small and simple C11 unit-testing framework.
+/* stctest: A small and simple C11 unit-testing framework.
Features:
- Requires C11. Should work with any C compiler supporting _Generic.
@@ -32,7 +32,7 @@
Example:
- #include "c11ut.h"
+ #include "stctest.h"
#include "mylib.h"
void test_sheep()
@@ -55,8 +55,8 @@
}
*/
-#ifndef STC_C11UT_INCLUDED
-#define STC_C11UT_INCLUDED
+#ifndef STCTEST_INCLUDED
+#define STCTEST_INCLUDED
#include <stdio.h>
#include <time.h>
@@ -65,58 +65,58 @@
#include <math.h>
#include <inttypes.h>
-#define C11UT_FLOAT_LIMIT 0.00000001
+#define STCTEST_FLOAT_LIMIT 0.00000001
#define EXPECT_TRUE(expression) \
- do { if (!_c11ut_assert(__FILE__, __LINE__, #expression, (expression) != 0)) puts(""); } while(0)
+ do { if (!_stctest_assert(__FILE__, __LINE__, #expression, (expression) != 0)) puts(""); } while(0)
#define EXPECT_FALSE(expression) \
- do { if (!_c11ut_assert(__FILE__, __LINE__, #expression, (expression) == 0)) puts(""); } while(0)
+ do { if (!_stctest_assert(__FILE__, __LINE__, #expression, (expression) == 0)) puts(""); } while(0)
-/* NB! Char pointers are compared as strings. Cast to (void*) to compare pointers only */
-#define EXPECT_EQ(a, b) _c11ut_COMPARE(a, ==, b)
-#define EXPECT_NE(a, b) _c11ut_COMPARE(a, !=, b)
-#define EXPECT_GT(a, b) _c11ut_COMPARE(a, >, b)
-#define EXPECT_LT(a, b) _c11ut_COMPARE(a, <, b)
-#define EXPECT_LE(a, b) _c11ut_COMPARE(a, <=, b)
-#define EXPECT_GE(a, b) _c11ut_COMPARE(a, >=, b)
+/* NB! (char*) are compared as strings. Cast to (void*) to compare pointers only */
+#define EXPECT_EQ(a, b) _stctest_COMPARE(a, ==, b)
+#define EXPECT_NE(a, b) _stctest_COMPARE(a, !=, b)
+#define EXPECT_GT(a, b) _stctest_COMPARE(a, >, b)
+#define EXPECT_LT(a, b) _stctest_COMPARE(a, <, b)
+#define EXPECT_LE(a, b) _stctest_COMPARE(a, <=, b)
+#define EXPECT_GE(a, b) _stctest_COMPARE(a, >=, b)
/* Run a test() function */
-#define RUN_TEST(test) do { \
- const int ps = _c11ut_s.passes; \
- const int fs = _c11ut_s.fails; \
+#define RUN_TEST(test, ...) do { \
+ puts(#test "(" #__VA_ARGS__ "):"); \
+ const int ps = _stctest_s.passes; \
+ const int fs = _stctest_s.fails; \
const clock_t start = clock(); \
- printf("%s():\n", #test); \
- test(); \
+ test(__VA_ARGS__); \
printf(" summary: %d/%d passed, duration: %dms\n", \
- _c11ut_s.passes - ps, _c11ut_s.passes + _c11ut_s.fails - (ps + fs), \
+ _stctest_s.passes - ps, _stctest_s.passes + _stctest_s.fails - (ps + fs), \
(int)((clock() - start) * 1000 / CLOCKS_PER_SEC)); \
} while (0)
-#define REPORT_TESTS() c11ut_report()
+#define REPORT_TESTS() stctest_report()
/* ----------------------------------------------------------------------------- */
-#define _c11ut_COMPARE(a, OP, b) \
- do { if (!_c11ut_assert(__FILE__, __LINE__, #a " " #OP " " #b, _c11ut_CMP(a, OP, b))) { \
- char _fmt[32]; sprintf(_fmt, ": %s %s %s\n", _c11ut_FMT(a), #OP, _c11ut_FMT(b)); \
+#define _stctest_COMPARE(a, OP, b) \
+ do { if (!_stctest_assert(__FILE__, __LINE__, #a " " #OP " " #b, _stctest_CMP(a, OP, b))) { \
+ char _fmt[32]; sprintf(_fmt, ": %s %s %s\n", _stctest_FMT(a), #OP, _stctest_FMT(b)); \
printf(_fmt, a, b); \
}} while (0)
-#define _c11ut_CMP(a, OP, b) _Generic((a), \
- const char*: _c11ut_strcmp, char*: _c11ut_strcmp, \
- double: _c11ut_dblcmp, float: _c11ut_dblcmp, \
- default: _c11ut_valcmp)((a) OP (b), #OP, a, b)
+#define _stctest_CMP(a, OP, b) _Generic((a), \
+ const char*: _stctest_strcmp, char*: _stctest_strcmp, \
+ double: _stctest_dblcmp, float: _stctest_dblcmp, \
+ default: _stctest_valcmp)((a) OP (b), #OP, a, b)
-#define _c11ut_FMT(a) _Generic((a), \
+#define _stctest_FMT(a) _Generic((a), \
float: "%.8gf", double: "%.14g", \
int64_t: "%" PRId64 "'i64", int32_t: "%" PRId32 "'i32", int16_t: "%" PRId16 "'i64", int8_t: "%" PRId8 "'i8", \
uint64_t: "%" PRIu64 "'u64", uint32_t: "%" PRIu32 "'u32", uint16_t: "%" PRIu16 "'u16", uint8_t: "%" PRIu8 "'u8", \
char*: "`%s`", const char*: "`%s`", \
default: "{%p}")
-static inline int _c11ut_strcmp(int res, const char* OP, ...) {
+static inline int _stctest_strcmp(int res, const char* OP, ...) {
va_list ap;
va_start(ap, OP);
const char* a = va_arg(ap, const char *);
@@ -132,7 +132,7 @@ static inline int _c11ut_strcmp(int res, const char* OP, ...) {
return c;
}
-static inline int _c11ut_dblcmp(int res, const char* OP, ...) {
+static inline int _stctest_dblcmp(int res, const char* OP, ...) {
va_list ap;
va_start(ap, OP);
double a = va_arg(ap, double);
@@ -140,49 +140,49 @@ static inline int _c11ut_dblcmp(int res, const char* OP, ...) {
double c = a - b;
va_end(ap);
switch (OP[0]) {
- case '=': return fabs(c) < C11UT_FLOAT_LIMIT;
- case '!': return fabs(c) > C11UT_FLOAT_LIMIT;
+ case '=': return fabs(c) < STCTEST_FLOAT_LIMIT;
+ case '!': return fabs(c) > STCTEST_FLOAT_LIMIT;
case '<': return OP[1] == '=' ? c <= 0 : c < 0;
case '>': return OP[1] == '=' ? c >= 0 : c > 0;
}
return res;
}
-static inline int _c11ut_valcmp(int res, const char* OP, ...)
+static inline int _stctest_valcmp(int res, const char* OP, ...)
{ return res; }
-#define _c11ut_COLOR_CODE 0x1B
-#define _c11ut_COLOR_RED "[1;31m"
-#define _c11ut_COLOR_GREEN "[1;32m"
-#define _c11ut_COLOR_RESET "[0m"
+#define _stctest_COLOR_CODE 0x1B
+#define _stctest_COLOR_RED "[1;31m"
+#define _stctest_COLOR_GREEN "[1;32m"
+#define _stctest_COLOR_RESET "[0m"
-static struct c11ut_s {
+static struct stctest_s {
int passes;
int fails;
const char* current_file;
-} _c11ut_s = {0};
+} _stctest_s = {0};
-static int _c11ut_assert(const char* file, int line, const char* expression, int pass) {
+static int _stctest_assert(const char* file, int line, const char* expression, int pass) {
if (pass)
- _c11ut_s.passes++;
+ _stctest_s.passes++;
else {
- _c11ut_s.fails++;
+ _stctest_s.fails++;
printf(" failed: %s:%d, In test (%s)", file, line, expression);
}
- _c11ut_s.current_file = file;
+ _stctest_s.current_file = file;
return pass;
}
-static int c11ut_report(void) {
- if (_c11ut_s.fails) {
+static int stctest_report(void) {
+ if (_stctest_s.fails) {
printf("%c%sFAILED%c%s [%s] (passed:%d, failed:%d, total:%d)\n",
- _c11ut_COLOR_CODE, _c11ut_COLOR_RED, _c11ut_COLOR_CODE, _c11ut_COLOR_RESET,
- _c11ut_s.current_file, _c11ut_s.passes, _c11ut_s.fails, _c11ut_s.passes + _c11ut_s.fails);
- return -_c11ut_s.fails;
+ _stctest_COLOR_CODE, _stctest_COLOR_RED, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
+ _stctest_s.current_file, _stctest_s.passes, _stctest_s.fails, _stctest_s.passes + _stctest_s.fails);
+ return -_stctest_s.fails;
} else {
printf("%c%sPASSED%c%s [%s] (total:%d)\n",
- _c11ut_COLOR_CODE, _c11ut_COLOR_GREEN, _c11ut_COLOR_CODE, _c11ut_COLOR_RESET,
- _c11ut_s.current_file, _c11ut_s.passes);
+ _stctest_COLOR_CODE, _stctest_COLOR_GREEN, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
+ _stctest_s.current_file, _stctest_s.passes);
return 0;
}
}