Convert a bunch of page_allocator to testing.allocator

This commit is contained in:
Benjamin Feng 2020-01-29 22:06:26 -06:00
parent ad93ad3e60
commit b7a236d68e
10 changed files with 23 additions and 19 deletions

View File

@ -83,6 +83,7 @@ pub const BufMap = struct {
};
test "BufMap" {
// TODO: uncomment and fix the leak
var bufmap = BufMap.init(std.heap.page_allocator);
defer bufmap.deinit();

View File

@ -65,7 +65,7 @@ pub const BufSet = struct {
};
test "BufSet" {
var bufset = BufSet.init(std.heap.page_allocator);
var bufset = BufSet.init(std.testing.allocator);
defer bufset.deinit();
try bufset.put("x");

View File

@ -1059,7 +1059,10 @@ pub const Builder = struct {
};
test "builder.findProgram compiles" {
// TODO: uncomment and fix the leak
// const builder = try Builder.create(std.testing.allocator, "zig", "zig-cache", "zig-cache");
const builder = try Builder.create(std.heap.page_allocator, "zig", "zig-cache", "zig-cache");
defer builder.destroy();
_ = builder.findProgram(&[_][]const u8{}, &[_][]const u8{}) catch null;
}

View File

@ -234,8 +234,8 @@ test "hash pointer" {
test "hash slice shallow" {
// Allocate one array dynamically so that we're assured it is not merged
// with the other by the optimization passes.
const array1 = try std.heap.page_allocator.create([6]u32);
defer std.heap.page_allocator.destroy(array1);
const array1 = try std.testing.allocator.create([6]u32);
defer std.testing.allocator.destroy(array1);
array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
const a = array1[0..];
@ -250,8 +250,8 @@ test "hash slice shallow" {
test "hash slice deep" {
// Allocate one array dynamically so that we're assured it is not merged
// with the other by the optimization passes.
const array1 = try std.heap.page_allocator.create([6]u32);
defer std.heap.page_allocator.destroy(array1);
const array1 = try std.testing.allocator.create([6]u32);
defer std.testing.allocator.destroy(array1);
array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
const a = array1[0..];
@ -278,7 +278,7 @@ test "hash struct deep" {
}
};
const allocator = std.heap.page_allocator;
const allocator = std.testing.allocator;
const foo = try Foo.init(allocator, 123, 1.0, true);
const bar = try Foo.init(allocator, 123, 1.0, true);
const baz = try Foo.init(allocator, 123, 1.0, false);

View File

@ -419,7 +419,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
}
test "basic hash map usage" {
var map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
var map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();
testing.expect((try map.put(1, 11)) == null);
@ -463,7 +463,7 @@ test "basic hash map usage" {
}
test "iterator hash map" {
var reset_map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer reset_map.deinit();
try reset_map.putNoClobber(1, 11);
@ -509,7 +509,7 @@ test "iterator hash map" {
}
test "ensure capacity" {
var map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
var map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();
try map.ensureCapacity(20);

View File

@ -235,8 +235,8 @@ test "pipe" {
}
test "argsAlloc" {
var args = try std.process.argsAlloc(std.heap.page_allocator);
std.process.argsFree(std.heap.page_allocator, args);
var args = try std.process.argsAlloc(std.testing.allocator);
std.process.argsFree(std.testing.allocator, args);
}
test "memfd_create" {

View File

@ -604,7 +604,7 @@ test "PackedIntArray at end of available memory" {
p: PackedArray,
};
const allocator = std.heap.page_allocator;
const allocator = std.testing.allocator;
var pad = try allocator.create(Padded);
defer allocator.destroy(pad);
@ -618,7 +618,7 @@ test "PackedIntSlice at end of available memory" {
}
const PackedSlice = PackedIntSlice(u11);
const allocator = std.heap.page_allocator;
const allocator = std.testing.allocator;
var page = try allocator.alloc(u8, std.mem.page_size);
defer allocator.free(page);

View File

@ -339,7 +339,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
test "std.SegmentedList" {
var a = std.heap.page_allocator;
var a = std.testing.allocator;
try testSegmentedList(0, a);
try testSegmentedList(1, a);

View File

@ -866,7 +866,7 @@ fn expectTokens(tl: *TokenList, src: [*:0]const u8, expected: []CToken) void {
}
test "tokenize macro" {
var tl = TokenList.init(std.heap.page_allocator);
var tl = TokenList.init(std.testing.allocator);
defer tl.deinit();
expectTokens(&tl, "TEST(0\n", &[_]CToken{
@ -904,7 +904,7 @@ test "tokenize macro" {
}
test "tokenize macro ops" {
var tl = TokenList.init(std.heap.page_allocator);
var tl = TokenList.init(std.testing.allocator);
defer tl.deinit();
expectTokens(&tl, "ADD A + B", &[_]CToken{

View File

@ -410,8 +410,8 @@ test "heap allocated async function frame" {
var x: i32 = 42;
fn doTheTest() !void {
const frame = try std.heap.page_allocator.create(@Frame(someFunc));
defer std.heap.page_allocator.destroy(frame);
const frame = try std.testing.allocator.create(@Frame(someFunc));
defer std.testing.allocator.destroy(frame);
expect(x == 42);
frame.* = async someFunc();
@ -671,7 +671,7 @@ fn testAsyncAwaitTypicalUsage(
}
fn amain() !void {
const allocator = std.heap.page_allocator; // TODO once we have the debug allocator, use that, so that this can detect leaks
const allocator = std.testing.allocator;
var download_frame = async fetchUrl(allocator, "https://example.com/");
var download_awaited = false;
errdefer if (!download_awaited) {