~donmcc/astr

ref: f4dd4ca4751396f256f3b9dd6c65c265c1a33ef1 astr/astr_tests.c -rw-r--r-- 1.9 KiB
f4dd4ca4Don McCaughey Change "formatted" to "format". 1 year, 10 months ago
                                                                                
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
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include "astr.h"


static void
test_astr_alloc_empty(void)
{
    char *s = astr_alloc_empty();
    assert(s);
    assert(0 == strlen(s));
    free(s);
}


static void
test_astr_alloc_format(void)
{
    char *s = astr_alloc_format("answer: %i", 42);
    assert(s);
    assert(10 == strlen(s));
    assert(astr_eq_bytes("answer: 42", s));
    free(s);
}


static void
test_astr_alloc_format_for_NULL_format(void)
{
    errno = 0;
    char *s = astr_alloc_format(NULL);
    assert(!s);
    assert(EINVAL == errno);
    free(s);
}


static char *
helper_alloc_format(char const *format, ...)
{
    va_list arguments;
    va_start(arguments, format);
    char *s = astr_alloc_format_args(format, arguments);
    va_end(arguments);
    return s;
}


static void
test_astr_alloc_format_args(void)
{
    char *s = helper_alloc_format("answer: %i", 42);
    assert(s);
    assert(10 == strlen(s));
    assert(astr_eq_bytes("answer: 42", s));
    free(s);
}


static void
test_astr_alloc_format_args_for_NULL_format(void)
{
    errno = 0;
    char *s = helper_alloc_format(NULL);
    assert(!s);
    assert(EINVAL == errno);
    free(s);
}


static void
test_astr_eq_bytes(void)
{
    char *s = astr_alloc_format("foo");

    assert(astr_eq_bytes("foo", s));
    assert(!astr_eq_bytes(s, "bar"));

    free(s);
}


static void
test_astr_eq_bytes_for_NULL_string(void)
{
    char *s = astr_alloc_format("foo");

    assert(!astr_eq_bytes(s, NULL));
    assert(!astr_eq_bytes(NULL, s));
    assert(astr_eq_bytes(NULL, NULL));

    free(s);
}


int
main(int argc, char *argv[])
{
    test_astr_alloc_empty();
    test_astr_alloc_format();
    test_astr_alloc_format_for_NULL_format();
    test_astr_alloc_format_args();
    test_astr_alloc_format_args_for_NULL_format();
    test_astr_eq_bytes();
    test_astr_eq_bytes_for_NULL_string();
    return EXIT_SUCCESS;
}