commit 4e832881c7ca6b853d8f3a999bf712e8451f12c1
parent cfde568e08d3ea202731dd87c6720aab7b6386b1
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 11 Jan 2021 11:03:58 -0500
Add slice test
Diffstat:
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/tests/08-slices.ha b/tests/08-slices.ha
@@ -0,0 +1,42 @@
+fn from_array() void = {
+ let src = [1, 2, 3];
+ let x: []int = src;
+ let ptr = &x: *struct {
+ data: *[*]int,
+ length: size,
+ capacity: size,
+ };
+ assert(ptr.data == &src);
+};
+
+fn storage() void = {
+ let x: []int = [1, 2, 3, 4, 5];
+ const expected = [1, 2, 3, 4, 5];
+
+ let ptr = &x: *struct {
+ data: *[*]int,
+ length: size,
+ capacity: size,
+ };
+
+ assert(len(x) == 5z);
+ assert(ptr.length == 5z);
+ assert(ptr.capacity == 5z);
+
+ for (let i = 0z; i < len(expected); i += 1z) {
+ assert(x[i] == expected[i]);
+ };
+};
+
+fn measurements() void = {
+ let x: []int = [1, 2, 3, 4, 5];
+ assert(size([]int) == size(*[*]int) + size(size) * 2z);
+ assert(len(x) == 5z);
+ assert(&x: uintptr: size % size(int) == 0z);
+};
+
+export fn main() void = {
+ from_array();
+ storage();
+ measurements();
+};
diff --git a/tests/configure b/tests/configure
@@ -10,7 +10,8 @@ tests() {
04-strings \
05-implicit-casts \
06-structs \
- 07-aliases
+ 07-aliases \
+ 08-slices
do
cat <<EOF
tests/$t: libhart.a tests/$t.ha