| author | |
| committer | |
| log | 6b2d06710c52c348aa01b62007a2e990b47eee72 |
| tree | fa9b66bc24fa7404250a7795e421be61807e72f6 |
| parent | 09c34352f8a87ed9597b4af0de564aa7d831761a |
16 files changed, 420 insertions(+), 611 deletions(-)
src/codegen.cpp+3-1| ... | @@ -2286,9 +2286,11 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, | ... | @@ -2286,9 +2286,11 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, |
| 2286 | case IrInstructionIdEnumTag: | 2286 | case IrInstructionIdEnumTag: |
| 2287 | return ir_render_enum_tag(g, executable, (IrInstructionEnumTag *)instruction); | 2287 | return ir_render_enum_tag(g, executable, (IrInstructionEnumTag *)instruction); |
| 2288 | case IrInstructionIdSwitchVar: | 2288 | case IrInstructionIdSwitchVar: |
| 2289 | zig_panic("TODO render switch var instruction to LLVM"); | ||
| 2289 | case IrInstructionIdContainerInitList: | 2290 | case IrInstructionIdContainerInitList: |
| 2291 | zig_panic("TODO render container init list instruction to LLVM"); | ||
| 2290 | case IrInstructionIdStructInit: | 2292 | case IrInstructionIdStructInit: |
| 2291 | zig_panic("TODO render more IR instructions to LLVM"); | 2293 | zig_panic("TODO render struct init to LLVM"); |
| 2292 | } | 2294 | } |
| 2293 | zig_unreachable(); | 2295 | zig_unreachable(); |
| 2294 | } | 2296 | } |
test/cases/err_wrapping.zig deleted-13| ... | @@ -1,13 +0,0 @@ | ||
| 1 | pub fn foo() -> %i32 { | ||
| 2 | const x = %return bar(); | ||
| 3 | return x + 1 | ||
| 4 | } | ||
| 5 | |||
| 6 | pub fn bar() -> %i32 { | ||
| 7 | return 13; | ||
| 8 | } | ||
| 9 | |||
| 10 | pub fn baz() -> %i32 { | ||
| 11 | const y = foo() %% 1234; | ||
| 12 | return y + 1; | ||
| 13 | } | ||
test/cases3/atomics.zig created+21| ... | @@ -0,0 +1,21 @@ | ||
| 1 | fn cmpxchg() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | var x: i32 = 1234; | ||
| 5 | while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} | ||
| 6 | assert(x == 5678); | ||
| 7 | } | ||
| 8 | |||
| 9 | fn fence() { | ||
| 10 | @setFnTest(this); | ||
| 11 | |||
| 12 | var x: i32 = 1234; | ||
| 13 | @fence(AtomicOrder.SeqCst); | ||
| 14 | x = 5678; | ||
| 15 | } | ||
| 16 | |||
| 17 | // TODO const assert = @import("std").debug.assert; | ||
| 18 | fn assert(ok: bool) { | ||
| 19 | if (!ok) | ||
| 20 | @unreachable(); | ||
| 21 | } | ||
test/cases3/disabled_export.zig created+8| ... | @@ -0,0 +1,8 @@ | ||
| 1 | export fn disabledExternFn() { | ||
| 2 | @setFnVisible(this, false); | ||
| 3 | } | ||
| 4 | |||
| 5 | fn callDisabledExternFn() { | ||
| 6 | @setFnTest(this); | ||
| 7 | disabledExternFn(); | ||
| 8 | } | ||
test/cases3/empty_fn_with_comments.zig created+6| ... | @@ -0,0 +1,6 @@ | ||
| 1 | // normal comment | ||
| 2 | /// this is a documentation comment | ||
| 3 | /// doc comment line 2 | ||
| 4 | fn emptyFunctionWithComments() { | ||
| 5 | @setFnTest(this); | ||
| 6 | } | ||
test/cases3/error.zig created+25| ... | @@ -0,0 +1,25 @@ | ||
| 1 | pub fn foo() -> %i32 { | ||
| 2 | const x = %return bar(); | ||
| 3 | return x + 1 | ||
| 4 | } | ||
| 5 | |||
| 6 | pub fn bar() -> %i32 { | ||
| 7 | return 13; | ||
| 8 | } | ||
| 9 | |||
| 10 | pub fn baz() -> %i32 { | ||
| 11 | const y = foo() %% 1234; | ||
| 12 | return y + 1; | ||
| 13 | } | ||
| 14 | |||
| 15 | fn errorWrapping() { | ||
| 16 | @setFnTest(this); | ||
| 17 | |||
| 18 | assert(%%baz() == 15); | ||
| 19 | } | ||
| 20 | |||
| 21 | // TODO const assert = @import("std").debug.assert; | ||
| 22 | fn assert(ok: bool) { | ||
| 23 | if (!ok) | ||
| 24 | @unreachable(); | ||
| 25 | } | ||
test/cases3/for.zig created+14| ... | @@ -0,0 +1,14 @@ | ||
| 1 | fn continueInForLoop() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | const array = []i32 {1, 2, 3, 4, 5}; | ||
| 5 | var sum : i32 = 0; | ||
| 6 | for (array) |x| { | ||
| 7 | sum += x; | ||
| 8 | if (x < 3) { | ||
| 9 | continue; | ||
| 10 | } | ||
| 11 | break; | ||
| 12 | } | ||
| 13 | if (sum != 6) @unreachable() | ||
| 14 | } | ||
test/cases3/generics.zig created+49| ... | @@ -0,0 +1,49 @@ | ||
| 1 | fn simpleGenericFn() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | assert(max(i32, 3, -1) == 3); | ||
| 5 | assert(max(f32, 0.123, 0.456) == 0.456); | ||
| 6 | assert(add(2, 3) == 5); | ||
| 7 | } | ||
| 8 | |||
| 9 | fn max(inline T: type, a: T, b: T) -> T { | ||
| 10 | return if (a > b) a else b; | ||
| 11 | } | ||
| 12 | |||
| 13 | fn add(inline a: i32, b: i32) -> i32 { | ||
| 14 | return @staticEval(a) + b; | ||
| 15 | } | ||
| 16 | |||
| 17 | const the_max = max(u32, 1234, 5678); | ||
| 18 | fn compileTimeGenericEval() { | ||
| 19 | @setFnTest(this); | ||
| 20 | assert(the_max == 5678); | ||
| 21 | } | ||
| 22 | |||
| 23 | fn gimmeTheBigOne(a: u32, b: u32) -> u32 { | ||
| 24 | max(u32, a, b) | ||
| 25 | } | ||
| 26 | |||
| 27 | fn shouldCallSameInstance(a: u32, b: u32) -> u32 { | ||
| 28 | max(u32, a, b) | ||
| 29 | } | ||
| 30 | |||
| 31 | fn sameButWithFloats(a: f64, b: f64) -> f64 { | ||
| 32 | max(f64, a, b) | ||
| 33 | } | ||
| 34 | |||
| 35 | fn fnWithInlineArgs() { | ||
| 36 | @setFnTest(this); | ||
| 37 | |||
| 38 | assert(gimmeTheBigOne(1234, 5678) == 5678); | ||
| 39 | assert(shouldCallSameInstance(34, 12) == 34); | ||
| 40 | assert(sameButWithFloats(0.43, 0.49) == 0.49); | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | // TODO const assert = @import("std").debug.assert; | ||
| 45 | fn assert(ok: bool) { | ||
| 46 | if (!ok) | ||
| 47 | @unreachable(); | ||
| 48 | } | ||
| 49 | |||
test/cases3/goto.zig created+45| ... | @@ -0,0 +1,45 @@ | ||
| 1 | fn gotoAndLabels() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | gotoLoop(); | ||
| 5 | assert(goto_counter == 10); | ||
| 6 | } | ||
| 7 | fn gotoLoop() { | ||
| 8 | var i: i32 = 0; | ||
| 9 | goto cond; | ||
| 10 | loop: | ||
| 11 | i += 1; | ||
| 12 | cond: | ||
| 13 | if (!(i < 10)) goto end; | ||
| 14 | goto_counter += 1; | ||
| 15 | goto loop; | ||
| 16 | end: | ||
| 17 | } | ||
| 18 | var goto_counter: i32 = 0; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | fn gotoLeaveDeferScope() { | ||
| 23 | @setFnTest(this); | ||
| 24 | |||
| 25 | testGotoLeaveDeferScope(true); | ||
| 26 | } | ||
| 27 | fn testGotoLeaveDeferScope(b: bool) { | ||
| 28 | var it_worked = false; | ||
| 29 | |||
| 30 | goto entry; | ||
| 31 | exit: | ||
| 32 | if (it_worked) { | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | @unreachable(); | ||
| 36 | entry: | ||
| 37 | defer it_worked = true; | ||
| 38 | if (b) goto exit; | ||
| 39 | } | ||
| 40 | |||
| 41 | // TODO const assert = @import("std").debug.assert; | ||
| 42 | fn assert(ok: bool) { | ||
| 43 | if (!ok) | ||
| 44 | @unreachable(); | ||
| 45 | } | ||
test/cases3/inlined_loop.zig created+16| ... | @@ -0,0 +1,16 @@ | ||
| 1 | |||
| 2 | fn inlinedLoop() { | ||
| 3 | @setFnTest(this); | ||
| 4 | |||
| 5 | inline var i = 0; | ||
| 6 | inline var sum = 0; | ||
| 7 | inline while (i <= 5; i += 1) | ||
| 8 | sum += i; | ||
| 9 | assert(sum == 15); | ||
| 10 | } | ||
| 11 | |||
| 12 | // TODO const assert = @import("std").debug.assert; | ||
| 13 | fn assert(ok: bool) { | ||
| 14 | if (!ok) | ||
| 15 | @unreachable(); | ||
| 16 | } | ||
test/cases3/math.zig created+58| ... | @@ -0,0 +1,58 @@ | ||
| 1 | fn exactDivision() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | assert(divExact(55, 11) == 5); | ||
| 5 | } | ||
| 6 | fn divExact(a: u32, b: u32) -> u32 { | ||
| 7 | @divExact(a, b) | ||
| 8 | } | ||
| 9 | |||
| 10 | fn floatDivision() { | ||
| 11 | @setFnTest(this); | ||
| 12 | |||
| 13 | assert(fdiv32(12.0, 3.0) == 4.0); | ||
| 14 | } | ||
| 15 | fn fdiv32(a: f32, b: f32) -> f32 { | ||
| 16 | a / b | ||
| 17 | } | ||
| 18 | |||
| 19 | fn overflowIntrinsics() { | ||
| 20 | @setFnTest(this); | ||
| 21 | |||
| 22 | var result: u8 = undefined; | ||
| 23 | assert(@addWithOverflow(u8, 250, 100, &result)); | ||
| 24 | assert(!@addWithOverflow(u8, 100, 150, &result)); | ||
| 25 | assert(result == 250); | ||
| 26 | } | ||
| 27 | |||
| 28 | fn shlWithOverflow() { | ||
| 29 | @setFnTest(this); | ||
| 30 | |||
| 31 | var result: u16 = undefined; | ||
| 32 | assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); | ||
| 33 | assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); | ||
| 34 | assert(result == 0b1011111111111100); | ||
| 35 | } | ||
| 36 | |||
| 37 | fn countLeadingZeroes() { | ||
| 38 | @setFnTest(this); | ||
| 39 | |||
| 40 | assert(@clz(u8(0b00001010)) == 4); | ||
| 41 | assert(@clz(u8(0b10001010)) == 0); | ||
| 42 | assert(@clz(u8(0b00000000)) == 8); | ||
| 43 | } | ||
| 44 | |||
| 45 | fn countTrailingZeroes() { | ||
| 46 | @setFnTest(this); | ||
| 47 | |||
| 48 | assert(@ctz(u8(0b10100000)) == 5); | ||
| 49 | assert(@ctz(u8(0b10001010)) == 1); | ||
| 50 | assert(@ctz(u8(0b00000000)) == 8); | ||
| 51 | } | ||
| 52 | |||
| 53 | // TODO const assert = @import("std").debug.assert; | ||
| 54 | fn assert(ok: bool) { | ||
| 55 | if (!ok) | ||
| 56 | @unreachable(); | ||
| 57 | } | ||
| 58 | |||
test/cases3/misc.zig created+111| ... | @@ -0,0 +1,111 @@ | ||
| 1 | fn intTypeBuiltin() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | assert(@intType(true, 8) == i8); | ||
| 5 | assert(@intType(true, 16) == i16); | ||
| 6 | assert(@intType(true, 32) == i32); | ||
| 7 | assert(@intType(true, 64) == i64); | ||
| 8 | |||
| 9 | assert(@intType(false, 8) == u8); | ||
| 10 | assert(@intType(false, 16) == u16); | ||
| 11 | assert(@intType(false, 32) == u32); | ||
| 12 | assert(@intType(false, 64) == u64); | ||
| 13 | |||
| 14 | assert(i8.bit_count == 8); | ||
| 15 | assert(i16.bit_count == 16); | ||
| 16 | assert(i32.bit_count == 32); | ||
| 17 | assert(i64.bit_count == 64); | ||
| 18 | |||
| 19 | assert(i8.is_signed); | ||
| 20 | assert(i16.is_signed); | ||
| 21 | assert(i32.is_signed); | ||
| 22 | assert(i64.is_signed); | ||
| 23 | assert(isize.is_signed); | ||
| 24 | |||
| 25 | assert(!u8.is_signed); | ||
| 26 | assert(!u16.is_signed); | ||
| 27 | assert(!u32.is_signed); | ||
| 28 | assert(!u64.is_signed); | ||
| 29 | assert(!usize.is_signed); | ||
| 30 | } | ||
| 31 | |||
| 32 | fn minValueAndMaxValue() { | ||
| 33 | @setFnTest(this); | ||
| 34 | |||
| 35 | assert(@maxValue(u8) == 255); | ||
| 36 | assert(@maxValue(u16) == 65535); | ||
| 37 | assert(@maxValue(u32) == 4294967295); | ||
| 38 | assert(@maxValue(u64) == 18446744073709551615); | ||
| 39 | |||
| 40 | assert(@maxValue(i8) == 127); | ||
| 41 | assert(@maxValue(i16) == 32767); | ||
| 42 | assert(@maxValue(i32) == 2147483647); | ||
| 43 | assert(@maxValue(i64) == 9223372036854775807); | ||
| 44 | |||
| 45 | assert(@minValue(u8) == 0); | ||
| 46 | assert(@minValue(u16) == 0); | ||
| 47 | assert(@minValue(u32) == 0); | ||
| 48 | assert(@minValue(u64) == 0); | ||
| 49 | |||
| 50 | assert(@minValue(i8) == -128); | ||
| 51 | assert(@minValue(i16) == -32768); | ||
| 52 | assert(@minValue(i32) == -2147483648); | ||
| 53 | assert(@minValue(i64) == -9223372036854775808); | ||
| 54 | } | ||
| 55 | |||
| 56 | fn shortCircuit() { | ||
| 57 | @setFnTest(this); | ||
| 58 | |||
| 59 | var hit_1 = false; | ||
| 60 | var hit_2 = false; | ||
| 61 | var hit_3 = false; | ||
| 62 | var hit_4 = false; | ||
| 63 | |||
| 64 | if (true || {assert(false); false}) { | ||
| 65 | hit_1 = true; | ||
| 66 | } | ||
| 67 | if (false || { hit_2 = true; false }) { | ||
| 68 | assert(false); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (true && { hit_3 = true; false }) { | ||
| 72 | assert(false); | ||
| 73 | } | ||
| 74 | if (false && {assert(false); false}) { | ||
| 75 | assert(false); | ||
| 76 | } else { | ||
| 77 | hit_4 = true; | ||
| 78 | } | ||
| 79 | assert(hit_1); | ||
| 80 | assert(hit_2); | ||
| 81 | assert(hit_3); | ||
| 82 | assert(hit_4); | ||
| 83 | } | ||
| 84 | |||
| 85 | fn truncate() { | ||
| 86 | @setFnTest(this); | ||
| 87 | |||
| 88 | assert(testTruncate(0x10fd) == 0xfd); | ||
| 89 | } | ||
| 90 | fn testTruncate(x: u32) -> u8 { | ||
| 91 | @truncate(u8, x) | ||
| 92 | } | ||
| 93 | |||
| 94 | fn assignToIfVarPtr() { | ||
| 95 | @setFnTest(this); | ||
| 96 | |||
| 97 | var maybe_bool: ?bool = true; | ||
| 98 | |||
| 99 | if (const *b ?= maybe_bool) { | ||
| 100 | *b = false; | ||
| 101 | } | ||
| 102 | |||
| 103 | assert(??maybe_bool == false); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | // TODO const assert = @import("std").debug.assert; | ||
| 108 | fn assert(ok: bool) { | ||
| 109 | if (!ok) | ||
| 110 | @unreachable(); | ||
| 111 | } | ||
test/cases3/switch.zig created+52| ... | @@ -0,0 +1,52 @@ | ||
| 1 | fn switchWithNumbers() { | ||
| 2 | @setFnTest(this); | ||
| 3 | |||
| 4 | testSwitchWithNumbers(13); | ||
| 5 | } | ||
| 6 | |||
| 7 | fn testSwitchWithNumbers(x: u32) { | ||
| 8 | const result = switch (x) { | ||
| 9 | 1, 2, 3, 4 ... 8 => false, | ||
| 10 | 13 => true, | ||
| 11 | else => false, | ||
| 12 | }; | ||
| 13 | assert(result); | ||
| 14 | } | ||
| 15 | |||
| 16 | fn switchWithAllRanges() { | ||
| 17 | @setFnTest(this); | ||
| 18 | |||
| 19 | assert(testSwitchWithAllRanges(50, 3) == 1); | ||
| 20 | assert(testSwitchWithAllRanges(101, 0) == 2); | ||
| 21 | assert(testSwitchWithAllRanges(300, 5) == 3); | ||
| 22 | assert(testSwitchWithAllRanges(301, 6) == 6); | ||
| 23 | } | ||
| 24 | |||
| 25 | fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { | ||
| 26 | switch (x) { | ||
| 27 | 0 ... 100 => 1, | ||
| 28 | 101 ... 200 => 2, | ||
| 29 | 201 ... 300 => 3, | ||
| 30 | else => y, | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | fn inlineSwitch() { | ||
| 35 | @setFnTest(this); | ||
| 36 | |||
| 37 | const x = 3 + 4; | ||
| 38 | const result = inline switch (x) { | ||
| 39 | 3 => 10, | ||
| 40 | 4 => 11, | ||
| 41 | 5, 6 => 12, | ||
| 42 | 7, 8 => 13, | ||
| 43 | else => 14, | ||
| 44 | }; | ||
| 45 | assert(result + 1 == 14); | ||
| 46 | } | ||
| 47 | |||
| 48 | // TODO const assert = @import("std").debug.assert; | ||
| 49 | fn assert(ok: bool) { | ||
| 50 | if (!ok) | ||
| 51 | @unreachable(); | ||
| 52 | } | ||
test/self_hosted.zig-259| ... | @@ -2,7 +2,6 @@ const std = @import("std"); | ... | @@ -2,7 +2,6 @@ const std = @import("std"); |
| 2 | const assert = std.debug.assert; | 2 | const assert = std.debug.assert; |
| 3 | const str = std.str; | 3 | const str = std.str; |
| 4 | const cstr = std.cstr; | 4 | const cstr = std.cstr; |
| 5 | // TODO '_' identifier for unused variable bindings | ||
| 6 | const test_return_type_type = @import("cases/return_type_type.zig"); | 5 | const test_return_type_type = @import("cases/return_type_type.zig"); |
| 7 | const test_zeroes = @import("cases/zeroes.zig"); | 6 | const test_zeroes = @import("cases/zeroes.zig"); |
| 8 | const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); | 7 | const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); |
| ... | @@ -16,12 +15,6 @@ const test_enum_with_members = @import("cases/enum_with_members.zig"); | ... | @@ -16,12 +15,6 @@ const test_enum_with_members = @import("cases/enum_with_members.zig"); |
| 16 | const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); | 15 | const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); |
| 17 | const test_this = @import("cases/this.zig"); | 16 | const test_this = @import("cases/this.zig"); |
| 18 | 17 | ||
| 19 | // normal comment | ||
| 20 | /// this is a documentation comment | ||
| 21 | /// doc comment line 2 | ||
| 22 | fn emptyFunctionWithComments() { | ||
| 23 | @setFnTest(this, true); | ||
| 24 | } | ||
| 25 | 18 | ||
| 26 | 19 | ||
| 27 | 20 | ||
| ... | @@ -129,41 +122,6 @@ fn getArrayLen(a: []u32) -> usize { | ... | @@ -129,41 +122,6 @@ fn getArrayLen(a: []u32) -> usize { |
| 129 | a.len | 122 | a.len |
| 130 | } | 123 | } |
| 131 | 124 | ||
| 132 | fn shortCircuit() { | ||
| 133 | @setFnTest(this, true); | ||
| 134 | |||
| 135 | var hit_1 = false; | ||
| 136 | var hit_2 = false; | ||
| 137 | var hit_3 = false; | ||
| 138 | var hit_4 = false; | ||
| 139 | |||
| 140 | if (true || {assertRuntime(false); false}) { | ||
| 141 | hit_1 = true; | ||
| 142 | } | ||
| 143 | if (false || { hit_2 = true; false }) { | ||
| 144 | assertRuntime(false); | ||
| 145 | } | ||
| 146 | |||
| 147 | if (true && { hit_3 = true; false }) { | ||
| 148 | assertRuntime(false); | ||
| 149 | } | ||
| 150 | if (false && {assertRuntime(false); false}) { | ||
| 151 | assertRuntime(false); | ||
| 152 | } else { | ||
| 153 | hit_4 = true; | ||
| 154 | } | ||
| 155 | assert(hit_1); | ||
| 156 | assert(hit_2); | ||
| 157 | assert(hit_3); | ||
| 158 | assert(hit_4); | ||
| 159 | } | ||
| 160 | |||
| 161 | fn assertRuntime(b: bool) { | ||
| 162 | @setFnStaticEval(this, false); | ||
| 163 | |||
| 164 | if (!b) @unreachable() | ||
| 165 | } | ||
| 166 | |||
| 167 | fn modifyOperators() { | 125 | fn modifyOperators() { |
| 168 | @setFnTest(this, true); | 126 | @setFnTest(this, true); |
| 169 | 127 | ||
| ... | @@ -505,22 +463,6 @@ enum AnEnumWithPayload { | ... | @@ -505,22 +463,6 @@ enum AnEnumWithPayload { |
| 505 | } | 463 | } |
| 506 | 464 | ||
| 507 | 465 | ||
| 508 | fn continueInForLoop() { | ||
| 509 | @setFnTest(this, true); | ||
| 510 | |||
| 511 | const array = []i32 {1, 2, 3, 4, 5}; | ||
| 512 | var sum : i32 = 0; | ||
| 513 | for (array) |x| { | ||
| 514 | sum += x; | ||
| 515 | if (x < 3) { | ||
| 516 | continue; | ||
| 517 | } | ||
| 518 | break; | ||
| 519 | } | ||
| 520 | if (sum != 6) @unreachable() | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | fn castBoolToInt() { | 466 | fn castBoolToInt() { |
| 525 | @setFnTest(this, true); | 467 | @setFnTest(this, true); |
| 526 | 468 | ||
| ... | @@ -727,23 +669,6 @@ struct ArrayDotLenConstExpr { | ... | @@ -727,23 +669,6 @@ struct ArrayDotLenConstExpr { |
| 727 | const some_array = []u8 {0, 1, 2, 3}; | 669 | const some_array = []u8 {0, 1, 2, 3}; |
| 728 | 670 | ||
| 729 | 671 | ||
| 730 | fn countLeadingZeroes() { | ||
| 731 | @setFnTest(this, true); | ||
| 732 | |||
| 733 | assert(@clz(u8, 0b00001010) == 4); | ||
| 734 | assert(@clz(u8, 0b10001010) == 0); | ||
| 735 | assert(@clz(u8, 0b00000000) == 8); | ||
| 736 | } | ||
| 737 | |||
| 738 | fn countTrailingZeroes() { | ||
| 739 | @setFnTest(this, true); | ||
| 740 | |||
| 741 | assert(@ctz(u8, 0b10100000) == 5); | ||
| 742 | assert(@ctz(u8, 0b10001010) == 1); | ||
| 743 | assert(@ctz(u8, 0b00000000) == 8); | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | fn multilineString() { | 672 | fn multilineString() { |
| 748 | @setFnTest(this, true); | 673 | @setFnTest(this, true); |
| 749 | 674 | ||
| ... | @@ -770,23 +695,6 @@ fn multilineCString() { | ... | @@ -770,23 +695,6 @@ fn multilineCString() { |
| 770 | 695 | ||
| 771 | 696 | ||
| 772 | 697 | ||
| 773 | fn simpleGenericFn() { | ||
| 774 | @setFnTest(this, true); | ||
| 775 | |||
| 776 | assert(max(i32, 3, -1) == 3); | ||
| 777 | assert(max(f32, 0.123, 0.456) == 0.456); | ||
| 778 | assert(add(2, 3) == 5); | ||
| 779 | } | ||
| 780 | |||
| 781 | fn max(inline T: type, a: T, b: T) -> T { | ||
| 782 | return if (a > b) a else b; | ||
| 783 | } | ||
| 784 | |||
| 785 | fn add(inline a: i32, b: i32) -> i32 { | ||
| 786 | return @constEval(a) + b; | ||
| 787 | } | ||
| 788 | |||
| 789 | |||
| 790 | fn constantEqualFunctionPointers() { | 698 | fn constantEqualFunctionPointers() { |
| 791 | @setFnTest(this, true); | 699 | @setFnTest(this, true); |
| 792 | 700 | ||
| ... | @@ -838,49 +746,6 @@ fn errorNameString() { | ... | @@ -838,49 +746,6 @@ fn errorNameString() { |
| 838 | } | 746 | } |
| 839 | 747 | ||
| 840 | 748 | ||
| 841 | fn gotoAndLabels() { | ||
| 842 | @setFnTest(this, true); | ||
| 843 | |||
| 844 | gotoLoop(); | ||
| 845 | assert(goto_counter == 10); | ||
| 846 | } | ||
| 847 | fn gotoLoop() { | ||
| 848 | var i: i32 = 0; | ||
| 849 | goto cond; | ||
| 850 | loop: | ||
| 851 | i += 1; | ||
| 852 | cond: | ||
| 853 | if (!(i < 10)) goto end; | ||
| 854 | goto_counter += 1; | ||
| 855 | goto loop; | ||
| 856 | end: | ||
| 857 | } | ||
| 858 | var goto_counter: i32 = 0; | ||
| 859 | |||
| 860 | |||
| 861 | |||
| 862 | fn gotoLeaveDeferScope() { | ||
| 863 | @setFnTest(this, true); | ||
| 864 | |||
| 865 | testGotoLeaveDeferScope(true); | ||
| 866 | } | ||
| 867 | fn testGotoLeaveDeferScope(b: bool) { | ||
| 868 | @setFnStaticEval(this, false); | ||
| 869 | |||
| 870 | var it_worked = false; | ||
| 871 | |||
| 872 | goto entry; | ||
| 873 | exit: | ||
| 874 | if (it_worked) { | ||
| 875 | return; | ||
| 876 | } | ||
| 877 | @unreachable(); | ||
| 878 | entry: | ||
| 879 | defer it_worked = true; | ||
| 880 | if (b) goto exit; | ||
| 881 | } | ||
| 882 | |||
| 883 | |||
| 884 | fn castUndefined() { | 749 | fn castUndefined() { |
| 885 | @setFnTest(this, true); | 750 | @setFnTest(this, true); |
| 886 | 751 | ||
| ... | @@ -1112,40 +977,6 @@ fn constantExpressions() { | ... | @@ -1112,40 +977,6 @@ fn constantExpressions() { |
| 1112 | const array_size : u8 = 20; | 977 | const array_size : u8 = 20; |
| 1113 | 978 | ||
| 1114 | 979 | ||
| 1115 | fn minValueAndMaxValue() { | ||
| 1116 | @setFnTest(this, true); | ||
| 1117 | |||
| 1118 | assert(@maxValue(u8) == 255); | ||
| 1119 | assert(@maxValue(u16) == 65535); | ||
| 1120 | assert(@maxValue(u32) == 4294967295); | ||
| 1121 | assert(@maxValue(u64) == 18446744073709551615); | ||
| 1122 | |||
| 1123 | assert(@maxValue(i8) == 127); | ||
| 1124 | assert(@maxValue(i16) == 32767); | ||
| 1125 | assert(@maxValue(i32) == 2147483647); | ||
| 1126 | assert(@maxValue(i64) == 9223372036854775807); | ||
| 1127 | |||
| 1128 | assert(@minValue(u8) == 0); | ||
| 1129 | assert(@minValue(u16) == 0); | ||
| 1130 | assert(@minValue(u32) == 0); | ||
| 1131 | assert(@minValue(u64) == 0); | ||
| 1132 | |||
| 1133 | assert(@minValue(i8) == -128); | ||
| 1134 | assert(@minValue(i16) == -32768); | ||
| 1135 | assert(@minValue(i32) == -2147483648); | ||
| 1136 | assert(@minValue(i64) == -9223372036854775808); | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | fn overflowIntrinsics() { | ||
| 1140 | @setFnTest(this, true); | ||
| 1141 | |||
| 1142 | var result: u8 = undefined; | ||
| 1143 | assert(@addWithOverflow(u8, 250, 100, &result)); | ||
| 1144 | assert(!@addWithOverflow(u8, 100, 150, &result)); | ||
| 1145 | assert(result == 250); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | |||
| 1149 | fn nestedArrays() { | 980 | fn nestedArrays() { |
| 1150 | @setFnTest(this, true); | 981 | @setFnTest(this, true); |
| 1151 | 982 | ||
| ... | @@ -1554,22 +1385,6 @@ fn assignToIfVarPtr() { | ... | @@ -1554,22 +1385,6 @@ fn assignToIfVarPtr() { |
| 1554 | assert(??maybe_bool == false); | 1385 | assert(??maybe_bool == false); |
| 1555 | } | 1386 | } |
| 1556 | 1387 | ||
| 1557 | fn cmpxchg() { | ||
| 1558 | @setFnTest(this, true); | ||
| 1559 | |||
| 1560 | var x: i32 = 1234; | ||
| 1561 | while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} | ||
| 1562 | assert(x == 5678); | ||
| 1563 | } | ||
| 1564 | |||
| 1565 | fn fence() { | ||
| 1566 | @setFnTest(this, true); | ||
| 1567 | |||
| 1568 | var x: i32 = 1234; | ||
| 1569 | @fence(AtomicOrder.SeqCst); | ||
| 1570 | x = 5678; | ||
| 1571 | } | ||
| 1572 | |||
| 1573 | fn unsignedWrapping() { | 1388 | fn unsignedWrapping() { |
| 1574 | @setFnTest(this, true); | 1389 | @setFnTest(this, true); |
| 1575 | 1390 | ||
| ... | @@ -1648,15 +1463,6 @@ fn testShlWrappingNoeval(x: u16) { | ... | @@ -1648,15 +1463,6 @@ fn testShlWrappingNoeval(x: u16) { |
| 1648 | assert(shifted == 65534); | 1463 | assert(shifted == 65534); |
| 1649 | } | 1464 | } |
| 1650 | 1465 | ||
| 1651 | fn shlWithOverflow() { | ||
| 1652 | @setFnTest(this, true); | ||
| 1653 | |||
| 1654 | var result: u16 = undefined; | ||
| 1655 | assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); | ||
| 1656 | assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); | ||
| 1657 | assert(result == 0b1011111111111100); | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | fn cStringConcatenation() { | 1466 | fn cStringConcatenation() { |
| 1661 | @setFnTest(this, true); | 1467 | @setFnTest(this, true); |
| 1662 | 1468 | ||
| ... | @@ -1709,28 +1515,6 @@ fn castSliceToU8Slice() { | ... | @@ -1709,28 +1515,6 @@ fn castSliceToU8Slice() { |
| 1709 | assert(bytes[11] == @maxValue(u8)); | 1515 | assert(bytes[11] == @maxValue(u8)); |
| 1710 | } | 1516 | } |
| 1711 | 1517 | ||
| 1712 | fn floatDivision() { | ||
| 1713 | @setFnTest(this, true); | ||
| 1714 | |||
| 1715 | assert(fdiv32(12.0, 3.0) == 4.0); | ||
| 1716 | } | ||
| 1717 | fn fdiv32(a: f32, b: f32) -> f32 { | ||
| 1718 | @setFnStaticEval(this, false); | ||
| 1719 | |||
| 1720 | a / b | ||
| 1721 | } | ||
| 1722 | |||
| 1723 | fn exactDivision() { | ||
| 1724 | @setFnTest(this, true); | ||
| 1725 | |||
| 1726 | assert(divExact(55, 11) == 5); | ||
| 1727 | } | ||
| 1728 | fn divExact(a: u32, b: u32) -> u32 { | ||
| 1729 | @setFnStaticEval(this, false); | ||
| 1730 | |||
| 1731 | @divExact(a, b) | ||
| 1732 | } | ||
| 1733 | |||
| 1734 | fn nullLiteralOutsideFunction() { | 1518 | fn nullLiteralOutsideFunction() { |
| 1735 | @setFnTest(this, true); | 1519 | @setFnTest(this, true); |
| 1736 | 1520 | ||
| ... | @@ -1744,17 +1528,6 @@ const here_is_a_null_literal = SillyStruct { | ... | @@ -1744,17 +1528,6 @@ const here_is_a_null_literal = SillyStruct { |
| 1744 | .context = null, | 1528 | .context = null, |
| 1745 | }; | 1529 | }; |
| 1746 | 1530 | ||
| 1747 | fn truncate() { | ||
| 1748 | @setFnTest(this, true); | ||
| 1749 | |||
| 1750 | assert(testTruncate(0x10fd) == 0xfd); | ||
| 1751 | } | ||
| 1752 | fn testTruncate(x: u32) -> u8 { | ||
| 1753 | @setFnStaticEval(this, false); | ||
| 1754 | |||
| 1755 | @truncate(u8, x) | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | fn constDeclsInStruct() { | 1531 | fn constDeclsInStruct() { |
| 1759 | @setFnTest(this, true); | 1532 | @setFnTest(this, true); |
| 1760 | 1533 | ||
| ... | @@ -1794,38 +1567,6 @@ struct DivResult { | ... | @@ -1794,38 +1567,6 @@ struct DivResult { |
| 1794 | remainder: u64, | 1567 | remainder: u64, |
| 1795 | } | 1568 | } |
| 1796 | 1569 | ||
| 1797 | fn intTypeBuiltin() { | ||
| 1798 | @setFnTest(this, true); | ||
| 1799 | |||
| 1800 | assert(@intType(true, 8) == i8); | ||
| 1801 | assert(@intType(true, 16) == i16); | ||
| 1802 | assert(@intType(true, 32) == i32); | ||
| 1803 | assert(@intType(true, 64) == i64); | ||
| 1804 | |||
| 1805 | assert(@intType(false, 8) == u8); | ||
| 1806 | assert(@intType(false, 16) == u16); | ||
| 1807 | assert(@intType(false, 32) == u32); | ||
| 1808 | assert(@intType(false, 64) == u64); | ||
| 1809 | |||
| 1810 | assert(i8.bit_count == 8); | ||
| 1811 | assert(i16.bit_count == 16); | ||
| 1812 | assert(i32.bit_count == 32); | ||
| 1813 | assert(i64.bit_count == 64); | ||
| 1814 | |||
| 1815 | assert(i8.is_signed); | ||
| 1816 | assert(i16.is_signed); | ||
| 1817 | assert(i32.is_signed); | ||
| 1818 | assert(i64.is_signed); | ||
| 1819 | assert(isize.is_signed); | ||
| 1820 | |||
| 1821 | assert(!u8.is_signed); | ||
| 1822 | assert(!u16.is_signed); | ||
| 1823 | assert(!u32.is_signed); | ||
| 1824 | assert(!u64.is_signed); | ||
| 1825 | assert(!usize.is_signed); | ||
| 1826 | |||
| 1827 | } | ||
| 1828 | |||
| 1829 | fn intToEnum() { | 1570 | fn intToEnum() { |
| 1830 | @setFnTest(this, true); | 1571 | @setFnTest(this, true); |
| 1831 | 1572 |
test/self_hosted2.zig-338| ... | @@ -1,91 +1,10 @@ | ... | @@ -1,91 +1,10 @@ |
| 1 | const case_namespace_fn_call = @import("cases/namespace_fn_call.zig"); | 1 | const case_namespace_fn_call = @import("cases/namespace_fn_call.zig"); |
| 2 | const case_err_wrapping = @import("cases/err_wrapping.zig"); | ||
| 3 | 2 | ||
| 4 | pub const SYS_write = 1; | ||
| 5 | pub const SYS_exit = 60; | ||
| 6 | pub const stdout_fileno = 1; | ||
| 7 | |||
| 8 | // normal comment | ||
| 9 | /// this is a documentation comment | ||
| 10 | /// doc comment line 2 | ||
| 11 | fn emptyFunctionWithComments() { | ||
| 12 | } | ||
| 13 | |||
| 14 | export fn disabledExternFn() { | ||
| 15 | @setFnVisible(this, false); | ||
| 16 | } | ||
| 17 | |||
| 18 | fn inlinedLoop() { | ||
| 19 | inline var i = 0; | ||
| 20 | inline var sum = 0; | ||
| 21 | inline while (i <= 5; i += 1) | ||
| 22 | sum += i; | ||
| 23 | assert(sum == 15); | ||
| 24 | } | ||
| 25 | |||
| 26 | fn switchWithNumbers() { | ||
| 27 | testSwitchWithNumbers(13); | ||
| 28 | } | ||
| 29 | |||
| 30 | fn testSwitchWithNumbers(x: u32) { | ||
| 31 | const result = switch (x) { | ||
| 32 | 1, 2, 3, 4 ... 8 => false, | ||
| 33 | 13 => true, | ||
| 34 | else => false, | ||
| 35 | }; | ||
| 36 | assert(result); | ||
| 37 | } | ||
| 38 | |||
| 39 | fn switchWithAllRanges() { | ||
| 40 | assert(testSwitchWithAllRanges(50, 3) == 1); | ||
| 41 | assert(testSwitchWithAllRanges(101, 0) == 2); | ||
| 42 | assert(testSwitchWithAllRanges(300, 5) == 3); | ||
| 43 | assert(testSwitchWithAllRanges(301, 6) == 6); | ||
| 44 | } | ||
| 45 | |||
| 46 | fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { | ||
| 47 | switch (x) { | ||
| 48 | 0 ... 100 => 1, | ||
| 49 | 101 ... 200 => 2, | ||
| 50 | 201 ... 300 => 3, | ||
| 51 | else => y, | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | fn testInlineSwitch() { | ||
| 56 | const x = 3 + 4; | ||
| 57 | const result = inline switch (x) { | ||
| 58 | 3 => 10, | ||
| 59 | 4 => 11, | ||
| 60 | 5, 6 => 12, | ||
| 61 | 7, 8 => 13, | ||
| 62 | else => 14, | ||
| 63 | }; | ||
| 64 | assert(result + 1 == 14); | ||
| 65 | } | ||
| 66 | 3 | ||
| 67 | fn testNamespaceFnCall() { | 4 | fn testNamespaceFnCall() { |
| 68 | assert(case_namespace_fn_call.foo() == 1234); | 5 | assert(case_namespace_fn_call.foo() == 1234); |
| 69 | } | 6 | } |
| 70 | 7 | ||
| 71 | fn gotoAndLabels() { | ||
| 72 | gotoLoop(); | ||
| 73 | assert(goto_counter == 10); | ||
| 74 | } | ||
| 75 | fn gotoLoop() { | ||
| 76 | var i: i32 = 0; | ||
| 77 | goto cond; | ||
| 78 | loop: | ||
| 79 | i += 1; | ||
| 80 | cond: | ||
| 81 | if (!(i < 10)) goto end; | ||
| 82 | goto_counter += 1; | ||
| 83 | goto loop; | ||
| 84 | end: | ||
| 85 | } | ||
| 86 | var goto_counter: i32 = 0; | ||
| 87 | |||
| 88 | |||
| 89 | 8 | ||
| 90 | const FooA = struct { | 9 | const FooA = struct { |
| 91 | fn add(a: i32, b: i32) -> i32 { a + b } | 10 | fn add(a: i32, b: i32) -> i32 { a + b } |
| ... | @@ -112,89 +31,6 @@ fn testCompileTimeFib() { | ... | @@ -112,89 +31,6 @@ fn testCompileTimeFib() { |
| 112 | assert(fib_7 == 13); | 31 | assert(fib_7 == 13); |
| 113 | } | 32 | } |
| 114 | 33 | ||
| 115 | fn max(inline T: type, a: T, b: T) -> T { | ||
| 116 | if (a > b) a else b | ||
| 117 | } | ||
| 118 | const the_max = max(u32, 1234, 5678); | ||
| 119 | |||
| 120 | fn testCompileTimeGenericEval() { | ||
| 121 | assert(the_max == 5678); | ||
| 122 | } | ||
| 123 | |||
| 124 | fn gimmeTheBigOne(a: u32, b: u32) -> u32 { | ||
| 125 | max(u32, a, b) | ||
| 126 | } | ||
| 127 | |||
| 128 | fn shouldCallSameInstance(a: u32, b: u32) -> u32 { | ||
| 129 | max(u32, a, b) | ||
| 130 | } | ||
| 131 | |||
| 132 | fn sameButWithFloats(a: f64, b: f64) -> f64 { | ||
| 133 | max(f64, a, b) | ||
| 134 | } | ||
| 135 | |||
| 136 | fn testFnWithInlineArgs() { | ||
| 137 | assert(gimmeTheBigOne(1234, 5678) == 5678); | ||
| 138 | assert(shouldCallSameInstance(34, 12) == 34); | ||
| 139 | assert(sameButWithFloats(0.43, 0.49) == 0.49); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | fn testContinueInForLoop() { | ||
| 144 | const array = []i32 {1, 2, 3, 4, 5}; | ||
| 145 | var sum : i32 = 0; | ||
| 146 | for (array) |x| { | ||
| 147 | sum += x; | ||
| 148 | if (x < 3) { | ||
| 149 | continue; | ||
| 150 | } | ||
| 151 | break; | ||
| 152 | } | ||
| 153 | assert(sum == 6); | ||
| 154 | } | ||
| 155 | |||
| 156 | fn shortCircuit() { | ||
| 157 | var hit_1 = false; | ||
| 158 | var hit_2 = false; | ||
| 159 | var hit_3 = false; | ||
| 160 | var hit_4 = false; | ||
| 161 | |||
| 162 | if (true || {assert(false); false}) { | ||
| 163 | hit_1 = true; | ||
| 164 | } | ||
| 165 | if (false || { hit_2 = true; false }) { | ||
| 166 | assert(false); | ||
| 167 | } | ||
| 168 | |||
| 169 | if (true && { hit_3 = true; false }) { | ||
| 170 | assert(false); | ||
| 171 | } | ||
| 172 | if (false && {assert(false); false}) { | ||
| 173 | assert(false); | ||
| 174 | } else { | ||
| 175 | hit_4 = true; | ||
| 176 | } | ||
| 177 | assert(hit_1); | ||
| 178 | assert(hit_2); | ||
| 179 | assert(hit_3); | ||
| 180 | assert(hit_4); | ||
| 181 | } | ||
| 182 | |||
| 183 | fn testGotoLeaveDeferScope(b: bool) { | ||
| 184 | var it_worked = false; | ||
| 185 | |||
| 186 | goto entry; | ||
| 187 | exit: | ||
| 188 | if (it_worked) { | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | @unreachable(); | ||
| 192 | entry: | ||
| 193 | defer it_worked = true; | ||
| 194 | if (it_worked) @unreachable(); | ||
| 195 | if (b) goto exit; | ||
| 196 | } | ||
| 197 | |||
| 198 | fn unwrapAndAddOne(blah: ?i32) -> i32 { | 34 | fn unwrapAndAddOne(blah: ?i32) -> i32 { |
| 199 | return ??blah + 1; | 35 | return ??blah + 1; |
| 200 | } | 36 | } |
| ... | @@ -217,28 +53,6 @@ fn testInlineVarsAgain() { | ... | @@ -217,28 +53,6 @@ fn testInlineVarsAgain() { |
| 217 | assert(gimme1or2(false) == 2); | 53 | assert(gimme1or2(false) == 2); |
| 218 | } | 54 | } |
| 219 | 55 | ||
| 220 | fn testMinValueAndMaxValue() { | ||
| 221 | assert(@maxValue(u8) == 255); | ||
| 222 | assert(@maxValue(u16) == 65535); | ||
| 223 | assert(@maxValue(u32) == 4294967295); | ||
| 224 | assert(@maxValue(u64) == 18446744073709551615); | ||
| 225 | |||
| 226 | assert(@maxValue(i8) == 127); | ||
| 227 | assert(@maxValue(i16) == 32767); | ||
| 228 | assert(@maxValue(i32) == 2147483647); | ||
| 229 | assert(@maxValue(i64) == 9223372036854775807); | ||
| 230 | |||
| 231 | assert(@minValue(u8) == 0); | ||
| 232 | assert(@minValue(u16) == 0); | ||
| 233 | assert(@minValue(u32) == 0); | ||
| 234 | assert(@minValue(u64) == 0); | ||
| 235 | |||
| 236 | assert(@minValue(i8) == -128); | ||
| 237 | assert(@minValue(i16) == -32768); | ||
| 238 | assert(@minValue(i32) == -2147483648); | ||
| 239 | assert(@minValue(i64) == -9223372036854775808); | ||
| 240 | } | ||
| 241 | |||
| 242 | fn first4KeysOfHomeRow() -> []const u8 { | 56 | fn first4KeysOfHomeRow() -> []const u8 { |
| 243 | "aoeu" | 57 | "aoeu" |
| 244 | } | 58 | } |
| ... | @@ -268,111 +82,8 @@ fn testErrorName() { | ... | @@ -268,111 +82,8 @@ fn testErrorName() { |
| 268 | assert(memeql(@errorName(error.ItBroke), "ItBroke")); | 82 | assert(memeql(@errorName(error.ItBroke), "ItBroke")); |
| 269 | } | 83 | } |
| 270 | 84 | ||
| 271 | //error One; | ||
| 272 | //fn getAnErrorValue (b: bool) -> %i32 { | ||
| 273 | // const result = if (b) error.One else i32(1234); | ||
| 274 | // return result; | ||
| 275 | //} | ||
| 276 | |||
| 277 | fn cmpxchg() { | ||
| 278 | var x: i32 = 1234; | ||
| 279 | while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} | ||
| 280 | assert(x == 5678); | ||
| 281 | } | ||
| 282 | |||
| 283 | fn fence() { | ||
| 284 | var x: i32 = 1234; | ||
| 285 | @fence(AtomicOrder.SeqCst); | ||
| 286 | x = 5678; | ||
| 287 | } | ||
| 288 | |||
| 289 | fn exactDivision() { | ||
| 290 | assert(divExact(55, 11) == 5); | ||
| 291 | } | ||
| 292 | fn divExact(a: u32, b: u32) -> u32 { | ||
| 293 | @divExact(a, b) | ||
| 294 | } | ||
| 295 | |||
| 296 | fn truncate() { | ||
| 297 | assert(testTruncate(0x10fd) == 0xfd); | ||
| 298 | } | ||
| 299 | fn testTruncate(x: u32) -> u8 { | ||
| 300 | @truncate(u8, x) | ||
| 301 | } | ||
| 302 | |||
| 303 | fn intTypeBuiltin() { | ||
| 304 | assert(@intType(true, 8) == i8); | ||
| 305 | assert(@intType(true, 16) == i16); | ||
| 306 | assert(@intType(true, 32) == i32); | ||
| 307 | assert(@intType(true, 64) == i64); | ||
| 308 | |||
| 309 | assert(@intType(false, 8) == u8); | ||
| 310 | assert(@intType(false, 16) == u16); | ||
| 311 | assert(@intType(false, 32) == u32); | ||
| 312 | assert(@intType(false, 64) == u64); | ||
| 313 | |||
| 314 | assert(i8.bit_count == 8); | ||
| 315 | assert(i16.bit_count == 16); | ||
| 316 | assert(i32.bit_count == 32); | ||
| 317 | assert(i64.bit_count == 64); | ||
| 318 | |||
| 319 | assert(i8.is_signed); | ||
| 320 | assert(i16.is_signed); | ||
| 321 | assert(i32.is_signed); | ||
| 322 | assert(i64.is_signed); | ||
| 323 | assert(isize.is_signed); | ||
| 324 | |||
| 325 | assert(!u8.is_signed); | ||
| 326 | assert(!u16.is_signed); | ||
| 327 | assert(!u32.is_signed); | ||
| 328 | assert(!u64.is_signed); | ||
| 329 | assert(!usize.is_signed); | ||
| 330 | |||
| 331 | } | ||
| 332 | |||
| 333 | fn overflowIntrinsics() { | ||
| 334 | var result: u8 = undefined; | ||
| 335 | assert(@addWithOverflow(u8, 250, 100, &result)); | ||
| 336 | assert(!@addWithOverflow(u8, 100, 150, &result)); | ||
| 337 | assert(result == 250); | ||
| 338 | } | ||
| 339 | |||
| 340 | fn shlWithOverflow() { | ||
| 341 | var result: u16 = undefined; | ||
| 342 | assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); | ||
| 343 | assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); | ||
| 344 | assert(result == 0b1011111111111100); | ||
| 345 | } | ||
| 346 | |||
| 347 | fn assignToIfVarPtr() { | ||
| 348 | |||
| 349 | var maybe_bool: ?bool = true; | ||
| 350 | |||
| 351 | if (const *b ?= maybe_bool) { | ||
| 352 | *b = false; | ||
| 353 | } | ||
| 354 | |||
| 355 | assert(??maybe_bool == false); | ||
| 356 | } | ||
| 357 | |||
| 358 | fn errorWrapping() { | ||
| 359 | assert(%%case_err_wrapping.baz() == 15); | ||
| 360 | } | ||
| 361 | |||
| 362 | fn assert(ok: bool) { | ||
| 363 | if (!ok) | ||
| 364 | @unreachable(); | ||
| 365 | } | ||
| 366 | |||
| 367 | fn runAllTests() { | 85 | fn runAllTests() { |
| 368 | emptyFunctionWithComments(); | ||
| 369 | disabledExternFn(); | ||
| 370 | inlinedLoop(); | ||
| 371 | switchWithNumbers(); | ||
| 372 | switchWithAllRanges(); | ||
| 373 | testInlineSwitch(); | ||
| 374 | testNamespaceFnCall(); | 86 | testNamespaceFnCall(); |
| 375 | gotoAndLabels(); | ||
| 376 | testStructStatic(); | 87 | testStructStatic(); |
| 377 | testStaticFnEval(); | 88 | testStaticFnEval(); |
| 378 | testCompileTimeFib(); | 89 | testCompileTimeFib(); |
| ... | @@ -380,58 +91,9 @@ fn runAllTests() { | ... | @@ -380,58 +91,9 @@ fn runAllTests() { |
| 380 | testFnWithInlineArgs(); | 91 | testFnWithInlineArgs(); |
| 381 | testContinueInForLoop(); | 92 | testContinueInForLoop(); |
| 382 | shortCircuit(); | 93 | shortCircuit(); |
| 383 | testGotoLeaveDeferScope(true); | ||
| 384 | testStaticAddOne(); | 94 | testStaticAddOne(); |
| 385 | testInlineVarsAgain(); | 95 | testInlineVarsAgain(); |
| 386 | testMinValueAndMaxValue(); | 96 | testMinValueAndMaxValue(); |
| 387 | testReturnStringFromFunction(); | 97 | testReturnStringFromFunction(); |
| 388 | testErrorName(); | 98 | testErrorName(); |
| 389 | cmpxchg(); | ||
| 390 | fence(); | ||
| 391 | exactDivision(); | ||
| 392 | truncate(); | ||
| 393 | intTypeBuiltin(); | ||
| 394 | overflowIntrinsics(); | ||
| 395 | shlWithOverflow(); | ||
| 396 | assignToIfVarPtr(); | ||
| 397 | errorWrapping(); | ||
| 398 | } | ||
| 399 | |||
| 400 | export nakedcc fn _start() -> unreachable { | ||
| 401 | myMain(); | ||
| 402 | } | ||
| 403 | |||
| 404 | fn myMain() -> unreachable { | ||
| 405 | runAllTests(); | ||
| 406 | const text = "OK\n"; | ||
| 407 | write(stdout_fileno, &text[0], text.len); | ||
| 408 | exit(0); | ||
| 409 | } | ||
| 410 | |||
| 411 | pub inline fn syscall1(number: usize, arg1: usize) -> usize { | ||
| 412 | asm volatile ("syscall" | ||
| 413 | : [ret] "={rax}" (-> usize) | ||
| 414 | : [number] "{rax}" (number), | ||
| 415 | [arg1] "{rdi}" (arg1) | ||
| 416 | : "rcx", "r11") | ||
| 417 | } | ||
| 418 | |||
| 419 | pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { | ||
| 420 | asm volatile ("syscall" | ||
| 421 | : [ret] "={rax}" (-> usize) | ||
| 422 | : [number] "{rax}" (number), | ||
| 423 | [arg1] "{rdi}" (arg1), | ||
| 424 | [arg2] "{rsi}" (arg2), | ||
| 425 | [arg3] "{rdx}" (arg3) | ||
| 426 | : "rcx", "r11") | ||
| 427 | } | ||
| 428 | |||
| 429 | pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { | ||
| 430 | syscall3(SYS_write, usize(fd), usize(buf), count) | ||
| 431 | } | 99 | } |
| 432 | |||
| 433 | pub fn exit(status: i32) -> unreachable { | ||
| 434 | syscall1(SYS_exit, usize(status)); | ||
| 435 | @unreachable() | ||
| 436 | } | ||
| 437 |
test/self_hosted3.zig created+12| ... | @@ -0,0 +1,12 @@ | ||
| 1 | // TODO '_' identifier for unused variable bindings | ||
| 2 | const test_empty_fn_with_comments = @import("cases3/empty_fn_with_comments.zig"); | ||
| 3 | const test_disabled_export = @import("cases3/disabled_export.zig"); | ||
| 4 | const test_inlined_loop = @import("cases3/inlined_loop.zig"); | ||
| 5 | const test_misc = @import("cases3/misc.zig"); | ||
| 6 | const test_switch = @import("cases3/switch.zig"); | ||
| 7 | const test_error = @import("cases3/error.zig"); | ||
| 8 | const test_goto = @import("cases3/goto.zig"); | ||
| 9 | const test_atomics = @import("cases3/atomics.zig"); | ||
| 10 | const test_for = @import("cases3/for.zig"); | ||
| 11 | const test_math = @import("cases3/math.zig"); | ||
| 12 | const test_generics = @import("cases3/generics.zig"); | ||