From: Michael D. Lowis Date: Mon, 21 Mar 2022 18:38:48 +0000 (-0400) Subject: rearranged lib sources and headers a bit. also reactivated unit tests X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=c35babc6165b381b999de7aa272b4adf83adbc52;p=projs%2Ftide.git rearranged lib sources and headers a bit. also reactivated unit tests --- diff --git a/Rsconscript b/Rsconscript index 024959a..f5f966a 100644 --- a/Rsconscript +++ b/Rsconscript @@ -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 index 5ce11c3..0000000 --- a/inc/atf.h +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include - -#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 index 0000000..f24e995 --- /dev/null +++ b/inc/test/atf.h @@ -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 +#include +#include +#include + +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 +#include +#ifndef NO_SIGNALS +#include +#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 */ diff --git a/inc/qcheck.h b/inc/test/qcheck.h similarity index 100% rename from inc/qcheck.h rename to inc/test/qcheck.h diff --git a/src/lib/abspath.c b/src/lib/io/abspath.c similarity index 100% rename from src/lib/abspath.c rename to src/lib/io/abspath.c diff --git a/src/lib/readfd.c b/src/lib/io/readfd.c similarity index 100% rename from src/lib/readfd.c rename to src/lib/io/readfd.c diff --git a/src/lib/readfile.c b/src/lib/io/readfile.c similarity index 100% rename from src/lib/readfile.c rename to src/lib/io/readfile.c diff --git a/src/lib/telem.c b/src/lib/io/telem.c similarity index 100% rename from src/lib/telem.c rename to src/lib/io/telem.c diff --git a/src/lib/writefd.c b/src/lib/io/writefd.c similarity index 100% rename from src/lib/writefd.c rename to src/lib/io/writefd.c diff --git a/tests/lib/buf.c b/tests/lib/buf.c index b5d11e2..b372a87 100644 --- a/tests/lib/buf.c +++ b/tests/lib/buf.c @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include #include #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); diff --git a/tests/lib/utf8.c b/tests/lib/utf8.c index 1ef63db..493223d 100644 --- a/tests/lib/utf8.c +++ b/tests/lib/utf8.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/tests/libedit.c b/tests/libedit.c index 782f76f..fe324a2 100644 --- a/tests/libedit.c +++ b/tests/libedit.c @@ -3,8 +3,12 @@ #include #define INCLUDE_DEFS -#include -#include +#include + +#define INCLUDE_DEFS +#include + +#define INCLUDE_DEFS #include "config.h" int main(int argc, char** argv)