Print the tree once.

mess
Karchnu 2020-12-22 18:18:34 +01:00
parent 5adcd203f9
commit 6d354fd7c2
1 changed files with 56 additions and 37 deletions

View File

@ -13,36 +13,6 @@ const NodeList = ArrayList(Node);
const Definitions = AutoHashMap([] const u8, Node);
// Private functions.
fn say(tosay: []const u8) void {
std.debug.print("{}", .{tosay});
}
fn print_node(node: Node) void {
std.debug.print("Node type {} (id: {})\n", .{node.type_name, node.id});
var it = node.properties.iterator();
while(it.next()) |kv| {
std.debug.print("\t{} => {}\n", .{kv.key, node.properties.get(kv.key)});
}
}
fn print_tree(tree: Tree) void {
say("tree.definitions:\n");
var it = tree.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => ", .{kv.key});
const node = tree.definitions.get(kv.key);
if(node) |n| { print_node(n); }
}
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v);
}
}
pub const Node = struct {
id: ?[] const u8,
type_name: [] const u8,
@ -109,16 +79,67 @@ pub const Tree = struct {
};
// TESTS.
// Private functions.
fn say(tosay: []const u8) void {
std.debug.print("{}", .{tosay});
}
fn print_node(node: Node) void {
std.debug.print("Node type {} (id: {})\n", .{node.type_name, node.id});
var it = node.properties.iterator();
while(it.next()) |kv| {
std.debug.print("\t{} => {}\n", .{kv.key, node.properties.get(kv.key)});
}
}
fn print_tree(tree: Tree) void {
say("tree.definitions:\n");
var it = tree.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => ", .{kv.key});
const node = tree.definitions.get(kv.key);
if(node) |n| { print_node(n); }
}
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v);
}
}
test "simple test about structures" {
const allocator = std.heap.page_allocator;
var gpa = std.heap.GeneralPurposeAllocator(.{.safety = true}){};
const allocator = &gpa.allocator;
// const allocator = std.heap.page_allocator;
var value = PropertyValue { .integer = 10 };
var properties = PropertyHashMap.init(allocator);
defer properties.deinit();
// We want to know if there are memory leaks.
defer {
properties.deinit();
// tree.deinit() catch |err| switch(err) {
// else => {say("an error occured\n");}
// } ;
say("\ntesting for memory leaks\n");
const leaks = gpa.deinit();
if (leaks) {
say("there were leaks, oh no\n");
}
else {
say("there were no leaks, youpi!\n");
}
say("\n");
}
try properties.put("hello", value);
std.debug.print("\n", .{});
say("\n");
say("We should see a single value here:\n");
var it = properties.iterator();
while(it.next()) |kv| {
std.debug.print("key: {} => value: {}\n", .{kv.key, properties.get(kv.key)});
@ -141,13 +162,10 @@ fn init_stuff(allocator: *Allocator) !Tree {
try tree.children.append(try Node.create(allocator, "OtherObject", null));
try tree.children.append(try Node.create(allocator, "Text", "my-id-for-text-object"));
print_tree(tree);
return tree;
}
test "init a Tree structure" {
// const allocator = std.heap.page_allocator;
// Allocator with safety on: checking for memory leaks.
var gpa = std.heap.GeneralPurposeAllocator(.{.safety = true}){};
const allocator = &gpa.allocator;
@ -164,7 +182,7 @@ test "init a Tree structure" {
// else => {say("an error occured\n");}
// } ;
say("\n\ntesting for memory leaks\n");
say("\ntesting for memory leaks\n");
const leaks = gpa.deinit();
if (leaks) {
say("there were leaks, oh no\n");
@ -172,6 +190,7 @@ test "init a Tree structure" {
else {
say("there were no leaks, youpi!\n");
}
say("\n");
}
print_tree(tree);