1
Fork 0
turbonss/src/main.zig

100 lines
3.2 KiB
Zig
Raw Normal View History

2022-02-08 13:47:52 +02:00
const std = @import("std");
const ArrayList = std.ArrayList;
const c = @cImport({
@cDefine("DEBUG", "1");
@cInclude("stdio.h");
@cInclude("cmph.h");
});
const tempMph = "temp.mph";
pub fn main() !void {
// var hash: ?*c.cmph_t = c.cmph_new(config);
// c.cmph_config_destroy(config);
// _ = c.cmph_dump(hash, mphf_fd);
// c.cmph_destroy(hash);
// _ = c.fclose(mphf_fd);
// mphf_fd = c.fopen("temp.mph", "r");
// hash = c.cmph_load(mphf_fd);
// while (i < nkeys) {
// var key: [*c]const u8 = vector[i];
// var id: c_uint = c.cmph_search(hash, key, @bitCast(c.cmph_uint32, @truncate(c_uint, 10)));
// try stdout.print("key:{s} -- hash:{u}\n", key, id);
// i +%= 1;
// }
// c.cmph_destroy(hash);
// c.cmph_io_vector_adapter_destroy(source);
// _ = c.fclose(mphf_fd);
// return 0;
//}
}
test "simple cmph usage" {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_instance.allocator();
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();
_ = stdout;
var vector = ArrayList([*:0]const u8).init(arena);
try vector.append("aaaaaaaaaa");
try vector.append("bbbbbbbbbb");
try vector.append("cccccccccc");
try vector.append("dddddddddd");
try vector.append("eeeeeeeeee");
try vector.append("ffffffffff");
try vector.append("gggggggggg");
try vector.append("hhhhhhhhhh");
try vector.append("iiiiiiiiii");
try vector.append("jjjjjjjjjj");
var mphf_fh = try std.fs.cwd().createFile(tempMph, .{});
defer {
std.fs.cwd().deleteFile(tempMph) catch |err| {
stderr.print("error removing file: {s}\n", .{@errorName(err)}) catch {};
};
}
var mphf_fd = c.fdopen(mphf_fh.handle, "w");
var vector_len = @truncate(c_uint, vector.items.len);
var cvector = @ptrCast([*c][*c]u8, vector.toOwnedSlice().ptr);
var source = c.cmph_io_vector_adapter(cvector, vector_len);
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse return error.OutOfMemory;
c.cmph_config_set_algo(config, c.CMPH_BRZ);
c.cmph_config_set_mphf_fd(config, mphf_fd);
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
c.cmph_config_destroy(config);
_ = c.cmph_dump(hash, mphf_fd);
c.cmph_destroy(hash);
mphf_fh.close();
mphf_fh = try std.fs.cwd().openFile(tempMph, .{ .mode = .read_only });
mphf_fd = c.fdopen(mphf_fh.handle, "r");
defer mphf_fh.close();
var got_hash: ?*c.cmph_t = c.cmph_load(mphf_fd);
if (got_hash) |real_hash| {
hash = real_hash;
} else {
try stderr.print("failed to unwrap hash\n", .{});
}
}
const expectEqual = std.testing.expectEqual;
test "char**" {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_instance.allocator();
var arrl = ArrayList([*:0]const u8).init(arena);
try arrl.append("foo");
try arrl.append("bar");
var arr: [*][*:0]const u8 = arrl.toOwnedSlice().ptr;
try expectEqual(@TypeOf(arr[0]), [*:0]const u8);
try expectEqual(arr[0][2], 'o');
try expectEqual(arr[0][3], 0);
}