summary refs log tree commit diff
path: root/src/main.zig
blob: 3a94dee147a5753fa783432e4463c409ad8bf035 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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(8);
    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 ht = hashtable.hashtable_init(8);
    defer _ = hashtable.hashtable_deinit(&ht);
    const data: i32 = 4;
    _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));
    const res: *align(1) i32 = @ptrCast(hashtable.hashtable_get(ht, @constCast("key")));
    try std.testing.expectEqual(@as(i32, 4), res.*);
}

test "removing element" {
    var ht = hashtable.hashtable_init(8);
    defer _ = hashtable.hashtable_deinit(&ht);
    const data: i32 = 4;
    _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));
    _ = hashtable.hashtable_remove(ht, @constCast("key"));
    const res: ?*anyopaque = hashtable.hashtable_get(ht, @constCast("key"));
    try std.testing.expectEqual(null, res);
}