zig/test/cases/undefined.zig
Andrew Kelley 3671582c15 syntax: functions require return type. remove ->
The purpose of this is:

 * Only one way to do things
 * Changing a function with void return type to return a possible
   error becomes a 1 character change, subtly encouraging
   people to use errors.

See #632

Here are some imperfect sed commands for performing this update:

remove arrow:

```
sed -i 's/\(\bfn\b.*\)-> /\1/g' $(find . -name "*.zig")
```

add void:

```
sed -i 's/\(\bfn\b.*\))\s*{/\1) void {/g' $(find ../ -name "*.zig")
```

Some cleanup may be necessary, but this should do the bulk of the work.
2018-01-25 04:10:11 -05:00

69 lines
1.4 KiB
Zig

const assert = @import("std").debug.assert;
const mem = @import("std").mem;
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();
test "init static array to undefined" {
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,
fn setFooXMethod(foo: &Foo) void {
foo.x = 3;
}
};
fn setFooX(foo: &Foo) void {
foo.x = 2;
}
test "assign undefined to struct" {
comptime {
var foo: Foo = undefined;
setFooX(&foo);
assert(foo.x == 2);
}
{
var foo: Foo = undefined;
setFooX(&foo);
assert(foo.x == 2);
}
}
test "assign undefined to struct with method" {
comptime {
var foo: Foo = undefined;
foo.setFooXMethod();
assert(foo.x == 3);
}
{
var foo: Foo = undefined;
foo.setFooXMethod();
assert(foo.x == 3);
}
}
test "type name of undefined" {
const x = undefined;
assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
}