123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #include "../Tests.h"
- #include "core/HashMap.h"
- #include "core/ToString.h"
- #include "core/Utility.h"
- static HashMap createIntMap() {
- HashMap map;
- initHashMap(&map, sizeof(int), sizeof(int));
- return map;
- }
- static HashMap getTestIntMap() {
- HashMap map = createIntMap();
- putTypedHashMapPair(&map, int, 1, int, 3);
- putTypedHashMapPair(&map, int, 2, int, 4);
- putTypedHashMapPair(&map, int, 3, int, 5);
- putTypedHashMapPair(&map, int, 0, int, 20);
- return map;
- }
- static void checkIntMap(HashMap* map) {
- int* a = searchTypedHashMapKey(map, int, 1, int);
- int* b = searchTypedHashMapKey(map, int, 2, int);
- int* c = searchTypedHashMapKey(map, int, 3, int);
- int* d = searchTypedHashMapKey(map, int, 0, int);
- if(TEST_NOT_NULL(a) && TEST_NOT_NULL(b) && TEST_NOT_NULL(c) &&
- TEST_NOT_NULL(d)) {
- TEST_INT(3, *a);
- TEST_INT(4, *b);
- TEST_INT(5, *c);
- TEST_INT(20, *d);
- }
- }
- static void testAdd() {
- HashMap map = createIntMap();
- putTypedHashMapPair(&map, int, 5, int, 4);
- int* value = searchTypedHashMapKey(&map, int, 5, int);
- if(TEST_NOT_NULL(value)) {
- TEST_INT(4, *value);
- }
- destroyHashMap(&map);
- }
- static void testMultipleAdd() {
- HashMap map = getTestIntMap();
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 0, int));
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 1, int));
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 2, int));
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 3, int));
- checkIntMap(&map);
- destroyHashMap(&map);
- }
- static void testSearch() {
- HashMap map = getTestIntMap();
- TEST_NULL(searchTypedHashMapKey(&map, int, 6, int));
- putTypedHashMapPair(&map, int, 5, int, 4);
- putTypedHashMapPair(&map, int, 10, int, 3);
- putTypedHashMapPair(&map, int, 15, int, 2);
- TEST_NULL(searchTypedHashMapKey(&map, int, 6, int));
- destroyHashMap(&map);
- }
- static void testSearchEmpty() {
- HashMap map = createIntMap();
- TEST_NULL(searchTypedHashMapKey(&map, int, 6, int));
- destroyHashMap(&map);
- }
- static void testAddReplace() {
- HashMap map = getTestIntMap();
- putTypedHashMapPair(&map, int, 5, int, 4);
- putTypedHashMapPair(&map, int, 5, int, 10);
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 5, int));
- int* a = searchTypedHashMapKey(&map, int, 5, int);
- if(TEST_NOT_NULL(a)) {
- TEST_INT(10, *a);
- }
- destroyHashMap(&map);
- }
- static void testClear() {
- HashMap map = getTestIntMap();
- putTypedHashMapPair(&map, int, 5, int, 4);
- putTypedHashMapPair(&map, int, 4, int, 10);
- clearHashMap(&map);
- TEST_NULL(searchTypedHashMapKey(&map, int, 5, int));
- TEST_NULL(searchTypedHashMapKey(&map, int, 4, int));
- destroyHashMap(&map);
- }
- static void testClearEmpty() {
- HashMap map = createIntMap();
- clearHashMap(&map);
- destroyHashMap(&map);
- }
- static void testOverflow(bool light) {
- int limit = light ? 10000 : 100000;
- HashMap map = getTestIntMap();
- for(int i = 0; i < limit; i++) {
- putTypedHashMapPair(&map, int, i, int, i);
- }
- for(int i = 0; i < limit; i++) {
- TEST_NOT_NULL(searchTypedHashMapKey(&map, int, i, int));
- }
- destroyHashMap(&map);
- }
- static void testToString() {
- HashMap map = getTestIntMap();
- char buffer[128];
- size_t n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
- TEST_SIZE(29, n);
- TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 0 = 20]", buffer);
- clearHashMap(&map);
- putTypedHashMapPair(&map, int, 1, int, 3);
- n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
- TEST_SIZE(7, n);
- TEST_STRING("[1 = 3]", buffer);
- clearHashMap(&map);
- n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
- TEST_SIZE(2, n);
- TEST_STRING("[]", buffer);
- destroyHashMap(&map);
- }
- static void testEntryForEach() {
- HashMap map = createIntMap();
- putTypedHashMapPair(&map, int, 0, int, -1);
- putTypedHashMapPair(&map, int, 5, int, 4);
- putTypedHashMapPair(&map, int, 10, int, 3);
- putTypedHashMapPair(&map, int, 15, int, 2);
- int counter = 0;
- HashMapIterator i;
- initHashMapIterator(&i, &map);
- while(hasNextHashMapNode(&i)) {
- HashMapNode* n = nextHashMapNode(&i);
- counter += *(const int*)n->key + *(int*)n->value;
- }
- TEST_INT(38, counter);
- destroyHashMap(&map);
- }
- static void testInvalidPut() {
- HashMap map = createIntMap();
- char buffer[128];
- toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
- TEST_STRING("[]", buffer);
- putTypedHashMapPair(&map, int, 0, int, 3);
- int* v = searchTypedHashMapKey(&map, int, 0, int);
- if(TEST_NOT_NULL(v)) {
- TEST_INT(3, *v);
- }
- toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
- TEST_STRING("[0 = 3]", buffer);
- clearHashMap(&map);
- TEST_NULL(searchTypedHashMapKey(&map, int, 0, int));
- destroyHashMap(&map);
- }
- static void testAddCollisions() {
- HashMap map = getTestIntMap();
- for(int i = 0; i < 16; i++) {
- putTypedHashMapPair(&map, int, i * 64, int, i);
- }
- destroyHashMap(&map);
- }
- static void testRemove() {
- HashMap map = createIntMap();
- putTypedHashMapPair(&map, int, 1, int, 3);
- putTypedHashMapPair(&map, int, 2, int, 4);
- putTypedHashMapPair(&map, int, 3, int, 5);
- TEST_TRUE(removeTypedHashMapKey(&map, size_t, 2));
- TEST_FALSE(removeTypedHashMapKey(&map, size_t, 7));
- int* a = searchTypedHashMapKey(&map, int, 1, int);
- int* b = searchTypedHashMapKey(&map, int, 2, int);
- int* c = searchTypedHashMapKey(&map, int, 3, int);
- TEST_NULL(b);
- if(TEST_NOT_NULL(a) && TEST_NOT_NULL(c)) {
- TEST_INT(3, *a);
- TEST_INT(5, *c);
- }
- destroyHashMap(&map);
- }
- static void testHash() {
- u32 buffer[] = {0xFFAA00BB, 0x00000000, 0x00FF00FF};
- TEST_SIZE(0xFF550044, hashKey(buffer, sizeof(buffer)));
- const char* s = "wusi";
- TEST_TRUE(hashString(&s, sizeof(&s)) != 0);
- TEST_SIZE(0, hashString(&s, 1));
- }
- typedef struct {
- size_t a;
- size_t b;
- } A;
- static void testSearchStruct() {
- HashMap map;
- initHashMap(&map, sizeof(A), sizeof(int));
- A a = {1, 2};
- A b = {1, 3};
- A c = {0, 0};
- TEST_NULL(searchHashMapKey(&map, &a));
- TEST_NULL(searchHashMapKey(&map, &b));
- TEST_NULL(searchHashMapKey(&map, &c));
- int v = 3;
- putHashMapPair(&map, &a, &v);
- int* ap = searchHashMapKey(&map, &a);
- if(TEST_NOT_NULL(ap)) {
- TEST_INT(3, *ap);
- }
- TEST_NULL(searchHashMapKey(&map, &b));
- TEST_NULL(searchHashMapKey(&map, &c));
- v = 4;
- putHashMapPair(&map, &c, &v);
- int* cp = searchHashMapKey(&map, &c);
- if(TEST_NOT_NULL(cp)) {
- TEST_INT(4, *cp);
- }
- destroyHashMap(&map);
- }
- static void testSearchSize() {
- HashMap map;
- initHashMap(&map, sizeof(size_t), sizeof(int));
- TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int));
- TEST_NULL(searchTypedHashMapKey(&map, size_t, 1, int));
- TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int));
- putTypedHashMapPair(&map, size_t, 1, int, 3);
- int* ap = searchTypedHashMapKey(&map, size_t, 1, int);
- if(TEST_NOT_NULL(ap)) {
- TEST_INT(3, *ap);
- }
- TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int));
- TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int));
- putTypedHashMapPair(&map, size_t, 0, int, 4);
- int* cp = searchTypedHashMapKey(&map, size_t, 0, int);
- if(TEST_NOT_NULL(cp)) {
- TEST_INT(4, *cp);
- }
- destroyHashMap(&map);
- }
- void testHashMap(bool light) {
- testAdd();
- testMultipleAdd();
- testSearch();
- testSearchEmpty();
- testAddReplace();
- testClear();
- testClearEmpty();
- testOverflow(light);
- testToString();
- testEntryForEach();
- testInvalidPut();
- testAddCollisions();
- testRemove();
- testHash();
- testSearchStruct();
- testSearchSize();
- }
|