2016-04-18 16:42:56 -07:00
|
|
|
const std = @import("std");
|
2016-05-17 13:32:43 -07:00
|
|
|
const assert = std.debug.assert;
|
2016-04-18 16:42:56 -07:00
|
|
|
const str = std.str;
|
2016-05-07 10:52:52 -07:00
|
|
|
const cstr = std.cstr;
|
2016-09-15 11:07:35 -07:00
|
|
|
const test_const_slice_child = @import("cases/const_slice_child.zig");
|
2016-09-20 13:10:34 -07:00
|
|
|
const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig");
|
2016-09-22 23:00:23 -07:00
|
|
|
const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig");
|
2016-09-23 12:18:02 -07:00
|
|
|
const test_enum_with_members = @import("cases/enum_with_members.zig");
|
2016-02-07 00:25:04 -08:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn explicitCastMaybePointers() {
|
2016-12-25 01:15:23 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-02-07 00:25:04 -08:00
|
|
|
const a: ?&i32 = undefined;
|
|
|
|
const b: ?&f32 = (?&f32)(a);
|
|
|
|
}
|
2016-02-07 11:38:51 -08:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn genericMallocFree() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
const a = %%memAlloc(u8, 10);
|
|
|
|
memFree(u8, a);
|
2016-04-07 15:09:55 -07:00
|
|
|
}
|
|
|
|
const some_mem : [100]u8 = undefined;
|
2016-08-16 22:42:50 -07:00
|
|
|
fn memAlloc(inline T: type, n: usize) -> %[]T {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-07 15:09:55 -07:00
|
|
|
return (&T)(&some_mem[0])[0...n];
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn memFree(inline T: type, mem: []T) { }
|
2016-04-07 15:09:55 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn castUndefined() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-10 12:04:25 -07:00
|
|
|
const array: [100]u8 = undefined;
|
|
|
|
const slice = ([]u8)(array);
|
2016-08-16 22:42:50 -07:00
|
|
|
testCastUndefined(slice);
|
2016-04-10 12:04:25 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testCastUndefined(x: []u8) {}
|
2016-04-11 22:57:32 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn castSmallUnsignedToLargerSigned() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(castSmallUnsignedToLargerSigned1(200) == i16(200));
|
|
|
|
assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999));
|
2016-04-11 22:57:32 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { x }
|
|
|
|
fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { x }
|
2016-04-12 13:30:52 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn implicitCastAfterUnreachable() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-12 13:30:52 -07:00
|
|
|
assert(outer() == 1234);
|
|
|
|
}
|
|
|
|
fn inner() -> i32 { 1234 }
|
2016-07-26 20:40:11 -07:00
|
|
|
fn outer() -> i64 {
|
2016-04-12 13:30:52 -07:00
|
|
|
return inner();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn staticallyInitalizedList() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-12 17:33:46 -07:00
|
|
|
assert(static_point_list[0].x == 1);
|
|
|
|
assert(static_point_list[0].y == 2);
|
|
|
|
assert(static_point_list[1].x == 3);
|
|
|
|
assert(static_point_list[1].y == 4);
|
|
|
|
}
|
|
|
|
struct Point {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) };
|
|
|
|
fn makePoint(x: i32, y: i32) -> Point {
|
2016-04-12 17:33:46 -07:00
|
|
|
return Point {
|
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn staticEvalListInit() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-13 10:13:49 -07:00
|
|
|
assert(static_vec3.data[2] == 1.0);
|
|
|
|
}
|
|
|
|
const static_vec3 = vec3(0.0, 0.0, 1.0);
|
|
|
|
pub struct Vec3 {
|
|
|
|
data: [3]f32,
|
|
|
|
}
|
|
|
|
pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
|
|
|
|
Vec3 {
|
|
|
|
.data = []f32 { x, y, z, },
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 10:39:03 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn genericFnWithImplicitCast() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(getFirstByte(u8, []u8 {13}) == 13);
|
|
|
|
assert(getFirstByte(u16, []u16 {0, 13}) == 0);
|
2016-04-14 10:39:03 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn getByte(ptr: ?&u8) -> u8 {*??ptr}
|
|
|
|
fn getFirstByte(inline T: type, mem: []T) -> u8 {
|
|
|
|
getByte((&u8)(&mem[0]))
|
2016-04-14 10:39:03 -07:00
|
|
|
}
|
2016-04-14 10:52:47 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn pointerDereferencing() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
var x = i32(3);
|
|
|
|
const y = &x;
|
|
|
|
|
|
|
|
*y += 1;
|
|
|
|
|
|
|
|
assert(x == 4);
|
|
|
|
assert(*y == 4);
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn constantExpressions() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
var array : [array_size]u8 = undefined;
|
|
|
|
assert(@sizeOf(@typeOf(array)) == 20);
|
2016-04-14 10:52:47 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
const array_size : u8 = 20;
|
2016-04-14 10:52:47 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn nestedArrays() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
|
2016-04-18 16:42:56 -07:00
|
|
|
for (array_of_strings) |s, i| {
|
|
|
|
if (i == 0) assert(str.eql(s, "hello"));
|
|
|
|
if (i == 1) assert(str.eql(s, "this"));
|
|
|
|
if (i == 2) assert(str.eql(s, "is"));
|
|
|
|
if (i == 3) assert(str.eql(s, "my"));
|
|
|
|
if (i == 4) assert(str.eql(s, "thing"));
|
2016-04-14 10:52:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn intToPtrCast() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
const x = isize(13);
|
|
|
|
const y = (&u8)(x);
|
|
|
|
const z = usize(y);
|
|
|
|
assert(z == 13);
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn constantStructWithNegation() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
assert(vertices[0].x == -0.6);
|
|
|
|
}
|
|
|
|
struct Vertex {
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
r: f32,
|
|
|
|
g: f32,
|
|
|
|
b: f32,
|
|
|
|
}
|
|
|
|
const vertices = []Vertex {
|
|
|
|
Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
|
|
|
|
Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
|
|
|
|
Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn returnStructByvalFromFunction() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
const bar = makeBar(1234, 5678);
|
2016-04-14 10:52:47 -07:00
|
|
|
assert(bar.y == 5678);
|
|
|
|
}
|
|
|
|
struct Bar {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn makeBar(x: i32, y: i32) -> Bar {
|
2016-04-14 10:52:47 -07:00
|
|
|
Bar {
|
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn functionPointers() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
|
2016-04-14 10:52:47 -07:00
|
|
|
for (fns) |f, i| {
|
|
|
|
assert(f() == u32(i) + 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn fn1() -> u32 {5}
|
|
|
|
fn fn2() -> u32 {6}
|
|
|
|
fn fn3() -> u32 {7}
|
|
|
|
fn fn4() -> u32 {8}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn staticallyInitalizedStruct() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
st_init_str_foo.x += 1;
|
|
|
|
assert(st_init_str_foo.x == 14);
|
|
|
|
}
|
|
|
|
struct StInitStrFoo {
|
|
|
|
x: i32,
|
|
|
|
y: bool,
|
|
|
|
}
|
|
|
|
var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn staticallyInitializedArrayLiteral() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-14 10:52:47 -07:00
|
|
|
const y : [4]u8 = st_init_arr_lit_x;
|
|
|
|
assert(y[3] == 4);
|
|
|
|
}
|
|
|
|
const st_init_arr_lit_x = []u8{1,2,3,4};
|
2016-04-18 13:06:17 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn pointerToVoidReturnType() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
%%testPointerToVoidReturnType();
|
2016-04-18 13:06:17 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testPointerToVoidReturnType() -> %void {
|
|
|
|
const a = testPointerToVoidReturnType2();
|
2016-04-18 13:06:17 -07:00
|
|
|
return *a;
|
|
|
|
}
|
|
|
|
const test_pointer_to_void_return_type_x = void{};
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testPointerToVoidReturnType2() -> &void {
|
2016-04-18 13:06:17 -07:00
|
|
|
return &test_pointer_to_void_return_type_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn callResultOfIfElseExpression() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-18 16:42:56 -07:00
|
|
|
assert(str.eql(f2(true), "a"));
|
|
|
|
assert(str.eql(f2(false), "b"));
|
2016-04-18 13:06:17 -07:00
|
|
|
}
|
|
|
|
fn f2(x: bool) -> []u8 {
|
2016-08-16 22:42:50 -07:00
|
|
|
return (if (x) fA else fB)();
|
2016-04-18 13:06:17 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn fA() -> []u8 { "a" }
|
|
|
|
fn fB() -> []u8 { "b" }
|
2016-04-18 13:06:17 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn constExpressionEvalHandlingOfVariables() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-18 13:06:17 -07:00
|
|
|
var x = true;
|
|
|
|
while (x) {
|
|
|
|
x = false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 17:15:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn constantEnumInitializationWithDifferingSizes() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-19 17:15:36 -07:00
|
|
|
test3_1(test3_foo);
|
|
|
|
test3_2(test3_bar);
|
|
|
|
}
|
|
|
|
enum Test3Foo {
|
|
|
|
One,
|
|
|
|
Two: f32,
|
|
|
|
Three: Test3Point,
|
|
|
|
}
|
|
|
|
struct Test3Point {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
}
|
2016-04-19 18:52:09 -07:00
|
|
|
const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}};
|
|
|
|
const test3_bar = Test3Foo.Two{13};
|
2016-04-19 17:15:36 -07:00
|
|
|
fn test3_1(f: Test3Foo) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-19 17:15:36 -07:00
|
|
|
switch (f) {
|
|
|
|
Three => |pt| {
|
|
|
|
assert(pt.x == 3);
|
|
|
|
assert(pt.y == 4);
|
|
|
|
},
|
2016-09-13 13:46:27 -07:00
|
|
|
else => @unreachable(),
|
2016-04-19 17:15:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn test3_2(f: Test3Foo) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-19 17:15:36 -07:00
|
|
|
switch (f) {
|
|
|
|
Two => |x| {
|
|
|
|
assert(x == 13);
|
|
|
|
},
|
2016-09-13 13:46:27 -07:00
|
|
|
else => @unreachable(),
|
2016-04-19 17:15:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-19 17:19:18 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn forLoopWithPointerElemVar() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-20 11:58:01 -07:00
|
|
|
const source = "abcdefg";
|
|
|
|
var target: [source.len]u8 = undefined;
|
|
|
|
@memcpy(&target[0], &source[0], source.len);
|
2016-08-16 22:42:50 -07:00
|
|
|
mangleString(target);
|
2016-04-20 11:58:01 -07:00
|
|
|
assert(str.eql(target, "bcdefgh"));
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn mangleString(s: []u8) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-20 11:58:01 -07:00
|
|
|
for (s) |*c| {
|
|
|
|
*c += 1;
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 09:47:41 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn emptyStructMethodCall() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-21 09:47:41 -07:00
|
|
|
const es = EmptyStruct{};
|
|
|
|
assert(es.method() == 1234);
|
|
|
|
}
|
|
|
|
struct EmptyStruct {
|
2016-09-27 23:33:32 -07:00
|
|
|
fn method(es: EmptyStruct) -> i32 {
|
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
1234
|
|
|
|
}
|
|
|
|
|
2016-04-21 09:47:41 -07:00
|
|
|
}
|
2016-04-22 15:24:18 -07:00
|
|
|
|
|
|
|
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-04-23 12:21:47 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn returnEmptyStructFromFn() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testReturnEmptyStructFromFn();
|
|
|
|
testReturnEmptyStructFromFnNoeval();
|
2016-04-23 12:21:47 -07:00
|
|
|
}
|
|
|
|
struct EmptyStruct2 {}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
|
2016-04-23 12:21:47 -07:00
|
|
|
EmptyStruct2 {}
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testReturnEmptyStructFromFnNoeval() -> EmptyStruct2 {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-23 12:21:47 -07:00
|
|
|
EmptyStruct2 {}
|
|
|
|
}
|
2016-04-23 13:58:30 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn passSliceOfEmptyStructToFn() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
|
2016-04-23 13:58:30 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
|
2016-04-23 13:58:30 -07:00
|
|
|
slice.len
|
|
|
|
}
|
2016-04-24 12:27:59 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn pointerComparison() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-24 12:27:59 -07:00
|
|
|
const a = ([]u8)("a");
|
|
|
|
const b = &a;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(ptrEql(b, b));
|
2016-04-24 12:27:59 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn ptrEql(a: &[]u8, b: &[]u8) -> bool {
|
2016-04-24 12:27:59 -07:00
|
|
|
a == b
|
|
|
|
}
|
2016-04-24 16:33:32 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn characterLiterals() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-04-24 16:33:32 -07:00
|
|
|
assert('\'' == single_quote);
|
|
|
|
}
|
|
|
|
const single_quote = '\'';
|
2016-04-25 21:51:04 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn switchWithMultipleExpressions() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
const x: i32 = switch (returnsFive()) {
|
2016-04-25 21:51:04 -07:00
|
|
|
1, 2, 3 => 1,
|
|
|
|
4, 5, 6 => 2,
|
|
|
|
else => 3,
|
|
|
|
};
|
|
|
|
assert(x == 2);
|
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
fn returnsFive() -> i32 {
|
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
5
|
|
|
|
}
|
|
|
|
|
2016-04-25 21:51:04 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn switchOnErrorUnion() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
const x = switch (returnsTen()) {
|
2016-04-25 21:51:04 -07:00
|
|
|
Ok => |val| val + 1,
|
|
|
|
ItBroke, NoMem => 1,
|
|
|
|
CrappedOut => 2,
|
|
|
|
};
|
|
|
|
assert(x == 11);
|
|
|
|
}
|
|
|
|
error ItBroke;
|
|
|
|
error NoMem;
|
|
|
|
error CrappedOut;
|
2016-09-27 23:33:32 -07:00
|
|
|
fn returnsTen() -> %i32 {
|
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
10
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn takeAddressOfParameter() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testTakeAddressOfParameter(12.34);
|
|
|
|
testTakeAddressOfParameterNoeval(12.34);
|
2016-04-27 17:34:53 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testTakeAddressOfParameter(f: f32) {
|
2016-04-27 17:34:53 -07:00
|
|
|
const f_ptr = &f;
|
|
|
|
assert(*f_ptr == 12.34);
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testTakeAddressOfParameterNoeval(f: f32) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-04-27 17:34:53 -07:00
|
|
|
const f_ptr = &f;
|
|
|
|
assert(*f_ptr == 12.34);
|
|
|
|
}
|
2016-04-28 18:03:44 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn ifVarMaybePointer() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
|
2016-05-03 15:06:34 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn shouldBeAPlus1(p: Particle) -> u64 {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-05-03 15:06:34 -07:00
|
|
|
var maybe_particle: ?Particle = p;
|
|
|
|
if (const *particle ?= maybe_particle) {
|
|
|
|
particle.a += 1;
|
|
|
|
}
|
|
|
|
if (const particle ?= maybe_particle) {
|
|
|
|
return particle.a;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
struct Particle {
|
|
|
|
a: u64,
|
|
|
|
b: u64,
|
|
|
|
c: u64,
|
|
|
|
d: u64,
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn unsignedWrapping() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testUnsignedWrappingEval(@maxValue(u32));
|
|
|
|
testUnsignedWrappingNoeval(@maxValue(u32));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testUnsignedWrappingEval(x: u32) {
|
2016-07-27 23:04:24 -07:00
|
|
|
const zero = x +% 1;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(zero == 0);
|
2016-07-27 23:04:24 -07:00
|
|
|
const orig = zero -% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(orig == @maxValue(u32));
|
2016-05-06 19:23:21 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testUnsignedWrappingNoeval(x: u32) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-07-27 23:04:24 -07:00
|
|
|
const zero = x +% 1;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(zero == 0);
|
2016-07-27 23:04:24 -07:00
|
|
|
const orig = zero -% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(orig == @maxValue(u32));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn signedWrapping() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testSignedWrappingEval(@maxValue(i32));
|
|
|
|
testSignedWrappingNoeval(@maxValue(i32));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testSignedWrappingEval(x: i32) {
|
2016-07-27 23:04:24 -07:00
|
|
|
const min_val = x +% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(min_val == @minValue(i32));
|
2016-07-27 23:04:24 -07:00
|
|
|
const max_val = min_val -% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(max_val == @maxValue(i32));
|
2016-05-06 19:23:21 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testSignedWrappingNoeval(x: i32) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-07-27 23:04:24 -07:00
|
|
|
const min_val = x +% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(min_val == @minValue(i32));
|
2016-07-27 23:04:24 -07:00
|
|
|
const max_val = min_val -% 1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(max_val == @maxValue(i32));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn negationWrapping() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testNegationWrappingEval(@minValue(i16));
|
|
|
|
testNegationWrappingNoeval(@minValue(i16));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testNegationWrappingEval(x: i16) {
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(x == -32768);
|
2016-07-27 23:04:24 -07:00
|
|
|
const neg = -%x;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(neg == -32768);
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testNegationWrappingNoeval(x: i16) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(x == -32768);
|
2016-07-27 23:04:24 -07:00
|
|
|
const neg = -%x;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(neg == -32768);
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn shlWrapping() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testShlWrappingEval(@maxValue(u16));
|
|
|
|
testShlWrappingNoeval(@maxValue(u16));
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testShlWrappingEval(x: u16) {
|
2016-07-27 23:04:24 -07:00
|
|
|
const shifted = x <<% 1;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(shifted == 65534);
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testShlWrappingNoeval(x: u16) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-07-27 23:04:24 -07:00
|
|
|
const shifted = x <<% 1;
|
2016-05-06 19:23:21 -07:00
|
|
|
assert(shifted == 65534);
|
2016-05-06 15:02:02 -07:00
|
|
|
}
|
2016-05-06 16:44:35 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn cStringConcatenation() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-05-07 10:05:59 -07:00
|
|
|
const a = c"OK" ++ c" IT " ++ c"WORKED";
|
|
|
|
const b = c"OK IT WORKED";
|
|
|
|
|
2016-05-07 10:52:52 -07:00
|
|
|
const len = cstr.len(b);
|
2016-05-07 10:05:59 -07:00
|
|
|
const len_with_null = len + 1;
|
2016-07-26 20:40:11 -07:00
|
|
|
{var i: u32 = 0; while (i < len_with_null; i += 1) {
|
2016-05-07 10:05:59 -07:00
|
|
|
assert(a[i] == b[i]);
|
|
|
|
}}
|
|
|
|
assert(a[len] == 0);
|
|
|
|
assert(b[len] == 0);
|
|
|
|
}
|
2016-05-07 17:00:58 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn genericStruct() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-05-07 17:00:58 -07:00
|
|
|
var a1 = GenNode(i32) {.value = 13, .next = null,};
|
|
|
|
var b1 = GenNode(bool) {.value = true, .next = null,};
|
|
|
|
assert(a1.value == 13);
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(a1.value == a1.getVal());
|
|
|
|
assert(b1.getVal());
|
2016-05-07 17:00:58 -07:00
|
|
|
}
|
|
|
|
struct GenNode(T: type) {
|
|
|
|
value: T,
|
|
|
|
next: ?&GenNode(T),
|
2016-08-16 22:42:50 -07:00
|
|
|
fn getVal(n: &const GenNode(T)) -> T { n.value }
|
2016-05-07 17:00:58 -07:00
|
|
|
}
|
2016-05-07 18:45:28 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn castSliceToU8Slice() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(@sizeOf(i32) == 4);
|
2016-05-07 18:45:28 -07:00
|
|
|
var big_thing_array = []i32{1, 2, 3, 4};
|
|
|
|
const big_thing_slice: []i32 = big_thing_array;
|
|
|
|
const bytes = ([]u8)(big_thing_slice);
|
|
|
|
assert(bytes.len == 4 * 4);
|
|
|
|
bytes[4] = 0;
|
|
|
|
bytes[5] = 0;
|
|
|
|
bytes[6] = 0;
|
|
|
|
bytes[7] = 0;
|
|
|
|
assert(big_thing_slice[1] == 0);
|
2016-05-08 00:59:21 -07:00
|
|
|
const big_thing_again = ([]i32)(bytes);
|
|
|
|
assert(big_thing_again[2] == 3);
|
|
|
|
big_thing_again[2] = -1;
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(bytes[8] == @maxValue(u8));
|
|
|
|
assert(bytes[9] == @maxValue(u8));
|
|
|
|
assert(bytes[10] == @maxValue(u8));
|
|
|
|
assert(bytes[11] == @maxValue(u8));
|
2016-05-07 18:45:28 -07:00
|
|
|
}
|
2016-05-07 19:58:02 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn nullLiteralOutsideFunction() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-05-08 01:34:00 -07:00
|
|
|
const is_null = if (const _ ?= here_is_a_null_literal.context) false else true;
|
|
|
|
assert(is_null);
|
2016-05-08 00:15:05 -07:00
|
|
|
}
|
|
|
|
struct SillyStruct {
|
|
|
|
context: ?i32,
|
|
|
|
}
|
|
|
|
const here_is_a_null_literal = SillyStruct {
|
|
|
|
.context = null,
|
|
|
|
};
|
2016-05-09 08:44:41 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn constDeclsInStruct() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-05-09 12:34:03 -07:00
|
|
|
assert(GenericDataThing(3).count_plus_one == 4);
|
|
|
|
}
|
|
|
|
struct GenericDataThing(count: isize) {
|
|
|
|
const count_plus_one = count + 1;
|
|
|
|
}
|
2016-05-09 13:44:29 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn useGenericParamInGenericParam() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(aGenericFn(i32, 3, 4) == 7);
|
2016-05-09 13:44:29 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn aGenericFn(inline T: type, inline a: T, b: T) -> T {
|
2016-05-09 13:44:29 -07:00
|
|
|
return a + b;
|
|
|
|
}
|
2016-05-15 00:25:13 -07:00
|
|
|
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn unsigned64BitDivision() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-05-15 00:42:48 -07:00
|
|
|
const result = div(1152921504606846976, 34359738365);
|
|
|
|
assert(result.quotient == 33554432);
|
|
|
|
assert(result.remainder == 100663296);
|
|
|
|
}
|
|
|
|
fn div(a: u64, b: u64) -> DivResult {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-05-15 00:42:48 -07:00
|
|
|
DivResult {
|
|
|
|
.quotient = a / b,
|
|
|
|
.remainder = a % b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct DivResult {
|
|
|
|
quotient: u64,
|
|
|
|
remainder: u64,
|
|
|
|
}
|
2016-05-15 01:05:24 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn intToEnum() {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnTest(this, true);
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
testIntToEnumEval(3);
|
|
|
|
testIntToEnumNoeval(3);
|
2016-07-09 15:21:50 -07:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testIntToEnumEval(x: i32) {
|
2016-07-09 15:21:50 -07:00
|
|
|
assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
|
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
fn testIntToEnumNoeval(x: i32) {
|
2016-09-27 23:33:32 -07:00
|
|
|
@setFnStaticEval(this, false);
|
|
|
|
|
2016-07-09 15:21:50 -07:00
|
|
|
assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
|
|
|
|
}
|
|
|
|
enum IntToEnumNumber {
|
|
|
|
Zero,
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
Four,
|
|
|
|
}
|