aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/basic.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-08-24 16:51:05 +0100
committermlugg <mlugg@mlugg.co.uk>2024-08-27 00:44:35 +0100
commitf47f6d766e236c75af3c26cc9dec07a46725dba0 (patch)
tree5880459f92a544d475e31e9f258d5617b55abe02 /test/behavior/basic.zig
parent6808ce27bdca14d3876ac607c94f75ea054db7b8 (diff)
downloadzig-f47f6d766e236c75af3c26cc9dec07a46725dba0.tar.gz
zig-f47f6d766e236c75af3c26cc9dec07a46725dba0.zip
behavior,cases: add `@branchHint` test coverage
Diffstat (limited to 'test/behavior/basic.zig')
-rw-r--r--test/behavior/basic.zig87
1 files changed, 82 insertions, 5 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index b96ab2fb06..4e5d0d55ab 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -107,13 +107,90 @@ test "non const ptr to aliased type" {
try expect(?*int == ?*i32);
}
-test "cold function" {
- thisIsAColdFn();
- comptime thisIsAColdFn();
+test "function branch hints" {
+ const S = struct {
+ fn none() void {
+ @branchHint(.none);
+ }
+ fn likely() void {
+ @branchHint(.likely);
+ }
+ fn unlikely() void {
+ @branchHint(.unlikely);
+ }
+ fn cold() void {
+ @branchHint(.cold);
+ }
+ fn unpredictable() void {
+ @branchHint(.unpredictable);
+ }
+ };
+ S.none();
+ S.likely();
+ S.unlikely();
+ S.cold();
+ S.unpredictable();
+ comptime S.none();
+ comptime S.likely();
+ comptime S.unlikely();
+ comptime S.cold();
+ comptime S.unpredictable();
+}
+
+test "if branch hints" {
+ var t: bool = undefined;
+ t = true;
+ if (t) {
+ @branchHint(.likely);
+ } else {
+ @branchHint(.cold);
+ }
}
-fn thisIsAColdFn() void {
- @branchHint(.cold);
+test "switch branch hints" {
+ var t: bool = undefined;
+ t = true;
+ switch (t) {
+ true => {
+ @branchHint(.likely);
+ },
+ false => {
+ @branchHint(.cold);
+ },
+ }
+}
+
+test "orelse branch hints" {
+ var x: ?u32 = undefined;
+ x = 123;
+ const val = x orelse val: {
+ @branchHint(.cold);
+ break :val 456;
+ };
+ try expect(val == 123);
+}
+
+test "catch branch hints" {
+ var x: error{Bad}!u32 = undefined;
+ x = 123;
+ const val = x catch val: {
+ @branchHint(.cold);
+ break :val 456;
+ };
+ try expect(val == 123);
+}
+
+test "and/or branch hints" {
+ var t: bool = undefined;
+ t = true;
+ try expect(t or b: {
+ @branchHint(.unlikely);
+ break :b false;
+ });
+ try expect(t and b: {
+ @branchHint(.likely);
+ break :b true;
+ });
}
test "unicode escape in character literal" {