summaryrefslogtreecommitdiff
path: root/app/dynstring/unittest/DynStringTest.c
blob: 33d4c9ab501e49be35d258b35096ff85b75fd684 (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
107
108
/** \file DynStringTest.c
* Unit tests for the dynstring library
*/

#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <cmocka.h>

#include "../dynstring.h"

#define TEXT1 "Pastry gummi bears candy canes jelly beans macaroon choc"
#define TEXT2 "olate jelly beans. Marshmallow cupcake tart jelly apple pie sesame snaps ju"
#define TEXT3 "jubes. Tootsie roll dessert gummi bears jelly."

static void PrintfString(void **state)
{
    DynString string;

    (void)state;
    DynStringMalloc(&string, 0);
    DynStringPrintf(&string, "%d", 1);
    assert_string_equal(DynStringToCStr(&string), "1");
    DynStringFree(&string);
}

static void CopyString(void **state)
{
    DynString string;
    DynString string2;
    (void)state;
    DynStringMalloc(&string, 0);
    DynStringCatCStr(&string, TEXT1);
    DynStringDupStr(&string2, &string);
    assert_int_equal(DynStringSize(&string2), strlen(TEXT1));
    assert_string_equal(DynStringToCStr(&string2), TEXT1);
    DynStringFree(&string2);
    DynStringMalloc(&string2, 0);
    DynStringCatCStr(&string2, TEXT2);
    DynStringCatStr(&string, &string2);
    assert_int_equal(DynStringSize(&string), strlen(TEXT1) + strlen(TEXT2));
    assert_string_equal(DynStringToCStr(&string), TEXT1 TEXT2);
}

static void VarStringCount(void **state)
{
    DynString string;
    (void)state;
    DynStringMalloc(&string, 0);
    DynStringCatCStrs(&string, TEXT1, TEXT2, TEXT3, NULL);
    assert_int_equal(DynStringSize(&string),
                     strlen(TEXT1) + strlen(TEXT2) + strlen(TEXT3));
    assert_string_equal(DynStringToCStr(&string), TEXT1 TEXT2 TEXT3);
    DynStringFree(&string);
}

static void MultipleStrings(void **state)
{
    DynString string;
    (void)state;
    DynStringMalloc(&string, 0);
    DynStringCatCStr(&string, TEXT1);
    DynStringCatCStr(&string, TEXT2);
    assert_int_equal(DynStringSize(&string), strlen(TEXT1)+strlen(TEXT2));
    assert_string_equal(DynStringToCStr(&string), TEXT1 TEXT2);
    DynStringFree(&string);
}

static void SingleString(void **state)
{
    DynString string;
    (void)state;
    DynStringMalloc(&string, 0);
    DynStringCatCStr(&string, TEXT1);
    assert_int_equal(DynStringSize(&string), strlen(TEXT1));
    assert_string_equal(DynStringToCStr(&string), TEXT1);

	DynStringClear(&string);
	assert_int_equal(DynStringSize(&string), 0);

    DynStringFree(&string);
}

static void SimpleInitialization(void **state)
{
    DynString string;
    (void)state;
    DynStringMalloc(&string, 0);
    assert_non_null((void *)&string);
    assert_false(isnas(&string));
    assert_int_equal(DynStringSize(&string), 0);
    DynStringFree(&string);
}

int main(void)
{
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(SimpleInitialization),
        cmocka_unit_test(SingleString),
        cmocka_unit_test(MultipleStrings),
        cmocka_unit_test(VarStringCount),
        cmocka_unit_test(CopyString),
        cmocka_unit_test(PrintfString)
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}