zig/test/behavior/bool.zig
Andrew Kelley 6ae0825e7f Sema: implement comptime variables
Sema now properly handles alloc_inferred and alloc_inferred_mut ZIR
instructions inside a comptime execution context. In this case it
creates Decl objects and points to them with the new `decl_ref_mut`
Value Tag. `storePtr` is updated to mutate such Decl types and values.
In this case it destroys the old arena and makes a new one, preventing
memory growth during comptime code execution.

Additionally:

 * Fix `storePtr` to emit a compile error for a pointer comptime-known
   to be undefined.
 * Fix `storePtr` to emit runtime instructions for all the cases that a
   pointer is comptime-known but does not support comptime
   dereferencing, such as `@intToPtr` on a hard-coded address, or an
   extern function.
 * Fix `ret_coerce` not coercing inside inline function call context.
2021-08-01 12:36:04 -07:00

80 lines
1.5 KiB
Zig

const expect = @import("std").testing.expect;
test "bool literals" {
try expect(true);
try expect(!false);
}
test "cast bool to int" {
const t = true;
const f = false;
try expect(@boolToInt(t) == @as(u32, 1));
try expect(@boolToInt(f) == @as(u32, 0));
try nonConstCastBoolToInt(t, f);
}
fn nonConstCastBoolToInt(t: bool, f: bool) !void {
try expect(@boolToInt(t) == @as(u32, 1));
try expect(@boolToInt(f) == @as(u32, 0));
}
test "bool cmp" {
try expect(testBoolCmp(true, false) == false);
}
fn testBoolCmp(a: bool, b: bool) bool {
return a == b;
}
const global_f = false;
const global_t = true;
const not_global_f = !global_f;
const not_global_t = !global_t;
test "compile time bool not" {
try expect(not_global_f);
try expect(!not_global_t);
}
test "short circuit" {
try testShortCircuit(false, true);
comptime try testShortCircuit(false, true);
}
fn testShortCircuit(f: bool, t: bool) !void {
var hit_1 = f;
var hit_2 = f;
var hit_3 = f;
var hit_4 = f;
if (t or x: {
try expect(f);
break :x f;
}) {
hit_1 = t;
}
if (f or x: {
hit_2 = t;
break :x f;
}) {
try expect(f);
}
if (t and x: {
hit_3 = t;
break :x f;
}) {
try expect(f);
}
if (f and x: {
try expect(f);
break :x f;
}) {
try expect(f);
} else {
hit_4 = t;
}
try expect(hit_1);
try expect(hit_2);
try expect(hit_3);
try expect(hit_4);
}