corpus test passing

This commit is contained in:
Motiejus Jakštys 2022-03-01 05:47:44 +02:00 committed by Motiejus Jakštys
parent 3eec9a3274
commit 7df1296ce1

View File

@ -14,7 +14,7 @@ const User = userImport.User;
const Group = groupImport.Group; const Group = groupImport.Group;
const Corpus = struct { const Corpus = struct {
allocator: Allocator, arena: std.heap.ArenaAllocator,
// sorted by name, by unicode codepoint // sorted by name, by unicode codepoint
users: []User, users: []User,
@ -35,10 +35,13 @@ const Corpus = struct {
error{NotFound}; error{NotFound};
pub fn init( pub fn init(
allocator: Allocator, baseAllocator: Allocator,
usersConst: []const User, usersConst: []const User,
groupsConst: []const Group, groupsConst: []const Group,
) initErr!Corpus { ) initErr!Corpus {
var arena = std.heap.ArenaAllocator.init(baseAllocator);
var allocator = arena.allocator();
var users = try allocator.alloc(User, usersConst.len); var users = try allocator.alloc(User, usersConst.len);
var groups = try allocator.alloc(Group, groupsConst.len); var groups = try allocator.alloc(Group, groupsConst.len);
std.mem.copy(User, users, usersConst); std.mem.copy(User, users, usersConst);
@ -92,15 +95,15 @@ const Corpus = struct {
return error.NotFound; return error.NotFound;
} }
var groupUsers: ArrayList(*const Group) = undefined; var groupUsers: *ArrayList(*const Group) = undefined;
var result = try username2groups.getOrPut(memberName.*); var result = try username2groups.getOrPut(memberName.*);
if (result.found_existing) { if (!result.found_existing) {
groupUsers = ArrayList(*const Group).init(allocator); groupUsers = &ArrayList(*const Group).init(allocator);
result.value_ptr.* = groupUsers; result.value_ptr = groupUsers;
} else { } else {
groupUsers = result.value_ptr.*; groupUsers = result.value_ptr;
} }
try groupUsers.append(&group); try groupUsers.*.append(&group);
} }
var result = try groupname2users.getOrPut(group.name); var result = try groupname2users.getOrPut(group.name);
@ -111,7 +114,7 @@ const Corpus = struct {
} }
return Corpus{ return Corpus{
.allocator = allocator, .arena = arena,
.users = users, .users = users,
.groups = groups, .groups = groups,
.name2user = name2user, .name2user = name2user,
@ -122,6 +125,11 @@ const Corpus = struct {
.username2groups = username2groups, .username2groups = username2groups,
}; };
} }
pub fn deinit(self: *Corpus) void {
self.arena.deinit();
self.* = undefined;
}
}; };
// cmpUser compares two users for sorting. By username's utf8 codepoints, ascending. // cmpUser compares two users for sorting. By username's utf8 codepoints, ascending.
@ -182,24 +190,34 @@ test "test corpus" {
.shell = "/", .shell = "/",
} }; } };
var members1 = try someMembers(allocator, &[_][]const u8{"vidmantas"});
defer members1.deinit();
var members2 = try someMembers(allocator, &[_][]const u8{ "svc-bar", "Name" ** 8 });
defer members2.deinit();
var members3 = try someMembers(allocator, &[_][]const u8{});
defer members3.deinit();
var groups = [_]Group{ Group{ var groups = [_]Group{ Group{
.gid = 1000, .gid = 1000,
.name = "vidmantas", .name = "vidmantas",
.members = try someMembers(allocator, &[_][]const u8{"vidmantas"}), .members = members1,
}, Group{ }, Group{
.gid = 1234, .gid = 1234,
.name = "service-account", .name = "service-account",
.members = try someMembers(allocator, &[_][]const u8{ "svc-bar", "Name" ** 8 }), .members = members2,
}, Group{ }, Group{
.gid = 9999, .gid = 9999,
.name = "empty", .name = "empty",
.members = try someMembers(allocator, &[_][]const u8{}), .members = members3,
} }; } };
const corpus = try Corpus.init(allocator, users[0..], groups[0..]); var corpus = try Corpus.init(allocator, users[0..], groups[0..]);
try testing.expectEqualStrings(corpus.users[0].name, "svc-bar"); defer corpus.deinit();
try testing.expectEqualStrings(corpus.users[1].name, "vidmantas"); try testing.expectEqualStrings(corpus.users[0].name, "Name" ** 8);
try testing.expectEqualStrings(corpus.users[2].name, "Name" ** 8); try testing.expectEqualStrings(corpus.users[1].name, "svc-bar");
try testing.expectEqualStrings(corpus.users[2].name, "vidmantas");
} }
fn testUser(name: []const u8) User { fn testUser(name: []const u8) User {