behavior,cases: add @branchHint test coverage

This commit is contained in:
mlugg
2024-08-24 16:51:05 +01:00
parent 6808ce27bd
commit f47f6d766e
2 changed files with 123 additions and 5 deletions

View File

@@ -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();
}
fn thisIsAColdFn() void {
@branchHint(.cold);
test "if branch hints" {
var t: bool = undefined;
t = true;
if (t) {
@branchHint(.likely);
} else {
@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" {