aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-09-27 22:04:38 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-09-27 22:04:38 -0400
commit9ae66b4c67a66c3dcf8ce6107eca7a4744edd66d (patch)
treefca648aa8f0cc8f2acece3bfde59133af151d8f5 /std
parent583ca36e62fc13b38348f9c42bbfb9ddd9fceaa0 (diff)
downloadzig-9ae66b4c67a66c3dcf8ce6107eca7a4744edd66d.tar.gz
zig-9ae66b4c67a66c3dcf8ce6107eca7a4744edd66d.zip
add test for std.mem.IncrementingAllocator
See #501
Diffstat (limited to 'std')
-rw-r--r--std/mem.zig24
1 files changed, 24 insertions, 0 deletions
diff --git a/std/mem.zig b/std/mem.zig
index c7ebeb0157..59ebfadebb 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -98,6 +98,10 @@ pub const IncrementingAllocator = struct {
self.end_index = 0;
}
+ fn bytesLeft(self: &const IncrementingAllocator) -> usize {
+ return self.bytes.len - self.end_index;
+ }
+
fn alloc(allocator: &Allocator, n: usize, alignment: usize) -> %[]u8 {
const self = @fieldParentPtr(IncrementingAllocator, "allocator", allocator);
const addr = @ptrToInt(&self.bytes[self.end_index]);
@@ -124,6 +128,26 @@ pub const IncrementingAllocator = struct {
}
};
+test "mem.IncrementingAllocator" {
+ const total_bytes = 100 * 1024 * 1024;
+ var inc_allocator = %%IncrementingAllocator.init(total_bytes);
+ defer inc_allocator.deinit();
+
+ const allocator = &inc_allocator.allocator;
+ const slice = %%allocator.alloc(&i32, 100);
+
+ for (slice) |*item, i| {
+ *item = %%allocator.create(i32);
+ **item = i32(i);
+ }
+
+ assert(inc_allocator.bytesLeft() == total_bytes - @sizeOf(i32) * 100 - @sizeOf(usize) * 100);
+
+ inc_allocator.reset();
+
+ assert(inc_allocator.bytesLeft() == total_bytes);
+}
+
/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
pub fn copy(comptime T: type, dest: []T, source: []const T) {