caio.co/de/fuzzyclock


Improve test_oclock to check fuzzyness by Caio 12 years ago (log)

Blob tests/check_fuzzyclock.c

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
114
115
116
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
#include "../src/fuzzyclock.h"

START_TEST(test_get_hour_string) {
    fail_if(strcmp(get_hour_string(1), "one") != 0, NULL);
    fail_if(strcmp(get_hour_string(2), "two") != 0, NULL);
    fail_if(strcmp(get_hour_string(3), "three") != 0, NULL);
    fail_if(strcmp(get_hour_string(4), "four") != 0, NULL);
    fail_if(strcmp(get_hour_string(5), "five") != 0, NULL);
    fail_if(strcmp(get_hour_string(6), "six") != 0, NULL);
    fail_if(strcmp(get_hour_string(7), "seven") != 0, NULL);
    fail_if(strcmp(get_hour_string(8), "eight") != 0, NULL);
    fail_if(strcmp(get_hour_string(9), "nine") != 0, NULL);
    fail_if(strcmp(get_hour_string(10), "ten") != 0, NULL);
    fail_if(strcmp(get_hour_string(11), "eleven") != 0, NULL);
    fail_if(strcmp(get_hour_string(12), "twelve") != 0, NULL);
}
END_TEST

void fuzzy_time_test_gen(int hour, int min, int start, int end,
                         char* expect_mask, int iter_on_hour) {

    char buff[MAX_MSG_SIZE];
    char test[MAX_MSG_SIZE];
    time_t now = time(NULL);
    struct tm* clock = localtime(&now);
    int i = 0;

    clock->tm_min = min;
    clock->tm_hour = hour;

    for (i = start; i < end; i++) {
        if (iter_on_hour) { clock->tm_hour = i; }
        else { clock->tm_min = i; }

        snprintf(test, MAX_MSG_SIZE, expect_mask,
                 get_hour_string((iter_on_hour) ? i : hour));

        get_fuzzy_time(clock, buff);

        fail_if(strcmp(test, buff) != 0, "T%0d:WANTED \"%s\", GOT \"%s\"", i, test, buff);
    }
}

START_TEST(test_oclock) {
    int i;
    for (i = 0; i < 3; i++) {
        fuzzy_time_test_gen(1, i, 1, 13, "%s o' clock", 1);
    }
}
END_TEST

START_TEST(test_fivepast) {
    fuzzy_time_test_gen(1, 0, 3, 8, "five past %s", 0);
}
END_TEST

START_TEST(test_tenpast) {
    fuzzy_time_test_gen(1, 0, 8, 13, "ten past %s", 0);
}
END_TEST

START_TEST(test_quarterpast) {
    fuzzy_time_test_gen(1, 0, 13, 18, "quarter past %s", 0);
}
END_TEST

START_TEST(test_twentypast) {
    fuzzy_time_test_gen(1, 0, 18, 23, "twenty past %s", 0);
}
END_TEST

START_TEST(test_twentyfivepast) {
    fuzzy_time_test_gen(1, 0, 23, 28, "twenty five past %s", 0);
}
END_TEST

START_TEST(test_haltpast) {
    fuzzy_time_test_gen(1, 0, 28, 33, "half past %s", 0);
}
END_TEST

Suite* fuzzy_suite(void) {
    Suite* suite = suite_create("fuzzyclock");

    TCase* tc_core = tcase_create("Core");

    tcase_add_test(tc_core, test_get_hour_string);
    tcase_add_test(tc_core, test_oclock);
    /* tcase_add_test(tc_core, test_fivepast); */
    /* tcase_add_test(tc_core, test_tenpast); */
    /* tcase_add_test(tc_core, test_quarterpast); */
    /* tcase_add_test(tc_core, test_twentypast); */
    /* tcase_add_test(tc_core, test_twentyfivepast); */
    /* tcase_add_test(tc_core, test_haltpast); */

    suite_add_tcase(suite, tc_core);

    return suite;
}

int main(int argc, char const* argv[])
{
    int num_failed = 0;
    Suite* suite = fuzzy_suite();
    SRunner* runner = srunner_create(suite);
    srunner_run_all(runner, CK_NORMAL);
    num_failed = srunner_ntests_failed(runner);
    srunner_free(runner);
    return (num_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}