ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

hmap_test.c (3575B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "hmap.h"
      6 
      7 static void
      8 hmap_test_basic(void)
      9 {
     10 	int i;
     11 	printf("Testing dict of integers...\n");
     12 
     13 	HashMap* map = hmap_create(sizeof(int));
     14 
     15 	struct {
     16 		const char* key;
     17 		int value;
     18 	} items[] = {
     19 		{ "one", 1 },
     20 		{ "two", 2 },
     21 		{ "three", 3 },
     22 	};
     23 
     24 	// Insert items
     25 	for (i = 0; i < 3; i++) {
     26 		hmap_put(map, items[i].key, &items[i].value);
     27 	}
     28 
     29 	// Retrieve and check
     30 	int v;
     31 	int all_ok = 1;
     32 	for (i = 0; i < 3; i++) {
     33 		if (hmap_get(map, items[i].key, &v) && v == items[i].value) {
     34 			printf("PASS: %s == %d\n", items[i].key, items[i].value);
     35 		} else {
     36 			printf("FAIL: %s\n", items[i].key);
     37 			all_ok = 0;
     38 		}
     39 	}
     40 	if (all_ok) printf("PASS: dict of integers test\n");
     41 }
     42 
     43 static void
     44 hmap_test_T(void)
     45 {
     46 	int i;
     47 	printf("Testing struct T...\n");
     48 
     49 	struct T {
     50 		int id;
     51 		char name[32];
     52 		int age;
     53 		char email[64];
     54 		float score;
     55 	};
     56 
     57 	HashMap* map = hmap_create(sizeof(struct T));
     58 
     59 	struct T items[] = {
     60 		{ 1, "alice", 30, "alice@example.com", 95.5f },
     61 		{ 2, "bob", 25, "bob@example.com", 88.0f },
     62 		{ 3, "carol", 28, "carol@example.com", 91.2f },
     63 	};
     64 
     65 	// Insert items
     66 	for (i = 0; i < 3; i++) {
     67 		hmap_put(map, items[i].name, &items[i]);
     68 	}
     69 
     70 	// Retrieve and check
     71 	struct T v;
     72 	int all_ok = 1;
     73 	for (i = 0; i < 3; i++) {
     74 		if (hmap_get(map, items[i].name, &v) && v.id == items[i].id && strcmp(v.name, items[i].name) == 0 && v.age == items[i].age
     75 			&& strcmp(v.email, items[i].email) == 0 && v.score == items[i].score) {
     76 			printf("PASS: %s == {id:%d, age:%d, email:%s, score:%.1f}\n",
     77 				items[i].name,
     78 				items[i].id,
     79 				items[i].age,
     80 				items[i].email,
     81 				items[i].score);
     82 		} else {
     83 			printf("FAIL: %s\n", items[i].name);
     84 			all_ok = 0;
     85 		}
     86 	}
     87 	if (all_ok) printf("PASS: struct T test\n");
     88 }
     89 
     90 static void
     91 hmap_test_memory_bumping(void)
     92 {
     93 	int i;
     94 	printf("Testing memory bumping...\n");
     95 
     96 	HashMap* map = hmap_create(sizeof(int));
     97 	const int N = 1000; // Large enough to trigger resizing
     98 
     99 	char key[32];
    100 	int all_ok = 1;
    101 
    102 	// Insert N items
    103 	for (i = 0; i < N; i++) {
    104 		snprintf(key, sizeof(key), "key_%d", i);
    105 		hmap_put(map, key, &i);
    106 	}
    107 
    108 	// Retrieve and check all N items
    109 	for (i = 0; i < N; i++) {
    110 		snprintf(key, sizeof(key), "key_%d", i);
    111 		int v = -1;
    112 		if (hmap_get(map, key, &v) && v == i) {
    113 			// Optionally print only a few
    114 			if (i < 3 || i > N - 3) printf("PASS: %s == %d\n", key, v);
    115 		} else {
    116 			printf("FAIL: %s\n", key);
    117 			all_ok = 0;
    118 		}
    119 	}
    120 	if (all_ok) printf("PASS: memory bumping test\n");
    121 }
    122 
    123 static void
    124 hmap_test_removal(void)
    125 {
    126 	int i;
    127 	printf("Testing removal...\n");
    128 
    129 	HashMap* map = hmap_create(sizeof(int));
    130 
    131 	struct {
    132 		const char* key;
    133 		int value;
    134 	} items[] = {
    135 		{ "alpha", 10 },
    136 		{ "beta", 20 },
    137 		{ "gamma", 30 },
    138 	};
    139 
    140 	// Insert items
    141 	for (i = 0; i < 3; i++) {
    142 		hmap_put(map, items[i].key, &items[i].value);
    143 	}
    144 
    145 	// Remove "beta"
    146 	hmap_remove(map, "beta");
    147 
    148 	// Check "beta" is gone, others remain
    149 	int v;
    150 	int all_ok = 1;
    151 	for (i = 0; i < 3; i++) {
    152 		int found = hmap_get(map, items[i].key, &v);
    153 		if (strcmp(items[i].key, "beta") == 0) {
    154 			if (!found) {
    155 				printf("PASS: %s removed\n", items[i].key);
    156 			} else {
    157 				printf("FAIL: %s still present\n", items[i].key);
    158 				all_ok = 0;
    159 			}
    160 		} else {
    161 			if (found && v == items[i].value) {
    162 				printf("PASS: %s == %d\n", items[i].key, items[i].value);
    163 			} else {
    164 				printf("FAIL: %s\n", items[i].key);
    165 				all_ok = 0;
    166 			}
    167 		}
    168 	}
    169 	if (all_ok) printf("PASS: removal test\n");
    170 }
    171 
    172 void
    173 hmap_tests(void)
    174 {
    175 	hmap_test_basic();
    176 	hmap_test_T();
    177 	hmap_test_memory_bumping();
    178 	hmap_test_removal();
    179 }