summaryrefslogtreecommitdiffstats
path: root/lib/x1000-installer/test/main.c
blob: 4d16271c98f3ca1d8956f381cbe7af034fb3aeee (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>

static int test_num_executed = 0;
static int test_num_failed = 0;
int test_num_asserts_executed = 0;
int test_num_asserts_failed = 0;

void test_failure(const char* file, int line, const char* msg)
{
    fprintf(stderr, "%s:%d: ASSERTION FAILED: %s\n", file, line, msg);
    ++test_num_asserts_failed;
}

typedef void(*test_t)(void);

struct test_info {
    const char* name;
    test_t func;
};

#define TEST(x) {#x, x}
static const struct test_info all_tests[] = {
};
#undef TEST

void run_test(const struct test_info* tinfo)
{
    int asserts_now = test_num_asserts_failed;
    ++test_num_executed;

    fprintf(stderr, "RUN %s\n", tinfo->name);
    tinfo->func();

    if(test_num_asserts_failed > asserts_now) {
        fprintf(stderr, "  %s: FAILED!\n", tinfo->name);
        ++test_num_failed;
    }
}

int main(int argc, char* argv[])
{
    (void)argc;
    (void)argv;

    size_t num_tests = sizeof(all_tests) / sizeof(struct test_info);
    for(size_t i = 0; i < num_tests; ++i)
        run_test(&all_tests[i]);

    fprintf(stderr, "------------------------------------------\n");
    fprintf(stderr, "TEST COMPLETE\n");
    fprintf(stderr, "  Tests            %d failed / %d executed\n",
            test_num_failed, test_num_executed);
    fprintf(stderr, "  Assertions       %d failed / %d executed\n",
            test_num_asserts_failed, test_num_asserts_executed);

    if(test_num_failed > 0)
        return 1;
}