summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 00:02:56 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 00:02:56 +0100
commit9034899b3e8158cb35d3d62a64082ce67c72d669 (patch)
tree44126a758f06f6cb7372138561be6e7c475b03fe /src/main.zig
parentswitch compiler to zig (diff)
downloadc-hashtable-9034899b3e8158cb35d3d62a64082ce67c72d669.tar.gz
c-hashtable-9034899b3e8158cb35d3d62a64082ce67c72d669.tar.bz2
c-hashtable-9034899b3e8158cb35d3d62a64082ce67c72d669.zip
Call c from zig
Diffstat (limited to '')
-rw-r--r--src/main.zig31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..926f26b
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,31 @@
+const std = @import("std");
+
+const hashtable = @cImport({
+    @cInclude("hashtable.c");
+});
+
+pub fn main() !void {
+    std.debug.print("Testing hashmap!\n", .{});
+
+    var ht = hashtable.hashtable_init();
+    defer _ = hashtable.hashtable_deinit(&ht);
+
+    const Example = struct {
+        data: i32 align(1),
+    };
+
+    const data = Example{
+        .data = 7,
+    };
+
+    _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));
+    const res: *Example = @ptrCast(hashtable.hashtable_get(ht, @constCast("key")));
+    std.debug.print("Result: {d}\n", .{res.*.data});
+}
+
+test "simple test" {
+    var list = std.ArrayList(i32).init(std.testing.allocator);
+    defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
+    try list.append(42);
+    try std.testing.expectEqual(@as(i32, 42), list.pop());
+}