diff options
| author | xackus <14938807+xackus@users.noreply.github.com> | 2021-03-05 20:51:19 +0100 |
|---|---|---|
| committer | xackus <14938807+xackus@users.noreply.github.com> | 2021-03-05 21:21:23 +0100 |
| commit | eee43a65aec2e2b104ff64cb23488a86437578e4 (patch) | |
| tree | fe4f83e9d67298c31edbcb57af9bc3c9ddaaf33e /lib | |
| parent | b4ef6fa09d945c6e7d0f8a73e77c4c03ba262fd7 (diff) | |
| download | zig-eee43a65aec2e2b104ff64cb23488a86437578e4.tar.gz zig-eee43a65aec2e2b104ff64cb23488a86437578e4.zip | |
add tests
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/meta.zig | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 46d31a87c3..fd3e03bdbd 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1096,7 +1096,7 @@ test "sizeof" { pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; -fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) type { +fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type { const signed_decimal = [_]type{ c_int, c_long, c_longlong }; const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; @@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt var pos = mem.indexOfScalar(type, list, SuffixType).?; while (pos < list.len) : (pos += 1) { - if (target >= math.minInt(list[pos]) and target <= math.maxInt(list[pos])) { + if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) { return list[pos]; } } - @compileError("Integer literal does not fit in compatible types"); + @compileError("Integer literal is too large"); } /// Promote the type of an integer literal until it fits as C would. /// This is for translate-c and is not intended for general use. -pub fn promoteIntLiteral(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) PromoteIntLiteralReturnType(SuffixType, target, radix) { - return target; +pub fn promoteIntLiteral( + comptime SuffixType: type, + comptime number: comptime_int, + comptime radix: CIntLiteralRadix, +) PromoteIntLiteralReturnType(SuffixType, number, radix) { + return number; +} + +test "promoteIntLiteral" { + const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal); + testing.expectEqual(c_uint, @TypeOf(signed_hex)); + + if (math.maxInt(c_longlong) == math.maxInt(c_int)) return; + + const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal); + const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal); + + if (math.maxInt(c_long) > math.maxInt(c_int)) { + testing.expectEqual(c_long, @TypeOf(signed_decimal)); + testing.expectEqual(c_ulong, @TypeOf(unsigned)); + } else { + testing.expectEqual(c_longlong, @TypeOf(signed_decimal)); + testing.expectEqual(c_ulonglong, @TypeOf(unsigned)); + } } /// For a given function type, returns a tuple type which fields will |
