aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/basic.zig
diff options
context:
space:
mode:
authorMatthew Lugg <mlugg@mlugg.co.uk>2024-08-27 06:10:56 +0100
committerGitHub <noreply@github.com>2024-08-27 06:10:56 +0100
commitd3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee (patch)
treea75bc8abc129a679fce0673165e04fa7283f2823 /test/behavior/basic.zig
parentd9147b91a601ad2442aaa43f4dba2d01b25d803d (diff)
parent4c0f021c2e4270c7392df7250a5d8f2431dcc54f (diff)
downloadzig-d3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee.tar.gz
zig-d3c6f7179c7a6086ab9cdbaed231da9a1f0b4dee.zip
Merge pull request #21214 from mlugg/branch-hint-and-export
Implement `@branchHint` and new `@export` usage
Diffstat (limited to 'test/behavior/basic.zig')
-rw-r--r--test/behavior/basic.zig89
1 files changed, 83 insertions, 6 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 9d34742604..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 {
- @setCold(true);
+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" {
@@ -734,7 +811,7 @@ test "extern variable with non-pointer opaque type" {
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
- @export(var_to_export, .{ .name = "opaque_extern_var" });
+ @export(&var_to_export, .{ .name = "opaque_extern_var" });
try expect(@as(*align(1) u32, @ptrCast(&opaque_extern_var)).* == 42);
}
extern var opaque_extern_var: opaque {};