aboutsummaryrefslogtreecommitdiff
path: root/tests/test_relocation.c
blob: a6c33f2fece29509778a33346e9d2d76fb4169e7 (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
#include "testing.h"

static const char *test_string = "The quick brown fox jumps over the lazy dog.";
static const char *targets[] = {
        "The", "^^^ quick brown fox jumps over the lazy dog.",
        "quick", "The ^^^ brown fox jumps over the lazy dog.",
        "brown fox jumps over the", "The quick ^^^ lazy dog.",
        "lazy", "The quick brown fox jumps over the ^^^ dog.",
        "dog", "The quick brown fox jumps over the lazy ^^^.",
};

void test_replace_text() {
    for (size_t i = 0; i < sizeof(targets) / sizeof(*targets); i += 2) {
        const char *target = targets[i];
        const char *expected = targets[i + 1];
        char input[BUFSIZ] = {0};
        strcpy(input, test_string);

        STASIS_ASSERT(replace_text(input, target, "^^^", 0) == 0, "string replacement failed");
        STASIS_ASSERT(strcmp(input, expected) == 0, "unexpected replacement");
    }

}

void test_file_replace_text() {
    for (size_t i = 0; i < sizeof(targets) / sizeof(*targets); i += 2) {
        const char *filename = "test_file_replace_text.txt";
        const char *target = targets[i];
        const char *expected = targets[i + 1];
        FILE *fp;

        fp = fopen(filename, "w");
        if (fp) {
            fprintf(fp, "%s", test_string);
            fclose(fp);
            STASIS_ASSERT(file_replace_text(filename, target, "^^^", 0) == 0, "string replacement failed");
        } else {
            STASIS_ASSERT(false, "failed to open file for writing");
            return;
        }

        char input[BUFSIZ] = {0};
        fp = fopen(filename, "r");
        if (fp) {
            fread(input, sizeof(*input), sizeof(input), fp);
            STASIS_ASSERT(strcmp(input, expected) == 0, "unexpected replacement");
        } else {
            STASIS_ASSERT(false, "failed to open file for reading");
            return;
        }
    }
}

int main(int argc, char *argv[]) {
    STASIS_TEST_BEGIN_MAIN();
    STASIS_TEST_FUNC *tests[] = {
        test_replace_text,
        test_file_replace_text,
    };
    STASIS_TEST_RUN(tests);
    STASIS_TEST_END_MAIN();
}