summaryrefslogtreecommitdiff
blob: 58b098ffd6fd8490649ee694273e8af3f747d9dc (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
#include "memento.h"
#include "xml.h"

#include <errno.h>
#include <stdio.h>


static int s_num_fails = 0;

static void s_check(
        int         values_equal,
        const char* text,
        int         ret,
        const char* value_s,
        int         errno_,
        const char* value_expected_s,
        int         errno_expected
        )
{
    int ok;
    if (errno_expected) {
        ok = (ret == -1 && errno_ == errno_expected);
    }
    else {
        ok = (ret == 0 && values_equal);
    }
    
    if (ok) printf("    ok:  ");
    else printf("    fail:");
    printf(" text=%16s", text);
    if (errno_expected) printf(" errno_expected=%6i", errno_expected);
    else                printf(" value_expected=%6s", value_expected_s);
    printf(". result: ret=%2i value=%6s errno=%3i", ret, value_s, errno_);
    printf(".\n");
    if (!ok) s_num_fails += 1;
}

static void s_check_int(const char* text, int value_expected, int expected_errno)
{
    int     value;
    int     ret = extract_xml_str_to_int(text, &value);
    char    value_s[32];
    char    value_expected_s[32];
    snprintf(value_s, sizeof(value_s), "%i", value);
    snprintf(value_expected_s, sizeof(value_expected_s), "%i", value_expected);
    s_check(value == value_expected, text, ret, value_s, errno, value_expected_s, expected_errno);
    return;
}

static void s_check_uint(const char* text, unsigned expected_value, int expected_errno)
{
    unsigned    value;
    int         ret = extract_xml_str_to_uint(text, &value);
    char        value_s[32];
    char        value_expected_s[32];
    snprintf(value_s, sizeof(value_s), "%u", value);
    snprintf(value_expected_s, sizeof(value_expected_s), "%u", value);
    s_check(value == expected_value, text, ret, value_s, errno, value_expected_s, expected_errno);
    return;
}

int main(void)
{
    printf("testing extract_xml_str_to_int():\n");
    s_check_int("2", 2, 0);
    s_check_int("-20", -20, 0);
    s_check_int("-20b", 0, EINVAL);
    s_check_int("123456789123", 0, ERANGE);
    
    printf("testing extract_xml_str_to_uint():\n");
    s_check_uint("2", 2, 0);
    s_check_uint("-20", 0, ERANGE);
    s_check_uint("-20b", 0, EINVAL);
    s_check_uint("123456789123", 0, ERANGE);
    
    printf("s_num_fails=%i\n", s_num_fails);
    
    if (s_num_fails) {
        printf("Failed\n");
        return 1;
    }
    else {
        printf("Succeeded\n");
        return 0;
    }
}