Test: create structures.

mess
Karchnu 2020-12-21 03:53:41 +01:00
parent 78080c43a0
commit eac5b58017
1 changed files with 29 additions and 3 deletions

View File

@ -7,6 +7,10 @@ const HashMap = std.HashMap;
const AutoHashMap = std.AutoHashMap;
const ArrayList = std.ArrayList;
fn say(tosay: []const u8) void {
std.debug.print("{}", .{tosay});
}
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(Node);
@ -19,6 +23,8 @@ pub const Node = struct {
properties: PropertyHashMap,
children: NodeList,
gpa: *Allocator,
pub fn deinit(self: *Node) void {
self.properties.deinit();
for (self.children.items) |k, v| {
@ -27,6 +33,17 @@ pub const Node = struct {
self.children.deinit();
}
pub fn create(allocator: *Allocator, type_name: []const u8, id: ?[]const u8) !Node {
return Node{
.id = id,
.type_name = type_name,
.properties = PropertyHashMap.init(allocator),
.children = NodeList.init(allocator),
.gpa = allocator,
};
}
};
const PropertyValueTags = enum {
@ -86,18 +103,27 @@ test "simple test about structures" {
test "init a Root structure" {
const allocator = std.heap.page_allocator;
say("\n");
say("creating a root\n");
var root = try Root.create(allocator);
defer root.deinit();
std.debug.print("\n", .{});
say("creating a definition and a few children\n");
try root.definitions.put("MyObject", try Node.create(allocator, "my-type-name", "my-id"));
try root.children.append(try Node.create(allocator, "object", "my-id"));
try root.children.append(try Node.create(allocator, "otherObject", null));
try root.children.append(try Node.create(allocator, "Text", "my-id-for-text-object"));
std.debug.print("root.definitions:\n", .{});
say("root.definitions:\n");
var it = root.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => {}:\n", .{kv.key, root.definitions.get(kv.key)});
}
std.debug.print("root.children:\n", .{});
say("root.children:\n");
for(root.children.items) |k, v| {
std.debug.print("{} => {}:\n", .{k, v});
}