1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "testcase.h"
#include <utils/printcode.h>

struct CaseNode
{
    struct TestCase
    {
        struct CodeNode
        {
            const char *line;
            struct CodeNode *next;
        } * codeNode[TC_NUM_CODE_BLOCK], *codeNodeLast[TC_NUM_CODE_BLOCK];
    } testCase;
    struct CaseNode *next;
} * caseNode, *caseNodeLast;

struct TestCase *
addTestCase()
{
    struct CaseNode *cn = malloc(sizeof(*caseNode));
    if (!cn) {
        fprintf(stderr, "addTestCase: out of memory\n");
        exit(1);
    }

    cn->testCase.codeNode[TC_INIT] = 0;
    cn->testCase.codeNode[TC_TEST] = 0;
    cn->testCase.codeNode[TC_CHECK] = 0;
    cn->next = 0;

    if (caseNode) {
        caseNodeLast = caseNodeLast->next = cn;
    } else {
        caseNode = caseNodeLast = cn;
    }
    return &cn->testCase;
}

void
appendTestCaseCode(struct TestCase *testCase, enum TestCaseCodeBlock codeBlock,
                   const struct Str *code)
{
    assert(codeBlock >= 0 && codeBlock < TC_NUM_CODE_BLOCK);
    assert(testCase);

    char *line = strndup(code->cstr, code->end - code->cstr);
    struct CodeNode *cn = malloc(sizeof(*cn));
    if (!cn || !line) {
        fprintf(stderr, "addTestCase: out of memory\n");
        exit(1);
    }
    cn->line = line;
    cn->next = 0;

    if (testCase->codeNode[codeBlock]) {
        testCase->codeNodeLast[codeBlock] =
          testCase->codeNodeLast[codeBlock]->next = cn;
    } else {
        testCase->codeNode[codeBlock] = testCase->codeNodeLast[codeBlock] = cn;
    }
}

void
printTestCaseList(FILE *out)
{
    size_t count = 1;
    for (const struct CaseNode *n = caseNode; n; n = n->next, ++count) {
        printCode(out, 0, "bool\n");
        printCode(out, 0, "check%04zu()\n", count);
        printCode(out, 0, "{\n");

        struct TestCase tc = n->testCase;
        for (struct CodeNode *cn = tc.codeNode[TC_INIT]; cn; cn = cn->next) {
            printCode(out, 0, "%s\n", cn->line);
        }
        printCode(out, 0, "\n");

        for (struct CodeNode *cn = tc.codeNode[TC_TEST]; cn; cn = cn->next) {
            printCode(out, 1, "ulm_load_str(\"%s\");\n", cn->line);
        }
        printCode(out, 0, "\n");
        printCode(out, 1, "run();\n");
        printCode(out, 0, "\n");

        for (struct CodeNode *cn = tc.codeNode[TC_CHECK]; cn; cn = cn->next) {
            printCode(out, 0, "%s\n", cn->line);
        }
        printCode(out, 0, "}\n");
    }
    printCode(out, 0, "\n");

    printCode(out, 0, "int\n");
    printCode(out, 0, "main(int argc, char **argv)\n", count);
    printCode(out, 0, "{\n");
    count = 1;
    for (const struct CaseNode *n = caseNode; n; n = n->next, ++count) {
        printCode(out, 1, "if (! check%04zu()) {\n", count);
        printCode(out, 2,
                  "fprintf(stderr, \"%%s: case %04zu failed\\n\", argv[0]);\n",
                  count);
        printCode(out, 2, "exit(1);\n");
        printCode(out, 1, "}\n");
    }
    printCode(
      out, 1,
      "fprintf(stderr, \"%%s: *** all tests passed ***\\n\", argv[0]);\n");
    printCode(out, 0, "}\n");
}