Kaynağa Gözat

Test struct as map key

Kajetan Johannes Hammerle 10 ay önce
ebeveyn
işleme
e2081b386c
1 değiştirilmiş dosya ile 35 ekleme ve 0 silme
  1. 35 0
      test/modules/HashMapTests.c

+ 35 - 0
test/modules/HashMapTests.c

@@ -210,6 +210,40 @@ static void testHash() {
     CORE_TEST_SIZE(0, coreHashString(&s, 1));
 }
 
+typedef struct {
+    size_t a;
+    size_t b;
+} A;
+
+static void testSearchStruct() {
+    CoreHashMap map =
+        CORE_HASH_MAP(sizeof(A), sizeof(int), coreHash, coreEqual);
+    A a = {1, 2};
+    A b = {1, 3};
+    A c = {0, 0};
+    CORE_TEST_NULL(coreHashMapSearchPointer(&map, &a));
+    CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b));
+    CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c));
+    int v = 3;
+    coreHashMapPutPointer(&map, &a, &v);
+
+    int* ap = coreHashMapSearchPointer(&map, &a);
+    if(CORE_TEST_NOT_NULL(ap)) {
+        CORE_TEST_INT(3, *ap);
+    }
+    CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b));
+    CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c));
+
+    v = 4;
+    coreHashMapPutPointer(&map, &c, &v);
+    int* cp = coreHashMapSearchPointer(&map, &c);
+    if(CORE_TEST_NOT_NULL(cp)) {
+        CORE_TEST_INT(4, *cp);
+    }
+
+    coreDestroyHashMap(&map);
+}
+
 void coreTestHashMap(bool light) {
     testAdd();
     testMultipleAdd();
@@ -225,4 +259,5 @@ void coreTestHashMap(bool light) {
     testAddCollisions();
     testRemove();
     testHash();
+    testSearchStruct();
 }