commit 995b64277541d29df3162b4521c2eae196ad8eb1
parent b6df30cf544f96796e536f65e70db9dfc73e7d5b
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 4 Feb 2021 14:39:02 -0500
bytes: add more test coverage
And fix a bug!
Diffstat:
3 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/bytes/copy.ha b/bytes/copy.ha
@@ -6,3 +6,10 @@ export fn copy(dest: []u8, src: []u8) void = {
dest[i] = src[i];
};
};
+
+@test fn copy() void = {
+ let a: [4]u8 = [1u8, 3u8, 3u8, 7u8];
+ let b: [4]u8 = [0u8...];
+ copy(b[..], a[..]);
+ assert(equal(a, b));
+};
diff --git a/bytes/index.ha b/bytes/index.ha
@@ -7,3 +7,19 @@ export fn index(haystack: []u8, needle: u8) (size | void) = {
};
};
};
+
+@test fn index() void = {
+ let a: [4]u8 = [1u8, 3u8, 3u8, 7u8];
+ match (index(a, 7u8)) {
+ n: size => assert(n == 3z),
+ void => abort(),
+ };
+ match (index(a, 42u8)) {
+ size => abort(),
+ void => void,
+ };
+ match (index([], 42u8)) {
+ size => abort(),
+ void => void,
+ };
+};
diff --git a/bytes/reverse.ha b/bytes/reverse.ha
@@ -1,5 +1,8 @@
// Reverses a slice of bytes.
export fn reverse(b: []u8) void = {
+ if (len(b) == 0z) {
+ return;
+ };
for (let s = 0z, e = len(b) - 1z; s < e) {
let x = b[s];
b[s] = b[e];
@@ -8,3 +11,15 @@ export fn reverse(b: []u8) void = {
e -= 1z;
};
};
+
+@test fn reverse() void = {
+ let a: [4]u8 = [1u8, 3u8, 3u8, 7u8];
+ reverse(a);
+ assert(equal(a, [7u8, 3u8, 3u8, 1u8]));
+ let b: [5]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];
+ reverse(b);
+ assert(equal(b, [5u8, 4u8, 3u8, 2u8, 1u8]));
+ let c: []u8 = [];
+ reverse(c);
+ assert(equal(c, []));
+};