summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 15:31:34 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 16:06:04 +0100
commit86f5a16173ee5a8e22130defca58b03a6b997354 (patch)
treefcc6aa3c55f132c888f3c75235353f469789fa65
parentvariable ht capacity in fuzzing (diff)
downloadc-hashtable-86f5a16173ee5a8e22130defca58b03a6b997354.tar.gz
c-hashtable-86f5a16173ee5a8e22130defca58b03a6b997354.tar.bz2
c-hashtable-86f5a16173ee5a8e22130defca58b03a6b997354.zip
Start implementing referencing hashmap impl in fuzzer
-rw-r--r--lib/hashtable.c4
-rw-r--r--src/main.zig37
2 files changed, 31 insertions, 10 deletions
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 2015b9c..e7799ad 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -108,9 +108,9 @@ int hashtable_remove(HashTable ht, char* key) {
 
 	for (int i = 0; i < bucket->length; ++i) {
 		HashTableData* data = &bucket->data[i];
-		if (strcmp(data->key, key) == 0) {
+		if (!data->deleted && strcmp(data->key, key) == 0) {
 			data->deleted = 1;
-			return 0;
+			return 1;
 		}
 	}
 
diff --git a/src/main.zig b/src/main.zig
index 5d74277..c443859 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -42,6 +42,7 @@ test "removing element" {
     try std.testing.expectEqual(null, res);
 }
 
+const allocator = std.heap.page_allocator;
 test "fuzzing" {
     try std.testing.fuzz(struct {
         pub fn func(source: []const u8) !void {
@@ -49,29 +50,49 @@ test "fuzzing" {
             const capacity: u8 = if (source[0] == 0) 1 else source[0];
             var ht = hashtable.hashtable_init(capacity);
             defer _ = hashtable.hashtable_deinit(&ht);
+
+            std.debug.print("START!\n", .{});
+
+            var reference_hashmap = std.AutoHashMap([*c]u8, *u8).init(allocator); //TODO: testing allocator
+
             var i: usize = 1;
             while (i + 2 < source.len) : (i += 2) {
-                const data: i32 = 4;
                 const operation: u8 = source[i];
-                const key: [*c]u8 = @constCast(@as([2]u8, .{ source[i + 1], 0 })[0..]);
+                const rawkey = try allocator.alloc(u8, 2); //TODO: Do we actually need this?
+                rawkey[0] = source[i + 1];
+                rawkey[1] = 0; // Null terminator
                 const value: u8 = source[i + 2];
 
+                std.debug.print("Rawkey: {any}\n", .{rawkey.ptr});
+
+                const key: [*c]u8 = @ptrCast(rawkey);
+
+                std.debug.print("Key: ptr {any} - value {d}\n", .{ key, key.* });
+
                 switch (operation % 3) {
                     0 => {
-                        _ = hashtable.hashtable_get(ht, key);
+                        // std.debug.print("Getting key {any}\n", .{key});
+                        const ret: ?*u8 = @ptrCast(hashtable.hashtable_get(ht, key));
+                        const reference_ret: ?*u8 = reference_hashmap.get(key);
+                        // std.debug.print("Reference get value: {any}\n", .{reference_ret});
+                        try std.testing.expectEqual(reference_ret, ret);
                     },
                     1 => {
+                        std.debug.print("Putting key {any} - {d}\n", .{ key, key.* });
                         _ = hashtable.hashtable_put(ht, key, @constCast(&value));
+                        try reference_hashmap.put(key, @constCast(&value));
                     },
                     2 => {
-                        _ = hashtable.hashtable_remove(ht, key);
+                        std.debug.print("Removing key {any} - {d}\n", .{ key, key.* });
+                        const ret = hashtable.hashtable_remove(ht, key);
+                        const reference_ret = reference_hashmap.remove(key);
+                        const a: i32 = if (reference_ret) 1 else 0;
+                        try std.testing.expectEqual(a, ret);
                     },
                     else => unreachable,
                 }
-
-                _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));
-                const res: *align(1) i32 = @ptrCast(hashtable.hashtable_get(ht, @constCast("key")));
-                try std.testing.expectEqual(4, res.*);
+                //we cannot free or it will reuse the same memory and also doesnt make sense
+                // allocator.free(rawkey);
             }
         }
     }.func, .{});