aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulien Dessaux2022-02-20 23:06:51 +0100
committerJulien Dessaux2022-02-20 23:06:51 +0100
commit8bd793daf0edc42b4649e79de6318b0dfb5198da (patch)
tree18bbf258381203ffbc7a151f782d47e5dfc4c90d /src
parentAdded code coverage to tests with zig build test -Dtest-coverage (diff)
downloadzigfunge98-8bd793daf0edc42b4649e79de6318b0dfb5198da.tar.gz
zigfunge98-8bd793daf0edc42b4649e79de6318b0dfb5198da.tar.bz2
zigfunge98-8bd793daf0edc42b4649e79de6318b0dfb5198da.zip
Rewrote stack tests
Diffstat (limited to 'src')
-rw-r--r--src/stack.zig83
1 files changed, 37 insertions, 46 deletions
diff --git a/src/stack.zig b/src/stack.zig
index 5545c76..6ba587b 100644
--- a/src/stack.zig
+++ b/src/stack.zig
@@ -14,6 +14,17 @@ pub const Stack = struct {
try self.push(self.data.items[self.data.items.len - 1]);
}
}
+ test "duplicate" {
+ var s = Stack.init(std.testing.allocator);
+ defer s.deinit();
+ try s.duplicate();
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
+ try s.pushVector([2]i64{ 1, 2 });
+ try s.duplicate();
+ try s.duplicate();
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 2 }));
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 2 }));
+ }
pub fn init(allocator: std.mem.Allocator) Stack {
return Stack{
.data = std.ArrayList(i64).init(allocator),
@@ -31,59 +42,39 @@ pub const Stack = struct {
try self.data.append(n);
}
pub fn pushVector(self: *Stack, v: [2]i64) !void {
- try self.data.appendSlice(v[0..2]);
+ try self.data.appendSlice(v[0..]);
+ }
+ test "pushVector" {
+ var s = Stack.init(std.testing.allocator);
+ defer s.deinit();
+ try s.pushVector([2]i64{ 1, -1 });
+ try s.pushVector([2]i64{ 2, -2 });
+ try std.testing.expectEqual(s.data.items.len, 4);
+ try std.testing.expectEqual(s.data.items[0], 1);
+ try std.testing.expectEqual(s.data.items[1], -1);
+ try std.testing.expectEqual(s.data.items[2], 2);
+ try std.testing.expectEqual(s.data.items[3], -2);
}
pub fn swap(self: *Stack) !void {
const v = self.popVector();
try self.pushVector([2]i64{ v[1], v[0] });
}
+ test "swap" {
+ var s = Stack.init(std.testing.allocator);
+ defer s.deinit();
+ try s.swap();
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
+ try s.push(1);
+ try s.swap();
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 0 }));
+ try s.push(2);
+ try s.swap();
+ try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 0 }));
+ }
+ //pub fn transfert(toss: *Stack, soss: *Stack, n: u64) !void {
+ //}
};
test "all" {
std.testing.refAllDecls(@This());
}
-
-test "clear" {
- var s = Stack.init(std.testing.allocator);
- defer s.deinit();
- s.clear();
- try std.testing.expect(s.pop() == 0);
- try s.pushVector([2]i64{ 1, 2 });
- s.clear();
- try std.testing.expect(s.pop() == 0);
-}
-test "duplicate" {
- var s = Stack.init(std.testing.allocator);
- defer s.deinit();
- try s.duplicate();
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
- try s.pushVector([2]i64{ 1, 2 });
- try s.duplicate();
- try s.duplicate();
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 2 }));
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 2 }));
-}
-test "push and pop" {
- var s = Stack.init(std.testing.allocator);
- defer s.deinit();
- try std.testing.expect(s.pop() == 0);
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
- try s.push(1);
- try std.testing.expect(s.pop() == 1);
- try std.testing.expect(s.pop() == 0);
- try s.pushVector([2]i64{ 2, 3 });
- try std.testing.expect(s.pop() == 3);
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 2 }));
-}
-test "swap" {
- var s = Stack.init(std.testing.allocator);
- defer s.deinit();
- try s.swap();
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
- try s.push(1);
- try s.swap();
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 0 }));
- try s.push(2);
- try s.swap();
- try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 0 }));
-}