commit 61fa96b7a8728967bf278cfbeed31806e62159eb
Author: Yersa Nordman <yersa@finwo.nl>
Date: Mon, 23 Jan 2023 21:44:06 +0100
Project init
Diffstat:
5 files changed, 237 insertions(+), 0 deletions(-)
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
@@ -0,0 +1,6 @@
+<!-- 46b43825-f791-485e-9445-415ee7bbbf2d -->
+# Contributor Code of Conduct
+
+This project adheres to No Code of Conduct. We are all adults. We accept anyone's contributions. Nothing else matters.
+
+For more information please visit the [No Code of Conduct](https://github.com/domgetter/NCoC) homepage.
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2023 finwo
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.html b/README.html
@@ -0,0 +1,45 @@
+<h1>C-Assert</h1>
+<p>Single-file unit-testing library for C</p>
+<h2>Features</h2>
+<ul>
+<li>Single header file, no other library dependencies</li>
+<li>Simple ANSI C. The library should work with virtually every C(++) compiler on
+virtually any playform</li>
+<li>Reporting of assertion failures, including the expression and location of the
+failure</li>
+<li>Stops test on first failed assertion</li>
+<li>ANSI color output for maximum visibility</li>
+<li>Easily embeddable in applications for runtime tests or separate testing
+applications</li>
+</ul>
+<h2>Todo</h2>
+<ul>
+<li>Disable assertions on definition, to allow production build without source modifications</li>
+</ul>
+<h2>Example Usage</h2>
+<pre><code class="language-c">#include "test.h"
+#include "mylib.h"
+
+void test_sheep() {
+ ASSERT("Sheep are cool", are_sheep_cool());
+ ASSERT_EQUALS(4, sheep.legs);
+}
+
+void test_cheese() {
+ ASSERT("Cheese is tangy", cheese.tanginess > 0);
+ ASSERT_STRING_EQUALS("Wensleydale", cheese.name);
+}
+
+int main() {
+ RUN(test_sheep);
+ RUN(test_cheese);
+ return TEST_REPORT();
+}
+</code></pre>
+<p>To run the tests, compile the tests as a binary and run it.</p>
+<h2>Credits</h2>
+<p>This library was heavily based on the
+<a href="https://github.com/joewalnes/tinytest">tinytest</a> library by
+<a href="https://github.com/joewalnes">Joe Walnes</a>. A license reference to his library
+could not be found, which is why this reference is in this file. Should I be
+contacted about licensing issues, I'll investigate further.</p>
diff --git a/README.md b/README.md
@@ -0,0 +1,57 @@
+C-Assert
+========
+
+Single-file unit-testing library for C
+
+Features
+--------
+
+- Single header file, no other library dependencies
+- Simple ANSI C. The library should work with virtually every C(++) compiler on
+ virtually any playform
+- Reporting of assertion failures, including the expression and location of the
+ failure
+- Stops test on first failed assertion
+- ANSI color output for maximum visibility
+- Easily embeddable in applications for runtime tests or separate testing
+ applications
+
+Todo
+----
+
+- Disable assertions on definition, to allow production build without source modifications
+
+Example Usage
+-------------
+
+```c
+#include "test.h"
+#include "mylib.h"
+
+void test_sheep() {
+ ASSERT("Sheep are cool", are_sheep_cool());
+ ASSERT_EQUALS(4, sheep.legs);
+}
+
+void test_cheese() {
+ ASSERT("Cheese is tangy", cheese.tanginess > 0);
+ ASSERT_STRING_EQUALS("Wensleydale", cheese.name);
+}
+
+int main() {
+ RUN(test_sheep);
+ RUN(test_cheese);
+ return TEST_REPORT();
+}
+```
+
+To run the tests, compile the tests as a binary and run it.
+
+Credits
+-------
+
+This library was heavily based on the
+[tinytest](https://github.com/joewalnes/tinytest) library by
+[Joe Walnes](https://github.com/joewalnes). A license reference to his library
+could not be found, which is why this reference is in this file. Should I be
+contacted about licensing issues, I'll investigate further.
diff --git a/include/test.h b/include/test.h
@@ -0,0 +1,122 @@
+/* Based on http://github.com/joewalnes/tinytest
+ *
+ * TinyTest: A really really really tiny and simple no-hassle C unit-testing framework.
+ *
+ * Features:
+ * - No library dependencies. Not even itself. Just a header file.
+ * - Simple ANSI C. Should work with virtually every C or C++ compiler on
+ * virtually any platform.
+ * - Reports assertion failures, including expressions and line numbers.
+ * - Stops test on first failed assertion.
+ * - ANSI color output for maximum visibility.
+ * - Easy to embed in apps for runtime tests (e.g. environment tests).
+ *
+ * Example Usage:
+ *
+ * #include "tinytest.h"
+ * #include "mylib.h"
+ *
+ * void test_sheep()
+ * {
+ * ASSERT("Sheep are cool", are_sheep_cool());
+ * ASSERT_EQUALS(4, sheep.legs);
+ * }
+ *
+ * void test_cheese()
+ * {
+ * ASSERT("Cheese is tangy", cheese.tanginess > 0);
+ * ASSERT_STRING_EQUALS("Wensleydale", cheese.name);
+ * }
+ *
+ * int main()
+ * {
+ * RUN(test_sheep);
+ * RUN(test_cheese);
+ * return TEST_REPORT();
+ * }
+ *
+ * To run the tests, compile the tests as a binary and run it.
+ *
+ * Project home page: http://github.com/joewalnes/tinytest
+ *
+ * 2010, -Joe Walnes <joe@walnes.com> http://joewalnes.com
+ */
+
+#ifndef __TINYTEST_INCLUDED_H__
+#define __TINYTEST_INCLUDED_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Main assertion method */
+#define ASSERT(msg, expression) if (!tap_assert(__FILE__, __LINE__, (msg), (#expression), (expression) ? 1 : 0)) return
+
+/* Convenient assertion methods */
+/* TODO: Generate readable error messages for assert_equals or assert_str_equals */
+#define ASSERT_EQUALS(expected, actual) ASSERT((#actual), (expected) == (actual))
+#define ASSERT_STRING_EQUALS(expected, actual) ASSERT((#actual), strcmp((expected),(actual)) == 0)
+
+/* Run a test() function */
+#define RUN(test_function) tap_execute((#test_function), (test_function))
+#define TEST_REPORT() tap_report()
+
+#define TAP_COLOR_CODE 0x1B
+#define TAP_COLOR_RED "[1;31m"
+#define TAP_COLOR_GREEN "[1;32m"
+#define TAP_COLOR_RESET "[0m"
+
+int tap_asserts = 0;
+int tap_passes = 0;
+int tap_fails = 0;
+const char *tap_current_name = NULL;
+
+void tap_execute(const char* name, void (*test_function)()) {
+ tap_current_name = name;
+ printf("# %s\n", name);
+ test_function();
+}
+
+int tap_assert(const char* file, int line, const char* msg, const char* expression, int pass) {
+ tap_asserts++;
+
+ if (pass) {
+ tap_passes++;
+ printf("%c%sok%c%s %d - %s\n",
+ TAP_COLOR_CODE, TAP_COLOR_GREEN,
+ TAP_COLOR_CODE, TAP_COLOR_RESET,
+ tap_asserts,
+ msg
+ );
+ } else {
+ tap_fails++;
+ printf(
+ "%c%snot ok%c%s %d - %s\n"
+ " On %s:%d, in test %s()\n"
+ " %s\n"
+ ,
+ TAP_COLOR_CODE, TAP_COLOR_RED,
+ TAP_COLOR_CODE, TAP_COLOR_RESET,
+ tap_asserts, msg,
+ file, line, tap_current_name,
+ expression
+ );
+ }
+
+ return pass;
+}
+
+int tap_report(void) {
+ printf(
+ "1..%d\n"
+ "# tests %d\n"
+ "# pass %d\n"
+ "# fail %d\n",
+ tap_asserts,
+ tap_asserts,
+ tap_passes,
+ tap_fails
+ );
+ return tap_fails ? 2 : 0;
+}
+
+#endif // __TINYTEST_INCLUDED_H__