first test master
authorNiki Roo <niki@nikiroo.be>
Wed, 17 Jul 2024 09:01:39 +0000 (11:01 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 17 Jul 2024 09:01:39 +0000 (11:01 +0200)
src/tests-cbook/main.c
src/tests-cbook/test-pl1.c [new file with mode: 0644]

index 352733302ef17a09c46ebf54435893ddfb915d30..55a83fafe34c0bcd74e54f9c674054a94c3788ce 100644 (file)
@@ -3,14 +3,12 @@
 
 // The tests we intend to run:
 
-// SRunner *test_XXX(SRunner *runner, int more);
-// ...
+SRunner *test_pl1(SRunner *runner, int more);
 
 SRunner *get_tests(int more) {
        SRunner *runner = NULL;
        
-       // runner = test_XXX(runner, more);
-       // ...
+       runner = test_pl1(runner, more);
        
        return runner;
 }
diff --git a/src/tests-cbook/test-pl1.c b/src/tests-cbook/test-pl1.c
new file mode 100644 (file)
index 0000000..989533d
--- /dev/null
@@ -0,0 +1,99 @@
+/* Note: fmemopen, open_memstream require gnu99 standard instead of c99 */
+
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include <check.h>
+
+#include "cutils/check/launcher.h"
+#include "cutils/array.h"
+
+#include "cbook/cbook.h"
+
+static void setup() {
+}
+
+static void teardown() {
+}
+
+static void reset() {
+       teardown();
+       setup();
+}
+
+static void print_line(line_t *line);
+static void print_line(line_t *line) {
+       if (!line) {
+               printf("DCL: (null)\n");
+               return;
+       }
+       
+       printf("DCL ");
+       if (line->level)
+       for (int i = 0 ; i < line->level ; i++)
+               printf("\t");
+       printf("%i %s: type %i (size: %zu = %zu bytes)\n",
+               line->level,
+               line->name,
+               line->type,
+               line->size,
+               line->bytes
+       );
+}
+
+START(simple_read_book)
+       char book_content[] = 
+       " DCL 1 book,          \n"
+       "     2 titi CHAR(10), \n"
+       " *;                   \n";
+
+       book_t *book = new_book();
+       FILE *fbook = fmemopen((void*)book_content, strlen(book_content), "rb");
+
+       int ok = read_book(fbook, book);
+       if (!ok)
+               failure("Simple book failed to be read");
+       
+       assert_str("Book name", "BOOK", book->name);
+       if (book->err_line || book->err_field || book->err_mess) 
+               failure("Unexpected error at line %zu in field %s: %s\n", 
+                       book->err_line, book->err_field, book->err_mess);
+       assert_sz("Book size in bytes", 10, book->bytes);
+       assert_sz("Number of lines 1", 1, book->lines->count);
+       line_t *line = *((line_t **)(array_first(book->lines)));
+       assert_int("Type of line", CBOOK_FMT_CHAR, line->type);
+       assert_sz("Number of sub lines", 0, line->children->count);
+
+       fclose(fbook);
+END
+
+SRunner *test_pl1(SRunner *runner, int more) {
+       Suite *suite = suite_create("pl/1");
+
+       TCase *core = tcase_create("core");
+       tcase_add_checked_fixture(core, setup, teardown);
+       
+       tcase_add_test(core, simple_read_book);
+       // ...
+       
+       suite_add_tcase(suite, core);
+
+       if (!runner)
+               runner = srunner_create(suite);
+       else
+               srunner_add_suite(runner, suite);
+       
+       if (more) {
+               Suite *suite = suite_create("pl/1 -- more (longer)");
+
+               TCase *tmore = tcase_create("more");
+               tcase_add_checked_fixture(tmore, setup, teardown);
+               // TODO
+
+               suite_add_tcase(suite, tmore);
+               srunner_add_suite(runner, suite);
+       }
+
+       return runner;
+}
+