-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
149 lines (104 loc) · 3.3 KB
/
main.c
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
145
146
147
148
149
#include <stdio.h>
#include "src/string.h"
char* testInitDestroy() {
string_t string;
string_init(&string);
if (!string_equalsc(&string, "")) {
return "testInitDestroy(): Equal to empty string check failed.";
}
string_destroy(&string);
return NULL;
}
char* testInitString() {
{
string_t string;
string_initString(&string, "s", 1);
if (!string_equalsc(&string, "s")) {
return "testInitString(): Equal to 's' string check failed.";
}
string_destroy(&string);
}
{
string_t string;
string_initString(&string, "Hello, World! How are you doing?", 32);
if (!string_equalsc(&string, "Hello, World! How are you doing?")) {
return "testInitString(): Equal to larger string check failed.";
}
string_destroy(&string);
}
return NULL;
}
char* testCopyString() {
{
string_t string;
string_initString(&string, "s", 1);
if (!string_equalsc(&string, "s")) {
return "testCopyString(): Primary equal to 's' string check failed.";
}
string_t target;
if (!string_copy(&string, &target)) {
return "testCopyString(): memory error.";
}
if (!string_equalsc(&string, "s")) {
return "testCopyString(): Second equal to 's' string check failed.";
}
if (!string_equalsc(&target, "s")) {
return "testCopyString(): Third equal to 's' string check failed.";
}
if (!string_equals(&target, &string)) {
return "testCopyString(): Fourth equal to 's' string check failed.";
}
if (!string_equals(&string, &target)) {
return "testCopyString(): Fifth equal to 's' string check failed.";
}
if (!string_equals(&string, &string)) {
return "testCopyString(): Six equal to 's' string check failed.";
}
if (!string_equals(&target, &target)) {
return "testCopyString(): Seventh equal to 's' string check failed.";
}
string_destroy(&string);
string_destroy(&target);
}
return NULL;
}
int main() {
char* (*tests[])() = {
testInitDestroy,
testInitString,
testCopyString,
NULL
};
printf("sizeof(void*) => %zu\n", sizeof(void*));
printf("sizeof(size_t) => %zu\n", sizeof(size_t));
printf("sizeof(string_t) => %zu\n", sizeof(string_t));
puts("Starting tests...");
size_t testsFailed = 0;
size_t testsPassed = 0;
char* (**currentTest)() = tests;
char* currentTestResponse;
while (*currentTest != NULL) {
if ((currentTestResponse = (*currentTest)()) == NULL) {
testsPassed++;
}
else {
if (testsFailed == 0) {
fputs("======[ FAIL ]======\n", stderr);
}
testsFailed++;
fprintf(stderr, "Error: Test #%zu failed:\n", currentTest - tests);
fprintf(stderr, "%s\n", currentTestResponse);
fputs("===", stderr);
}
currentTest++;
}
if (testsFailed == 0) {
puts("======[ PASS ]======");
puts("All tests successfully passed.");
return 0;
}
else {
fputs("======[ FAIL ]======\n", stderr);
}
return 0;
}