From d5359ea541f83160e520ef030e33bfc104ec13e2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 5 Mar 2020 15:51:21 +1100 Subject: [PATCH 1/4] std: use testing.expectEqualSlices from tests --- lib/std/fmt.zig | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 3729b7dc87..4c470d9eec 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1165,19 +1165,20 @@ pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: va test "bufPrintInt" { var buffer: [100]u8 = undefined; const buf = buffer[0..]; - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}), "-101111000110000101001110")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}), "-12345678")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}), "-bc614e")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}), "-BC614E")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}), "12345678")); + std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{})); + std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{})); + std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{})); + std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{})); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }), " 666")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }), " 1234")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }), "1234")); + std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{})); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }), "+42")); - std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }), "-42")); + std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 })); + std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 })); + std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 })); + + std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 })); + std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 })); } fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 { From 4f58bfe1a81a385ac9120510b816ff081ab73437 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 5 Mar 2020 15:52:19 +1100 Subject: [PATCH 2/4] std: fix formatting of i1 integers --- lib/std/fmt.zig | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 4c470d9eec..a439ecbdd6 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -943,19 +943,17 @@ fn formatIntSigned( .precision = options.precision, .fill = options.fill, }; - - const uint = std.meta.IntType(false, @TypeOf(value).bit_count); + const bit_count = @typeInfo(@TypeOf(value)).Int.bits; + const Uint = std.meta.IntType(false, bit_count); if (value < 0) { - const minus_sign: u8 = '-'; - try output(context, @as(*const [1]u8, &minus_sign)[0..]); - const new_value = @intCast(uint, -(value + 1)) + 1; + try output(context, "-"); + const new_value = ~@bitCast(Uint, value +% -1); return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); } else if (options.width == null or options.width.? == 0) { - return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output); + return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output); } else { - const plus_sign: u8 = '+'; - try output(context, @as(*const [1]u8, &plus_sign)[0..]); - const new_value = @intCast(uint, value); + try output(context, "+"); + const new_value = @intCast(Uint, value); return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); } } @@ -1166,6 +1164,8 @@ test "bufPrintInt" { var buffer: [100]u8 = undefined; const buf = buffer[0..]; + std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{})); + std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{})); From 488ba1560fb80c1115b3b4794031859c0063ba3e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 5 Mar 2020 15:59:19 +1100 Subject: [PATCH 3/4] std: fix math.absCast on i1 --- lib/std/fmt.zig | 2 +- lib/std/math.zig | 36 ++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index a439ecbdd6..240443bbde 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -947,7 +947,7 @@ fn formatIntSigned( const Uint = std.meta.IntType(false, bit_count); if (value < 0) { try output(context, "-"); - const new_value = ~@bitCast(Uint, value +% -1); + const new_value = math.absCast(value); return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); } else if (options.width == null or options.width.? == 0) { return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output); diff --git a/lib/std/math.zig b/lib/std/math.zig index 9a1143a17d..a411b58fac 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -670,23 +670,35 @@ fn testRem() void { /// Returns the absolute value of the integer parameter. /// Result is an unsigned integer. -pub fn absCast(x: var) t: { - if (@TypeOf(x) == comptime_int) { - break :t comptime_int; - } else { - break :t std.meta.IntType(false, @TypeOf(x).bit_count); +pub fn absCast(x: var) switch(@typeInfo(@TypeOf(x))) { + .ComptimeInt => comptime_int, + .Int => |intInfo| std.meta.IntType(false, intInfo.bits), + else => @compileError("absCast only accepts integers"), } -} { - if (@TypeOf(x) == comptime_int) { - return if (x < 0) -x else x; +{ + switch(@typeInfo(@TypeOf(x))) { + .ComptimeInt => { + if (x < 0) { + return -x; + } else { + return x; + } + }, + .Int => |intInfo| { + const Uint = std.meta.IntType(false, intInfo.bits); + if (x < 0) { + return ~@bitCast(Uint, x +% -1); + } else { + return @intCast(Uint, x); + } + }, + else => unreachable, } - const uint = std.meta.IntType(false, @TypeOf(x).bit_count); - if (x >= 0) return @intCast(uint, x); - - return @intCast(uint, -(x + 1)) + 1; } test "math.absCast" { + testing.expect(absCast(@as(i1, -1)) == 1); + testing.expect(absCast(@as(i32, -999)) == 999); testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32); From e9c3b65bf4c7dc6c5fc07b9bcec195a5fe709db8 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 5 Mar 2020 16:02:26 +1100 Subject: [PATCH 4/4] std: use testing.expectEqual in math.absCast tests --- lib/std/math.zig | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/std/math.zig b/lib/std/math.zig index a411b58fac..ed13e3cd27 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -697,18 +697,11 @@ pub fn absCast(x: var) switch(@typeInfo(@TypeOf(x))) { } test "math.absCast" { - testing.expect(absCast(@as(i1, -1)) == 1); - - testing.expect(absCast(@as(i32, -999)) == 999); - testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32); - - testing.expect(absCast(@as(i32, 999)) == 999); - testing.expect(@TypeOf(absCast(@as(i32, 999))) == u32); - - testing.expect(absCast(@as(i32, minInt(i32))) == -minInt(i32)); - testing.expect(@TypeOf(absCast(@as(i32, minInt(i32)))) == u32); - - testing.expect(absCast(-999) == 999); + testing.expectEqual(@as(u1, 1), absCast(@as(i1, -1))); + testing.expectEqual(@as(u32, 999), absCast(@as(i32, -999))); + testing.expectEqual(@as(u32, 999), absCast(@as(i32, 999))); + testing.expectEqual(@as(u32, -minInt(i32)), absCast(@as(i32, minInt(i32)))); + testing.expectEqual(999, absCast(-999)); } /// Returns the negation of the integer parameter.