Skip to content
Myr

stb_ds: fix bug with shgeti not returning correct value

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent a5cbacd · Browse files · 1 file+25 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−17 removed

  • other:+25 added−17 removed

1 file+25 added−17 removed

modified:stb_ds.h+25 added-17 removed
312 unmodified lines
313 enable STBDS_SIPHASH_2_4 (see the 'Compile-time options' section), and pass314 a strong random number to stbds_rand_seed.315 316 * The default value for the hash table is stored in foo[-1], so if you317 use code like 'hmget(T,k)->value = 5' you can overwrite the value318 stored by hmdefault if 'k' is not present.317 use code like 'hmget(T,k)->value = 5' you can accidentally overwrite318 the value stored by hmdefault if 'k' is not present.319320CREDITS321322 Sean Barrett -- library, idea for dynamic array API/implementation323 Per Vognsen -- idea for hash table API/implementation324 Rafael Sachetto -- arrpop()325326 Bugfixes:327 Vinh Truong327 Andy Durdin328 Shane Liesgang329 Vinh Truong330*/331332#ifdef STBDS_UNIT_TESTS333#define _CRT_SECURE_NO_WARNINGS
185 unmodified lines
519#define stbds_hmlenu(t) ((t) ? stbds_header((t)-1)->length-1 : 0)520521#define stbds_shput(t, k, v) \522 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \522 (t)[stbds_temp(t-1)].value = (v))523 (t)[stbds_temp((t)-1)].value = (v))524525#define stbds_shputs(t, s) \526 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \526 (t)[stbds_temp(t-1)] = (s))527 (t)[stbds_temp((t)-1)] = (s))528529#define stbds_shgeti(t,k) \530 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \530 stbds_temp(t))531 stbds_temp((t)-1))532533#define stbds_shgetp(t, k) \533 ((void) stbds_shgeti(t,k), &(t)[stbds_temp(t-1)])534 ((void) stbds_shgeti(t,k), &(t)[stbds_temp((t)-1)])535536#define stbds_shdel(t,k) \537 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)538
975 unmodified lines
1514 stbds_string_arena sa = { 0 };15151516 int i,j;15171518 STBDS_ASSERT(arrlen(arr)==0);1519 for (i=0; i < 20000; i += 50) {1520 for (j=0; j < i; ++j)1521 arrpush(arr,j);1522 arrfree(arr);
16 unmodified lines
1539 STBDS_ASSERT(arr[4] == 4);1540 arrfree(arr);1541 }15421541 hmdefault(intmap, -1);1542 i=1; STBDS_ASSERT(hmget(intmap, i) == -1);1543 i = 1;1544 STBDS_ASSERT(hmgeti(intmap,i) == -1);1545 hmdefault(intmap, -2);1546 STBDS_ASSERT(hmgeti(intmap, i) == -1);1547 STBDS_ASSERT(hmget (intmap, i) == -2);1548 for (i=0; i < testsize; i+=2)1549 hmput(intmap, i, i*5);1550 for (i=0; i < testsize; i+=1)1546 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1551 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );1552 else STBDS_ASSERT(hmget(intmap, i) == i*5);1553 for (i=0; i < testsize; i+=2)1554 hmput(intmap, i, i*3);1555 for (i=0; i < testsize; i+=1)1551 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1556 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );1557 else STBDS_ASSERT(hmget(intmap, i) == i*3);1558 for (i=2; i < testsize; i+=4)1559 hmdel(intmap, i); // delete half the entries1560 for (i=0; i < testsize; i+=1)1556 if (i & 3) STBDS_ASSERT(hmget(intmap, i) == -1 );1561 if (i & 3) STBDS_ASSERT(hmget(intmap, i) == -2 );1562 else STBDS_ASSERT(hmget(intmap, i) == i*3);1563 for (i=0; i < testsize; i+=1)1564 hmdel(intmap, i); // delete the rest of the entries 1565 for (i=0; i < testsize; i+=1)1561 STBDS_ASSERT(hmget(intmap, i) == -1 );1566 STBDS_ASSERT(hmget(intmap, i) == -2 );1567 hmfree(intmap);1568 for (i=0; i < testsize; i+=2)1569 hmput(intmap, i, i*3);1570 hmfree(intmap);
14 unmodified lines
1585 stralloc(&sa, strkey(i));1586 strreset(&sa);15871588 for (j=0; j < 2; ++j) {1589 STBDS_ASSERT(shgeti(strmap,"foo") == -1);1590 if (j == 0)1591 sh_new_strdup(strmap);1592 else1593 sh_new_arena(strmap);1594 STBDS_ASSERT(shgeti(strmap,"foo") == -1);1588 shdefault(strmap, -1);1595 shdefault(strmap, -2);1596 STBDS_ASSERT(shgeti(strmap,"foo") == -1);1597 for (i=0; i < testsize; i+=2)1598 shput(strmap, strkey(i), i*3);1599 for (i=0; i < testsize; i+=1)1592 if (i & 1) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1600 if (i & 1) STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );1601 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1602 for (i=2; i < testsize; i+=4)1603 shdel(strmap, strkey(i)); // delete half the entries1604 for (i=0; i < testsize; i+=1)1597 if (i & 3) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1605 if (i & 3) STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );1606 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1607 for (i=0; i < testsize; i+=1)1608 shdel(strmap, strkey(i)); // delete the rest of the entries 1609 for (i=0; i < testsize; i+=1)1602 STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1610 STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );1611 shfree(strmap);1612 }16131614 {
85 unmodified lines