kble/src/level.zig

45 lines
1.1 KiB
Zig

const std = @import("std");
const expect = std.testing.expect;
pub const Level = struct {
width: u16,
height: u16,
buffer: []u16,
pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Level {
var level = Level{
.width = width,
.height = height,
.buffer = undefined,
};
level.buffer = try allocator.alloc(u16, width * height);
return level;
}
pub fn deinit(self: *Level, allocator: *std.mem.Allocator) void {
allocator.free(self.buffer);
}
};
test "create level buffer" {
// Create allocator.
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const leaked = gpa.deinit();
if (leaked) expect(false); //fail test
}
const allocator = &gpa.allocator;
// Initialize level struct (twice 'cause why not?).
var level: Level = try Level.init(allocator, 64, 32);
level.deinit(allocator);
level = try Level.init(allocator, 64, 64);
defer level.deinit(allocator);
level.buffer[128] = 32;
expect(level.buffer[128] == 32);
expect(level.width == 64);
expect(level.height == 64);
}