corpus test passing
This commit is contained in:
parent
3eec9a3274
commit
7df1296ce1
@ -14,7 +14,7 @@ const User = userImport.User;
|
||||
const Group = groupImport.Group;
|
||||
|
||||
const Corpus = struct {
|
||||
allocator: Allocator,
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
// sorted by name, by unicode codepoint
|
||||
users: []User,
|
||||
@ -35,10 +35,13 @@ const Corpus = struct {
|
||||
error{NotFound};
|
||||
|
||||
pub fn init(
|
||||
allocator: Allocator,
|
||||
baseAllocator: Allocator,
|
||||
usersConst: []const User,
|
||||
groupsConst: []const Group,
|
||||
) initErr!Corpus {
|
||||
var arena = std.heap.ArenaAllocator.init(baseAllocator);
|
||||
var allocator = arena.allocator();
|
||||
|
||||
var users = try allocator.alloc(User, usersConst.len);
|
||||
var groups = try allocator.alloc(Group, groupsConst.len);
|
||||
std.mem.copy(User, users, usersConst);
|
||||
@ -92,15 +95,15 @@ const Corpus = struct {
|
||||
return error.NotFound;
|
||||
}
|
||||
|
||||
var groupUsers: ArrayList(*const Group) = undefined;
|
||||
var groupUsers: *ArrayList(*const Group) = undefined;
|
||||
var result = try username2groups.getOrPut(memberName.*);
|
||||
if (result.found_existing) {
|
||||
groupUsers = ArrayList(*const Group).init(allocator);
|
||||
result.value_ptr.* = groupUsers;
|
||||
if (!result.found_existing) {
|
||||
groupUsers = &ArrayList(*const Group).init(allocator);
|
||||
result.value_ptr = groupUsers;
|
||||
} else {
|
||||
groupUsers = result.value_ptr.*;
|
||||
groupUsers = result.value_ptr;
|
||||
}
|
||||
try groupUsers.append(&group);
|
||||
try groupUsers.*.append(&group);
|
||||
}
|
||||
|
||||
var result = try groupname2users.getOrPut(group.name);
|
||||
@ -111,7 +114,7 @@ const Corpus = struct {
|
||||
}
|
||||
|
||||
return Corpus{
|
||||
.allocator = allocator,
|
||||
.arena = arena,
|
||||
.users = users,
|
||||
.groups = groups,
|
||||
.name2user = name2user,
|
||||
@ -122,6 +125,11 @@ const Corpus = struct {
|
||||
.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.
|
||||
@ -182,24 +190,34 @@ test "test corpus" {
|
||||
.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{
|
||||
.gid = 1000,
|
||||
.name = "vidmantas",
|
||||
.members = try someMembers(allocator, &[_][]const u8{"vidmantas"}),
|
||||
.members = members1,
|
||||
}, Group{
|
||||
.gid = 1234,
|
||||
.name = "service-account",
|
||||
.members = try someMembers(allocator, &[_][]const u8{ "svc-bar", "Name" ** 8 }),
|
||||
.members = members2,
|
||||
}, Group{
|
||||
.gid = 9999,
|
||||
.name = "empty",
|
||||
.members = try someMembers(allocator, &[_][]const u8{}),
|
||||
.members = members3,
|
||||
} };
|
||||
|
||||
const corpus = try Corpus.init(allocator, users[0..], groups[0..]);
|
||||
try testing.expectEqualStrings(corpus.users[0].name, "svc-bar");
|
||||
try testing.expectEqualStrings(corpus.users[1].name, "vidmantas");
|
||||
try testing.expectEqualStrings(corpus.users[2].name, "Name" ** 8);
|
||||
var corpus = try Corpus.init(allocator, users[0..], groups[0..]);
|
||||
defer corpus.deinit();
|
||||
try testing.expectEqualStrings(corpus.users[0].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 {
|
||||
|
Loading…
Reference in New Issue
Block a user