2017-02-02 11:55:01 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
2017-10-21 10:14:10 -07:00
|
|
|
const mem = @import("std").mem;
|
2017-02-02 11:55:01 -08:00
|
|
|
|
|
|
|
fn initStaticArray() -> [10]i32 {
|
|
|
|
var array: [10]i32 = undefined;
|
|
|
|
array[0] = 1;
|
|
|
|
array[4] = 2;
|
|
|
|
array[7] = 3;
|
|
|
|
array[9] = 4;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
const static_array = initStaticArray();
|
2017-05-23 18:38:31 -07:00
|
|
|
test "init static array to undefined" {
|
2017-02-02 11:55:01 -08:00
|
|
|
assert(static_array[0] == 1);
|
|
|
|
assert(static_array[4] == 2);
|
|
|
|
assert(static_array[7] == 3);
|
|
|
|
assert(static_array[9] == 4);
|
|
|
|
|
|
|
|
comptime {
|
|
|
|
assert(static_array[0] == 1);
|
|
|
|
assert(static_array[4] == 2);
|
|
|
|
assert(static_array[7] == 3);
|
|
|
|
assert(static_array[9] == 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Foo = struct {
|
|
|
|
x: i32,
|
2017-02-02 12:03:00 -08:00
|
|
|
|
|
|
|
fn setFooXMethod(foo: &Foo) {
|
|
|
|
foo.x = 3;
|
|
|
|
}
|
2017-02-02 11:55:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
fn setFooX(foo: &Foo) {
|
|
|
|
foo.x = 2;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "assign undefined to struct" {
|
2017-02-02 11:55:01 -08:00
|
|
|
comptime {
|
|
|
|
var foo: Foo = undefined;
|
|
|
|
setFooX(&foo);
|
|
|
|
assert(foo.x == 2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var foo: Foo = undefined;
|
|
|
|
setFooX(&foo);
|
|
|
|
assert(foo.x == 2);
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 12:03:00 -08:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "assign undefined to struct with method" {
|
2017-02-02 12:03:00 -08:00
|
|
|
comptime {
|
|
|
|
var foo: Foo = undefined;
|
|
|
|
foo.setFooXMethod();
|
|
|
|
assert(foo.x == 3);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var foo: Foo = undefined;
|
|
|
|
foo.setFooXMethod();
|
|
|
|
assert(foo.x == 3);
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 10:14:10 -07:00
|
|
|
|
|
|
|
test "type name of undefined" {
|
|
|
|
const x = undefined;
|
|
|
|
assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
|
|
|
|
}
|