zig/test/behavior/if.zig

131 lines
3.3 KiB
Zig
Raw Normal View History

const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
2017-01-05 16:57:48 +08:00
2017-09-10 10:53:32 +08:00
test "if statements" {
2016-12-22 13:20:14 +08:00
shouldBeEqual(1, 1);
firstEqlThird(2, 1, 2);
}
fn shouldBeEqual(a: i32, b: i32) void {
2016-12-22 13:20:14 +08:00
if (a != b) {
unreachable;
2016-12-22 13:20:14 +08:00
} else {
return;
}
}
fn firstEqlThird(a: i32, b: i32, c: i32) void {
2016-12-22 13:20:14 +08:00
if (a == b) {
unreachable;
2016-12-22 13:20:14 +08:00
} else if (b == c) {
unreachable;
2016-12-22 13:20:14 +08:00
} else if (a == c) {
return;
} else {
unreachable;
2016-12-22 13:20:14 +08:00
}
}
2017-09-10 10:53:32 +08:00
test "else if expression" {
try expect(elseIfExpressionF(1) == 1);
2016-12-22 14:42:30 +08:00
}
fn elseIfExpressionF(c: u8) u8 {
2016-12-22 14:42:30 +08:00
if (c == 0) {
return 0;
2016-12-22 14:42:30 +08:00
} else if (c == 1) {
return 1;
2016-12-22 14:42:30 +08:00
} else {
2019-11-07 12:25:57 +08:00
return @as(u8, 2);
2016-12-22 14:42:30 +08:00
}
}
// #2297
var global_with_val: anyerror!u32 = 0;
var global_with_err: anyerror!u32 = error.SomeError;
test "unwrap mutable global var" {
2022-02-22 21:15:09 +08:00
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (global_with_val) |v| {
try expect(v == 0);
2021-06-20 09:10:22 +08:00
} else |_| {
unreachable;
}
if (global_with_err) |_| {
unreachable;
} else |e| {
try expect(e == error.SomeError);
}
}
test "labeled break inside comptime if inside runtime if" {
var answer: i32 = 0;
var c = true;
if (c) {
answer = if (true) blk: {
2019-11-07 12:25:57 +08:00
break :blk @as(i32, 42);
};
}
try expect(answer == 42);
}
test "const result loc, runtime if cond, else unreachable" {
const Num = enum { One, Two };
var t = true;
const x = if (t) Num.Two else unreachable;
try expect(x == .Two);
}
test "if copies its payload" {
2022-02-22 21:15:09 +08:00
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
const S = struct {
fn doTheTest() !void {
var tmp: ?i32 = 10;
if (tmp) |value| {
// Modify the original variable
tmp = null;
try expect(value == 10);
} else unreachable;
}
};
try S.doTheTest();
comptime try S.doTheTest();
}
test "if prongs cast to expected type instead of peer type resolution" {
const S = struct {
fn doTheTest(f: bool) !void {
var x: i32 = 0;
x = if (f) 1 else 2;
try expect(x == 2);
var b = true;
const y: i32 = if (b) 1 else 2;
try expect(y == 1);
}
};
try S.doTheTest(false);
comptime try S.doTheTest(false);
}
test "if peer expressions inferred optional type" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
var self: []const u8 = "abcdef";
var index: usize = 0;
var left_index = (index << 1) + 1;
var right_index = left_index + 1;
var left = if (left_index < self.len) self[left_index] else null;
var right = if (right_index < self.len) self[right_index] else null;
try expect(left_index < self.len);
try expect(right_index < self.len);
try expect(left.? == 98);
try expect(right.? == 99);
}