aboutsummaryrefslogtreecommitdiffstats
path: root/tests/internals/test_internals.c
blob: f5dbed54db672b22b0fc6cb1a7114779d2b12a81 (plain) (blame)
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

#include "test_common.h"
#include "misc.h"
#include <string.h>
#include <stdlib.h>

void test_my_dirname(char *arg, const char *expected)
{
    char *orig = strdup(arg);

    const char *ret = my_dirname(arg);
    if (strcmp(ret, expected) != 0) {
        printf("Expected my_dirname(`%s') to return `%s' but got `%s'\n", orig, expected, ret);
        failures++;
    }

    free(orig);
}

void my_dirname_suite()
{
    char buf[256];

    strcpy(buf, "/foo/bar/baz");
    test_my_dirname(buf, "/foo/bar");

    strcpy(buf, "/foo/bar");
    test_my_dirname(buf, "/foo");

    strcpy(buf, "/foo");
    test_my_dirname(buf, "/");

    strcpy(buf, "/foo/");
    test_my_dirname(buf, "/foo");

    strcpy(buf, "/");
    test_my_dirname(buf, "/");

    strcpy(buf, "foo");
    test_my_dirname(buf, ".");

    strcpy(buf, "foo/bar");
    test_my_dirname(buf, "foo");

    strcpy(buf, "./foo/bar");
    test_my_dirname(buf, "./foo");

    strcpy(buf, "./foo");
    test_my_dirname(buf, ".");

    strcpy(buf, ".");
    test_my_dirname(buf, "..");
}

void sprintf_new_suite() {
    char *result;

    result = sprintf_new("Hello %d %s", 123, "World");
    TEST_ASSERT(strcmp(result, "Hello 123 World") == 0);
    free(result);

    result = sprintf_new("A %s", "loooooooooooooooooooooooooong result");
    TEST_ASSERT(strcmp(result, "A loooooooooooooooooooooooooong result") == 0);
    free(result);
}

void test_internal_suite() {
    my_dirname_suite();
    sprintf_new_suite();
}

TEST_MAIN(test_internal_suite);