libsheepyCSmallContainerCuTest.c (9829B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "CuTest/CuTest.h" 6 7 #define ck_assert_str_eq(a,b) CuAssertStrEquals(tc, b, a) 8 #define ck_assert_str_ne(a,b) CuAssertStrNotEquals(tc, b, a) 9 #define ck_assert_ptr_eq(a,b) CuAssertPtrEquals(tc, b, a) 10 #define ck_assert_ptr_ne(a,b) CuAssertPtrNotEquals(tc, b, a) 11 #define ck_assert_uint_eq(a,b) CuAssertUintEquals(tc, b, a) 12 #define ck_assert_uint_ne(a,b) CuAssertUintNotEquals(tc, b, a) 13 #define ck_assert_int_eq(a,b) CuAssertIntEquals(tc, b, a) 14 #define ck_assert_int_ne(a,b) CuAssertIntNotEquals(tc, b, a) 15 #define ck_assert(a) CuAssertTrue(tc, a) 16 17 18 #include "../libsheepy.h" 19 #include "../libsheepyObject.h" 20 21 #ifdef __GNUC__ 22 #define UNUSED __attribute__ ((unused)) 23 #else 24 #define UNUSED 25 #endif 26 27 // TODO redirect stderr 28 29 30 void allocSmallContainerT(CuTest *tc UNUSED) { 31 32 smallContainert* r; 33 void *data = null; 34 35 r = allocSmallContainer(&data); 36 ck_assert_ptr_ne(r, null); 37 ck_assert_ptr_eq(getValO(r), &data); 38 terminateO(r); 39 40 } 41 42 i32 something = 123; 43 44 char *classToString(void *data) { 45 i32 *p = data; 46 ret intToS(*p); 47 } 48 49 char *objToString(void *data) { 50 i32 *p = data; 51 ret intToS(*p); 52 } 53 54 55 void toStringSmallContainerT(CuTest *tc UNUSED) { 56 57 char* r; 58 smallContainert *self = allocSmallContainer(&something); 59 60 r = toStringO(self); 61 ck_assert_ptr_ne(r, null); 62 ck_assert_str_eq(r, "<data smallContainer>"); 63 free(r); 64 // class toString 65 something = 123; 66 setClassDataToStringO(self, classToString); 67 r = toStringO(self); 68 ck_assert_ptr_ne(r, null); 69 ck_assert_str_eq(r, "123"); 70 free(r); 71 setClassDataToStringO(self, null); 72 // object toString 73 something = 456; 74 setObjectDataToStringO(self, objToString); 75 r = toStringO(self); 76 ck_assert_ptr_ne(r, null); 77 ck_assert_str_eq(r, "456"); 78 free(r); 79 // empty smallContainer 80 freeO(self); 81 r = toStringO(self); 82 ck_assert_ptr_eq(r, null); 83 terminateO(self); 84 85 } 86 87 void* dataDuplicateSmallContainer(void *data) { 88 ret data; 89 } 90 91 void duplicateSmallContainerT(CuTest *tc UNUSED) { 92 93 smallContainert* r; 94 smallContainert *self = allocSmallContainer(&r); 95 96 // data (&r) not duplicated 97 r = duplicateO(self); 98 ck_assert_ptr_ne(r, null); 99 ck_assert_ptr_ne(r->data, null); 100 ck_assert_ptr_eq(r->data->data, null); 101 terminateO(r); 102 // class duplicate function 103 self->f->setClassDataDuplicate(self, dataDuplicateSmallContainer); 104 r = duplicateO(self); 105 self->f->setClassDataDuplicate(self, null); 106 ck_assert_ptr_ne(r, null); 107 ck_assert_ptr_ne(r->data, null); 108 ck_assert_ptr_eq(r->data->data, &r); 109 terminateO(r); 110 // container duplicate function 111 self->f->setObjectDataDuplicate(self, dataDuplicateSmallContainer); 112 r = duplicateO(self); 113 self->f->setObjectDataDuplicate(self, null); 114 ck_assert_ptr_ne(r, null); 115 ck_assert_ptr_ne(r->data, null); 116 ck_assert_ptr_eq(r->data->data, &r); 117 terminateO(r); 118 // empty smallContainer 119 freeO(self); 120 r = duplicateO(self); 121 terminateO(r); 122 terminateO(self); 123 124 } 125 126 int freeResult = 0; 127 128 void dataFreeSmallContainer(void *data UNUSED) { 129 freeResult = 1; 130 } 131 132 void setClassDataFreeSmallContainerT(CuTest *tc UNUSED) { 133 134 smallContainert* r; 135 smallContainert *self = allocSmallContainer(null); 136 137 freeResult = 0; 138 r = setClassDataFreeO(self, dataFreeSmallContainer); 139 ck_assert_ptr_eq(r, self); 140 ck_assert_int_eq(freeResult, 0); 141 freeO(self); 142 ck_assert_int_eq(freeResult, 1); 143 freeResult = 0; 144 // free sContainer in an array 145 setValO(self, &r); 146 createAllocateSmallArray(a); 147 a->f->pushNFreeSmallContainer(a, self); 148 terminateO(a); 149 ck_assert_int_eq(freeResult, 1); 150 freeResult = 0; 151 152 } 153 154 155 void setObjectDataFreeSmallContainerT(CuTest *tc UNUSED) { 156 157 smallContainert* r; 158 smallContainert *self = allocSmallContainer(null); 159 160 freeResult = 0; 161 r = setObjectDataFreeO(self, dataFreeSmallContainer); 162 ck_assert_ptr_eq(r, self); 163 ck_assert_int_eq(freeResult, 0); 164 freeO(self); 165 ck_assert_int_eq(freeResult, 1); 166 freeResult = 0; 167 // free sContainer in an array without the free function in sContainer 168 setClassDataFreeO(self, NULL); 169 setValO(self, &r); 170 createAllocateSmallArray(a); 171 a->f->pushNFreeSmallContainer(a, self); 172 terminateO(a); 173 ck_assert_int_eq(freeResult, 0); 174 // free sContainer in an array with the free function in sContainer 175 self = allocSmallContainer(null); 176 r = setObjectDataFreeO(self, dataFreeSmallContainer); 177 ck_assert_ptr_eq(r, self); 178 setValO(self, &r); 179 a = allocSmallArray(); 180 a->f->pushNFreeSmallContainer(a, self); 181 terminateO(a); 182 ck_assert_int_eq(freeResult, 1); 183 freeResult = 0; 184 185 } 186 187 188 void setClassDataToStringSmallContainerT(CuTest *tc UNUSED) { 189 190 smallContainert* r; 191 smallContainert *self = allocSmallContainer(&something); 192 193 char *s = toStringO(self); 194 ck_assert_ptr_ne(s, null); 195 ck_assert_str_eq(s, "<data smallContainer>"); 196 free(s); 197 // toString with class function 198 r = setClassDataToStringO(self, classToString); 199 ck_assert_ptr_ne(r, null); 200 something = 123; 201 s = toStringO(self); 202 ck_assert_ptr_ne(s, null); 203 ck_assert_str_eq(s, "123"); 204 free(s); 205 terminateO(self); 206 207 } 208 209 210 void setObjectDataToStringSmallContainerT(CuTest *tc UNUSED) { 211 212 smallContainert* r; 213 smallContainert *self = allocSmallContainer(&something); 214 // remove class toString function 215 setClassDataToStringO(self, NULL); 216 217 char *s = toStringO(self); 218 ck_assert_ptr_ne(s, null); 219 ck_assert_str_eq(s, "<data smallContainer>"); 220 free(s); 221 // toString with class function 222 r = setObjectDataToStringO(self, objToString); 223 ck_assert_ptr_ne(r, null); 224 something = 123; 225 s = toStringO(self); 226 ck_assert_ptr_ne(s, null); 227 ck_assert_str_eq(s, "123"); 228 free(s); 229 terminateO(self); 230 231 } 232 233 234 void setClassDataDuplicateSmallContainerT(CuTest *tc UNUSED) { 235 236 smallContainert* r; 237 smallContainert *self = allocSmallContainer(&r); 238 239 // data (&r) not duplicated 240 r = duplicateO(self); 241 ck_assert_ptr_ne(r, null); 242 ck_assert_ptr_ne(r->data, null); 243 ck_assert_ptr_eq(r->data->data, null); 244 terminateO(r); 245 // class duplicate function 246 r = setClassDataDuplicateO(self, dataDuplicateSmallContainer); 247 ck_assert_ptr_ne(r, null); 248 r = duplicateO(self); 249 setClassDataDuplicateO(self, null); 250 ck_assert_ptr_ne(r, null); 251 ck_assert_ptr_ne(r->data, null); 252 ck_assert_ptr_eq(r->data->data, &r); 253 terminateO(r); 254 terminateO(self); 255 256 } 257 258 259 void setObjectDataDuplicateSmallContainerT(CuTest *tc UNUSED) { 260 261 smallContainert* r; 262 smallContainert *self = allocSmallContainer(&r); 263 264 // data (&r) not duplicated 265 r = duplicateO(self); 266 ck_assert_ptr_ne(r, null); 267 ck_assert_ptr_ne(r->data, null); 268 ck_assert_ptr_eq(r->data->data, null); 269 terminateO(r); 270 // object duplicate function 271 r = setObjectDataDuplicateO(self, dataDuplicateSmallContainer); 272 ck_assert_ptr_ne(r, null); 273 r = duplicateO(self); 274 setObjectDataDuplicateO(self, null); 275 ck_assert_ptr_ne(r, null); 276 ck_assert_ptr_ne(r->data, null); 277 ck_assert_ptr_eq(r->data->data, &r); 278 terminateO(r); 279 terminateO(self); 280 281 } 282 283 284 void smashSmallContainerT(CuTest *tc UNUSED) { 285 286 smallContainert *self = allocSmallContainer(null); 287 288 smashO(self); 289 self = allocSmallContainer(null); 290 freeO(self); 291 smashO(self); 292 ck_assert_ptr_eq(self, null); 293 294 } 295 296 297 void getSmallContainerT(CuTest *tc UNUSED) { 298 299 void* r; 300 smallContainert *self = allocSmallContainer(&r); 301 302 r = getValO(self); 303 ck_assert_ptr_eq(r, &r); 304 // empty smallContainer 305 freeO(self); 306 r = getValO(self); 307 ck_assert_ptr_eq(r, null); 308 terminateO(self); 309 310 } 311 312 313 void setSmallContainerT(CuTest *tc UNUSED) { 314 315 smallContainert* r; 316 smallContainert *self = allocSmallContainer(null); 317 318 r = setValO(self, &r); 319 ck_assert_ptr_ne(r, null); 320 void *r2 = getValO(self); 321 ck_assert_ptr_eq(r2, &r); 322 terminateO(self); 323 324 } 325 326 327 void duplicateSmallContainerGT(CuTest *tc UNUSED) { 328 329 smallContainert* r; 330 smallContainert *self = allocSmallContainer(null); 331 332 r = duplicateSmallContainerG(self); 333 ck_assert_ptr_ne(r, null); 334 ck_assert_ptr_ne(r->data, null); 335 ck_assert_ptr_eq(r->data->data, null); 336 terminateO(r); 337 terminateO(self); 338 339 } 340 341 342 void freeSmallContainerGT(CuTest *tc UNUSED) { 343 344 smallContainert *self = allocSmallContainer(null); 345 346 freeSmallContainerG(self); 347 ck_assert_ptr_eq(self->data, null); 348 terminateO(self); 349 350 } 351 352 353 void getSmallContainerGT(CuTest *tc UNUSED) { 354 355 void* r; 356 smallContainert *self = allocSmallContainer(&r); 357 358 r = getSmallContainerG(self, null, 0); 359 ck_assert_ptr_eq(r, &r); 360 terminateO(self); 361 362 } 363 364 365 void setSmallContainerGT(CuTest *tc UNUSED) { 366 367 smallContainert* r; 368 smallContainert *self = allocSmallContainer(null); 369 370 r = setSmallContainerG(self, &r); 371 ck_assert_ptr_ne(r, null); 372 void *r2 = getValO(self); 373 ck_assert_ptr_eq(r2, &r); 374 terminateO(self); 375 376 } 377 378 379 380 381 382 383 int main(int n UNUSED, char**v UNUSED) { 384 // disable btrace to make the test run faster 385 btraceDisable(); 386 CuString *output = CuStringNew(); 387 CuSuite *suite = CuSuiteNew(); 388 389 SUITE_ADD_TEST(suite, allocSmallContainerT); 390 SUITE_ADD_TEST(suite, toStringSmallContainerT); 391 SUITE_ADD_TEST(suite, duplicateSmallContainerT); 392 SUITE_ADD_TEST(suite, setClassDataFreeSmallContainerT); 393 SUITE_ADD_TEST(suite, setObjectDataFreeSmallContainerT); 394 SUITE_ADD_TEST(suite, setClassDataToStringSmallContainerT); 395 SUITE_ADD_TEST(suite, setObjectDataToStringSmallContainerT); 396 SUITE_ADD_TEST(suite, setClassDataDuplicateSmallContainerT); 397 SUITE_ADD_TEST(suite, setObjectDataDuplicateSmallContainerT); 398 SUITE_ADD_TEST(suite, smashSmallContainerT); 399 SUITE_ADD_TEST(suite, getSmallContainerT); 400 SUITE_ADD_TEST(suite, setSmallContainerT); 401 SUITE_ADD_TEST(suite, duplicateSmallContainerGT); 402 SUITE_ADD_TEST(suite, freeSmallContainerGT); 403 SUITE_ADD_TEST(suite, getSmallContainerGT); 404 SUITE_ADD_TEST(suite, setSmallContainerGT); 405 406 407 CuSuiteRun(suite); 408 CuSuiteDetails(suite, output); 409 printf ("%s\n", output->buffer); 410 return suite->failCount; 411 }