authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-03-05 20:51:19+01:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-03-05 21:21:23+01:00
logeee43a65aec2e2b104ff64cb23488a86437578e4
treefe4f83e9d67298c31edbcb57af9bc3c9ddaaf33e
parentb4ef6fa09d945c6e7d0f8a73e77c4c03ba262fd7

add tests


2 files changed, 47 insertions(+), 5 deletions(-)

lib/std/meta.zig+27-5
...@@ -1096,7 +1096,7 @@ test "sizeof" {...@@ -1096,7 +1096,7 @@ test "sizeof" {
10961096
1097pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };1097pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };
10981098
1099fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) type {1099fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type {
1100 const signed_decimal = [_]type{ c_int, c_long, c_longlong };1100 const signed_decimal = [_]type{ c_int, c_long, c_longlong };
1101 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };1101 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
1102 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };1102 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
...@@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt...@@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt
1111 var pos = mem.indexOfScalar(type, list, SuffixType).?;1111 var pos = mem.indexOfScalar(type, list, SuffixType).?;
11121112
1113 while (pos < list.len) : (pos += 1) {1113 while (pos < list.len) : (pos += 1) {
1114 if (target >= math.minInt(list[pos]) and target <= math.maxInt(list[pos])) {1114 if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) {
1115 return list[pos];1115 return list[pos];
1116 }1116 }
1117 }1117 }
1118 @compileError("Integer literal does not fit in compatible types");1118 @compileError("Integer literal is too large");
1119}1119}
11201120
1121/// Promote the type of an integer literal until it fits as C would.1121/// Promote the type of an integer literal until it fits as C would.
1122/// This is for translate-c and is not intended for general use.1122/// This is for translate-c and is not intended for general use.
1123pub fn promoteIntLiteral(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) PromoteIntLiteralReturnType(SuffixType, target, radix) {1123pub fn promoteIntLiteral(
1124 return target;1124 comptime SuffixType: type,
1125 comptime number: comptime_int,
1126 comptime radix: CIntLiteralRadix,
1127) PromoteIntLiteralReturnType(SuffixType, number, radix) {
1128 return number;
1129}
1130
1131test "promoteIntLiteral" {
1132 const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal);
1133 testing.expectEqual(c_uint, @TypeOf(signed_hex));
1134
1135 if (math.maxInt(c_longlong) == math.maxInt(c_int)) return;
1136
1137 const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal);
1138 const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal);
1139
1140 if (math.maxInt(c_long) > math.maxInt(c_int)) {
1141 testing.expectEqual(c_long, @TypeOf(signed_decimal));
1142 testing.expectEqual(c_ulong, @TypeOf(unsigned));
1143 } else {
1144 testing.expectEqual(c_longlong, @TypeOf(signed_decimal));
1145 testing.expectEqual(c_ulonglong, @TypeOf(unsigned));
1146 }
1125}1147}
11261148
1127/// For a given function type, returns a tuple type which fields will1149/// For a given function type, returns a tuple type which fields will
test/translate_c.zig+20
...@@ -3392,4 +3392,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3392,4 +3392,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3392 \\ unnamed_0: struct_unnamed_2,3392 \\ unnamed_0: struct_unnamed_2,
3393 \\};3393 \\};
3394 });3394 });
3395
3396 cases.add("integer literal promotion",
3397 \\#define GUARANTEED_TO_FIT_1 1024
3398 \\#define GUARANTEED_TO_FIT_2 10241024L
3399 \\#define GUARANTEED_TO_FIT_3 20482048LU
3400 \\#define MAY_NEED_PROMOTION_1 10241024
3401 \\#define MAY_NEED_PROMOTION_2 307230723072L
3402 \\#define MAY_NEED_PROMOTION_3 819281928192LU
3403 \\#define MAY_NEED_PROMOTION_HEX 0x80000000
3404 \\#define MAY_NEED_PROMOTION_OCT 020000000000
3405 , &[_][]const u8{
3406 \\pub const GUARANTEED_TO_FIT_1 = @as(c_int, 1024);
3407 \\pub const GUARANTEED_TO_FIT_2 = @as(c_long, 10241024);
3408 \\pub const GUARANTEED_TO_FIT_3 = @as(c_ulong, 20482048);
3409 \\pub const MAY_NEED_PROMOTION_1 = @import("std").meta.promoteIntLiteral(c_int, 10241024, .decimal);
3410 \\pub const MAY_NEED_PROMOTION_2 = @import("std").meta.promoteIntLiteral(c_long, 307230723072, .decimal);
3411 \\pub const MAY_NEED_PROMOTION_3 = @import("std").meta.promoteIntLiteral(c_ulong, 819281928192, .decimal);
3412 \\pub const MAY_NEED_PROMOTION_HEX = @import("std").meta.promoteIntLiteral(c_int, 0x80000000, .hexadecimal);
3413 \\pub const MAY_NEED_PROMOTION_OCT = @import("std").meta.promoteIntLiteral(c_int, 0o020000000000, .octal);
3414 });
3395}3415}