Function testAllocator [src]

This one should not try alignments that exceed what C malloc can handle.

Prototype

pub fn testAllocator(base_allocator: mem.Allocator) !void

Parameters

base_allocator: mem.Allocator

Source

pub fn testAllocator(base_allocator: mem.Allocator) !void { var validationAllocator = mem.validationWrap(base_allocator); const allocator = validationAllocator.allocator(); var slice = try allocator.alloc(*i32, 100); try testing.expect(slice.len == 100); for (slice, 0..) |*item, i| { item.* = try allocator.create(i32); item.*.* = @as(i32, @intCast(i)); } slice = try allocator.realloc(slice, 20000); try testing.expect(slice.len == 20000); for (slice[0..100], 0..) |item, i| { try testing.expect(item.* == @as(i32, @intCast(i))); allocator.destroy(item); } if (allocator.resize(slice, 50)) { slice = slice[0..50]; if (allocator.resize(slice, 25)) { slice = slice[0..25]; try testing.expect(allocator.resize(slice, 0)); slice = slice[0..0]; slice = try allocator.realloc(slice, 10); try testing.expect(slice.len == 10); } } allocator.free(slice); // Zero-length allocation const empty = try allocator.alloc(u8, 0); allocator.free(empty); // Allocation with zero-sized types const zero_bit_ptr = try allocator.create(u0); zero_bit_ptr.* = 0; allocator.destroy(zero_bit_ptr); const oversize = try allocator.alignedAlloc(u32, null, 5); try testing.expect(oversize.len >= 5); for (oversize) |*item| { item.* = 0xDEADBEEF; } allocator.free(oversize); }