aboutsummaryrefslogtreecommitdiff
path: root/std/heap.zig
diff options
context:
space:
mode:
authorBas van den Berg <mailbasvandenberg@gmail.com>2018-07-14 18:03:06 +0200
committerBas van den Berg <mailbasvandenberg@gmail.com>2018-07-14 18:04:23 +0200
commit8be6c98ca63dac5cf5e769171bb72cd888efbbf4 (patch)
tree463d700d6a2c528fc45420b53b236940bf756055 /std/heap.zig
parent2a719ee6c598b7096060168a0e7c93ae3e244008 (diff)
downloadzig-8be6c98ca63dac5cf5e769171bb72cd888efbbf4.tar.gz
zig-8be6c98ca63dac5cf5e769171bb72cd888efbbf4.zip
Create unit test that tests aligned reallocation.
Diffstat (limited to 'std/heap.zig')
-rw-r--r--std/heap.zig36
1 files changed, 35 insertions, 1 deletions
diff --git a/std/heap.zig b/std/heap.zig
index ef22c8d0c5..4f21ef5cd1 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -442,6 +442,7 @@ test "DirectAllocator" {
const allocator = &direct_allocator.allocator;
try testAllocator(allocator);
+ try testAllocatorAligned(allocator, 16);
try testAllocatorLargeAlignment(allocator);
}
@@ -453,6 +454,7 @@ test "ArenaAllocator" {
defer arena_allocator.deinit();
try testAllocator(&arena_allocator.allocator);
+ try testAllocatorAligned(&arena_allocator.allocator, 16);
try testAllocatorLargeAlignment(&arena_allocator.allocator);
}
@@ -461,6 +463,7 @@ test "FixedBufferAllocator" {
var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
try testAllocator(&fixed_buffer_allocator.allocator);
+ try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
}
@@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" {
var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
try testAllocator(&fixed_buffer_allocator.allocator);
+ try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
}
fn testAllocator(allocator: *mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);
-
+ assert(slice.len == 100);
for (slice) |*item, i| {
item.* = try allocator.create(@intCast(i32, i));
}
@@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void {
}
slice = try allocator.realloc(*i32, slice, 20000);
+ assert(slice.len == 20000);
slice = try allocator.realloc(*i32, slice, 50);
+ assert(slice.len == 50);
slice = try allocator.realloc(*i32, slice, 25);
+ assert(slice.len == 25);
+ slice = try allocator.realloc(*i32, slice, 0);
+ assert(slice.len == 0);
slice = try allocator.realloc(*i32, slice, 10);
+ assert(slice.len == 10);
allocator.free(slice);
}
+fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void {
+ // initial
+ var slice = try allocator.alignedAlloc(u8, alignment, 10);
+ assert(slice.len == 10);
+ // grow
+ slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
+ assert(slice.len == 100);
+ // shrink
+ slice = try allocator.alignedRealloc(u8, alignment, slice, 10);
+ assert(slice.len == 10);
+ // go to zero
+ slice = try allocator.alignedRealloc(u8, alignment, slice, 0);
+ assert(slice.len == 0);
+ // realloc from zero
+ slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
+ assert(slice.len == 100);
+ // shrink with shrink
+ slice = allocator.alignedShrink(u8, alignment, slice, 10);
+ assert(slice.len == 10);
+ // shrink to zero
+ slice = allocator.alignedShrink(u8, alignment, slice, 0);
+ assert(slice.len == 0);
+}
+
fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
//Maybe a platform's page_size is actually the same as or
// very near usize?