-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.c
More file actions
144 lines (121 loc) · 4.15 KB
/
Copy pathtests.c
File metadata and controls
144 lines (121 loc) · 4.15 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "sarena.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_PASS 0
#define TEST_FAIL 1
#define TEST_ASSERT(condition) \
do {\
if (!(condition)) {\
return TEST_FAIL;\
}\
} while(0)
int test_arena_init_and_free(void) {
// Create an arena and verify its member values are the expected values after initialization
Arena arena;
TEST_ASSERT(arena_init(&arena, 9) == 0);
TEST_ASSERT(arena.capacity == 9);
TEST_ASSERT(arena.size == 0);
TEST_ASSERT(arena.data != NULL);
// Free the arena and verify that the pointer to its data is now NULL
arena_free(&arena);
TEST_ASSERT(arena.data == NULL);
TEST_ASSERT(arena.capacity == 0);
TEST_ASSERT(arena.size == 0);
// Attempt to allocate an arena of 900 GiB (highly unlikely system has this much)
const size_t num_bytes = 966367641600;
Arena arena2;
int result = arena_init(&arena2, num_bytes);
if (arena2.data != NULL) {
// This should be unreachable, but compiler will optimize it away if
// the pointer is unused and essentially not malloc, causing it to "successfully"
// allocate 900 GiB
char* string1 = arena_alloc(&arena2, 6);
string1[0] = 'E';
string1[1] = 'R';
string1[2] = 'R';
string1[3] = 'O';
string1[4] = 'R';
string1[5] = '\0';
fprintf(stderr, "%s\n", string1);
fprintf(stderr, "%p\n", (void*)string1);
return TEST_FAIL;
}
TEST_ASSERT(result == -1);
return TEST_PASS;
}
int test_arena_alloc(void) {
Arena arena;
arena_init(&arena, 10);
// Allocate 2 strings of length 5 bytes within the 10 byte arena
char* string1 = arena_alloc(&arena, 5);
TEST_ASSERT(string1 != NULL);
char* string2 = arena_alloc(&arena, 5);
TEST_ASSERT(string1 != NULL);
// Try to allocate an 11th byte in the 10 byte arena, should fail
char* too_much = arena_alloc(&arena, 1);
TEST_ASSERT(too_much == NULL);
// Place some data in the strings and verify with strcmp()
memcpy(string1, "test\0", 5);
TEST_ASSERT(strcmp(string1, "test") == 0);
memcpy(string2, "tset\0", 5);
TEST_ASSERT(strcmp(string2, "tset") == 0);
// Write some more junk data to string1 and check the entire arena byte array to see if it matches what is expected
snprintf(string1, 5, "%dt_e", 2);
TEST_ASSERT(memcmp(arena.data, "2t_e\0tset\0", 10) == 0);
arena_free(&arena);
return TEST_PASS;
}
// Test struct. Consists of a function pointer to the test function and its test name
typedef struct {
int (*function)(void);
char* testname;
} Test;
int run_tests(Test tests[], int num_of_tests);
int main(void) {
// Add test names and their function pointers here
Test tests[] = {
{
.function = test_arena_init_and_free,
.testname = "test_arena_init_and_free()"
},
{
.function = test_arena_alloc,
.testname = "test_arena_alloc()"
},
};
// Get the number of tests and run them
int num_of_tests = sizeof(tests) / sizeof(tests[0]);
return run_tests(tests, num_of_tests);
}
int run_tests(Test tests[], int num_of_tests) {
// Keep track of if any tests failed
int failed = 0;
for (int i = 0; i < num_of_tests; i++) {
printf("%s: Running test..\n", tests[i].testname);
int result = tests[i].function();
if (result == TEST_PASS) {
printf("%s: PASS\n", tests[i].testname);
}
else if (result == TEST_FAIL) {
fprintf(stderr, "%s: FAIL\n", tests[i].testname);
fflush(stderr);
failed++;
}
else {
fprintf(stderr, "ERROR: Test %s returned a value other than expected TEST_PASS or TEST_FAIL. Returned value: %d\n", tests[i].testname, result);
fflush(stderr);
exit(1);
}
}
// Output result of all tests
if (failed != 0) {
fprintf(stderr, "%d/%d Tests passed, Overall result: FAIL\n", num_of_tests - failed, num_of_tests);
fflush(stderr);
return 1;
}
else {
printf("%d/%d Tests passed, Overall result: PASS\n", num_of_tests, num_of_tests);
return 0;
}
}