libsheepyObjectTest.c (64392B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <check.h> 4 5 //START MEM TEST ANCHOR 6 7 #include "../libsheepy.h" 8 #include "../libsheepyObject.h" 9 10 #ifdef __GNUC__ 11 #define UNUSED __attribute__ ((unused)) 12 #else 13 #define UNUSED 14 #endif 15 16 // TODO redirect stderr 17 18 START_TEST(cSmallDictT) 19 20 // local object 21 createSmallDict(obj); 22 ck_assert_str_eq(obj.type, "smallDict"); 23 // object 24 createAllocateSmallDict(obj2); 25 ck_assert_str_eq(obj2->type, "smallDict"); 26 // toString 27 char *s = obj2->f->toString(obj2); 28 ck_assert_str_eq(s, "{}"); 29 free(s); 30 // duplicate 31 smallDictt *o; 32 createAllocateSmallBool(oBool2); 33 oBool2->f->set(oBool2, true); 34 obj2->f->set(obj2, "lib", (baset *)oBool2); 35 finishO(oBool2); 36 o = obj2->f->duplicate(obj2); 37 smallBoolt *oBool3; 38 oBool3 = (smallBoolt *)o->f->get(o, "lib"); 39 ck_assert_ptr_ne(oBool3, NULL); 40 ck_assert(oBool3->value->value == true); 41 finishO(oBool3); 42 terminateO(o); 43 // toString 44 createAllocateUndefined(oU); 45 obj2->f->set(obj2, "u", (baset *)oU); 46 finishO(oU); 47 createAllocateSmallString(oStr); 48 oStr->f->set(oStr, "sheepy"); 49 obj2->f->set(obj2, "str", (baset *)oStr); 50 finishO(oStr); 51 s = obj2->f->toString(obj2); 52 ck_assert_str_eq(s, "{\"lib\":true,\"u\":null,\"str\":\"sheepy\"}"); 53 free(s); 54 // dispose 55 o = obj2->f->duplicate(obj2); 56 undefinedt *u = (undefinedt *) o->f->get(o, "u"); 57 smallStringt *st = (smallStringt *) o->f->get(o, "str"); 58 oBool2 = (smallBoolt *) o->f->get(o, "lib"); 59 smallt *data = sDictGetTiny(o->d, "u"); 60 sFree(data); 61 data = sDictGetTiny(o->d, "str"); 62 sFree(data); 63 data = sDictGetTiny(o->d, "lib"); 64 sFree(data); 65 o->f->dispose(o); 66 ck_assert_uint_eq(o->f->len(o), 0); 67 terminateO(u); 68 smashO(st); 69 smashO(oBool2); 70 terminateO(o); 71 // smash 72 o = obj2->f->duplicate(obj2); 73 u = (undefinedt *) o->f->get(o, "u"); 74 st = (smallStringt *) o->f->get(o, "str"); 75 oBool2 = (smallBoolt *) o->f->get(o, "lib"); 76 data = sDictGetTiny(o->d, "u"); 77 sFree(data); 78 data = sDictGetTiny(o->d, "str"); 79 sFree(data); 80 data = sDictGetTiny(o->d, "lib"); 81 sFree(data); 82 o->f->smash(&o); 83 terminateO(u); 84 smashO(st); 85 smashO(oBool2); 86 ck_assert_ptr_eq(o, NULL); 87 // set NULL (not possible) 88 obj2->f->set(obj2, NULL, NULL); 89 obj2->f->set(obj2, "no", NULL); 90 ck_assert_uint_eq(obj2->f->len(obj2), 3); 91 // get non existing element 92 oBool3 = (smallBoolt *)obj2->f->get(obj2, "non existing"); 93 ck_assert_ptr_eq(oBool3, NULL); 94 ck_assert_ptr_eq(obj2->f->get(obj2, NULL), NULL); 95 // delete element 96 obj2->f->del(obj2, "lib"); 97 oBool3 = (smallBoolt *)obj2->f->get(obj2, "lib"); 98 ck_assert_ptr_eq(oBool3, NULL); 99 // delete non existing element 100 obj2->f->del(obj2, NULL); 101 obj2->f->del(obj2, "non existing"); 102 // has 103 ck_assert(!obj2->f->has(obj2, "qwe")); 104 ck_assert(!obj2->f->has(obj2, NULL)); 105 ck_assert(obj2->f->has(obj2, "u")); 106 // keys 107 char **keys = obj2->f->keys(obj2); 108 size_t len = listLengthS(keys); 109 ck_assert_uint_eq(len, 2); 110 ck_assert_str_eq(keys[0], "u"); 111 ck_assert_str_eq(keys[1], "str"); 112 listFreeS(keys); 113 // empty dict 114 initiateAllocateSmallDict(&o); 115 keys = o->f->keys(o); 116 ck_assert_ptr_eq(keys, NULL); 117 terminateO(o); 118 // values 119 smallArrayt *values = obj2->f->values(obj2); 120 s = toStringO(values); 121 ck_assert_str_eq(s, "[null,\"sheepy\"]"); 122 free(s); 123 values->f->smash(&values); 124 // empty dict 125 initiateAllocateSmallDict(&o); 126 values = o->f->values(o); 127 ck_assert_ptr_eq(values, NULL); 128 terminateO(o); 129 // merge 130 smallDictt *oM; 131 o = obj2->f->duplicate(obj2); 132 initiateAllocateSmallDict(&oM); 133 initiateAllocateSmallString(&st); 134 st->f->set(st, "SHEEPY MERGED"); 135 oM->f->set(oM, "str", (baset *) st); 136 finishO(st); 137 s = toStringO(o); 138 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 139 free(s); 140 o->f->merge(o, oM); 141 s = toStringO(o); 142 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"SHEEPY MERGED\"}"); 143 free(s); 144 oM->f->smash(&oM); 145 terminateO(o); 146 // empty dict 147 o = obj2->f->duplicate(obj2); 148 initiateAllocateSmallDict(&oM); 149 s = toStringO(o); 150 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 151 free(s); 152 o->f->merge(o, oM); 153 s = toStringO(o); 154 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 155 free(s); 156 oM->f->smash(&oM); 157 terminateO(o); 158 // NULL dict 159 o = obj2->f->duplicate(obj2); 160 s = toStringO(o); 161 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 162 free(s); 163 o->f->merge(o, NULL); 164 s = toStringO(o); 165 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 166 free(s); 167 terminateO(o); 168 // append 169 o = obj2->f->duplicate(obj2); 170 initiateAllocateSmallDict(&oM); 171 initiateAllocateSmallString(&st); 172 initiateAllocateUndefined(&oU); 173 st->f->set(st, "SHEEPY MERGED"); 174 oM->f->set(oM, "str", (baset *) st); 175 finishO(st); 176 oM->f->set(oM, "u2", (baset *) oU); 177 finishO(oU); 178 s = toStringO(o); 179 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 180 free(s); 181 o->f->append(o, oM); 182 s = toStringO(o); 183 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\",\"u2\":null}"); 184 free(s); 185 data = sDictGetTiny(oM->d, "str"); 186 sFree(data); 187 oM->f->smash(&oM); 188 terminateO(o); 189 // empty dict 190 o = obj2->f->duplicate(obj2); 191 initiateAllocateSmallDict(&oM); 192 s = toStringO(o); 193 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 194 free(s); 195 o->f->append(o, oM); 196 s = toStringO(o); 197 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 198 free(s); 199 oM->f->smash(&oM); 200 terminateO(o); 201 // NULL dict 202 o = obj2->f->duplicate(obj2); 203 s = toStringO(o); 204 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 205 free(s); 206 o->f->append(o, NULL); 207 s = toStringO(o); 208 ck_assert_str_eq(s, "{\"u\":null,\"str\":\"sheepy\"}"); 209 free(s); 210 terminateO(o); 211 // len 212 ck_assert_uint_eq(obj2->f->len(obj2), 2); 213 // empty dict 214 initiateAllocateSmallDict(&o); 215 ck_assert_uint_eq(o->f->len(o), 0); 216 terminateO(o); 217 // empty 218 o = obj2->f->duplicate(obj2); 219 o->f->empty(o); 220 ck_assert_uint_eq(o->f->len(o), 0); 221 terminateO(o); 222 // typeString type typeStrings 223 initiateAllocateSmallDict(&oM); 224 o = allocSmallDict(); 225 oU = allocUndefined(); 226 o->f->set(o, "u", (baset *)oU); 227 finishO(oU); 228 st = allocSmallString("sheepy"); 229 o->f->set(o, "str", (baset *) st); 230 finishO(st); 231 oBool2 = allocSmallBool(true); 232 o->f->set(o, "b", (baset *)oBool2); 233 finishO(oBool2); 234 oBool2 = allocSmallBool(true); 235 o->f->set(o, "B", (baset *)oBool2); 236 finishO(oBool2); 237 o->f->del(o, "B"); 238 // typeString 239 s = (char *)o->f->typeString(o, "b"); 240 ck_assert_str_eq(s, "bool"); 241 // non existing key 242 ck_assert_ptr_eq(o->f->typeString(o, "B"), NULL); 243 // empty object 244 ck_assert_ptr_eq(oM->f->typeString(oM, "B"), NULL); 245 // type 246 char c; 247 c = o->f->type(o, "str"); 248 ck_assert_uint_eq(c, STRING); 249 // non existing key 250 ck_assert(!o->f->type(o, "B")); 251 // empty object 252 ck_assert(!oM->f->type(oM, "B")); 253 // typeStrings 254 smallDictt *oT = o->f->typeStrings(o); 255 s = toStringO(oT); 256 ck_assert_str_eq(s, "{\"u\":\"undefined\",\"str\":\"string\",\"b\":\"bool\"}"); 257 free(s); 258 terminateO(oT); 259 // empty object 260 oT = oM->f->typeStrings(oM); 261 ck_assert_ptr_eq(oT, NULL); 262 terminateO(o); 263 terminateO(oM); 264 // free local object 265 obj.f->free(&obj); 266 ck_assert_str_eq(obj.type, "smallDict"); 267 // free object 268 obj2->f->terminate(&obj2); 269 ck_assert_ptr_eq(obj2, NULL); 270 271 END_TEST 272 273 274 START_TEST(cSmallJsonT) 275 276 // local object 277 createSmallJson(obj); 278 ck_assert_str_eq(obj.type, "smallJson"); 279 // object 280 createAllocateSmallJson(obj2); 281 ck_assert_str_eq(obj2->type, "smallJson"); 282 // toString 283 char *s = obj2->f->toString(obj2); 284 ck_assert_str_eq(s, "{}"); 285 free(s); 286 createAllocateSmallInt(oInt); 287 oInt->f->set(oInt, 123); 288 obj2->f->set(obj2, "int", (baset *) oInt); 289 finishO(oInt); 290 initiateAllocateSmallInt(&oInt); 291 oInt->f->set(oInt, 123); 292 obj2->f->set(obj2, "int2", (baset *) oInt); 293 finishO(oInt); 294 // no effect - no push to dictionary 295 initiateAllocateSmallInt(&oInt); 296 obj2->f->push(obj2, (baset *) oInt); 297 finishO(oInt); 298 s = obj2->f->toString(obj2); 299 ck_assert_str_eq(s, "{\"int\":123,\"int2\":123}"); 300 free(s); 301 terminateO(obj2); 302 initiateAllocateSmallJson(&obj2); 303 createAllocateSmallInt(oInt2); 304 oInt2->f->set(oInt2, 123); 305 obj2->f->push(obj2, (baset *) oInt2); 306 finishO(oInt2); 307 initiateAllocateSmallInt(&oInt2); 308 oInt2->f->set(oInt2, 123); 309 obj2->f->push(obj2, (baset *) oInt2); 310 finishO(oInt2); 311 // no effect - no set with key to array 312 initiateAllocateSmallInt(&oInt2); 313 obj2->f->set(obj2, "noEffect", (baset *) oInt2); 314 finishO(oInt2); 315 s = obj2->f->toString(obj2); 316 ck_assert_str_eq(s, "[123,123]"); 317 free(s); 318 // duplicate 319 smallJsont *o; 320 o = obj2->f->duplicate(obj2); 321 s = toStringO(o); 322 ck_assert_str_eq(s, "[123,123]"); 323 free(s); 324 terminateO(o); 325 terminateO(obj2); 326 initiateAllocateSmallJson(&obj2); 327 createAllocateSmallInt(oInt3); 328 oInt3->f->set(oInt3, 123); 329 obj2->f->set(obj2, "int", (baset *) oInt3); 330 finishO(oInt3); 331 o = obj2->f->duplicate(obj2); 332 s = toStringO(o); 333 ck_assert_str_eq(s, "{\"int\":123}"); 334 free(s); 335 terminateO(o); 336 // dispose 337 // array 338 initiateAllocateSmallJson(&o); 339 initiateAllocateSmallInt(&oInt); 340 oInt->f->set(oInt, 123); 341 o->f->push(o, (baset*)oInt); 342 finishO(oInt); 343 ck_assert_uint_eq(o->topA->count, 1); 344 smallt *i = sArrayGetTiny(o->topA, 0); 345 o->f->dispose(o); 346 ck_assert_ptr_eq(o->topA, NULL); 347 sFree(i); 348 terminateO(o); 349 // dictionary 350 initiateAllocateSmallJson(&o); 351 initiateAllocateSmallInt(&oInt); 352 oInt->f->set(oInt, 123); 353 o->f->set(o, "in", (baset*)oInt); 354 finishO(oInt); 355 ck_assert_uint_eq(o->top->count, 1); 356 smallt *data = o->top->elements.data; 357 sFree(data); 358 o->f->dispose(o); 359 ck_assert_ptr_eq(o->top, NULL); 360 terminateO(o); 361 // smash 362 initiateAllocateSmallJson(&o); 363 // array 364 initiateAllocateSmallInt(&oInt); 365 oInt->f->set(oInt, 123); 366 o->f->push(o, (baset*)oInt); 367 finishO(oInt); 368 ck_assert_uint_eq(o->topA->count, 1); 369 i = sArrayGetTiny(o->topA, 0); 370 o->f->smash(&o); 371 ck_assert_ptr_eq(o, NULL); 372 sFree(i); 373 // dict 374 initiateAllocateSmallJson(&o); 375 initiateAllocateSmallInt(&oInt); 376 oInt->f->set(oInt, 123); 377 o->f->set(o, "in", (baset*)oInt); 378 finishO(oInt); 379 ck_assert_uint_eq(o->top->count, 1); 380 data = o->top->elements.data; 381 sFree(data); 382 o->f->smash(&o); 383 ck_assert_ptr_eq(o, NULL); 384 // setType* 385 initiateAllocateSmallJson(&o); 386 o->f->setTypeUndefined(o); 387 ck_assert_str_eq(o->f->getTopType(o), "undefined"); 388 o->f->setTypeBool(o); 389 ck_assert_str_eq(o->f->getTopType(o), "bool"); 390 o->f->setTypeDouble(o); 391 ck_assert_str_eq(o->f->getTopType(o), "double"); 392 o->f->setTypeInt(o); 393 ck_assert_str_eq(o->f->getTopType(o), "int"); 394 o->f->setTypeString(o); 395 ck_assert_str_eq(o->f->getTopType(o), "string"); 396 o->f->setTypeDict(o); 397 ck_assert_str_eq(o->f->getTopType(o), "dict"); 398 o->f->setTypeArray(o); 399 ck_assert_str_eq(o->f->getTopType(o), "array"); 400 terminateO(o); 401 // getTopTypeJson setTopJson getTopJson - undefined bool double int string 402 baset *b; 403 smallJsont *o2; 404 initiateAllocateSmallJson(&o); 405 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 406 // non json object 407 createAllocateSmallContainer(jcontainer); 408 // setTop 409 o->f->setTop(o, (baset *)jcontainer); 410 terminateO(jcontainer); 411 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 412 // getTop 413 b = o->f->getTop(o); 414 ck_assert_ptr_eq(b, NULL); 415 // undefined 416 createAllocateUndefined(jUndef); 417 // setTop 418 o->f->setTop(o, (baset *)jUndef); 419 finishO(jUndef); 420 // getTopType 421 ck_assert_str_eq(o->f->getTopType(o), "undefined"); 422 // getTop 423 b = o->f->getTop(o); 424 ck_assert_str_eq(b->type, "undefined"); 425 finishO(b); 426 // duplicateO 427 o2 = duplicateO(o); 428 // toStringO 429 s = toStringO(o2); 430 ck_assert_str_eq(s, "null"); 431 free(s); 432 // stringify 433 s = o->f->stringify(o,2); 434 ck_assert_str_eq(s, "null"); 435 free(s); 436 // toYML 437 s = o->f->toYML(o,2); 438 ck_assert_str_eq(s, "---\n null"); 439 free(s); 440 // smash 441 o2->f->smash(&o2); 442 // freeO 443 freeO(o); 444 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 445 terminateO(o); 446 // cBool 447 initiateAllocateSmallJson(&o); 448 createAllocateSmallBool(jBool); 449 // setTop 450 o->f->setTop(o, (baset *)jBool); 451 finishO(jBool); 452 // getTopType 453 ck_assert_str_eq(o->f->getTopType(o), "bool"); 454 // getTop 455 b = o->f->getTop(o); 456 ck_assert_str_eq(b->type, "smallBool"); 457 finishO(b); 458 // duplicateO 459 o2 = duplicateO(o); 460 // toStringO 461 s = toStringO(o2); 462 ck_assert_str_eq(s, "false"); 463 free(s); 464 // stringify 465 s = o->f->stringify(o,2); 466 ck_assert_str_eq(s, "false"); 467 free(s); 468 // toYML 469 s = o->f->toYML(o,2); 470 ck_assert_str_eq(s, "---\n false"); 471 free(s); 472 // smash 473 o2->f->smash(&o2); 474 // freeO 475 freeO(o); 476 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 477 terminateO(o); 478 // cDouble 479 initiateAllocateSmallJson(&o); 480 createAllocateSmallDouble(jDouble); 481 // setTop 482 o->f->setTop(o, (baset *)jDouble); 483 finishO(jDouble); 484 // getTopType 485 ck_assert_str_eq(o->f->getTopType(o), "double"); 486 // getTop 487 b = o->f->getTop(o); 488 ck_assert_str_eq(b->type, "smallDouble"); 489 finishO(b); 490 // duplicateO 491 o2 = duplicateO(o); 492 // toStringO 493 s = toStringO(o2); 494 ck_assert_str_eq(s, "0.000000e+00"); 495 free(s); 496 // stringify 497 s = o->f->stringify(o,2); 498 ck_assert_str_eq(s, "0.000000e+00"); 499 free(s); 500 // toYML 501 s = o->f->toYML(o,2); 502 ck_assert_str_eq(s, "---\n 0.000000e+00"); 503 free(s); 504 // smash 505 o2->f->smash(&o2); 506 // freeO 507 freeO(o); 508 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 509 terminateO(o); 510 // cInt 511 initiateAllocateSmallJson(&o); 512 createAllocateSmallInt(jInt); 513 // setTop 514 o->f->setTop(o, (baset *)jInt); 515 finishO(jInt); 516 // getTopType 517 ck_assert_str_eq(o->f->getTopType(o), "int"); 518 // getTop 519 b = o->f->getTop(o); 520 ck_assert_str_eq(b->type, "smallInt"); 521 finishO(b); 522 // duplicateO 523 o2 = duplicateO(o); 524 // toStringO 525 s = toStringO(o2); 526 ck_assert_str_eq(s, "0"); 527 free(s); 528 // stringify 529 s = o->f->stringify(o,2); 530 ck_assert_str_eq(s, "0"); 531 free(s); 532 // toYML 533 s = o->f->toYML(o,2); 534 ck_assert_str_eq(s, "---\n 0"); 535 free(s); 536 // smash 537 o2->f->smash(&o2); 538 // freeO 539 freeO(o); 540 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 541 terminateO(o); 542 // string 543 initiateAllocateSmallJson(&o); 544 createAllocateSmallString(jString); 545 jString->f->set(jString, "sheepy"); 546 // setTop 547 o->f->setTop(o, (baset *)jString); 548 finishO(jString); 549 // getTopType 550 ck_assert_str_eq(o->f->getTopType(o), "string"); 551 // getTop 552 b = o->f->getTop(o); 553 ck_assert_str_eq(b->type, "smallString"); 554 finishO(b); 555 // duplicateO 556 o2 = duplicateO(o); 557 // toStringO 558 s = toStringO(o2); 559 ck_assert_str_eq(s, "sheepy"); 560 free(s); 561 // stringify 562 s = o->f->stringify(o,2); 563 ck_assert_str_eq(s, "\"sheepy\""); 564 free(s); 565 // toYML 566 s = o->f->toYML(o,2); 567 ck_assert_str_eq(s, "---\n \"sheepy\""); 568 free(s); 569 // smash 570 o2->f->smash(&o2); 571 // freeO 572 freeO(o); 573 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 574 terminateO(o); 575 // dict 576 initiateAllocateSmallJson(&o); 577 createAllocateSmallDict(jdict); 578 // setTop 579 o->f->setTop(o, (baset *)jdict); 580 finishO(jdict); 581 // getTopType 582 ck_assert_str_eq(o->f->getTopType(o), "dict"); 583 // getTop 584 b = o->f->getTop(o); 585 ck_assert_str_eq(b->type, "smallDict"); 586 finishO(b); 587 // duplicateO 588 o2 = duplicateO(o); 589 // toStringO 590 s = toStringO(o2); 591 ck_assert_str_eq(s, "{}"); 592 free(s); 593 // stringify 594 s = o->f->stringify(o,2); 595 ck_assert_str_eq(s, "{}\n"); 596 free(s); 597 // toYML 598 s = o->f->toYML(o,2); 599 ck_assert_str_eq(s, "---\n {}\n"); 600 free(s); 601 // smash 602 o2->f->smash(&o2); 603 // freeO 604 freeO(o); 605 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 606 terminateO(o); 607 // array 608 initiateAllocateSmallJson(&o); 609 createAllocateSmallArray(jArray); 610 // setTop 611 o->f->setTop(o, (baset *)jArray); 612 finishO(jArray); 613 // getTopType 614 ck_assert_str_eq(o->f->getTopType(o), "array"); 615 // getTop 616 b = o->f->getTop(o); 617 ck_assert_str_eq(b->type, "smallArray"); 618 finishO(b); 619 // duplicateO 620 o2 = duplicateO(o); 621 // toStringO 622 s = toStringO(o2); 623 ck_assert_str_eq(s, "[]"); 624 free(s); 625 // stringify 626 s = o->f->stringify(o,2); 627 ck_assert_str_eq(s, "[]\n"); 628 free(s); 629 // toYML 630 s = o->f->toYML(o,2); 631 ck_assert_str_eq(s, "---\n []\n"); 632 free(s); 633 // smash 634 o2->f->smash(&o2); 635 // freeO 636 freeO(o); 637 ck_assert_ptr_eq(o->f->getTopType(o), NULL); 638 terminateO(o); 639 // get 640 smallIntt *in = (smallIntt *) obj2->f->get(obj2, "int"); 641 ck_assert(in->value->value == 123); 642 createAllocateSmallDict(oD2); 643 smallIntt *in2 = duplicateO(in); 644 oD2->f->set(oD2, "int", (baset *)in2); 645 finishO(in2); 646 in2 = duplicateO(in); 647 oD2->f->set(oD2, "int2", (baset *)in2); 648 finishO(in2); 649 obj2->f->set(obj2, "dict", (baset *)oD2); 650 finishO(oD2); 651 createAllocateSmallArray(oTA2); 652 in2 = duplicateO(in); 653 oTA2->f->push(oTA2, (baset *)in2); 654 finishO(in2); 655 in2 = duplicateO(in); 656 oTA2->f->push(oTA2, (baset *)in2); 657 finishO(in2); 658 smashO(in); 659 obj2->f->set(obj2, "array", (baset *)oTA2); 660 finishO(oTA2); 661 // get dict element 662 in = (smallIntt *) obj2->f->get(obj2, "\"dict\".\"int2\""); 663 ck_assert(in->value->value == 123); 664 smashO(in); 665 // non existing element 666 in = (smallIntt *) obj2->f->get(obj2, "\"dict\".\"void\""); 667 ck_assert_ptr_eq(in, NULL); 668 // missing " at the end 669 in = (smallIntt *) obj2->f->get(obj2, "\"dict\".\"int2"); 670 ck_assert_ptr_eq(in, NULL); 671 // get array element 672 in = (smallIntt *) obj2->f->get(obj2, "\"array\"[0]"); 673 ck_assert(in->value->value == 123); 674 smashO(in); 675 // missing ] at the end 676 in = (smallIntt *) obj2->f->get(obj2, "\"array\"[0"); 677 ck_assert_ptr_eq(in, NULL); 678 // json array 679 createAllocateSmallJson(json); 680 createAllocateSmallInt(oInt4); 681 oInt4->f->set(oInt4, 345); 682 json->f->push(json, (baset *) oInt4); 683 finishO(oInt4); 684 in = (smallIntt *) json->f->get(json, "[0]"); 685 ck_assert(in->value->value == 345); 686 smashO(in); 687 // set get json path 688 createSmallJson(jpath); 689 // dict is top 690 jpath.f->parse(&jpath, "{ \"a\": {\"a\": true}, \"b\": 234, \"c\": [\"qwe\",32]}"); 691 // get non existing element in 'c' array 692 b = jpath.f->get(&jpath,"\"c\"[3]"); 693 ck_assert_ptr_eq(b, NULL); 694 finishO(b); 695 // dictionary keys should not be unescaped 696 createSmallBool(ba); 697 jpath.f->set(&jpath, "b\\\\", cBa(&ba)); 698 jBool = (smallBoolt*)jpath.f->get(&jpath,"b\\\\"); 699 ck_assert(jBool->value->value == false); 700 finishO(jBool); 701 // keys in json paths should be unescaped 702 createSmallBool(bb); 703 bb.f->set(&bb, true); 704 jpath.f->set(&jpath, "\"b\\\\\"", (baset*)&bb); 705 jBool = (smallBoolt*)jpath.f->get(&jpath,"\"b\\\\\""); 706 ck_assert(jBool->value->value == true); 707 finishO(jBool); 708 freeO(&jpath); 709 // array is top 710 // get dict in dict 711 jpath.f->parse(&jpath, "[1,{\"a\": {\"a\": true}, \"b\": 234, \"c\": [\"qwe\",32]}, [[11],22,33]]"); 712 b = jpath.f->get(&jpath,"[1].\"a\""); 713 ck_assert_str_eq(b->type, "smallDict"); 714 finishO(b); 715 // get bool in dict 716 jBool = (smallBoolt*)jpath.f->get(&jpath,"[1]\"a\"\"a\""); 717 ck_assert(jBool->value->value == true); 718 finishO(jBool); 719 // get array in array 720 jArray = (smallArrayt*)jpath.f->get(&jpath,"[2][0]"); 721 ck_assert_str_eq(jArray->type, "smallArray"); 722 ck_assert_uint_eq(lenO(jArray), 1); 723 finishG(jArray); 724 // get element in array 725 in = (smallIntt*)(jpath.f->get(&jpath,"[2][0][0]")); 726 ck_assert_uint_eq(in->value->value, 11); 727 finishG(in); 728 // set element in array with negative index 729 createSmallBool(be); 730 jpath.f->set(&jpath, "[-1][0][0]", (baset*)&be); 731 // get element in array with negative index 732 jBool = (smallBoolt*)jpath.f->get(&jpath,"[-1][0][0]"); 733 ck_assert(jBool->value->value == false); 734 finishG(jBool); 735 // set new element in dict 736 createSmallBool(b2); 737 o2 = jpath.f->set(&jpath, "[1]\"a\"\"b\\\"\"", (baset*)&b2); 738 ck_assert_ptr_ne(o2, NULL); 739 jBool = (smallBoolt*)jpath.f->get(&jpath,"[1]\"a\"\"b\\\"\""); 740 ck_assert(jBool->value->value == false); 741 finishG(jBool); 742 createSmallBool(b3); 743 o2 = jpath.f->set(&jpath, "[1]\"a\"\"b\\\\\"", (baset*)&b3); 744 ck_assert_ptr_ne(o2, NULL); 745 jBool = (smallBoolt*)jpath.f->get(&jpath,"[1]\"a\"\"b\\\\\""); 746 ck_assert(jBool->value->value == false); 747 finishG(jBool); 748 // escape key in json path 749 // \\\"" 750 char *ks = jpath.f->makeKey(&jpath, "\\\\\\\"\""); 751 ck_assert_str_eq(ks, "\"\\\\\\\\\\\\\\\"\\\"\""); 752 createSmallBool(b4); 753 pErrorNULL(iPrependS(&ks, "[1]")); 754 o2 = jpath.f->set(&jpath, ks, (baset*)&b4); 755 ck_assert_ptr_ne(o2, NULL); 756 jBool = (smallBoolt*)jpath.f->get(&jpath,ks); 757 ck_assert(jBool->value->value == false); 758 finishG(jBool); 759 free(ks); 760 // wrong path 761 b = jpath.f->get(&jpath,"[3][0][0]"); 762 ck_assert_ptr_eq(b, NULL); 763 finishG(b); 764 // missing index 765 b = jpath.f->get(&jpath,"[][0][0]"); 766 ck_assert_ptr_eq(b, NULL); 767 finishG(b); 768 // try to assign dictionary key to array, wrong 769 createSmallBool(b0); 770 o2 = jpath.f->set(&jpath, "[2][0]\"sdf\\\"", (baset*)&b0); 771 ck_assert_ptr_eq(o2, NULL); 772 freeO(&jpath); 773 // len 774 initiateAllocateSmallJson(&o); 775 ck_assert_uint_eq(o->f->len(o), 0); 776 o->f->setTypeUndefined(o); 777 ck_assert_uint_eq(o->f->len(o), 1); 778 o->f->setTypeBool(o); 779 ck_assert_uint_eq(o->f->len(o), 1); 780 o->f->setTypeDouble(o); 781 ck_assert_uint_eq(o->f->len(o), 1); 782 o->f->setTypeInt(o); 783 ck_assert_uint_eq(o->f->len(o), 1); 784 o->f->setTypeString(o); 785 ck_assert_uint_eq(o->f->len(o), 0); 786 o->f->setTypeDict(o); 787 ck_assert_uint_eq(o->f->len(o), 0); 788 o->f->setTypeArray(o); 789 ck_assert_uint_eq(o->f->len(o), 0); 790 terminateO(o); 791 // empty 792 initiateAllocateSmallJson(&o); 793 // empty empty 794 o->f->empty(o); 795 o->f->setTypeString(o); 796 sFree((smallt *)o->topS); 797 o->topS = allocSStringTiny("er"); 798 o->f->empty(o); 799 ck_assert_uint_eq(o->f->len(o), 0); 800 o->f->setTypeDict(o); 801 initiateAllocateSmallInt(&in); 802 o->f->set(o, "wer", (baset *)in); 803 finishO(in); 804 o->f->empty(o); 805 ck_assert_uint_eq(o->f->len(o), 0); 806 o->f->setTypeArray(o); 807 initiateAllocateSmallInt(&in); 808 o->f->push(o, (baset *)in); 809 finishO(in); 810 o->f->empty(o); 811 ck_assert_uint_eq(o->f->len(o), 0); 812 terminateO(o); 813 // stringify 814 // array 815 initiateAllocateSmallString(&jString); 816 jString->f->set(jString, "sheepy"); 817 json->f->push(json, (baset *) jString); 818 finishO(jString); 819 initiateAllocateSmallDict(&jdict); 820 json->f->push(json, (baset *) jdict); 821 finishO(jdict); 822 initiateAllocateSmallArray(&jArray); 823 json->f->push(json, (baset *) jArray); 824 finishO(jArray); 825 initiateAllocateSmallDict(&jdict); 826 initiateAllocateSmallString(&jString); 827 jString->f->set(jString, "sheepy"); 828 jdict->f->set(jdict, "string", (baset *) jString); 829 finishO(jString); 830 initiateAllocateSmallContainer(&jcontainer); 831 jdict->f->set(jdict, "container", (baset *) jcontainer); 832 finishO(jcontainer); 833 json->f->push(json, (baset *) jdict); 834 finishO(jdict); 835 initiateAllocateSmallArray(&jArray); 836 initiateAllocateSmallString(&jString); 837 jString->f->set(jString, "sheepy"); 838 jArray->f->push(jArray, (baset *) jString); 839 finishO(jString); 840 initiateAllocateSmallContainer(&jcontainer); 841 jArray->f->push(jArray, (baset *) jcontainer); 842 finishO(jcontainer); 843 json->f->push(json, (baset *) jArray); 844 finishO(jArray); 845 846 s = json->f->stringify(json, 2); 847 ck_assert_str_eq(s, "[\n 345,\n \"sheepy\",\n {},\n [],\n {\n \"string\": \"sheepy\",\n \"container\": \"<data container>\"\n },\n [\n \"sheepy\",\n \"<data container>\"\n ]\n]\n"); 848 free(s); 849 // empty 850 createAllocateSmallJson(json2); 851 s = json2->f->stringify(json2, 2); 852 ck_assert_str_eq(s, "{}"); 853 free(s); 854 // dict 855 initiateAllocateSmallString(&jString); 856 jString->f->set(jString, "sheepy"); 857 json2->f->set(json2, "s", (baset *) jString); 858 finishO(jString); 859 initiateAllocateSmallDict(&jdict); 860 json2->f->set(json2, "d",(baset *) jdict); 861 finishO(jdict); 862 initiateAllocateSmallArray(&jArray); 863 json2->f->set(json2, "a", (baset *) jArray); 864 finishO(jArray); 865 initiateAllocateSmallDict(&jdict); 866 initiateAllocateSmallString(&jString); 867 jString->f->set(jString, "sheepy"); 868 jdict->f->set(jdict, "string", (baset *) jString); 869 finishO(jString); 870 initiateAllocateSmallContainer(&jcontainer); 871 jdict->f->set(jdict, "container", (baset *) jcontainer); 872 finishO(jcontainer); 873 json2->f->set(json2, "d2",(baset *) jdict); 874 finishO(jdict); 875 initiateAllocateSmallArray(&jArray); 876 initiateAllocateSmallString(&jString); 877 jString->f->set(jString, "sheepy"); 878 jArray->f->push(jArray, (baset *) jString); 879 finishO(jString); 880 initiateAllocateSmallContainer(&jcontainer); 881 jArray->f->push(jArray, (baset *) jcontainer); 882 finishO(jcontainer); 883 json2->f->set(json2, "a2", (baset *) jArray); 884 finishO(jArray); 885 initiateAllocateSmallInt(&oInt4); 886 oInt4->f->set(oInt4, 345); 887 json2->f->set(json2, "int", (baset *) oInt4); 888 finishO(oInt4); 889 s = json2->f->stringify(json2, 2); 890 ck_assert_str_eq(s, "{\n \"s\": \"sheepy\",\n \"d\": {},\n \"a\": [],\n \"d2\": {\n \"string\": \"sheepy\",\n \"container\": \"<data container>\"\n },\n \"a2\": [\n \"sheepy\",\n \"<data container>\"\n ],\n \"int\": 345\n}\n"); 891 free(s); 892 // toYML 893 // array 894 s = json->f->toYML(json, 2); 895 ck_assert_str_eq(s, "---\n - 345\n - sheepy\n - {}\n - []\n - string: sheepy\n container: \"<data container>\"\n - - sheepy\n - \"<data container>\"\n"); 896 free(s); 897 // empty 898 createAllocateSmallJson(json3); 899 s = json3->f->toYML(json3, 2); 900 ck_assert_str_eq(s, "---\n"); 901 free(s); 902 terminateO(json3); 903 // dict 904 s = json2->f->toYML(json2, 2); 905 ck_assert_str_eq(s, "---\n s: sheepy\n d:\n {}\n a:\n []\n d2:\n string: sheepy\n container: \"<data container>\"\n a2:\n - sheepy\n - \"<data container>\"\n int: 345\n"); 906 free(s); 907 terminateO(json2); 908 terminateO(json); 909 // parse 910 // top null - undefined 911 initiateAllocateSmallJson(&json); 912 json->f->parse(json, "null"); 913 ck_assert_str_eq(json->f->getTopType(json), "undefined"); 914 terminateO(json); 915 // top bool 916 initiateAllocateSmallJson(&json); 917 json->f->parse(json, "true"); 918 ck_assert_str_eq(json->f->getTopType(json), "bool"); 919 terminateO(json); 920 // top double 921 initiateAllocateSmallJson(&json); 922 json->f->parse(json, "0.01"); 923 ck_assert_str_eq(json->f->getTopType(json), "double"); 924 terminateO(json); 925 // top int 926 initiateAllocateSmallJson(&json); 927 json->f->parse(json, "10"); 928 ck_assert_str_eq(json->f->getTopType(json), "int"); 929 terminateO(json); 930 // top string 931 initiateAllocateSmallJson(&json); 932 json->f->parse(json, "\"wef\""); 933 ck_assert_str_eq(json->f->getTopType(json), "string"); 934 terminateO(json); 935 // from file dictionary 936 char **file = readText("file.json"); 937 char *jsonText = join(file, "\n"); 938 createAllocateSmallJson(jsOfromF); 939 jsOfromF->f->parse(jsOfromF, jsonText); 940 s = toStringO(jsOfromF); 941 //printf("JSON FILE: %s\n", s); 942 ck_assert_str_eq(s, "{\"dasd\":\"asd\",\"asd\":234,\"dict\":{\"array\":[1,1],\"asd\":true,\"float\":3.434000e+01},\"bool\":false,\"zzz\":null}"); 943 free(s); 944 free(jsonText); 945 listFreeS(file); 946 terminateO(jsOfromF); 947 // json array top 948 file = readText("fileA.json"); 949 jsonText = join(file, "\n"); 950 initiateAllocateSmallJson(&jsOfromF); 951 jsOfromF->f->parse(jsOfromF, jsonText); 952 s = toStringO(jsOfromF); 953 ck_assert_str_eq(s, "[\"asd\",234,{\"array\":[1,1],\"asd\":true,\"float\":3.434000e+01}]"); 954 free(s); 955 free(jsonText); 956 listFreeS(file); 957 terminateO(jsOfromF); 958 // wrong json incomplete dict 959 initiateAllocateSmallJson(&json); 960 json->f->parse(json, "{\"wef\": {"); 961 s = toStringO(json); 962 ck_assert_str_eq(s, "{}"); 963 free(s); 964 terminateO(json); 965 // wrong json incomplete array 966 initiateAllocateSmallJson(&json); 967 json->f->parse(json, "{\"wef\": ["); 968 s = toStringO(json); 969 ck_assert_str_eq(s, "{}"); 970 free(s); 971 terminateO(json); 972 // wrong json incomplete string 973 initiateAllocateSmallJson(&json); 974 json->f->parse(json, "{\"wef\": \"wer"); 975 s = toStringO(json); 976 ck_assert_str_eq(s, "{}"); 977 free(s); 978 terminateO(json); 979 // wrong json incomplete top dict 980 initiateAllocateSmallJson(&json); 981 json->f->parse(json, "{ \"wef"); 982 s = toStringO(json); 983 ck_assert_str_eq(s, "{}"); 984 free(s); 985 terminateO(json); 986 // wrong json incomplete top array 987 initiateAllocateSmallJson(&json); 988 json->f->parse(json, "[\"wef"); 989 s = toStringO(json); 990 ck_assert_str_eq(s, "{}"); 991 free(s); 992 terminateO(json); 993 // wrong json incomplete top string 994 initiateAllocateSmallJson(&json); 995 json->f->parse(json, "\"wef"); 996 s = toStringO(json); 997 ck_assert_str_eq(s, "{}"); 998 free(s); 999 terminateO(json); 1000 // parseYML 1001 // yml only top dict or array - NO: top null undefined top bool top double top int top string 1002 // from file dictionary 1003 file = readText("file.yml"); 1004 jsonText = join(file, "\n"); 1005 initiateAllocateSmallJson(&jsOfromF); 1006 jsOfromF->f->parseYML(jsOfromF, jsonText); 1007 s = toStringO(jsOfromF); 1008 ck_assert_str_eq(s, "{\"dict\":{\"array\":[1,1],\"float\":3.434000e+01,\"asd\":true},\"dasd\":\"asd\",\"asd\":234,\"bool\":false,\"zzz\":null}"); 1009 free(s); 1010 free(jsonText); 1011 listFreeS(file); 1012 terminateO(jsOfromF); 1013 // json array top 1014 file = readText("fileA.yml"); 1015 jsonText = join(file, "\n"); 1016 initiateAllocateSmallJson(&jsOfromF); 1017 jsOfromF->f->parseYML(jsOfromF, jsonText); 1018 s = toStringO(jsOfromF); 1019 //printf("JSON FILE: %s\n", toStringO(jsOfromF)); 1020 ck_assert_str_eq(s, "[{\"dict\":\"hello\",\"array\":[1,1],\"float\":3.434000e+01,\"asd\":true},\"asd\",234]"); 1021 free(s); 1022 free(jsonText); 1023 listFreeS(file); 1024 terminateO(jsOfromF); 1025 // serial deserial 1026 smallBytest *B; 1027 initiateAllocateSmallJson(&o); 1028 ck_assert_ptr_eq(o->f->serial(o), NULL); 1029 // non json object 1030 initiateAllocateSmallContainer(&jcontainer); 1031 o->f->setTop(o, (baset *)jcontainer); 1032 ck_assert_ptr_eq(o->f->serial(o), NULL); 1033 terminateO(jcontainer); 1034 // undefined 1035 undefinedt *oU = allocUndefined(); 1036 o->f->setTop(o, (baset *)oU); 1037 finishO(oU); 1038 B = o->f->serial(o); 1039 s = sToString((smallt *) B->B); 1040 ck_assert_str_eq(s, "[0x01]"); 1041 free(s); 1042 freeO(o); 1043 o->f->deserial(o, B); 1044 s = toStringO(o); 1045 ck_assert_str_eq(s, "null"); 1046 free(s); 1047 terminateO(B); 1048 terminateO(o); 1049 // Bool 1050 initiateAllocateSmallJson(&o); 1051 smallBoolt *oBool = allocSmallBool(true); 1052 o->f->setTop(o, (baset *)oBool); 1053 finishO(oBool); 1054 B = o->f->serial(o); 1055 s = sToString((smallt *) B->B); 1056 ck_assert_str_eq(s, "[0x02,0x01]"); 1057 free(s); 1058 o->f->deserial(o, B); 1059 s = toStringO(o); 1060 ck_assert_str_eq(s, "true"); 1061 free(s); 1062 terminateO(B); 1063 terminateO(o); 1064 // Double 1065 initiateAllocateSmallJson(&o); 1066 smallDoublet *od = allocSmallDouble(10); 1067 o->f->setTop(o, (baset *)od); 1068 finishO(od); 1069 B = o->f->serial(o); 1070 s = sToString((smallt *) B->B); 1071 ck_assert_str_eq(s, "[0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x40]"); 1072 free(s); 1073 o->f->deserial(o, B); 1074 s = toStringO(o); 1075 ck_assert_str_eq(s, "1.000000e+01"); 1076 free(s); 1077 terminateO(B); 1078 terminateO(o); 1079 // Int 1080 initiateAllocateSmallJson(&o); 1081 oInt = allocSmallInt(85); 1082 o->f->setTop(o, (baset *)oInt); 1083 finishO(oInt); 1084 B = o->f->serial(o); 1085 s = sToString((smallt *) B->B); 1086 ck_assert_str_eq(s, "[0x07,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00]"); 1087 free(s); 1088 o->f->deserial(o, B); 1089 s = toStringO(o); 1090 ck_assert_str_eq(s, "85"); 1091 free(s); 1092 terminateO(B); 1093 terminateO(o); 1094 // string 1095 initiateAllocateSmallJson(&o); 1096 smallStringt *st = allocSmallString("sheepy"); 1097 o->f->setTop(o, (baset *)st); 1098 finishO(st); 1099 B = o->f->serial(o); 1100 s = sToString((smallt *) B->B); 1101 ck_assert_str_eq(s, "[0x08,0x73,0x68,0x65,0x65,0x70,0x79,0x00]"); 1102 free(s); 1103 o->f->deserial(o, B); 1104 s = toStringO(o); 1105 ck_assert_str_eq(s, "sheepy"); 1106 free(s); 1107 terminateO(B); 1108 terminateO(o); 1109 // dict 1110 initiateAllocateSmallJson(&o); 1111 smallDictt *sDD = allocSmallDict(); 1112 o->f->setTop(o, (baset *)sDD); 1113 finishO(sDD); 1114 B = o->f->serial(o); 1115 s = sToString((smallt *) B->B); 1116 ck_assert_str_eq(s, "[0x04,0x00,0x00,0x00,0x00]"); 1117 free(s); 1118 o->f->deserial(o, B); 1119 s = toStringO(o); 1120 ck_assert_str_eq(s, "{}"); 1121 free(s); 1122 terminateO(B); 1123 terminateO(o); 1124 // array 1125 initiateAllocateSmallJson(&o); 1126 smallArrayt *sAA = allocSmallArray(); 1127 o->f->setTop(o, (baset *)sAA); 1128 finishO(sAA); 1129 B = o->f->serial(o); 1130 s = sToString((smallt *) B->B); 1131 ck_assert_str_eq(s, "[0x0a,0x00,0x00,0x00,0x00]"); 1132 free(s); 1133 o->f->deserial(o, B); 1134 s = toStringO(o); 1135 ck_assert_str_eq(s, "[]"); 1136 free(s); 1137 terminateO(B); 1138 terminateO(o); 1139 // deserial non json object 1140 initiateAllocateSmallJson(&o); 1141 initiateAllocateSmallBytes(&B); 1142 sContainert *c = allocSContainer(NULL); 1143 B->B = sSerial((smallt *) c); 1144 sFree((smallt *) c); 1145 o->f->deserial(o, B); 1146 ck_assert_uint_eq(o->topIsA, 0/*=JSON_IS_EMPTY*/); 1147 terminateO(B); 1148 terminateO(o); 1149 // deserial empty sBytest 1150 initiateAllocateSmallJson(&o); 1151 initiateAllocateSmallBytes(&B); 1152 o->f->deserial(o, B); 1153 ck_assert_uint_eq(o->topIsA, 0/*=JSON_IS_EMPTY*/); 1154 terminateO(B); 1155 terminateO(o); 1156 // NULL object 1157 initiateAllocateSmallJson(&o); 1158 o->f->setTypeUndefined(o); 1159 sFree((smallt *) o->topU); 1160 o->topU = NULL; 1161 ck_assert_ptr_eq(o->f->serial(o),NULL); 1162 o->f->deserial(o, NULL); 1163 terminateO(o); 1164 // free local object 1165 obj.f->free(&obj); 1166 ck_assert_str_eq(obj.type, "smallJson"); 1167 // free object 1168 obj2->f->terminate(&obj2); 1169 ck_assert_ptr_eq(obj2, NULL); 1170 initiateAllocateSmallJson(&o); 1171 terminateO(o); 1172 ck_assert_ptr_eq(o, NULL); 1173 // undefined 1174 initiateAllocateSmallJson(&o); 1175 o->f->setTypeUndefined(o); 1176 terminateO(o); 1177 ck_assert_ptr_eq(o, NULL); 1178 // bool 1179 initiateAllocateSmallJson(&o); 1180 o->f->setTypeBool(o); 1181 terminateO(o); 1182 ck_assert_ptr_eq(o, NULL); 1183 // double 1184 initiateAllocateSmallJson(&o); 1185 o->f->setTypeDouble(o); 1186 terminateO(o); 1187 ck_assert_ptr_eq(o, NULL); 1188 // int 1189 initiateAllocateSmallJson(&o); 1190 o->f->setTypeInt(o); 1191 terminateO(o); 1192 ck_assert_ptr_eq(o, NULL); 1193 // string 1194 initiateAllocateSmallJson(&o); 1195 o->f->setTypeString(o); 1196 terminateO(o); 1197 ck_assert_ptr_eq(o, NULL); 1198 // dict 1199 initiateAllocateSmallJson(&o); 1200 o->f->setTypeDict(o); 1201 terminateO(o); 1202 ck_assert_ptr_eq(o, NULL); 1203 // array 1204 initiateAllocateSmallJson(&o); 1205 o->f->setTypeArray(o); 1206 terminateO(o); 1207 ck_assert_ptr_eq(o, NULL); 1208 1209 END_TEST 1210 1211 1212 START_TEST(cSmallArrayT) 1213 1214 // local object 1215 createSmallArray(obj); 1216 ck_assert_str_eq(obj.type, "smallArray"); 1217 // object 1218 createAllocateSmallArray(obj2); 1219 ck_assert_str_eq(obj2->type, "smallArray"); 1220 // toString 1221 char *s = obj2->f->toString(obj2); 1222 ck_assert_str_eq(s, "[]"); 1223 free(s); 1224 createAllocateUndefined(oU); 1225 obj2->f->push(obj2, (baset *)oU); 1226 finishO(oU); 1227 createAllocateSmallInt(oInt); 1228 oInt->f->set(oInt, 123); 1229 obj2->f->push(obj2, (baset *)oInt); 1230 finishO(oInt); 1231 createAllocateSmallString(oStr); 1232 oStr->f->set(oStr, "sheepy"); 1233 obj2->f->push(obj2, (baset *)oStr); 1234 finishO(oStr); 1235 s = obj2->f->toString(obj2); 1236 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1237 free(s); 1238 // delete an element 1239 smallArrayt *o2; 1240 o2 = obj2->f->duplicate(obj2); 1241 o2->f->delElem(o2, 0); 1242 s = o2->f->toString(o2); 1243 ck_assert_str_eq(s, "[123,\"sheepy\"]"); 1244 free(s); 1245 terminateO(o2); 1246 // duplicate 1247 smallArrayt *o; 1248 o = obj2->f->duplicate(obj2); 1249 undefinedt *u; 1250 u = (undefinedt *) o->f->getAt(o, 0); 1251 ck_assert(isOType(u, "undefined")); 1252 terminateO(u); 1253 smallIntt *in; 1254 in = (smallIntt *) o->f->getAt(o, 1); 1255 ck_assert(in->value->value == 123); 1256 smashO(in); 1257 smallStringt *st; 1258 st = (smallStringt *) o->f->getAt(o, 2); 1259 bool r = strEq(st->f->get(st), "sheepy"); 1260 ck_assert(r); 1261 smashO(st); 1262 terminateO(o); 1263 // delete an element 1264 o2 = obj2->f->duplicate(obj2); 1265 o2->f->delElem(o2, 0); 1266 o = o2->f->duplicate(o2); 1267 in = (smallIntt *) o->f->getAt(o, 0); 1268 ck_assert(in->value->value == 123); 1269 smashO(in); 1270 st = (smallStringt *) o->f->getAt(o, 1); 1271 r = strEq(st->f->get(st), "sheepy"); 1272 ck_assert(r); 1273 smashO(st); 1274 terminateO(o); 1275 terminateO(o2); 1276 // fromArray 1277 initiateAllocateSmallArray(&o); 1278 char *array[] = {"1", "22", "333"}; 1279 char *arrayNULL[] = {"1", NULL, "333"}; 1280 // copy array to list 1281 o->f->fromArray(o, (void *)array, 3); 1282 ck_assert_uint_eq(o->f->len(o),3); 1283 smallStringt *strFrom; 1284 strFrom = (smallStringt*) o->f->getAt(o,0); 1285 ck_assert_str_eq(strFrom->f->get(strFrom), "1"); 1286 smashO(strFrom); 1287 strFrom = (smallStringt*) o->f->getAt(o,1); 1288 ck_assert_str_eq(strFrom->f->get(strFrom), "22"); 1289 smashO(strFrom); 1290 strFrom = (smallStringt*) o->f->getAt(o,2); 1291 ck_assert_str_eq(strFrom->f->get(strFrom), "333"); 1292 smashO(strFrom); 1293 // array with NULL inside 1294 o->f->fromArray(o, (void *)arrayNULL, 3); 1295 ck_assert_uint_eq(o->f->len(o),3); 1296 strFrom = (smallStringt*) o->f->getAt(o,0); 1297 ck_assert_str_eq(strFrom->f->get(strFrom), "1"); 1298 smashO(strFrom); 1299 strFrom = (smallStringt*) o->f->getAt(o,1); 1300 ck_assert_ptr_eq(strFrom, NULL); 1301 strFrom = (smallStringt*) o->f->getAt(o,2); 1302 ck_assert_str_eq(strFrom->f->get(strFrom), "333"); 1303 smashO(strFrom); 1304 // char** list 1305 char **shpList = listCreateS("lib", "sheepy"); 1306 o->f->fromArray(o, shpList, 0); 1307 ck_assert_uint_eq(o->f->len(o),2); 1308 listFreeS(shpList); 1309 // NULL pointer to list - list not changed 1310 o->f->fromArray(o, (void *)arrayNULL, 3); 1311 o->f->fromArray(o, NULL, 3); 1312 ck_assert_uint_eq(o->f->len(o),3); 1313 strFrom = (smallStringt*) o->f->getAt(o,0); 1314 ck_assert_str_eq(strFrom->f->get(strFrom), "1"); 1315 smashO(strFrom); 1316 strFrom = (smallStringt*) o->f->getAt(o,1); 1317 ck_assert_ptr_eq(strFrom, NULL); 1318 strFrom = (smallStringt*) o->f->getAt(o,2); 1319 ck_assert_str_eq(strFrom->f->get(strFrom), "333"); 1320 smashO(strFrom); 1321 terminateO(o); 1322 // push 1323 o = obj2->f->duplicate(obj2); 1324 initiateAllocateSmallInt(&in); 1325 in->f->set(in, 654); 1326 o->f->push(o, (baset *)in); 1327 finishO(in); 1328 in = (smallIntt *) o->f->getAt(o, 3); 1329 ck_assert_uint_eq(o->f->len(o), 4); 1330 ck_assert_uint_eq(in->value->value, 654); 1331 smashO(in); 1332 // NULL 1333 o->f->push(o, NULL); 1334 ck_assert_uint_eq(o->f->len(o), 4); 1335 terminateO(o); 1336 // pop 1337 o = obj2->f->duplicate(obj2); 1338 ck_assert_uint_eq(o->f->len(o), 3); 1339 st = (smallStringt *) o->f->pop(o); 1340 ck_assert_uint_eq(o->f->len(o), 2); 1341 ck_assert_str_eq(st->f->get(st), "sheepy"); 1342 terminateO(st); 1343 in = (smallIntt *) o->f->pop(o); 1344 ck_assert_uint_eq(in->value->value, 123); 1345 terminateO(in); 1346 u = (undefinedt *) o->f->pop(o); 1347 ck_assert(isOType(u, "undefined")); 1348 terminateO(u); 1349 // empty list 1350 u = (undefinedt *) o->f->pop(o); 1351 ck_assert_ptr_eq(u,NULL); 1352 // after initialize 1353 o->f->free(o); 1354 ck_assert_uint_eq(o->f->len(o), 0); 1355 u = (undefinedt *) o->f->pop(o); 1356 ck_assert_uint_eq(o->f->len(o), 0); 1357 ck_assert_ptr_eq(u,NULL); 1358 terminateO(o); 1359 // append 1360 o = obj2->f->duplicate(obj2); 1361 o2 = obj2->f->duplicate(obj2); 1362 in = (smallIntt *) o2->f->getAt(o2, 1); 1363 in->value->value = 789; 1364 o2->f->setAt(o2, 1, (baset *)in); 1365 finishO(in); 1366 o->f->append(o, o2); 1367 s = toStringO(o); 1368 ck_assert_str_eq(s, "[null,123,\"sheepy\",null,789,\"sheepy\"]"); 1369 free(s); 1370 // same list 1371 o->f->append(o, o); 1372 s = toStringO(o); 1373 ck_assert_str_eq(s, "[null,123,\"sheepy\",null,789,\"sheepy\"]"); 1374 free(s); 1375 o->f->free(o); 1376 o2->f->smash(&o2); 1377 // empty list + list 1378 o2 = obj2->f->duplicate(obj2); 1379 in = (smallIntt *) o2->f->getAt(o2, 1); 1380 in->value->value = 789; 1381 o2->f->setAt(o2, 1, (baset *)in); 1382 finishO(in); 1383 o->f->append(o, o2); 1384 s = toStringO(o); 1385 ck_assert_str_eq(s, "[null,789,\"sheepy\"]"); 1386 free(s); 1387 // list + empty list 1388 o2->f->dispose(o2); 1389 o->f->append(o, o2); 1390 s = toStringO(o); 1391 ck_assert_str_eq(s, "[null,789,\"sheepy\"]"); 1392 free(s); 1393 // empty list + empty list 1394 o->f->free(o); 1395 o->f->append(o, o2); 1396 s = toStringO(o); 1397 ck_assert_str_eq(s, "[]"); 1398 free(s); 1399 terminateO(o2); 1400 // NULL list 1401 o->f->append(o, NULL); 1402 s = toStringO(o); 1403 ck_assert_str_eq(s, "[]"); 1404 free(s); 1405 terminateO(o); 1406 // slice 1407 o = obj2->f->duplicate(obj2); 1408 initiateAllocateSmallInt(&in); 1409 in->f->set(in, 5345); 1410 o->f->push(o, (baset *) in); 1411 finishO(in); 1412 o->f->slice(o,1,-1); 1413 s = toStringO(o); 1414 ck_assert_str_eq(s, "[123,\"sheepy\"]"); 1415 free(s); 1416 terminateO(o); 1417 // start outside 1418 o = obj2->f->duplicate(obj2); 1419 initiateAllocateSmallInt(&in); 1420 in->f->set(in, 5345); 1421 o->f->push(o, (baset *) in); 1422 finishO(in); 1423 o->f->slice(o,20,-1); 1424 s = toStringO(o); 1425 ck_assert_str_eq(s, "[]"); 1426 free(s); 1427 terminateO(o); 1428 // start negative and outside 1429 o = obj2->f->duplicate(obj2); 1430 initiateAllocateSmallInt(&in); 1431 in->f->set(in, 5345); 1432 o->f->push(o, (baset *) in); 1433 finishO(in); 1434 o->f->slice(o,-20,1); 1435 s = toStringO(o); 1436 ck_assert_str_eq(s, "[null]"); 1437 free(s); 1438 terminateO(o); 1439 // end outside 1440 o = obj2->f->duplicate(obj2); 1441 initiateAllocateSmallInt(&in); 1442 in->f->set(in, 5345); 1443 o->f->push(o, (baset *) in); 1444 finishO(in); 1445 o->f->slice(o,2,40); 1446 s = toStringO(o); 1447 ck_assert_str_eq(s, "[\"sheepy\",5345]"); 1448 free(s); 1449 terminateO(o); 1450 // end negative and outside 1451 o = obj2->f->duplicate(obj2); 1452 initiateAllocateSmallInt(&in); 1453 in->f->set(in, 5345); 1454 o->f->push(o, (baset *) in); 1455 finishO(in); 1456 o->f->slice(o,2,-40); 1457 s = toStringO(o); 1458 ck_assert_str_eq(s, "[]"); 1459 free(s); 1460 terminateO(o); 1461 // end before start 1462 o = obj2->f->duplicate(obj2); 1463 initiateAllocateSmallInt(&in); 1464 in->f->set(in, 5345); 1465 o->f->push(o, (baset *) in); 1466 finishO(in); 1467 o->f->slice(o,3,2); 1468 s = toStringO(o); 1469 ck_assert_str_eq(s, "[]"); 1470 free(s); 1471 terminateO(o); 1472 // negative start last element 1473 o = obj2->f->duplicate(obj2); 1474 initiateAllocateSmallInt(&in); 1475 in->f->set(in, 5345); 1476 o->f->push(o, (baset *) in); 1477 finishO(in); 1478 o->f->slice(o,-1,0); 1479 s = toStringO(o); 1480 ck_assert_str_eq(s, "[5345]"); 1481 free(s); 1482 terminateO(o); 1483 // start = end 1484 o = obj2->f->duplicate(obj2); 1485 initiateAllocateSmallInt(&in); 1486 in->f->set(in, 5345); 1487 o->f->push(o, (baset *) in); 1488 finishO(in); 1489 o->f->slice(o,1,1); 1490 s = toStringO(o); 1491 ck_assert_str_eq(s, "[]"); 1492 free(s); 1493 terminateO(o); 1494 // empty list 1495 initiateAllocateSmallArray(&o); 1496 o->f->slice(o,0,0); 1497 s = toStringO(o); 1498 ck_assert_str_eq(s, "[]"); 1499 free(s); 1500 terminateO(o); 1501 // insert 1502 o = obj2->f->duplicate(obj2); 1503 o2 = obj2->f->duplicate(obj2); 1504 in = (smallIntt *) o2->f->getAt(o2, 1); 1505 in->value->value = 789; 1506 o2->f->setAt(o2, 1, (baset *)in); 1507 finishO(in); 1508 o->f->insert(o, 0, o2); 1509 s = toStringO(o); 1510 ck_assert_str_eq(s, "[null,789,\"sheepy\",null,123,\"sheepy\"]"); 1511 free(s); 1512 terminateO(o); 1513 o2->f->smash(&o2); 1514 // negative index 1515 o = obj2->f->duplicate(obj2); 1516 o2 = obj2->f->duplicate(obj2); 1517 in = (smallIntt *) o2->f->getAt(o2, 1); 1518 in->value->value = 789; 1519 o2->f->setAt(o2, 1, (baset *)in); 1520 finishO(in); 1521 o->f->insert(o, -1, o2); 1522 s = toStringO(o); 1523 ck_assert_str_eq(s, "[null,123,\"sheepy\",null,789,\"sheepy\"]"); 1524 free(s); 1525 terminateO(o); 1526 o2->f->smash(&o2); 1527 // edge 1528 o = obj2->f->duplicate(obj2); 1529 o2 = obj2->f->duplicate(obj2); 1530 in = (smallIntt *) o2->f->getAt(o2, 1); 1531 in->value->value = 789; 1532 o2->f->setAt(o2, 1, (baset *)in); 1533 finishO(in); 1534 o->f->insert(o, 3, o2); 1535 s = toStringO(o); 1536 ck_assert_str_eq(s, "[null,123,\"sheepy\",null,789,\"sheepy\"]"); 1537 free(s); 1538 terminateO(o); 1539 o2->f->smash(&o2); 1540 // outside 1541 o = obj2->f->duplicate(obj2); 1542 o2 = obj2->f->duplicate(obj2); 1543 in = (smallIntt *) o2->f->getAt(o2, 1); 1544 in->value->value = 789; 1545 o2->f->setAt(o2, 1, (baset *)in); 1546 finishO(in); 1547 o->f->insert(o, 4, o2); 1548 s = toStringO(o); 1549 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1550 free(s); 1551 o->f->insert(o, -5, o2); 1552 s = toStringO(o); 1553 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1554 free(s); 1555 terminateO(o); 1556 terminateO(o2); 1557 // negative index in a one element list 1558 o = obj2->f->duplicate(obj2); 1559 o2 = obj2->f->duplicate(obj2); 1560 in = (smallIntt *) o2->f->getAt(o2, 1); 1561 in->value->value = 789; 1562 o2->f->setAt(o2, 1, (baset *)in); 1563 finishO(in); 1564 o->f->delElem(o, 1); 1565 o->f->delElem(o, 2); 1566 o->f->trim(o); 1567 o->f->insert(o, -1, o2); 1568 s = toStringO(o); 1569 ck_assert_str_eq(s, "[null,null,789,\"sheepy\"]"); 1570 free(s); 1571 terminateO(o); 1572 o2->f->smash(&o2); 1573 // empty list 1574 initiateAllocateSmallArray(&o); 1575 o2 = obj2->f->duplicate(obj2); 1576 in = (smallIntt *) o2->f->getAt(o2, 1); 1577 in->value->value = 789; 1578 o2->f->setAt(o2, 1, (baset *)in); 1579 finishO(in); 1580 o->f->insert(o, 0, o2); 1581 s = toStringO(o); 1582 ck_assert_str_eq(s, "[null,789,\"sheepy\"]"); 1583 free(s); 1584 terminateO(o); 1585 o2->f->dispose(o2); 1586 // empty insert list 1587 o = obj2->f->duplicate(obj2); 1588 o->f->insert(o, 0, o2); 1589 s = toStringO(o); 1590 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1591 free(s); 1592 terminateO(o); 1593 o2->f->smash(&o2); 1594 //NULL insert 1595 o = obj2->f->duplicate(obj2); 1596 o->f->insert(o, 0, NULL); 1597 s = toStringO(o); 1598 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1599 free(s); 1600 terminateO(o); 1601 // del 1602 o = obj2->f->duplicate(obj2); 1603 initiateAllocateSmallInt(&in); 1604 in->f->set(in, 5345); 1605 o->f->push(o, (baset *) in); 1606 finishO(in); 1607 o->f->del(o,1,-1); 1608 s = toStringO(o); 1609 ck_assert_str_eq(s, "[null,5345]"); 1610 free(s); 1611 terminateO(o); 1612 // start outside 1613 o = obj2->f->duplicate(obj2); 1614 initiateAllocateSmallInt(&in); 1615 in->f->set(in, 5345); 1616 o->f->push(o, (baset *) in); 1617 finishO(in); 1618 o->f->del(o,20,-1); 1619 s = toStringO(o); 1620 ck_assert_str_eq(s, "[null,123,\"sheepy\",5345]"); 1621 free(s); 1622 terminateO(o); 1623 // start negative and outside 1624 o = obj2->f->duplicate(obj2); 1625 initiateAllocateSmallInt(&in); 1626 in->f->set(in, 5345); 1627 o->f->push(o, (baset *) in); 1628 finishO(in); 1629 o->f->del(o,-20,1); 1630 s = toStringO(o); 1631 ck_assert_str_eq(s, "[123,\"sheepy\",5345]"); 1632 free(s); 1633 terminateO(o); 1634 // end outside 1635 o = obj2->f->duplicate(obj2); 1636 initiateAllocateSmallInt(&in); 1637 in->f->set(in, 5345); 1638 o->f->push(o, (baset *) in); 1639 finishO(in); 1640 o->f->del(o,2,40); 1641 s = toStringO(o); 1642 ck_assert_str_eq(s, "[null,123]"); 1643 free(s); 1644 terminateO(o); 1645 // end negative and outside 1646 o = obj2->f->duplicate(obj2); 1647 initiateAllocateSmallInt(&in); 1648 in->f->set(in, 5345); 1649 o->f->push(o, (baset *) in); 1650 finishO(in); 1651 o->f->del(o,2,-40); 1652 s = toStringO(o); 1653 ck_assert_str_eq(s, "[null,123,\"sheepy\",5345]"); 1654 free(s); 1655 terminateO(o); 1656 // end before start 1657 o = obj2->f->duplicate(obj2); 1658 initiateAllocateSmallInt(&in); 1659 in->f->set(in, 5345); 1660 o->f->push(o, (baset *) in); 1661 finishO(in); 1662 o->f->del(o,3,2); 1663 s = toStringO(o); 1664 ck_assert_str_eq(s, "[null,123,\"sheepy\",5345]"); 1665 free(s); 1666 terminateO(o); 1667 // negative start last element 1668 o = obj2->f->duplicate(obj2); 1669 initiateAllocateSmallInt(&in); 1670 in->f->set(in, 5345); 1671 o->f->push(o, (baset *) in); 1672 finishO(in); 1673 o->f->del(o,-1,0); 1674 s = toStringO(o); 1675 ck_assert_str_eq(s, "[null,123,\"sheepy\"]"); 1676 free(s); 1677 terminateO(o); 1678 // start = end 1679 o = obj2->f->duplicate(obj2); 1680 initiateAllocateSmallInt(&in); 1681 in->f->set(in, 5345); 1682 o->f->push(o, (baset *) in); 1683 finishO(in); 1684 o->f->del(o,1,1); 1685 s = toStringO(o); 1686 ck_assert_str_eq(s, "[null,123,\"sheepy\",5345]"); 1687 free(s); 1688 terminateO(o); 1689 // empty list 1690 initiateAllocateSmallArray(&o); 1691 o->f->del(o,0,0); 1692 s = toStringO(o); 1693 ck_assert_str_eq(s, "[]"); 1694 free(s); 1695 terminateO(o); 1696 // len 1697 ck_assert_uint_eq(obj2->f->len(obj2), 3); 1698 // trim 1699 o = obj2->f->duplicate(obj2); 1700 ck_assert_uint_eq(o->f->len(o), 3); 1701 st = (smallStringt *) o->f->pop(o); 1702 terminateO(st); 1703 o->f->delElem(o, 0); 1704 o->f->trim(o); 1705 ck_assert_uint_eq(o->f->len(o), 1); 1706 s = toStringO(o); 1707 ck_assert_str_eq(s, "[123]"); 1708 free(s); 1709 terminateO(o); 1710 // getAt 1711 o = obj2->f->duplicate(obj2); 1712 u = (undefinedt *) o->f->getAt(o, 0); 1713 ck_assert_str_eq(u->type, "undefined"); 1714 terminateO(u); 1715 st = (smallStringt *) o->f->getAt(o, -1); 1716 ck_assert_str_eq(st->type, "smallString"); 1717 smashO(st); 1718 // outside 1719 u = (undefinedt *) o->f->getAt(o, 4); 1720 ck_assert_ptr_eq(u, NULL); 1721 u = (undefinedt *) o->f->getAt(o, -4); 1722 ck_assert_ptr_eq(u, NULL); 1723 terminateO(o); 1724 // empty 1725 initiateAllocateSmallArray(&o); 1726 u = (undefinedt *) o->f->getAt(o, 0); 1727 ck_assert_ptr_eq(u, NULL); 1728 terminateO(o); 1729 // setAt 1730 o = obj2->f->duplicate(obj2); 1731 initiateAllocateSmallInt(&in); 1732 o->f->setAt(o, 0, (baset *) in); 1733 finishO(in); 1734 s = toStringO(o); 1735 ck_assert_str_eq(s, "[0,123,\"sheepy\"]"); 1736 free(s); 1737 initiateAllocateSmallInt(&in); 1738 o->f->setAt(o, -1, (baset *) in); 1739 finishO(in); 1740 s = toStringO(o); 1741 ck_assert_str_eq(s, "[0,123,0]"); 1742 free(s); 1743 // outside 1744 o->f->setAt(o, 4, (baset *) in); 1745 s = toStringO(o); 1746 ck_assert_str_eq(s, "[0,123,0]"); 1747 free(s); 1748 o->f->setAt(o, -4, (baset *) in); 1749 s = toStringO(o); 1750 ck_assert_str_eq(s, "[0,123,0]"); 1751 free(s); 1752 terminateO(o); 1753 // empty 1754 o = obj2->f->duplicate(obj2); 1755 o->f->empty(o); 1756 ck_assert(o->f->isEmpty(o)); 1757 terminateO(o); 1758 // isEmpty 1759 o = obj2->f->duplicate(obj2); 1760 ck_assert(!o->f->isEmpty(o)); 1761 terminateO(o); 1762 initiateAllocateSmallArray(&o); 1763 ck_assert(o->f->isEmpty(o)); 1764 terminateO(o); 1765 // typeString type typeStrings types 1766 initiateAllocateSmallArray(&o2); 1767 o = allocSmallArray(); 1768 oU = allocUndefined(); 1769 o->f->push(o, (baset *)oU); 1770 finishO(oU); 1771 oInt = allocSmallInt(123); 1772 o->f->push(o, (baset *)oInt); 1773 finishO(oInt); 1774 oStr = allocSmallString("sheepy"); 1775 o->f->push(o, (baset *)oStr); 1776 finishO(oStr); 1777 o->f->push(o, NULL); 1778 // typeString 1779 s = (char *)o->f->typeString(o, 0); 1780 ck_assert_str_eq(s, "undefined"); 1781 // type 1782 char c = o->f->type(o, 2); 1783 ck_assert_uint_eq(c, STRING); 1784 // negative index 1785 c = o->f->type(o, -2); 1786 ck_assert_uint_eq(c, INT); 1787 // outside 1788 ck_assert(!o->f->type(o, 10)); 1789 ck_assert(!o->f->type(o, -14)); 1790 // NULL element 1791 c = o->f->type(o, 0); 1792 ck_assert_uint_eq(c, UNDEFINED); 1793 // empty object 1794 ck_assert(!o2->f->type(o2, 0)); 1795 // typeStrings 1796 const char **l = o->f->typeStrings(o); 1797 s = joinCS(l, ","); 1798 ck_assert_str_eq(s, "undefined,int,string"); 1799 free(s); 1800 free(l); 1801 // empty object 1802 ck_assert_ptr_eq(o2->f->typeStrings(o2), NULL); 1803 // types 1804 smallBytest *B = o->f->types(o); 1805 s = sToStringTiny((smallt *)B->B); 1806 ck_assert_str_eq(s, "[0x01,0x07,0x08]"); 1807 free(s); 1808 // empty object 1809 ck_assert_ptr_eq(o2->f->types(o2), NULL); 1810 terminateO(B); 1811 terminateO(o); 1812 terminateO(o2); 1813 // free local object 1814 obj.f->free(&obj); 1815 ck_assert_str_eq(obj.type, "smallArray"); 1816 // free object 1817 obj2->f->terminate(&obj2); 1818 ck_assert_ptr_eq(obj2, NULL); 1819 // init NULL 1820 initiateAllocateSmallArray(NULL); 1821 1822 END_TEST 1823 1824 1825 START_TEST(cUndefinedT) 1826 1827 // local object 1828 createUndefined(obj); 1829 ck_assert_str_eq(obj.type, "undefined"); 1830 // object 1831 createAllocateUndefined(obj2); 1832 ck_assert_str_eq(obj2->type, "undefined"); 1833 // toString 1834 char *s = obj2->f->toString(obj2); 1835 ck_assert_str_eq(s, "null"); 1836 free(s); 1837 // duplicate 1838 undefinedt *o; 1839 o = obj2->f->duplicate(obj2); 1840 terminateO(o); 1841 // free local object 1842 obj.f->free(&obj); 1843 ck_assert_str_eq(obj.type, "undefined"); 1844 // free object 1845 obj2->f->terminate(&obj2); 1846 ck_assert_ptr_eq(obj2, NULL); 1847 1848 END_TEST 1849 1850 1851 1852 START_TEST(cSmallBytesT) 1853 1854 // local object 1855 createSmallBytes(obj); 1856 ck_assert_str_eq(obj.type, "smallBytes"); 1857 // object 1858 createAllocateSmallBytes(obj2); 1859 ck_assert_str_eq(obj2->type, "smallBytes"); 1860 // alloc - pushBuffer 1861 smallBytest *obj3 = allocSmallBytes(NULL, 0); 1862 terminateO(obj3); 1863 // toString 1864 char *s = obj2->f->toString(obj2); 1865 ck_assert_str_eq(s, "[]"); 1866 free(s); 1867 // duplicate 1868 obj2->B = allocSBytes(); 1869 sBytesPush(&(obj2->B), 2); 1870 obj3 = duplicateO(obj2); 1871 ck_assert_uint_eq(obj3->B->data, 2); 1872 terminateO(obj3); 1873 // get 1874 obj3 = duplicateO(obj2); 1875 char *r = obj3->f->get(obj3); 1876 ck_assert_uint_eq(*r, 2); 1877 terminateO(obj3); 1878 // push 1879 obj3 = duplicateO(obj2); 1880 obj3->f->push(obj3, 4); 1881 r = obj3->f->get(obj3); 1882 ck_assert_uint_eq(r[1], 4); 1883 terminateO(obj3); 1884 // getAt 1885 obj3 = duplicateO(obj2); 1886 char c = obj3->f->getAt(obj3,0); 1887 ck_assert_uint_eq(c, 2); 1888 c = obj3->f->getAt(obj3,1); 1889 ck_assert_uint_eq(c, 0); 1890 terminateO(obj3); 1891 // NULL 1892 initiateAllocateSmallBytes(&obj3); 1893 c = obj3->f->getAt(obj3,0); 1894 ck_assert_uint_eq(c, 0); 1895 terminateO(obj3); 1896 // free local object 1897 obj.f->free(&obj); 1898 ck_assert_str_eq(obj.type, "smallBytes"); 1899 // free object 1900 obj2->f->terminate(&obj2); 1901 ck_assert_ptr_eq(obj2, NULL); 1902 1903 END_TEST 1904 1905 1906 START_TEST(putsOT) 1907 1908 createAllocateSmallArray(l); 1909 1910 // object 1911 putsO(l); 1912 // NULL 1913 putsO(NULL); 1914 1915 terminateO(l); 1916 1917 END_TEST 1918 1919 1920 START_TEST(execOT) 1921 1922 createAllocateSmallArray(l); 1923 1924 // command 1925 execO("ls libsheepyObjectTest.c", l, NULL); 1926 ck_assert_uint_eq(l->f->len(l),1); 1927 ck_assert_str_eq(l->f->getAtS(l, 0), "libsheepyObjectTest.c"); 1928 freeO(l); 1929 1930 // invalid command 1931 execO("randomComand", l, NULL); 1932 ck_assert_uint_eq(l->f->len(l),0); 1933 terminateO(l); 1934 // NULL command 1935 ck_assert_int_eq(execO(NULL, NULL, NULL), 0); 1936 1937 END_TEST 1938 1939 1940 START_TEST(walkDirOT) 1941 1942 smallArrayt *l; 1943 1944 // existing directory 1945 l = walkDirO("../dirTest.null"); 1946 ck_assert_uint_eq(l->f->len(l),3); 1947 ck_assert_str_eq(l->f->getAtS(l, 0), "../dirTest.null/one"); 1948 ck_assert_str_eq(l->f->getAtS(l, 1), "../dirTest.null/two/four"); 1949 ck_assert_str_eq(l->f->getAtS(l, 2), "../dirTest.null/two/three"); 1950 terminateO(l); 1951 // empty path 1952 ck_assert_ptr_eq(walkDirO(""), NULL); 1953 // non existing directory 1954 l = walkDirO("nonExisting.null"); 1955 ck_assert(l->f->isEmpty(l)); 1956 terminateO(l); 1957 // NULL path 1958 ck_assert_ptr_eq(walkDirO(NULL), NULL); 1959 1960 END_TEST 1961 1962 1963 START_TEST(expandHomeOT) 1964 1965 // no ~/ 1966 createAllocateSmallString(v); 1967 v->f->set(v, "sheepy"); 1968 expandHomeO(v); 1969 ck_assert(v->f->equalS(v, "sheepy")); 1970 terminateO(v); 1971 // NULL var 1972 expandHomeO(NULL); 1973 1974 END_TEST 1975 1976 1977 START_TEST(chDirOT) 1978 1979 // change directory 1980 smallStringt *c; 1981 c = getCwdO(); 1982 createAllocateSmallString(v); 1983 v->f->set(v, "../dirTest.null"); 1984 ck_assert(chDirO(v)); 1985 char *s = getCwd(); 1986 bool r = (size_t)findS(s, "dirTest.null"); 1987 ck_assert(r); 1988 chDirO(c); 1989 free(s); 1990 terminateO(c); 1991 // non existing dir 1992 createAllocateSmallString(n); 1993 n->f->set(n, "RandomNonExistingDir"); 1994 ck_assert(!chDirO(n)); 1995 // NULL path 1996 ck_assert(!chDirO(NULL)); 1997 terminateO(v); 1998 terminateO(n); 1999 2000 END_TEST 2001 2002 2003 START_TEST(fileExistsOT) 2004 2005 createAllocateSmallArray(sA); 2006 char **l = listCreateS("../libsheepyTest.c", "wefwepfk34.c", "../../src", ""); 2007 sA->f->fromArrayNFree(sA, l, 0); 2008 // detect existing file 2009 smallStringt *S = sA->f->getAtSmallString(sA, 0); 2010 ck_assert(fileExistsO(S)); 2011 finishO(S); 2012 // non existing file 2013 S = sA->f->getAtSmallString(sA, 1); 2014 ck_assert(!fileExistsO(S)); 2015 finishO(S); 2016 // folder 2017 S = sA->f->getAtSmallString(sA, 2); 2018 ck_assert(fileExistsO(S)); 2019 finishO(S); 2020 // empty path 2021 S = sA->f->getAtSmallString(sA, 3); 2022 ck_assert(!fileExistsO(S)); 2023 finishO(S); 2024 // NULL path 2025 ck_assert(!fileExistsO(NULL)); 2026 terminateO(sA); 2027 2028 END_TEST 2029 2030 2031 START_TEST(fileChmodOT) 2032 2033 // existing file 2034 createAllocateSmallString(v); 2035 v->f->set(v, "../chmodTest.null"); 2036 ck_assert(fileChmodO(v, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)); 2037 ck_assert(fileChmodO(v, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); 2038 // non existing file 2039 createAllocateSmallString(n); 2040 n->f->set(n, "qweqwe_null"); 2041 ck_assert(!fileChmodO(n, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); 2042 // empty path 2043 createAllocateSmallString(e); 2044 e->f->set(e, ""); 2045 ck_assert(!fileChmodO(e,0)); 2046 // NULL path 2047 ck_assert(!fileChmodO(NULL,0)); 2048 terminateO(v); 2049 terminateO(n); 2050 terminateO(e); 2051 2052 END_TEST 2053 2054 2055 START_TEST(fileSizeOT) 2056 2057 // existing file 2058 createAllocateSmallString(s); 2059 s->f->set(s, "../sizeTest.null"); 2060 ck_assert_uint_eq(fileSizeO(s), 743); 2061 // empty file 2062 createAllocateSmallString(v); 2063 v->f->set(v, "../chmodTest.null"); 2064 ck_assert_uint_eq(fileSizeO(v), 0); 2065 // non existing file 2066 createAllocateSmallString(n); 2067 n->f->set(n, "qweqwe_null"); 2068 ck_assert_int_eq(fileSizeO(n), -1); 2069 // empty path 2070 createAllocateSmallString(e); 2071 e->f->set(e, ""); 2072 ck_assert_int_eq(fileSizeO(e), -1); 2073 // NULL path 2074 ck_assert_int_eq(fileSizeO(NULL), -1); 2075 terminateO(s); 2076 terminateO(v); 2077 terminateO(n); 2078 terminateO(e); 2079 2080 END_TEST 2081 2082 2083 START_TEST(mkdirParentsOT) 2084 2085 // directory 2086 pError0(rmAll("mkdirTest.null/null")); 2087 createAllocateSmallString(s); 2088 s->f->set(s, "mkdirTest.null/null"); 2089 ck_assert_int_eq(mkdirParentsO(s),1); 2090 // not allowed 2091 createAllocateSmallString(v); 2092 v->f->set(v, "/usr/null"); 2093 ck_assert_int_eq(mkdirParentsO(v),0); 2094 // empty path 2095 createAllocateSmallString(e); 2096 e->f->set(e, ""); 2097 ck_assert_int_eq(mkdirParentsO(e),0); 2098 // NULL path 2099 ck_assert_int_eq(mkdirParentsO(NULL),0); 2100 terminateO(s); 2101 terminateO(e); 2102 terminateO(v); 2103 2104 END_TEST 2105 2106 2107 START_TEST(rmAllOT) 2108 2109 // directory 2110 createAllocateSmallString(s); 2111 s->f->set(s, "rmAllTest.null/null"); 2112 pError0(mkdirParents("rmAllTest.null/null")); 2113 ck_assert_int_eq(rmAllO(s),1); 2114 // empty path 2115 createAllocateSmallString(e); 2116 e->f->set(e,""); 2117 ck_assert_int_eq(rmAllO(e),0); 2118 // too little permissions 2119 createAllocateSmallString(v); 2120 v->f->set(v,"/var/lock"); 2121 ck_assert_int_eq(rmAllO(v),0); 2122 // NULL path 2123 ck_assert_int_eq(rmAllO(NULL),0); 2124 terminateO(s); 2125 terminateO(e); 2126 terminateO(v); 2127 2128 2129 END_TEST 2130 2131 2132 START_TEST(copyOT) 2133 2134 // file 2135 createAllocateSmallString(s); 2136 createAllocateSmallString(d); 2137 s->f->set(s, "../chmodTest.null"); 2138 d->f->set(d, "../copyTest.null"); 2139 ck_assert_int_eq(copyO(s, d),1); 2140 bool r = fileExists("../copyTest.null"); 2141 ck_assert(r); 2142 // too little permissions 2143 pError0(fileChmod("../copyTest.null", 1)); 2144 ck_assert_int_eq(copyO(d, d),0); 2145 pError0(fileChmod("../copyTest.null", 777)); 2146 pError0(rmAll("../copyTest.null")); 2147 // empty path 2148 createAllocateSmallString(e); 2149 e->f->set(e, ""); 2150 ck_assert_int_eq(copyO(e, d),0); 2151 ck_assert(!fileExists("copyTest.null")); 2152 ck_assert_int_eq(copyO(s, e),0); 2153 ck_assert(!fileExists("copyTest.null")); 2154 // NULL path 2155 ck_assert_int_eq(copyO(NULL, d),0); 2156 ck_assert_int_eq(copyO(s, NULL),0); 2157 ck_assert_int_eq(copyO(NULL, NULL),0); 2158 terminateO(s); 2159 terminateO(d); 2160 terminateO(e); 2161 2162 END_TEST 2163 2164 2165 START_TEST(randomSOT) 2166 2167 // get random string 2168 smallStringt *s = randomSO(10); 2169 ck_assert_ptr_ne(s, NULL); 2170 terminateO(s); 2171 // invalid length (0) 2172 ck_assert_ptr_eq(randomSO(0), NULL); 2173 2174 randomUrandomClose(); 2175 2176 END_TEST 2177 2178 2179 START_TEST(randomAlphaNumSOT) 2180 2181 // get random string 2182 smallStringt *s = randomAlphaNumSO(10); 2183 ck_assert_ptr_ne(s, NULL); 2184 terminateO(s); 2185 // invalid length (0) 2186 ck_assert_ptr_eq(randomAlphaNumSO(0), NULL); 2187 2188 randomUrandomClose(); 2189 2190 END_TEST 2191 2192 2193 START_TEST(readLineOT) 2194 2195 FILE *fp; 2196 smallStringt *s; 2197 2198 // file with data 2199 fp = fopen("../textTest.null", "r"); 2200 s = readLineO(fp); 2201 fclose(fp); 2202 ck_assert_str_eq(s->f->get(s), "LINE 1"); 2203 terminateO(s); 2204 // empty file or end of stream 2205 fp = fopen("../chmodTest.null", "r"); 2206 s = readLineO(fp); 2207 fclose(fp); 2208 ck_assert_str_eq(ssGet(s), ""); 2209 terminateO(s); 2210 // NULL stream 2211 ck_assert_ptr_eq(readLineO(NULL), NULL); 2212 2213 2214 END_TEST 2215 2216 2217 START_TEST(toSmalltT) 2218 2219 smallStringt *s; 2220 smallIntt *i; 2221 2222 // undefined 2223 createAllocateUndefined(u); 2224 sUndefinedt *su = (sUndefinedt *) toSmallt((baset *)u); 2225 finishO(u); 2226 ck_assert(isSType(su, UNDEFINED)); 2227 sFree((smallt *) su); 2228 // smallArray 2229 createAllocateSmallArray(a); 2230 initiateAllocateSmallString(&s); 2231 s->f->set(s, "sheepy"); 2232 a->f->push(a, (baset *) s); 2233 finishO(s); 2234 initiateAllocateSmallInt(&i); 2235 i->f->set(i, 20); 2236 a->f->push(a, (baset *) i); 2237 finishO(i); 2238 sArrayt *sa = (sArrayt *) toSmallt((baset *)a); 2239 finishO(a); 2240 ck_assert_uint_eq(sa->count, 2); 2241 char *S = sToString((smallt *) sa); 2242 ck_assert_str_eq(S, "[\"sheepy\",20]"); 2243 free(S); 2244 sFree((smallt *) sa); 2245 // smallDict 2246 createAllocateSmallDict(d); 2247 initiateAllocateSmallString(&s); 2248 s->f->set(s, "sheepy"); 2249 d->f->set(d, "s", (baset *) s); 2250 finishO(s); 2251 initiateAllocateSmallInt(&i); 2252 i->f->set(i, 20); 2253 d->f->set(d, "i", (baset *) i); 2254 finishO(i); 2255 sDictt *sd = (sDictt *) toSmallt((baset *)d); 2256 finishO(d); 2257 ck_assert_uint_eq(sd->count, 2); 2258 S = sToString((smallt *) sd); 2259 ck_assert_str_eq(S, "{\"s\":\"sheepy\",\"i\":20}"); 2260 free(S); 2261 sFree((smallt *) sd); 2262 // smallJson 2263 createAllocateSmallJson(j); 2264 smallt *so = toSmallt((baset *)j); 2265 ck_assert_ptr_ne(so, NULL); 2266 ck_assert_int_eq(so->type, CONTAINER); 2267 ck_assert_ptr_eq(((sContainert*)so)->data, j); 2268 sFree(so); // sFree does terminateO(j); because smallJson is stored in a container 2269 // NULL 2270 ck_assert_ptr_eq(toSmallt(NULL), NULL); 2271 2272 END_TEST 2273 2274 2275 START_TEST(toBasetT) 2276 2277 // bool 2278 sBoolt *sb = allocSBool(true); 2279 smallBoolt *b = (smallBoolt *) toBaset((smallt *)sb); 2280 ck_assert(b->value->value); 2281 terminateO(b); 2282 // container 2283 sContainert *sc = allocSContainer(strdup("sheepy")); 2284 smallContainert *c = (smallContainert *) toBaset((smallt *)sc); 2285 ck_assert_str_eq(c->data->data, "sheepy"); 2286 free(c->data->data); 2287 terminateO(c); 2288 // double 2289 sDoublet *sD = allocSDouble(10); 2290 smallDoublet *D = (smallDoublet *) toBaset((smallt *)sD); 2291 ck_assert_uint_eq((uint)D->value->value, 10); 2292 terminateO(D); 2293 // int 2294 sIntt *si = allocSInt(10); 2295 smallIntt *i = (smallIntt *) toBaset((smallt *)si); 2296 ck_assert_uint_eq(i->value->value, 10); 2297 terminateO(i); 2298 // string 2299 sStringt *ss = allocSStringTiny("sheepy"); 2300 smallStringt *s = (smallStringt *) toBaset((smallt *)ss); 2301 char *r = ssGet(s); 2302 ck_assert_str_eq(r, "sheepy"); 2303 terminateO(s); 2304 // undefined 2305 sUndefinedt *su = allocSUndefined(); 2306 undefinedt *u = (undefinedt *) toBaset((smallt *)su); 2307 ck_assert(isOType(u, "undefined")); 2308 sFree((smallt *) su); 2309 terminateO(u); 2310 // smallArray 2311 sArrayt *sa = allocSArray(); 2312 ss = allocSStringTiny("sheepy"); 2313 sArrayPushTiny(&sa, (smallt *) ss); 2314 si = allocSInt(20); 2315 sArrayPushTiny(&sa, (smallt *) si); 2316 smallArrayt *a = (smallArrayt *) toBaset((smallt *)sa); 2317 ck_assert_uint_eq(a->a->count, 2); 2318 char *S = toStringO(a); 2319 ck_assert_str_eq(S, "[\"sheepy\",20]"); 2320 free(S); 2321 terminateO(a); 2322 // smallDict 2323 sDictt *sd = allocSDict(); 2324 ss = allocSStringTiny("sheepy"); 2325 sDictPushTiny(&sd, "s", (smallt *) ss); 2326 si = allocSInt(20); 2327 sDictPushTiny(&sd, "i", (smallt *) si); 2328 smallDictt *d = (smallDictt *) toBaset((smallt *)sd); 2329 ck_assert_uint_eq(d->d->count, 2); 2330 S = toStringO(d); 2331 ck_assert_str_eq(S, "{\"s\":\"sheepy\",\"i\":20}"); 2332 free(S); 2333 terminateO(d); 2334 // NULL 2335 ck_assert_ptr_eq(toBaset(NULL), NULL); 2336 2337 END_TEST 2338 2339 2340 2341 2342 2343 Suite * libsheepySuite(void) { 2344 Suite *s; 2345 TCase *tc_core; 2346 2347 s = suite_create("libsheepy"); 2348 2349 /* Core test case */ 2350 tc_core = tcase_create("Object"); 2351 2352 setLogMode(LOG_VERBOSE); 2353 2354 // disable btrace to make the test run faster 2355 btraceDisable(); 2356 tcase_add_test(tc_core, cSmallDictT); 2357 tcase_add_test(tc_core, cSmallJsonT); 2358 tcase_add_test(tc_core, cSmallArrayT); 2359 tcase_add_test(tc_core, cUndefinedT); 2360 tcase_add_test(tc_core, cSmallBytesT); 2361 tcase_add_test(tc_core, putsOT); 2362 tcase_add_test(tc_core, execOT); 2363 tcase_add_test(tc_core, walkDirOT); 2364 tcase_add_test(tc_core, expandHomeOT); 2365 tcase_add_test(tc_core, chDirOT); 2366 tcase_add_test(tc_core, fileExistsOT); 2367 tcase_add_test(tc_core, fileChmodOT); 2368 tcase_add_test(tc_core, fileSizeOT); 2369 tcase_add_test(tc_core, mkdirParentsOT); 2370 tcase_add_test(tc_core, rmAllOT); 2371 tcase_add_test(tc_core, copyOT); 2372 tcase_add_test(tc_core, randomSOT); 2373 tcase_add_test(tc_core, randomAlphaNumSOT); 2374 tcase_add_test(tc_core, readLineOT); 2375 tcase_add_test(tc_core, toSmalltT); 2376 tcase_add_test(tc_core, toBasetT); 2377 2378 2379 suite_add_tcase(s, tc_core); 2380 2381 return s; 2382 } 2383 2384 int main(int ARGC UNUSED, char** ARGV UNUSED) { 2385 int number_failed; 2386 Suite *s; 2387 SRunner *sr; 2388 2389 s = libsheepySuite(); 2390 sr = srunner_create(s); 2391 2392 srunner_run_all(sr, CK_NORMAL); 2393 number_failed = srunner_ntests_failed(sr); 2394 srunner_free(sr); 2395 2396 exit((number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE); 2397 }