7983
|
1 |
#include <check.h>
|
|
2 |
#include <stdlib.h>
|
|
3 |
#include <stdio.h>
|
|
4 |
#include "check_check.h"
|
|
5 |
#include "../src/system.h"
|
|
6 |
|
|
7 |
void check_string(string255 str)
|
|
8 |
{
|
10015
|
9 |
fail_unless(strlen(str.str) == str.len, "String internal inconsistency error");
|
7983
|
10 |
}
|
|
11 |
|
|
12 |
static string255 make_string(const char* str)
|
|
13 |
{
|
10015
|
14 |
string255 s;
|
|
15 |
s.len = strlen(str);
|
|
16 |
memcpy(s.str, str, s.len + 1);
|
|
17 |
return s;
|
7983
|
18 |
}
|
|
19 |
|
|
20 |
START_TEST (test_copy)
|
10015
|
21 |
{
|
|
22 |
string255 s = STRINIT("1234567");
|
|
23 |
string255 t;
|
7983
|
24 |
|
10015
|
25 |
t = fpcrtl_copy(s, 1, 1);
|
|
26 |
fail_if(strcmp(t.str, "1"), "Test copy fail 1");
|
7983
|
27 |
|
10015
|
28 |
t = fpcrtl_copy(s, 7, 1);
|
|
29 |
fail_if(strcmp(t.str, "7"), "Test copy fail 2");
|
7983
|
30 |
|
10015
|
31 |
t = fpcrtl_copy(s, 8, 1);
|
|
32 |
fail_if(t.len != 0, "Test copy fail 3");
|
7983
|
33 |
|
10015
|
34 |
t = fpcrtl_copy(s, 8, 100);
|
|
35 |
fail_if(t.len != 0, "Test copy fail 4");
|
|
36 |
check_string(t);
|
7983
|
37 |
|
10015
|
38 |
t = fpcrtl_copy(s, 0, 100);
|
|
39 |
fail_if(strcmp(t.str, "1234567"), "Test copy fail 5");
|
7983
|
40 |
|
10015
|
41 |
t = fpcrtl_copy(s, 0, 5);
|
|
42 |
fail_if(strcmp(t.str, "12345"), "Test copy fail 6");
|
7983
|
43 |
|
10015
|
44 |
t = fpcrtl_copy(s, 4, 100);
|
|
45 |
fail_if(strcmp(t.str, "4567"), "Test copy fail 7");
|
7983
|
46 |
|
10015
|
47 |
t = fpcrtl_copy(s, 4, 2);
|
|
48 |
fail_if(strcmp(t.str, "45"), "Test copy fail 8");
|
|
49 |
}END_TEST
|
7983
|
50 |
|
|
51 |
START_TEST (test_delete)
|
10015
|
52 |
{
|
|
53 |
string255 s = STRINIT("1234567");
|
|
54 |
string255 s2 = STRINIT("1234567");
|
|
55 |
string255 s3 = STRINIT("1234567");
|
7983
|
56 |
|
10015
|
57 |
fpcrtl_delete(s, 0, 10);
|
|
58 |
fail_if(strcmp(s.str, "1234567"), "delete(\"1234567\", 0, 10)");
|
|
59 |
check_string(s);
|
7983
|
60 |
|
10015
|
61 |
fpcrtl_delete(s, 1, 1);
|
|
62 |
fail_if(strcmp(s.str, "234567"), "delete(\"1234567\", 1, 1)");
|
|
63 |
check_string(s);
|
7983
|
64 |
|
10015
|
65 |
fpcrtl_delete(s, 1, 100);
|
|
66 |
fail_if(strcmp(s.str, ""), "delete(\"234567\", 1, 100)");
|
|
67 |
check_string(s);
|
7983
|
68 |
|
10015
|
69 |
fpcrtl_delete(s2, 3, 2);
|
|
70 |
fail_if(strcmp(s2.str, "12567"), "delete(\"1234567\", 3, 2)");
|
|
71 |
check_string(s2);
|
7983
|
72 |
|
10015
|
73 |
fpcrtl_delete(s3, 3, 100);
|
|
74 |
fail_if(strcmp(s3.str, "12"), "delete(\"1234567\", 3, 100)");
|
|
75 |
check_string(s3);
|
7983
|
76 |
|
10015
|
77 |
}
|
7983
|
78 |
END_TEST
|
|
79 |
|
|
80 |
START_TEST (test_FloatToStr)
|
10015
|
81 |
{
|
|
82 |
double s = 1.2345;
|
|
83 |
string255 t = fpcrtl_floatToStr(s);
|
|
84 |
printf("-----Entering test floatToStr-----\n");
|
|
85 |
printf("FloatToStr(%f) = %s\n", s, t.str);
|
|
86 |
printf("-----Leaving test floatToStr-----\n");
|
|
87 |
}
|
7983
|
88 |
END_TEST
|
|
89 |
|
|
90 |
START_TEST (test_random)
|
10015
|
91 |
{
|
|
92 |
fpcrtl_randomize();
|
|
93 |
printf("-----Entering test random-----\n");
|
|
94 |
printf("random(5000) = %d\n", fpcrtl_random(5000));
|
|
95 |
printf("random(1) = %d\n", fpcrtl_random(1));
|
|
96 |
printf("random(2) = %d\n", fpcrtl_random(2));
|
|
97 |
printf("-----Leaving test random-----\n");
|
7983
|
98 |
|
10015
|
99 |
}
|
7983
|
100 |
END_TEST
|
|
101 |
|
|
102 |
START_TEST (test_posS)
|
10015
|
103 |
{
|
|
104 |
string255 substr1 = STRINIT("123");
|
|
105 |
string255 str1 = STRINIT("12345");
|
7983
|
106 |
|
10015
|
107 |
string255 substr2 = STRINIT("45");
|
|
108 |
string255 str2 = STRINIT("12345");
|
7983
|
109 |
|
10015
|
110 |
string255 substr3 = STRINIT("");
|
|
111 |
string255 str3 = STRINIT("12345");
|
7983
|
112 |
|
10015
|
113 |
string255 substr4 = STRINIT("123");
|
|
114 |
string255 str4 = STRINIT("");
|
7983
|
115 |
|
10015
|
116 |
string255 substr5 = STRINIT("123");
|
|
117 |
string255 str5 = STRINIT("456");
|
7983
|
118 |
|
10015
|
119 |
fail_unless(fpcrtl_posS(substr1, str1) == 1, "pos(123, 12345)");
|
|
120 |
fail_unless(fpcrtl_posS(substr2, str2) == 4, "pos(45, 12345)");
|
|
121 |
fail_unless(fpcrtl_posS(substr3, str3) == 0, "pos(, 12345)");
|
|
122 |
fail_unless(fpcrtl_posS(substr4, str4) == 0, "pos(123, )");
|
|
123 |
fail_unless(fpcrtl_posS(substr5, str5) == 0, "pos(123, 456)");
|
|
124 |
}
|
7983
|
125 |
END_TEST
|
|
126 |
|
|
127 |
START_TEST (test_trunc)
|
10015
|
128 |
{
|
|
129 |
fail_unless(fpcrtl_trunc(123.456) == 123, "trunc(123.456)");
|
|
130 |
fail_unless(fpcrtl_trunc(-123.456) == -123, "trunc(-123.456)");
|
|
131 |
fail_unless(fpcrtl_trunc(12.3456) == 12, "trunc(12.3456)");
|
|
132 |
fail_unless(fpcrtl_trunc(-12.3456) == -12, "trunc(-12.3456)");
|
|
133 |
}
|
7983
|
134 |
END_TEST
|
|
135 |
|
|
136 |
START_TEST (test_odd)
|
|
137 |
{
|
10015
|
138 |
fail_unless(fpcrtl_odd(123) != 0, "odd(123)");
|
|
139 |
fail_unless(fpcrtl_odd(124) == 0, "odd(124)");
|
|
140 |
fail_unless(fpcrtl_odd(0) == 0, "odd(0)");
|
|
141 |
fail_unless(fpcrtl_odd(-1) != 0, "odd(-1)");
|
|
142 |
fail_unless(fpcrtl_odd(-2) == 0, "odd(-2)");
|
7983
|
143 |
}
|
|
144 |
END_TEST
|
|
145 |
|
|
146 |
START_TEST (test_sqr)
|
|
147 |
{
|
10015
|
148 |
fail_unless(fpcrtl_sqr(0) == 0, "sqr(0)");
|
|
149 |
fail_unless(fpcrtl_sqr(5) == 25, "sqr(5)");
|
|
150 |
fail_unless(fpcrtl_sqr(-5) == 25, "sqr(-5)");
|
7983
|
151 |
}
|
|
152 |
END_TEST
|
|
153 |
|
|
154 |
START_TEST (test_lowercase)
|
|
155 |
{
|
10015
|
156 |
string255 s1 = STRINIT("");
|
|
157 |
string255 s2 = STRINIT("a");
|
|
158 |
string255 s3 = STRINIT("abc");
|
|
159 |
string255 t;
|
7983
|
160 |
|
10015
|
161 |
t = fpcrtl_lowerCase(make_string(""));
|
|
162 |
fail_if(strcmp(t.str, s1.str), "lowerCase(\"\")");
|
7983
|
163 |
|
10015
|
164 |
t = fpcrtl_lowerCase(make_string("a"));
|
|
165 |
fail_if(strcmp(t.str, s2.str), "lowerCase(\"a\")");
|
7983
|
166 |
|
10015
|
167 |
t = fpcrtl_lowerCase(make_string("A"));
|
|
168 |
fail_if(strcmp(t.str, s2.str), "lowerCase(\"A\")");
|
7983
|
169 |
|
10015
|
170 |
t = fpcrtl_lowerCase(make_string("AbC"));
|
|
171 |
fail_if(strcmp(t.str, s3.str), "lowerCase(\"AbC\")");
|
7983
|
172 |
|
10015
|
173 |
t = fpcrtl_lowerCase(make_string("abc"));
|
|
174 |
fail_if(strcmp(t.str, s3.str), "lowerCase(\"abc\")");
|
7983
|
175 |
}
|
|
176 |
END_TEST
|
|
177 |
|
|
178 |
START_TEST (test_str)
|
|
179 |
{
|
10015
|
180 |
int8_t a1 = -8;
|
|
181 |
uint8_t a2 = 8;
|
|
182 |
int16_t a3 = -13;
|
|
183 |
uint16_t a4 = 13;
|
|
184 |
int32_t a5 = -19;
|
|
185 |
uint32_t a6 = 22;
|
|
186 |
int64_t a7 = -199999999999999;
|
|
187 |
uint64_t a8 = 200000000000000;
|
7983
|
188 |
|
10015
|
189 |
float a9 = 12345.6789;
|
|
190 |
double a10 = -9876.54321;
|
7983
|
191 |
|
10015
|
192 |
string255 s;
|
7983
|
193 |
|
10015
|
194 |
printf("-----Entering test str-----\n");
|
7983
|
195 |
|
10015
|
196 |
fpcrtl_str(a1, s);
|
|
197 |
printf("%d == %s\n", a1, s.str);
|
7983
|
198 |
|
10015
|
199 |
fpcrtl_str(a2, s);
|
|
200 |
printf("%u == %s\n", a2, s.str);
|
7983
|
201 |
|
10015
|
202 |
fpcrtl_str(a3, s);
|
|
203 |
printf("%d == %s\n", a3, s.str);
|
7983
|
204 |
|
10015
|
205 |
fpcrtl_str(a4, s);
|
|
206 |
printf("%u == %s\n", a4, s.str);
|
7983
|
207 |
|
10015
|
208 |
fpcrtl_str(a5, s);
|
|
209 |
printf("%d == %s\n", a5, s.str);
|
7983
|
210 |
|
10015
|
211 |
fpcrtl_str(a6, s);
|
|
212 |
printf("%u == %s\n", a6, s.str);
|
7983
|
213 |
|
10015
|
214 |
fpcrtl_str(a7, s);
|
|
215 |
printf("%lld == %s\n", a7, s.str);
|
7983
|
216 |
|
10015
|
217 |
fpcrtl_str(a8, s);
|
|
218 |
printf("%llu == %s\n", a8, s.str);
|
7983
|
219 |
|
10015
|
220 |
fpcrtl_str(a9, s);
|
|
221 |
printf("%f == %s\n", a9, s.str);
|
7983
|
222 |
|
10015
|
223 |
fpcrtl_str(a10, s);
|
|
224 |
printf("%f == %s\n", a10, s.str);
|
7983
|
225 |
|
10015
|
226 |
printf("-----Leaving test str------\n");
|
7983
|
227 |
}
|
|
228 |
END_TEST
|
|
229 |
|
|
230 |
Suite* system_suite(void)
|
|
231 |
{
|
10015
|
232 |
Suite *s = suite_create("system");
|
7983
|
233 |
|
10015
|
234 |
TCase *tc_core = tcase_create("Core");
|
7983
|
235 |
|
10015
|
236 |
tcase_add_test(tc_core, test_copy);
|
|
237 |
tcase_add_test(tc_core, test_FloatToStr);
|
|
238 |
tcase_add_test(tc_core, test_random);
|
|
239 |
tcase_add_test(tc_core, test_posS);
|
|
240 |
tcase_add_test(tc_core, test_trunc);
|
|
241 |
tcase_add_test(tc_core, test_delete);
|
|
242 |
tcase_add_test(tc_core, test_odd);
|
|
243 |
tcase_add_test(tc_core, test_sqr);
|
|
244 |
tcase_add_test(tc_core, test_lowercase);
|
|
245 |
tcase_add_test(tc_core, test_str);
|
7983
|
246 |
|
10015
|
247 |
suite_add_tcase(s, tc_core);
|
7983
|
248 |
|
10015
|
249 |
return s;
|
7983
|
250 |
}
|
|
251 |
|