Skip to content

Commit 57e949c

Browse files
refactor: replace independent testing allocators with arenas
1 parent 5e191a4 commit 57e949c

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

src/util.zig

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,21 @@ fn initTests() !void {
399399
}
400400

401401
test "build paths" {
402-
var allocator: STD.mem.Allocator = STD.testing.allocator;
402+
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
403+
defer mem_arena.deinit();
403404

404-
const PATH_BUILDER_INSTANCE = try allocator.create(PATH_BUILDER);
405+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
406+
407+
const PATH_BUILDER_INSTANCE = try ALLOCATOR.create(PATH_BUILDER);
405408
PATH_BUILDER_INSTANCE.init();
406-
defer PATH_BUILDER_INSTANCE.denit(allocator);
409+
defer PATH_BUILDER_INSTANCE.denit(ALLOCATOR);
407410

408-
try PATH_BUILDER_INSTANCE.add(allocator, ".");
409-
try PATH_BUILDER_INSTANCE.addSlice(allocator, &[_][]const u8{ "hello", "world" });
411+
try PATH_BUILDER_INSTANCE.add(ALLOCATOR, ".");
412+
try PATH_BUILDER_INSTANCE.addSlice(ALLOCATOR, &[_][]const u8{ "hello", "world" });
410413

411414
var path: STD.ArrayList(u8) = .empty;
412-
defer path.deinit(allocator);
413-
try PATH_BUILDER_INSTANCE.build(allocator, &path);
415+
defer path.deinit(ALLOCATOR);
416+
try PATH_BUILDER_INSTANCE.build(ALLOCATOR, &path);
414417

415418
const EXPECTED_OUTPUT: []const u8 = switch (NATIVE_OS) {
416419
.windows => ".\\hello\\world",
@@ -423,17 +426,22 @@ test "build paths" {
423426
test "iterate test directory and filter files" {
424427
try initTests();
425428

429+
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
430+
defer mem_arena.deinit();
431+
432+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
433+
426434
var list_of_contents: STD.ArrayList([]const u8) = .empty;
427435
defer {
428436
for (list_of_contents.items) |path| {
429-
STD.testing.allocator.free(path);
437+
ALLOCATOR.free(path);
430438
}
431439

432-
list_of_contents.deinit(STD.testing.allocator);
440+
list_of_contents.deinit(ALLOCATOR);
433441
}
434442

435443
try iterateDirectory(
436-
STD.testing.allocator,
444+
ALLOCATOR,
437445
TEST_SUB_DIR,
438446
.{
439447
.allow_move_contents = true,
@@ -448,22 +456,25 @@ test "iterate test directory and filter files" {
448456
}
449457

450458
test "build and search directory tree" {
451-
var allocator: STD.mem.Allocator = STD.testing.allocator;
459+
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
460+
defer mem_arena.deinit();
461+
462+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
452463

453-
const dir_tree: *DIR_TREE = try allocator.create(DIR_TREE);
454-
try dir_tree.init(allocator, TEST_DIR);
455-
defer dir_tree.deinit(allocator);
464+
const dir_tree: *DIR_TREE = try ALLOCATOR.create(DIR_TREE);
465+
try dir_tree.init(ALLOCATOR, TEST_DIR);
466+
defer dir_tree.deinit(ALLOCATOR);
456467

457468
var filtered_dir_contents: STD.ArrayList([]const u8) = .empty;
458469
defer {
459470
for (filtered_dir_contents.items) |content| {
460-
allocator.free(content);
471+
ALLOCATOR.free(content);
461472
}
462473

463-
filtered_dir_contents.deinit(allocator);
474+
filtered_dir_contents.deinit(ALLOCATOR);
464475
}
465476

466-
try dir_tree.buildandSearchFSTree(allocator, dir_tree.root_node, .{
477+
try dir_tree.buildandSearchFSTree(ALLOCATOR, dir_tree.root_node, .{
467478
.is_debug_mode = false,
468479
.include_filter = ".txt",
469480
.allow_move_contents = true,
@@ -476,11 +487,16 @@ test "build and search directory tree" {
476487
}
477488

478489
test "run sub process with default arguments" {
490+
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
491+
defer mem_arena.deinit();
492+
493+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
494+
479495
var process_output: STD.ArrayList(u8) = .empty;
480-
defer process_output.deinit(STD.testing.allocator);
496+
defer process_output.deinit(ALLOCATOR);
481497

482498
try runSubProcess(
483-
STD.testing.allocator,
499+
ALLOCATOR,
484500
&process_output,
485501
.{},
486502
);
@@ -491,11 +507,16 @@ test "run sub process with default arguments" {
491507
}
492508

493509
test "run hello world sub process" {
510+
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
511+
defer mem_arena.deinit();
512+
513+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
514+
494515
var process_output: STD.ArrayList(u8) = .empty;
495-
defer process_output.deinit(STD.testing.allocator);
516+
defer process_output.deinit(ALLOCATOR);
496517

497518
try runSubProcess(
498-
STD.testing.allocator,
519+
ALLOCATOR,
499520
&process_output,
500521
.{ .args = "echo hello world" },
501522
);
@@ -504,4 +525,3 @@ test "run hello world sub process" {
504525
STD.mem.eql(u8, process_output.items, "hello world\n"),
505526
);
506527
}
507-

0 commit comments

Comments
 (0)