Common structures: grooming.

mess
Karchnu 2020-12-22 20:53:58 +01:00
parent 6d354fd7c2
commit 80d46f16e7
1 changed files with 50 additions and 63 deletions

View File

@ -1,17 +1,17 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const hashString = std.hash_map.hashString;
const eqlString = std.hash_map.eqlString;
const HashMap = std.HashMap;
const AutoHashMap = std.AutoHashMap;
const ArrayList = std.ArrayList;
/// This file introduces the main structures used by guid once running.
/// Tests show how to use them.
/// Type.create do no allocate memory for the structure, only to its attributes.
/// Type.deinit works in cascade (deinit its children).
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(Node);
const Definitions = AutoHashMap([] const u8, Node);
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(Node);
const DefinitionHashMap = AutoHashMap([] const u8, Node);
pub const Node = struct {
id: ?[] const u8,
@ -56,7 +56,7 @@ pub const PropertyValue = union(PropertyValueTags) {
};
pub const Tree = struct {
definitions: Definitions,
definitions: DefinitionHashMap,
children: NodeList,
gpa: *Allocator,
@ -71,7 +71,7 @@ pub const Tree = struct {
pub fn create(allocator: *Allocator) !Tree {
return Tree{
.definitions = Definitions.init(allocator),
.definitions = DefinitionHashMap.init(allocator),
.children = NodeList.init(allocator),
.gpa = allocator,
};
@ -79,23 +79,26 @@ pub const Tree = struct {
};
// TESTS.
// TESTS and private util functions.
// 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();
fn print_properties(properties: PropertyHashMap) void {
var it = properties.iterator();
while(it.next()) |kv| {
std.debug.print("\t{} => {}\n", .{kv.key, node.properties.get(kv.key)});
std.debug.print("\t{} => {}\n", .{kv.key, properties.get(kv.key)});
}
}
fn print_node(node: Node) void {
std.debug.print("Node type {} (id: {})\n", .{node.type_name, node.id});
print_properties(node.properties);
}
fn print_tree(tree: Tree) void {
say("tree.definitions:\n");
say("\ntree.definitions:\n");
var it = tree.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => ", .{kv.key});
@ -113,43 +116,33 @@ fn print_tree(tree: Tree) void {
test "simple test about structures" {
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);
// 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);
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)});
// Displaying the content.
// say("\n");
// print_properties(properties);
// Freeing the properties.
properties.deinit();
// Testing memory leaks at the end of the test.
const leaks = gpa.deinit();
if (leaks) {
say("\nthere were leaks, oh no\n");
}
else {
say("\nno leaks, yay!\n");
}
std.testing.expect(! leaks);
}
fn init_stuff(allocator: *Allocator) !Tree {
var tree = try Tree.create(allocator);
say("creating a definition and a few children\n");
// Creating a definition and a few children.
try tree.definitions.put("MyObject", try Node.create(allocator, "my-type-name", "my-id"));
var new_node = try Node.create(allocator, "Object", "some-id-for-this-object");
var value = PropertyValue { .integer = 10 };
@ -170,29 +163,23 @@ test "init a Tree structure" {
var gpa = std.heap.GeneralPurposeAllocator(.{.safety = true}){};
const allocator = &gpa.allocator;
say("\n");
say("creating a tree\n");
// Creating a tree.
var tree = try init_stuff(allocator);
// We want to know if there are memory leaks.
defer {
tree.deinit();
// tree.deinit() catch |err| switch(err) {
// else => {say("an error occured\n");}
// } ;
// Display the content of the tree.
// print_tree(tree);
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");
// Freeing the tree.
tree.deinit();
// Testing memory leaks at the end of the test.
const leaks = gpa.deinit();
if (leaks) {
say("\nthere were leaks, oh no\n");
}
print_tree(tree);
else {
say("\nno leaks, yay!\n");
}
std.testing.expect(! leaks);
}