1195994880
before, when we initialized a variable by copying the initialization value, it made the internal const value references point to a duplicate value, resulting in a phony duplicate global value being updated instead of the real on. now the behavior is as expected. thanks to hoppetosse for pointing out this bug on IRC.
95 lines
1.8 KiB
Zig
95 lines
1.8 KiB
Zig
const assert = @import("std").debug.assert;
|
|
const mem = @import("std").mem;
|
|
|
|
fn arrays() {
|
|
@setFnTest(this);
|
|
|
|
var array : [5]u32 = undefined;
|
|
|
|
var i : u32 = 0;
|
|
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);
|
|
}
|
|
fn getArrayLen(a: []const u32) -> usize {
|
|
a.len
|
|
}
|
|
|
|
fn voidArrays() {
|
|
@setFnTest(this);
|
|
|
|
var array: [4]void = undefined;
|
|
array[0] = void{};
|
|
array[1] = array[2];
|
|
assert(@sizeOf(@typeOf(array)) == 0);
|
|
assert(array.len == 4);
|
|
}
|
|
|
|
fn arrayLiteral() {
|
|
@setFnTest(this);
|
|
|
|
const hex_mult = []u16{4096, 256, 16, 1};
|
|
|
|
assert(hex_mult.len == 4);
|
|
assert(hex_mult[1] == 256);
|
|
}
|
|
|
|
fn arrayDotLenConstExpr() {
|
|
@setFnTest(this);
|
|
|
|
assert(comptime {some_array.len == 4});
|
|
}
|
|
|
|
const ArrayDotLenConstExpr = struct {
|
|
y: [some_array.len]u8,
|
|
};
|
|
const some_array = []u8 {0, 1, 2, 3};
|
|
|
|
|
|
fn nestedArrays() {
|
|
@setFnTest(this);
|
|
|
|
const array_of_strings = [][]const u8 {"hello", "this", "is", "my", "thing"};
|
|
for (array_of_strings) |s, i| {
|
|
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"));
|
|
}
|
|
}
|
|
|
|
|
|
var s_array: [8]Sub = undefined;
|
|
const Sub = struct {
|
|
b: u8,
|
|
};
|
|
const Str = struct {
|
|
a: []Sub,
|
|
};
|
|
fn setGlobalVarArrayViaSliceEmbeddedInStruct() {
|
|
@setFnTest(this);
|
|
|
|
var s = Str { .a = s_array[0...]};
|
|
|
|
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);
|
|
}
|