authorgravatar for mailbasvandenberg@gmail.comBas van den Berg <mailbasvandenberg@gmail.com> 2018-07-14 18:03:06+02:00
committergravatar for mailbasvandenberg@gmail.comBas van den Berg <mailbasvandenberg@gmail.com> 2018-07-14 18:04:23+02:00
log8be6c98ca63dac5cf5e769171bb72cd888efbbf4
tree463d700d6a2c528fc45420b53b236940bf756055
parent2a719ee6c598b7096060168a0e7c93ae3e244008

Create unit test that tests aligned reallocation.


1 files changed, 35 insertions(+), 1 deletions(-)

std/heap.zig+35-1
......@@ -442,6 +442,7 @@ test "DirectAllocator" {
442442
443443 const allocator = &direct_allocator.allocator;
444444 try testAllocator(allocator);
445 try testAllocatorAligned(allocator, 16);
445446 try testAllocatorLargeAlignment(allocator);
446447}
447448
......@@ -453,6 +454,7 @@ test "ArenaAllocator" {
453454 defer arena_allocator.deinit();
454455
455456 try testAllocator(&arena_allocator.allocator);
457 try testAllocatorAligned(&arena_allocator.allocator, 16);
456458 try testAllocatorLargeAlignment(&arena_allocator.allocator);
457459}
458460
......@@ -461,6 +463,7 @@ test "FixedBufferAllocator" {
461463 var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
462464
463465 try testAllocator(&fixed_buffer_allocator.allocator);
466 try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
464467 try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
465468}
466469
......@@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" {
468471 var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
469472
470473 try testAllocator(&fixed_buffer_allocator.allocator);
474 try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
471475 try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
472476}
473477
474478fn testAllocator(allocator: *mem.Allocator) !void {
475479 var slice = try allocator.alloc(*i32, 100);
476
480 assert(slice.len == 100);
477481 for (slice) |*item, i| {
478482 item.* = try allocator.create(@intCast(i32, i));
479483 }
......@@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void {
483487 }
484488
485489 slice = try allocator.realloc(*i32, slice, 20000);
490 assert(slice.len == 20000);
486491 slice = try allocator.realloc(*i32, slice, 50);
492 assert(slice.len == 50);
487493 slice = try allocator.realloc(*i32, slice, 25);
494 assert(slice.len == 25);
495 slice = try allocator.realloc(*i32, slice, 0);
496 assert(slice.len == 0);
488497 slice = try allocator.realloc(*i32, slice, 10);
498 assert(slice.len == 10);
489499
490500 allocator.free(slice);
491501}
492502
503fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void {
504 // initial
505 var slice = try allocator.alignedAlloc(u8, alignment, 10);
506 assert(slice.len == 10);
507 // grow
508 slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
509 assert(slice.len == 100);
510 // shrink
511 slice = try allocator.alignedRealloc(u8, alignment, slice, 10);
512 assert(slice.len == 10);
513 // go to zero
514 slice = try allocator.alignedRealloc(u8, alignment, slice, 0);
515 assert(slice.len == 0);
516 // realloc from zero
517 slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
518 assert(slice.len == 100);
519 // shrink with shrink
520 slice = allocator.alignedShrink(u8, alignment, slice, 10);
521 assert(slice.len == 10);
522 // shrink to zero
523 slice = allocator.alignedShrink(u8, alignment, slice, 0);
524 assert(slice.len == 0);
525}
526
493527fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
494528 //Maybe a platform's page_size is actually the same as or
495529 // very near usize?