summaryrefslogtreecommitdiff
path: root/cesar/lib/test/test/src/test_test.c
blob: 1f3ef9df94efc866060cbd010220e82c48b1bd8c (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
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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_test.c
 * \brief   Test the test infrastructure.
 * \ingroup test
 */
#include "common/std.h"

#include "lib/test.h"

bool
mytest_ok_test_case (test_t t)
{
    uint old_attempted = test_nb_attempted (t);
    uint old_failed = test_nb_failed (t);
    test_case_begin (t, "ok");
    test_begin (t, "test no fail")
    {
    } test_end;
    test_begin (t, "test_fail_unless ok")
    {
        test_fail_unless (1 == 1);
    } test_end;
    test_begin (t, "catch assert")
    {
        dbg_fatal_try_begin
        {
            dbg_assert (1 == 2);
        }
        dbg_fatal_try_catch (const char *the_error)
        {
            test_verbose_print ("catched: %s", the_error);
        }
        dbg_fatal_try_end;
    } test_end;
    return test_nb_attempted (t) == old_attempted + 3
        && test_nb_failed (t) == old_failed + 0;
}

bool
mytest_nok_test_case (test_t t)
{
    uint i;
    uint old_attempted = test_nb_attempted (t);
    uint old_failed = test_nb_failed (t);
    test_case_begin (t, "nok");
    test_begin (t, "test_fail_unless nok")
    {
        test_fail_unless (1 == 2);
    } test_end;
    test_begin (t, "test_fail_if print nok")
    {
        test_fail_if (1 != 2, "%d is not %d", 1, 2);
    } test_end;
    test_begin (t, "test_fail_if loop nok after 5")
    {
        for (i = 0; i < 10; i++)
        {
            test_verbose_print ("i = %d", i);
            if (i > 5)
                test_fail ("fail at i = %d", i);
        }
    } test_end;
    test_begin (t, "no catch assert")
    {
        dbg_assert (1 == 2);
    } test_end;
    test_begin (t, "division by zero")
    {
        uint a = 1;
        uint b = 0;
        uint c = a / b;
        test_verbose_print ("can you compute this: %d", c);
    } test_end;
    test_begin (t, "segmentation fault")
    {
        *(u32 *) NULL = 0;
    } test_end;
    return test_nb_attempted (t) == old_attempted + 6
        && test_nb_failed (t) == old_failed + 6;
}

bool
mytest_test_suite (test_t t)
{
    test_suite_begin (t, "mytest");
    bool success_ok = mytest_ok_test_case (t);
    bool success_nok = mytest_nok_test_case (t);
    return success_ok && success_nok;
}

int
main (int argc, char **argv)
{
    test_t t;
    test_init (t, argc, argv);
    bool success = mytest_test_suite (t);
    test_result (t);
    return success ? 0 : 1;
}