IR: port more tests

This commit is contained in:
Andrew Kelley 2016-12-22 01:42:30 -05:00
parent 5a71718757
commit dab3ddab45
13 changed files with 286 additions and 284 deletions

View File

@ -43,6 +43,18 @@ fn arrayLiteral() {
assert(hex_mult[1] == 256);
}
fn arrayDotLenConstExpr() {
@setFnTest(this);
assert(@staticEval(some_array.len) == 4);
}
const ArrayDotLenConstExpr = struct {
y: [some_array.len]u8,
};
const some_array = []u8 {0, 1, 2, 3};
// TODO const assert = @import("std").debug.assert;

View File

@ -20,6 +20,15 @@ fn nonConstCastBoolToInt(t: bool, f: bool) {
assert(i32(f) == i32(0));
}
fn boolCmp() {
@setFnTest(this);
assert(testBoolCmp(true, false) == false);
}
fn testBoolCmp(a: bool, b: bool) -> bool {
a == b
}
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)

14
test/cases3/cast.zig Normal file
View File

@ -0,0 +1,14 @@
//fn intToPtrCast() {
// @setFnTest(this);
//
// const x = isize(13);
// const y = (&u8)(x);
// const z = usize(y);
// assert(z == 13);
//}
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)
@unreachable();
}

View File

@ -25,8 +25,11 @@ fn gimmeItBroke() -> []const u8 {
fn errorName() {
@setFnTest(this);
assert(memeql(@errorName(error.ItBroke), "ItBroke"));
assert(memeql(@errorName(error.AnError), "AnError"));
assert(memeql(@errorName(error.ALongerErrorName), "ALongerErrorName"));
}
error AnError;
error ALongerErrorName;
fn errorValues() {
@ -53,6 +56,32 @@ fn shouldBeNotEqual(a: error, b: error) {
}
fn errBinaryOperator() {
@setFnTest(this);
const a = errBinaryOperatorG(true) %% 3;
const b = errBinaryOperatorG(false) %% 3;
assert(a == 3);
assert(b == 10);
}
error ItBroke;
fn errBinaryOperatorG(x: bool) -> %isize {
if (x) {
error.ItBroke
} else {
isize(10)
}
}
fn unwrapSimpleValueFromError() {
@setFnTest(this);
const i = %%unwrapSimpleValueFromErrorDo();
assert(i == 13);
}
fn unwrapSimpleValueFromErrorDo() -> %isize { 13 }

View File

@ -1,11 +1,14 @@
fn fib(x: i32) -> i32 {
if (x < 2) x else fib(x - 1) + fib(x - 2)
}
const fib_7 = fib(7);
fn compileTimeFib() {
fn compileTimeRecursion() {
@setFnTest(this);
assert(fib_7 == 13);
assert(some_data.len == 21);
}
var some_data: [usize(fibbonaci(7))]u8 = undefined;
fn fibbonaci(x: i32) -> i32 {
if (x <= 1) return 1;
return fibbonaci(x - 1) + fibbonaci(x - 2);
}
fn unwrapAndAddOne(blah: ?i32) -> i32 {
@ -40,6 +43,17 @@ fn inlineVariableGetsResultOfConstIf() {
}
fn staticFunctionEvaluation() {
@setFnTest(this);
assert(statically_added_number == 3);
}
const statically_added_number = staticAdd(1, 2);
fn staticAdd(a: i32, b: i32) -> i32 { a + b }
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {

View File

@ -60,6 +60,18 @@ fn separateBlockScopes() {
assert(c == 10);
}
fn callFnWithEmptyString() {
@setFnTest(this);
acceptsString("");
}
fn acceptsString(foo: []u8) { }
fn @"weird function name"() {
@setFnTest(this);
}
// TODO const assert = @import("std").debug.assert;

View File

@ -24,3 +24,23 @@ fn firstEqlThird(a: i32, b: i32, c: i32) {
}
fn elseIfExpression() {
@setFnTest(this);
assert(elseIfExpressionF(1) == 1);
}
fn elseIfExpressionF(c: u8) -> u8 {
if (c == 0) {
0
} else if (c == 1) {
1
} else {
u8(2)
}
}
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)
@unreachable();
}

View File

@ -152,6 +152,83 @@ fn globalVariables() {
}
fn memcpyAndMemsetIntrinsics() {
@setFnTest(this);
var foo : [20]u8 = undefined;
var bar : [20]u8 = undefined;
@memset(&foo[0], 'A', foo.len);
@memcpy(&bar[0], &foo[0], bar.len);
if (bar[11] != 'A') @unreachable();
}
fn builtinStaticEval() {
@setFnTest(this);
const x : i32 = @staticEval(1 + 2 + 3);
assert(x == @staticEval(6));
}
fn slicing() {
@setFnTest(this);
var array : [20]i32 = undefined;
array[5] = 1234;
var slice = array[5...10];
if (slice.len != 5) @unreachable();
const ptr = &slice[0];
if (ptr[0] != 1234) @unreachable();
var slice_rest = array[10...];
if (slice_rest.len != 10) @unreachable();
}
fn constantEqualFunctionPointers() {
@setFnTest(this);
const alias = emptyFn;
assert(@staticEval(emptyFn == alias));
}
fn emptyFn() {}
fn hexEscape() {
@setFnTest(this);
assert(memeql("\x68\x65\x6c\x6c\x6f", "hello"));
}
fn stringConcatenation() {
@setFnTest(this);
assert(memeql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
}
fn arrayMultOperator() {
@setFnTest(this);
assert(memeql("ab" ** 5, "ababababab"));
}
fn stringEscapes() {
@setFnTest(this);
assert(memeql("\"", "\x22"));
assert(memeql("\'", "\x27"));
assert(memeql("\n", "\x0a"));
assert(memeql("\r", "\x0d"));
assert(memeql("\t", "\x09"));
assert(memeql("\\", "\x5c"));
assert(memeql("\u1234\u0069", "\xe1\x88\xb4\x69"));
}
// TODO import from std.str

View File

@ -26,6 +26,19 @@ fn nullableType() {
assert(num == 13);
}
fn assignToIfVarPtr() {
@setFnTest(this);
var maybe_bool: ?bool = true;
if (const *b ?= maybe_bool) {
*b = false;
}
assert(??maybe_bool == false);
}
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)

View File

@ -123,6 +123,40 @@ fn callStructField(foo: Foo) -> i32 {
}
fn storeMemberFunctionInVariable() {
@setFnTest(this);
const instance = MemberFnTestFoo { .x = 1234, };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
assert(result == 1234);
}
const MemberFnTestFoo = struct {
x: i32,
fn member(foo: MemberFnTestFoo) -> i32 { foo.x }
};
fn callMemberFunctionDirectly() {
@setFnTest(this);
const instance = MemberFnTestFoo { .x = 1234, };
const result = MemberFnTestFoo.member(instance);
assert(result == 1234);
}
fn memberFunctions() {
@setFnTest(this);
const r = MemberFnRand {.seed = 1234};
assert(r.getSeed() == 1234);
}
const MemberFnRand = struct {
seed: u32,
pub fn getSeed(r: MemberFnRand) -> u32 {
r.seed
}
};
// TODO const assert = @import("std").debug.assert;

View File

@ -31,6 +31,50 @@ fn staticWhileLoop2() -> i32 {
}
}
fn continueAndBreak() {
@setFnTest(this);
runContinueAndBreakTest();
assert(continue_and_break_counter == 8);
}
var continue_and_break_counter: i32 = 0;
fn runContinueAndBreakTest() {
var i : i32 = 0;
while (true) {
continue_and_break_counter += 2;
i += 1;
if (i < 4) {
continue;
}
break;
}
assert(i == 4);
}
fn returnWithImplicitCastFromWhileLoop() {
@setFnTest(this);
%%returnWithImplicitCastFromWhileLoopTest();
}
fn returnWithImplicitCastFromWhileLoopTest() -> %void {
while (true) {
return;
}
}
fn whileWithContinueExpr() {
@setFnTest(this);
var sum: i32 = 0;
{var i: i32 = 0; while (i < 10; i += 1) {
if (i == 5) continue;
sum += i;
}}
assert(sum == 40);
}
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)

View File

@ -107,55 +107,6 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 {
}
fn builtinConstEval() {
@setFnTest(this, true);
const x : i32 = @constEval(1 + 2 + 3);
assert(x == @constEval(6));
}
fn slicing() {
@setFnTest(this, true);
var array : [20]i32 = undefined;
array[5] = 1234;
var slice = array[5...10];
if (slice.len != 5) @unreachable();
const ptr = &slice[0];
if (ptr[0] != 1234) @unreachable();
var slice_rest = array[10...];
if (slice_rest.len != 10) @unreachable();
}
fn memcpyAndMemsetIntrinsics() {
@setFnTest(this, true);
var foo : [20]u8 = undefined;
var bar : [20]u8 = undefined;
@memset(&foo[0], 'A', foo.len);
@memcpy(&bar[0], &foo[0], bar.len);
if (bar[11] != 'A') @unreachable();
}
fn arrayDotLenConstExpr() {
@setFnTest(this, true);
}
struct ArrayDotLenConstExpr {
y: [@constEval(some_array.len)]u8,
}
const some_array = []u8 {0, 1, 2, 3};
fn multilineString() {
@setFnTest(this, true);
@ -182,16 +133,6 @@ fn multilineCString() {
fn constantEqualFunctionPointers() {
@setFnTest(this, true);
const alias = emptyFn;
assert(@constEval(emptyFn == alias));
}
fn emptyFn() {}
fn genericMallocFree() {
@setFnTest(this, true);
@ -207,32 +148,6 @@ fn memAlloc(inline T: type, n: usize) -> %[]T {
fn memFree(inline T: type, mem: []T) { }
fn callFnWithEmptyString() {
@setFnTest(this, true);
acceptsString("");
}
fn acceptsString(foo: []u8) { }
fn hexEscape() {
@setFnTest(this, true);
assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello"));
}
error AnError;
error ALongerErrorName;
fn errorNameString() {
@setFnTest(this, true);
assert(str.eql(@errorName(error.AnError), "AnError"));
assert(str.eql(@errorName(error.ALongerErrorName), "ALongerErrorName"));
}
fn castUndefined() {
@setFnTest(this, true);
@ -264,90 +179,6 @@ fn outer() -> i64 {
}
fn elseIfExpression() {
@setFnTest(this, true);
assert(elseIfExpressionF(1) == 1);
}
fn elseIfExpressionF(c: u8) -> u8 {
if (c == 0) {
0
} else if (c == 1) {
1
} else {
2
}
}
fn errBinaryOperator() {
@setFnTest(this, true);
const a = errBinaryOperatorG(true) %% 3;
const b = errBinaryOperatorG(false) %% 3;
assert(a == 3);
assert(b == 10);
}
error ItBroke;
fn errBinaryOperatorG(x: bool) -> %isize {
if (x) {
error.ItBroke
} else {
10
}
}
fn unwrapSimpleValueFromError() {
@setFnTest(this, true);
const i = %%unwrapSimpleValueFromErrorDo();
assert(i == 13);
}
fn unwrapSimpleValueFromErrorDo() -> %isize { 13 }
fn storeMemberFunctionInVariable() {
@setFnTest(this, true);
const instance = MemberFnTestFoo { .x = 1234, };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
assert(result == 1234);
}
struct MemberFnTestFoo {
x: i32,
fn member(foo: MemberFnTestFoo) -> i32 { foo.x }
}
fn callMemberFunctionDirectly() {
@setFnTest(this, true);
const instance = MemberFnTestFoo { .x = 1234, };
const result = MemberFnTestFoo.member(instance);
assert(result == 1234);
}
fn memberFunctions() {
@setFnTest(this, true);
const r = MemberFnRand {.seed = 1234};
assert(r.getSeed() == 1234);
}
struct MemberFnRand {
seed: u32,
pub fn getSeed(r: MemberFnRand) -> u32 {
r.seed
}
}
fn staticFunctionEvaluation() {
@setFnTest(this, true);
assert(statically_added_number == 3);
}
const statically_added_number = staticAdd(1, 2);
fn staticAdd(a: i32, b: i32) -> i32 { a + b }
fn staticallyInitalizedList() {
@setFnTest(this, true);
@ -369,17 +200,6 @@ fn makePoint(x: i32, y: i32) -> Point {
}
fn staticEvalRecursive() {
@setFnTest(this, true);
assert(some_data.len == 21);
}
var some_data: [usize(fibbonaci(7))]u8 = undefined;
fn fibbonaci(x: i32) -> i32 {
if (x <= 1) return 1;
return fibbonaci(x - 1) + fibbonaci(x - 2);
}
fn staticEvalListInit() {
@setFnTest(this, true);
@ -407,27 +227,6 @@ fn getFirstByte(inline T: type, mem: []T) -> u8 {
getByte((&u8)(&mem[0]))
}
fn continueAndBreak() {
@setFnTest(this, true);
runContinueAndBreakTest();
assert(continue_and_break_counter == 8);
}
var continue_and_break_counter: i32 = 0;
fn runContinueAndBreakTest() {
var i : i32 = 0;
while (true) {
continue_and_break_counter += 2;
i += 1;
if (i < 4) {
continue;
}
break;
}
assert(i == 4);
}
fn pointerDereferencing() {
@setFnTest(this, true);
@ -471,12 +270,6 @@ fn intToPtrCast() {
assert(z == 13);
}
fn stringConcatenation() {
@setFnTest(this, true);
assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
}
fn constantStructWithNegation() {
@setFnTest(this, true);
@ -496,17 +289,6 @@ const vertices = []Vertex {
};
fn returnWithImplicitCastFromWhileLoop() {
@setFnTest(this, true);
%%returnWithImplicitCastFromWhileLoopTest();
}
fn returnWithImplicitCastFromWhileLoopTest() -> %void {
while (true) {
return;
}
}
fn returnStructByvalFromFunction() {
@setFnTest(this, true);
@ -641,18 +423,6 @@ fn test3_2(f: Test3Foo) {
fn whileWithContinueExpr() {
@setFnTest(this, true);
var sum: i32 = 0;
{var i: i32 = 0; while (i < 10; i += 1) {
if (i == 5) continue;
sum += i;
}}
assert(sum == 40);
}
fn forLoopWithPointerElemVar() {
@setFnTest(this, true);
@ -685,9 +455,6 @@ struct EmptyStruct {
}
fn @"weird function name"() {
@setFnTest(this, true);
}
@ -771,20 +538,6 @@ fn returnsTen() -> %i32 {
10
}
fn boolCmp() {
@setFnTest(this, true);
assert(testBoolCmp(true, false) == false);
}
fn testBoolCmp(a: bool, b: bool) -> bool {
@setFnStaticEval(this, false);
a == b
}
fn takeAddressOfParameter() {
@setFnTest(this, true);
@ -803,24 +556,6 @@ fn testTakeAddressOfParameterNoeval(f: f32) {
}
fn arrayMultOperator() {
@setFnTest(this, true);
assert(str.eql("ab" ** 5, "ababababab"));
}
fn stringEscapes() {
@setFnTest(this, true);
assert(str.eql("\"", "\x22"));
assert(str.eql("\'", "\x27"));
assert(str.eql("\n", "\x0a"));
assert(str.eql("\r", "\x0d"));
assert(str.eql("\t", "\x09"));
assert(str.eql("\\", "\x5c"));
assert(str.eql("\u1234\u0069", "\xe1\x88\xb4\x69"));
}
fn ifVarMaybePointer() {
@setFnTest(this, true);
@ -845,18 +580,6 @@ struct Particle {
d: u64,
}
fn assignToIfVarPtr() {
@setFnTest(this, true);
var maybe_bool: ?bool = true;
if (const *b ?= maybe_bool) {
*b = false;
}
assert(??maybe_bool == false);
}
fn unsignedWrapping() {
@setFnTest(this, true);

View File

@ -2,6 +2,7 @@
const test_array = @import("cases3/array.zig");
const test_atomics = @import("cases3/atomics.zig");
const test_bool = @import("cases3/bool.zig");
const test_cast= @import("cases3/cast.zig");
const test_defer = @import("cases3/defer.zig");
const test_enum = @import("cases3/enum.zig");
const test_error = @import("cases3/error.zig");