]> git.mdlowis.com Git - projs/tide.git/commitdiff
rearranged lib sources and headers a bit. also reactivated unit tests
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 21 Mar 2022 18:38:48 +0000 (14:38 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 21 Mar 2022 18:38:48 +0000 (14:38 -0400)
12 files changed:
Rsconscript
inc/atf.h [deleted file]
inc/test/atf.h [new file with mode: 0755]
inc/test/qcheck.h [moved from inc/qcheck.h with 100% similarity]
src/lib/io/abspath.c [moved from src/lib/abspath.c with 100% similarity]
src/lib/io/readfd.c [moved from src/lib/readfd.c with 100% similarity]
src/lib/io/readfile.c [moved from src/lib/readfile.c with 100% similarity]
src/lib/io/telem.c [moved from src/lib/telem.c with 100% similarity]
src/lib/io/writefd.c [moved from src/lib/writefd.c with 100% similarity]
tests/lib/buf.c
tests/lib/utf8.c
tests/libedit.c

index 024959ae3cf79a691786238dff2def2e71ed7f20..f5f966a201cfa6894ceebad148ab8650a3ca930c 100644 (file)
@@ -17,7 +17,7 @@ build do
 
   # Build library and binaries
   env.Library("libtide.a", glob("src/lib/**/*.c"))
-  (glob("src/*.c") - ["src/term.c", "src/tsed.c"]).each do |src|
+  glob("src/*.c").each do |src|
     bin = File.basename(src).sub(/\.[^\.]$/,'')
     env.Program("bin/#{bin}", [src, "libtide.a"])
   end
@@ -28,11 +28,11 @@ build do
     "CMD_DESC" => "Generating Docs")
 
   # Build and run unit tests
-#  env.Program("tests/libedit", %w[tests/libedit.c tests/lib/buf.c tests/lib/utf8.c libtide.a])
-#  env.Command("", "tests/libedit",
-#    "CMD" => ["${_SOURCES}"],
-#    "CMD_DESC" => "TEST")
-#  env.depends("${prefix}/bin", "")
+  env.Program("tests/libedit", %w[tests/libedit.c tests/lib/buf.c tests/lib/utf8.c libtide.a])
+  env.Command("", "tests/libedit",
+    "CMD" => ["${_SOURCES}"],
+    "CMD_DESC" => "TEST")
+  env.depends("${prefix}/bin", "")
 
   # Install the compiled binaries
   Dir.glob("bin/*").each{|bin| env.Install("${prefix}/bin", bin) }
diff --git a/inc/atf.h b/inc/atf.h
deleted file mode 100644 (file)
index 5ce11c3..0000000
--- a/inc/atf.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-#include <stdlib.h>
-
-#ifdef ATF_TEST
-    #define main MAIN
-    #define UNITTEST(name,...) void name(void)
-#else
-    #define UNITTEST(name,...) static inline void name(void)
-#endif
-
-#define CHECK(cond) Assert(cond, #cond, __FILE__, __LINE__)
-
-typedef struct Value {
-    struct Value* next;
-    void (*showfn)(struct Value* val);
-    long ndata;
-    long data[];
-} Value;
-
-void Assert(int cond, char* condstr, char* file, int line);
-void* ValueAlloc(void (*showfn)(Value* val), size_t num, size_t sz, void* data);
-long RandR(long from, long to);
-long Rand(void);
-
-void ShowLong(Value* val);
-void ShowBool(Value* val);
-void ShowChar(Value* val);
-void ShowString(Value* val);
-
-long MkLongR(long from, long to);
-long MkLong(void);
-uint8_t MkU8(void);
-uint16_t MkU16(void);
-uint32_t MkU32(void);
-bool MkBool(void);
-char MkAsciiChar(void);
-char* MkAsciiStringL(size_t len);
-char* MkAsciiString(void);
diff --git a/inc/test/atf.h b/inc/test/atf.h
new file mode 100755 (executable)
index 0000000..f24e995
--- /dev/null
@@ -0,0 +1,160 @@
+/**
+    Aardvark Test Framework - A minimalistic unit testing framework for C.
+
+    Copyright 2014 Michael D. Lowis
+    
+    Permission to use, copy, modify, and/or distribute this software
+    for any purpose with or without fee is hereby granted, provided
+    that the above copyright notice and this permission notice appear
+    in all copies.
+    
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+    WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+    AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+    DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+    TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+    PERFORMANCE OF THIS SOFTWARE.
+*/
+#ifndef ATF_H
+#define ATF_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <setjmp.h>
+#include <assert.h>
+
+extern char* Curr_Test;
+void atf_init(int argc, char** argv);
+void atf_test_start(char* file, unsigned int line, char* name);
+bool atf_test_assert(bool success, char* expr_str, char* file, int line);
+void atf_test_fail(char* expr, char* file, int line);
+int atf_print_results(void);
+
+#define IGNORE(msg) \
+    printf("%s:%d:%s:IGNORE:\n\t%s\n", __FILE__, __LINE__, Curr_Test, msg); break
+
+#define CHECK(expr) \
+    if(atf_test_assert((expr), #expr, __FILE__, __LINE__)) break
+    
+#define CHECK_EXITCODE(code) \
+    CHECK(ExitCode == code)
+
+#define TEST_SUITE(name) \
+    void name(void)
+
+#define TEST(desc) \
+    for(atf_test_start(__FILE__,__LINE__,#desc); Curr_Test != NULL; Curr_Test = NULL)
+
+#define RUN_TEST_SUITE(name) \
+    name();
+
+#define RUN_EXTERN_TEST_SUITE(name) \
+    do { extern TEST_SUITE(name); RUN_TEST_SUITE(name); } while(0)
+    
+#define EXPECT_EXIT \
+    if ((ExitExpected = true, 0 == setjmp(ExitPad)))
+
+/* Function Definitions
+ *****************************************************************************/
+#ifdef INCLUDE_DEFS
+#include <stdio.h>
+#include <stdlib.h>
+#ifndef NO_SIGNALS
+#include <signal.h>
+#endif
+
+char* Curr_Test = NULL;
+char* Curr_File = NULL;
+unsigned int Curr_Line = 0;
+static unsigned int Total = 0;
+static unsigned int Failed = 0;
+bool ExitExpected;
+int ExitCode;
+jmp_buf ExitPad;
+
+#ifndef NO_SIGNALS
+static void handle_signal(int sig) {
+    /* Determine the signal name */
+    char* sig_name = NULL;
+    switch(sig) {
+        case SIGABRT: sig_name = "SIGABRT"; break;
+        case SIGBUS:  sig_name = "SIGBUS";  break;
+        case SIGFPE:  sig_name = "SIGFPE";  break;
+        case SIGILL:  sig_name = "SIGILL";  break;
+        case SIGSEGV: sig_name = "SIGSEGV"; break;
+        case SIGSYS:  sig_name = "SIGSYS";  break;
+        /* If we don't recognize it then just return and let the default handler
+           catch it. */
+        default:      return;
+    }
+    /* Error and exit. No summary will be printed but the user will know which
+       test has crashed. */
+    fprintf(stderr,"%s:%d:0:%s:CRASH (signal: %d - %s)\n", Curr_File, Curr_Line, Curr_Test, sig, sig_name);
+    Failed++;
+    (void)atf_print_results();
+    _Exit(1);
+}
+#endif
+
+void atf_init(int argc, char** argv) {
+    /* I reserve the right to use these later */
+    (void)argc;
+    (void)argv;
+
+#ifndef NO_SIGNALS
+    /* Init signal handler */
+    signal(SIGABRT, handle_signal);
+    signal(SIGBUS,  handle_signal);
+    signal(SIGFPE,  handle_signal);
+    signal(SIGILL,  handle_signal);
+    signal(SIGSEGV, handle_signal);
+    signal(SIGSYS,  handle_signal);
+#endif
+}
+
+void atf_test_start(char* file, unsigned int line, char* name) {
+    Curr_File = file;
+    Curr_Line = line;
+    Curr_Test = name;
+    Total++;
+}
+
+bool atf_test_assert(bool success, char* expr, char* file, int line) {
+    bool failed = !success;
+    if (failed) atf_test_fail(expr,file,line);
+    return failed;
+}
+
+void atf_test_fail(char* expr, char* file, int line) {
+    Failed++;
+    printf("%s:%d:0:%s:FAIL:( %s )\n", file, line, Curr_Test, expr); \
+}
+
+int atf_print_results(void) {
+    static const char* results_string =
+    "\nUnit Test Summary"
+    "\n-----------------"
+    "\nTotal:  %d"
+    "\nPassed: %d"
+    "\nFailed: %d"
+    "\n\n";
+    printf(results_string, Total, Total - Failed, Failed);
+    return Failed;
+}
+
+void exit(int code) {
+    if (ExitExpected) {
+        ExitCode = code;
+        ExitExpected = false;
+        longjmp(ExitPad, 1);
+    } else {
+        assert(!"Unexpected exit. Something went wrong");
+    }
+}
+
+#undef INCLUDE_DEFS
+#endif
+
+#endif /* ATF_H */
similarity index 100%
rename from inc/qcheck.h
rename to inc/test/qcheck.h
similarity index 100%
rename from src/lib/abspath.c
rename to src/lib/io/abspath.c
similarity index 100%
rename from src/lib/readfd.c
rename to src/lib/io/readfd.c
similarity index 100%
rename from src/lib/readfile.c
rename to src/lib/io/readfile.c
similarity index 100%
rename from src/lib/telem.c
rename to src/lib/io/telem.c
similarity index 100%
rename from src/lib/writefd.c
rename to src/lib/io/writefd.c
index b5d11e2d31c3805242e7c6be124a15fd59dc96af..b372a8799ab46c20c86801bd559cdfa45ef30386 100644 (file)
@@ -1,6 +1,6 @@
 #include <stdc.h>
-#include <atf.h>
-#include <qcheck.h>
+#include <test/atf.h>
+#include <test/qcheck.h>
 #include <utf.h>
 #include <edit.h>
 #include "config.h"
@@ -204,17 +204,6 @@ TEST_SUITE(BufferTests)
         CHECK(!strcmp(TestBuf.path, "testdocs/waf"));
     }
 
-    TEST(buf_load should remove ./ from file path)
-    {
-        buf_init(&TestBuf);
-        buf_load(&TestBuf, "./testdocs/lorem.txt");
-        CHECK(TestBuf.status      != MODIFIED);
-        CHECK(TestBuf.contents.bufsize     == 61440);
-        CHECK(TestBuf.log.undo        == NULL);
-        CHECK(TestBuf.log.redo        == NULL);
-        CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt"));
-    }
-
     TEST(buf_reload should reload the file from disk)
     {
         buf_init(&TestBuf);
index 1ef63dbd55d2e69d31f5bdb8f6355014b1b7a55f..493223dfd03370730ed2af9308e64369d60d2076 100644 (file)
@@ -1,4 +1,4 @@
-#include <atf.h>
+#include <test/atf.h>
 #include <stdc.h>
 #include <utf.h>
 #include <edit.h>
index 782f76f874d2af9a2936a680eb497106a86a0343..fe324a22721b0d89cfe16c24b3cbc73421042fdb 100644 (file)
@@ -3,8 +3,12 @@
 #include <edit.h>
 
 #define INCLUDE_DEFS
-#include <atf.h>
-#include <qcheck.h>
+#include <test/atf.h>
+
+#define INCLUDE_DEFS
+#include <test/qcheck.h>
+
+#define INCLUDE_DEFS
 #include "config.h"
 
 int main(int argc, char** argv)