ox

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

hmap_test.c (3568B)


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