2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
2017-02-07 14:19:51 -08:00
|
|
|
const mem = @import("std").mem;
|
2017-01-05 00:57:48 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "arrays" {
|
2018-04-30 17:35:54 -07:00
|
|
|
var array: [5]u32 = undefined;
|
2016-12-21 21:55:21 -08:00
|
|
|
|
2018-04-30 17:35:54 -07:00
|
|
|
var i: u32 = 0;
|
2016-12-21 21:55:21 -08:00
|
|
|
while (i < 5) {
|
|
|
|
array[i] = i + 1;
|
|
|
|
i = array[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
var accumulator = u32(0);
|
|
|
|
while (i < 5) {
|
|
|
|
accumulator += array[i];
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(accumulator == 15);
|
|
|
|
assert(getArrayLen(array) == 5);
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn getArrayLen(a: []const u32) usize {
|
2017-12-21 21:50:30 -08:00
|
|
|
return a.len;
|
2016-12-21 21:55:21 -08:00
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "void arrays" {
|
2016-12-21 22:20:08 -08:00
|
|
|
var array: [4]void = undefined;
|
|
|
|
array[0] = void{};
|
|
|
|
array[1] = array[2];
|
|
|
|
assert(@sizeOf(@typeOf(array)) == 0);
|
|
|
|
assert(array.len == 4);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "array literal" {
|
2018-04-30 17:35:54 -07:00
|
|
|
const hex_mult = []u16 {
|
|
|
|
4096,
|
|
|
|
256,
|
|
|
|
16,
|
|
|
|
1,
|
|
|
|
};
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
assert(hex_mult.len == 4);
|
|
|
|
assert(hex_mult[1] == 256);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "array dot len const expr" {
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(comptime x: {
|
|
|
|
break :x some_array.len == 4;
|
|
|
|
});
|
2016-12-21 22:42:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ArrayDotLenConstExpr = struct {
|
|
|
|
y: [some_array.len]u8,
|
|
|
|
};
|
2018-04-30 17:35:54 -07:00
|
|
|
const some_array = []u8 {
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
};
|
2016-12-21 22:42:30 -08:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "nested arrays" {
|
2018-04-30 17:35:54 -07:00
|
|
|
const array_of_strings = [][]const u8 {
|
|
|
|
"hello",
|
|
|
|
"this",
|
|
|
|
"is",
|
|
|
|
"my",
|
|
|
|
"thing",
|
|
|
|
};
|
2016-12-26 00:44:59 -08:00
|
|
|
for (array_of_strings) |s, i| {
|
2017-02-12 14:22:35 -08:00
|
|
|
if (i == 0) assert(mem.eql(u8, s, "hello"));
|
|
|
|
if (i == 1) assert(mem.eql(u8, s, "this"));
|
|
|
|
if (i == 2) assert(mem.eql(u8, s, "is"));
|
|
|
|
if (i == 3) assert(mem.eql(u8, s, "my"));
|
|
|
|
if (i == 4) assert(mem.eql(u8, s, "thing"));
|
2016-12-26 00:44:59 -08:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 20:50:04 -08:00
|
|
|
|
2017-02-26 21:05:08 -08:00
|
|
|
var s_array: [8]Sub = undefined;
|
|
|
|
const Sub = struct {
|
|
|
|
b: u8,
|
|
|
|
};
|
|
|
|
const Str = struct {
|
|
|
|
a: []Sub,
|
|
|
|
};
|
2017-05-23 18:38:31 -07:00
|
|
|
test "set global var array via slice embedded in struct" {
|
2018-04-30 17:35:54 -07:00
|
|
|
var s = Str {
|
|
|
|
.a = s_array[0..],
|
|
|
|
};
|
2017-02-26 21:05:08 -08:00
|
|
|
|
|
|
|
s.a[0].b = 1;
|
|
|
|
s.a[1].b = 2;
|
|
|
|
s.a[2].b = 3;
|
|
|
|
|
|
|
|
assert(s_array[0].b == 1);
|
|
|
|
assert(s_array[1].b == 2);
|
|
|
|
assert(s_array[2].b == 3);
|
|
|
|
}
|
2017-08-31 13:30:46 -07:00
|
|
|
|
|
|
|
test "array literal with specified size" {
|
2018-04-30 17:35:54 -07:00
|
|
|
var array = [2]u8 {
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
};
|
2017-08-31 13:30:46 -07:00
|
|
|
assert(array[0] == 1);
|
|
|
|
assert(array[1] == 2);
|
|
|
|
}
|
2017-09-07 20:10:23 -07:00
|
|
|
|
|
|
|
test "array child property" {
|
|
|
|
var x: [5]i32 = undefined;
|
2017-09-13 11:30:57 -07:00
|
|
|
assert(@typeOf(x).Child == i32);
|
2017-09-07 20:10:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "array len property" {
|
|
|
|
var x: [5]i32 = undefined;
|
|
|
|
assert(@typeOf(x).len == 5);
|
|
|
|
}
|