| ... | ... | @@ -1501,9 +1501,9 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { |
| 1501 | 1501 | } |
| 1502 | 1502 | |
| 1503 | 1503 | test parseInt { |
| 1504 | | try std.testing.expect((try parseInt(i32, "-10", 10)) == -10); |
| 1505 | | try std.testing.expect((try parseInt(i32, "+10", 10)) == 10); |
| 1506 | | try std.testing.expect((try parseInt(u32, "+10", 10)) == 10); |
| 1504 | try std.testing.expectEqual(-10, try parseInt(i32, "-10", 10)); |
| 1505 | try std.testing.expectEqual(10, try parseInt(i32, "+10", 10)); |
| 1506 | try std.testing.expectEqual(10, try parseInt(u32, "+10", 10)); |
| 1507 | 1507 | try std.testing.expectError(error.Overflow, parseInt(u32, "-10", 10)); |
| 1508 | 1508 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10)); |
| 1509 | 1509 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10)); |
| ... | ... | @@ -1511,17 +1511,17 @@ test parseInt { |
| 1511 | 1511 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10)); |
| 1512 | 1512 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10)); |
| 1513 | 1513 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10)); |
| 1514 | | try std.testing.expect((try parseInt(u8, "255", 10)) == 255); |
| 1514 | try std.testing.expectEqual(255, try parseInt(u8, "255", 10)); |
| 1515 | 1515 | try std.testing.expectError(error.Overflow, parseInt(u8, "256", 10)); |
| 1516 | 1516 | |
| 1517 | 1517 | // +0 and -0 should work for unsigned |
| 1518 | | try std.testing.expect((try parseInt(u8, "-0", 10)) == 0); |
| 1519 | | try std.testing.expect((try parseInt(u8, "+0", 10)) == 0); |
| 1518 | try std.testing.expectEqual(0, try parseInt(u8, "-0", 10)); |
| 1519 | try std.testing.expectEqual(0, try parseInt(u8, "+0", 10)); |
| 1520 | 1520 | |
| 1521 | 1521 | // ensure minInt is parsed correctly |
| 1522 | | try std.testing.expect((try parseInt(i1, "-1", 10)) == math.minInt(i1)); |
| 1523 | | try std.testing.expect((try parseInt(i8, "-128", 10)) == math.minInt(i8)); |
| 1524 | | try std.testing.expect((try parseInt(i43, "-4398046511104", 10)) == math.minInt(i43)); |
| 1522 | try std.testing.expectEqual(math.minInt(i1), try parseInt(i1, "-1", 10)); |
| 1523 | try std.testing.expectEqual(math.minInt(i8), try parseInt(i8, "-128", 10)); |
| 1524 | try std.testing.expectEqual(math.minInt(i43), try parseInt(i43, "-4398046511104", 10)); |
| 1525 | 1525 | |
| 1526 | 1526 | // empty string or bare +- is invalid |
| 1527 | 1527 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10)); |
| ... | ... | @@ -1532,22 +1532,22 @@ test parseInt { |
| 1532 | 1532 | try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10)); |
| 1533 | 1533 | |
| 1534 | 1534 | // autodectect the base |
| 1535 | | try std.testing.expect((try parseInt(i32, "111", 0)) == 111); |
| 1536 | | try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); |
| 1537 | | try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); |
| 1538 | | try std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7); |
| 1539 | | try std.testing.expect((try parseInt(i32, "+0B111", 0)) == 7); |
| 1540 | | try std.testing.expect((try parseInt(i32, "+0b1_11", 0)) == 7); |
| 1541 | | try std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73); |
| 1542 | | try std.testing.expect((try parseInt(i32, "+0O111", 0)) == 73); |
| 1543 | | try std.testing.expect((try parseInt(i32, "+0o11_1", 0)) == 73); |
| 1544 | | try std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273); |
| 1545 | | try std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7); |
| 1546 | | try std.testing.expect((try parseInt(i32, "-0b11_1", 0)) == -7); |
| 1547 | | try std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73); |
| 1548 | | try std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273); |
| 1549 | | try std.testing.expect((try parseInt(i32, "-0X111", 0)) == -273); |
| 1550 | | try std.testing.expect((try parseInt(i32, "-0x1_11", 0)) == -273); |
| 1535 | try std.testing.expectEqual(111, try parseInt(i32, "111", 0)); |
| 1536 | try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0)); |
| 1537 | try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0)); |
| 1538 | try std.testing.expectEqual(7, try parseInt(i32, "+0b111", 0)); |
| 1539 | try std.testing.expectEqual(7, try parseInt(i32, "+0B111", 0)); |
| 1540 | try std.testing.expectEqual(7, try parseInt(i32, "+0b1_11", 0)); |
| 1541 | try std.testing.expectEqual(73, try parseInt(i32, "+0o111", 0)); |
| 1542 | try std.testing.expectEqual(73, try parseInt(i32, "+0O111", 0)); |
| 1543 | try std.testing.expectEqual(73, try parseInt(i32, "+0o11_1", 0)); |
| 1544 | try std.testing.expectEqual(273, try parseInt(i32, "+0x111", 0)); |
| 1545 | try std.testing.expectEqual(-7, try parseInt(i32, "-0b111", 0)); |
| 1546 | try std.testing.expectEqual(-7, try parseInt(i32, "-0b11_1", 0)); |
| 1547 | try std.testing.expectEqual(-73, try parseInt(i32, "-0o111", 0)); |
| 1548 | try std.testing.expectEqual(-273, try parseInt(i32, "-0x111", 0)); |
| 1549 | try std.testing.expectEqual(-273, try parseInt(i32, "-0X111", 0)); |
| 1550 | try std.testing.expectEqual(-273, try parseInt(i32, "-0x1_11", 0)); |
| 1551 | 1551 | |
| 1552 | 1552 | // bare binary/octal/decimal prefix is invalid |
| 1553 | 1553 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0)); |
| ... | ... | @@ -1643,31 +1643,31 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError! |
| 1643 | 1643 | } |
| 1644 | 1644 | |
| 1645 | 1645 | test parseUnsigned { |
| 1646 | | try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124); |
| 1647 | | try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535); |
| 1648 | | try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535); |
| 1646 | try std.testing.expectEqual(50124, try parseUnsigned(u16, "050124", 10)); |
| 1647 | try std.testing.expectEqual(65535, try parseUnsigned(u16, "65535", 10)); |
| 1648 | try std.testing.expectEqual(65535, try parseUnsigned(u16, "65_535", 10)); |
| 1649 | 1649 | try std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10)); |
| 1650 | 1650 | |
| 1651 | | try std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); |
| 1652 | | try std.testing.expect((try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16)) == 0xffffffffffffffff); |
| 1651 | try std.testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0ffffffffffffffff", 16)); |
| 1652 | try std.testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16)); |
| 1653 | 1653 | try std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16)); |
| 1654 | 1654 | |
| 1655 | | try std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); |
| 1655 | try std.testing.expectEqual(0xDEADBEEF, try parseUnsigned(u32, "DeadBeef", 16)); |
| 1656 | 1656 | |
| 1657 | | try std.testing.expect((try parseUnsigned(u7, "1", 10)) == 1); |
| 1658 | | try std.testing.expect((try parseUnsigned(u7, "1000", 2)) == 8); |
| 1657 | try std.testing.expectEqual(1, try parseUnsigned(u7, "1", 10)); |
| 1658 | try std.testing.expectEqual(8, try parseUnsigned(u7, "1000", 2)); |
| 1659 | 1659 | |
| 1660 | 1660 | try std.testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10)); |
| 1661 | 1661 | try std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8)); |
| 1662 | 1662 | |
| 1663 | | try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); |
| 1663 | try std.testing.expectEqual(1442151747, try parseUnsigned(u32, "NUMBER", 36)); |
| 1664 | 1664 | |
| 1665 | 1665 | // these numbers should fit even though the base itself doesn't fit in the destination type |
| 1666 | | try std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0); |
| 1667 | | try std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1); |
| 1666 | try std.testing.expectEqual(0, try parseUnsigned(u1, "0", 10)); |
| 1667 | try std.testing.expectEqual(1, try parseUnsigned(u1, "1", 10)); |
| 1668 | 1668 | try std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10)); |
| 1669 | | try std.testing.expect((try parseUnsigned(u1, "001", 16)) == 1); |
| 1670 | | try std.testing.expect((try parseUnsigned(u2, "3", 16)) == 3); |
| 1669 | try std.testing.expectEqual(1, try parseUnsigned(u1, "001", 16)); |
| 1670 | try std.testing.expectEqual(3, try parseUnsigned(u2, "3", 16)); |
| 1671 | 1671 | try std.testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16)); |
| 1672 | 1672 | |
| 1673 | 1673 | // parseUnsigned does not expect a sign |
| ... | ... | @@ -1717,15 +1717,15 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize { |
| 1717 | 1717 | } |
| 1718 | 1718 | |
| 1719 | 1719 | test parseIntSizeSuffix { |
| 1720 | | try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2); |
| 1721 | | try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2); |
| 1722 | | try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000); |
| 1723 | | try std.testing.expect(try parseIntSizeSuffix("2k", 10) == 2000); |
| 1724 | | try std.testing.expect(try parseIntSizeSuffix("2KiB", 10) == 2048); |
| 1725 | | try std.testing.expect(try parseIntSizeSuffix("2Ki", 10) == 2048); |
| 1726 | | try std.testing.expect(try parseIntSizeSuffix("aKiB", 16) == 10240); |
| 1727 | | try std.testing.expect(parseIntSizeSuffix("", 10) == error.InvalidCharacter); |
| 1728 | | try std.testing.expect(parseIntSizeSuffix("2iB", 10) == error.InvalidCharacter); |
| 1720 | try std.testing.expectEqual(2, try parseIntSizeSuffix("2", 10)); |
| 1721 | try std.testing.expectEqual(2, try parseIntSizeSuffix("2B", 10)); |
| 1722 | try std.testing.expectEqual(2000, try parseIntSizeSuffix("2kB", 10)); |
| 1723 | try std.testing.expectEqual(2000, try parseIntSizeSuffix("2k", 10)); |
| 1724 | try std.testing.expectEqual(2048, try parseIntSizeSuffix("2KiB", 10)); |
| 1725 | try std.testing.expectEqual(2048, try parseIntSizeSuffix("2Ki", 10)); |
| 1726 | try std.testing.expectEqual(10240, try parseIntSizeSuffix("aKiB", 16)); |
| 1727 | try std.testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("", 10)); |
| 1728 | try std.testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("2iB", 10)); |
| 1729 | 1729 | } |
| 1730 | 1730 | |
| 1731 | 1731 | pub const parseFloat = @import("fmt/parse_float.zig").parseFloat; |
| ... | ... | @@ -1854,7 +1854,7 @@ test "parse u64 digit too big" { |
| 1854 | 1854 | |
| 1855 | 1855 | test "parse unsigned comptime" { |
| 1856 | 1856 | comptime { |
| 1857 | | try std.testing.expect((try parseUnsigned(usize, "2", 10)) == 2); |
| 1857 | try std.testing.expectEqual(2, try parseUnsigned(usize, "2", 10)); |
| 1858 | 1858 | } |
| 1859 | 1859 | } |
| 1860 | 1860 | |
| ... | ... | @@ -1963,15 +1963,15 @@ test "buffer" { |
| 1963 | 1963 | var buf1: [32]u8 = undefined; |
| 1964 | 1964 | var fbs = std.io.fixedBufferStream(&buf1); |
| 1965 | 1965 | try formatType(1234, "", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth); |
| 1966 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234")); |
| 1966 | try std.testing.expectEqualStrings("1234", fbs.getWritten()); |
| 1967 | 1967 | |
| 1968 | 1968 | fbs.reset(); |
| 1969 | 1969 | try formatType('a', "c", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth); |
| 1970 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "a")); |
| 1970 | try std.testing.expectEqualStrings("a", fbs.getWritten()); |
| 1971 | 1971 | |
| 1972 | 1972 | fbs.reset(); |
| 1973 | 1973 | try formatType(0b1100, "b", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth); |
| 1974 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100")); |
| 1974 | try std.testing.expectEqualStrings("1100", fbs.getWritten()); |
| 1975 | 1975 | } |
| 1976 | 1976 | } |
| 1977 | 1977 | |
| ... | ... | @@ -2372,10 +2372,10 @@ test "union" { |
| 2372 | 2372 | |
| 2373 | 2373 | var buf: [100]u8 = undefined; |
| 2374 | 2374 | const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst}); |
| 2375 | | try std.testing.expect(mem.eql(u8, uu_result[0..18], "fmt.test.union.UU@")); |
| 2375 | try std.testing.expectEqualStrings("fmt.test.union.UU@", uu_result[0..18]); |
| 2376 | 2376 | |
| 2377 | 2377 | const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst}); |
| 2378 | | try std.testing.expect(mem.eql(u8, eu_result[0..18], "fmt.test.union.EU@")); |
| 2378 | try std.testing.expectEqualStrings("fmt.test.union.EU@", eu_result[0..18]); |
| 2379 | 2379 | } |
| 2380 | 2380 | |
| 2381 | 2381 | test "struct.self-referential" { |
| ... | ... | @@ -2476,7 +2476,7 @@ test "formatIntValue with comptime_int" { |
| 2476 | 2476 | var buf: [20]u8 = undefined; |
| 2477 | 2477 | var fbs = std.io.fixedBufferStream(&buf); |
| 2478 | 2478 | try formatIntValue(value, "", FormatOptions{}, fbs.writer()); |
| 2479 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789")); |
| 2479 | try std.testing.expectEqualStrings("123456789123456789", fbs.getWritten()); |
| 2480 | 2480 | } |
| 2481 | 2481 | |
| 2482 | 2482 | test "formatFloatValue with comptime_float" { |
| ... | ... | @@ -2542,19 +2542,19 @@ test "formatType max_depth" { |
| 2542 | 2542 | var buf: [1000]u8 = undefined; |
| 2543 | 2543 | var fbs = std.io.fixedBufferStream(&buf); |
| 2544 | 2544 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 0); |
| 2545 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ ... }")); |
| 2545 | try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ ... }", fbs.getWritten()); |
| 2546 | 2546 | |
| 2547 | 2547 | fbs.reset(); |
| 2548 | 2548 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 1); |
| 2549 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }")); |
| 2549 | try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten()); |
| 2550 | 2550 | |
| 2551 | 2551 | fbs.reset(); |
| 2552 | 2552 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 2); |
| 2553 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }")); |
| 2553 | try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten()); |
| 2554 | 2554 | |
| 2555 | 2555 | fbs.reset(); |
| 2556 | 2556 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 3); |
| 2557 | | try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }")); |
| 2557 | try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten()); |
| 2558 | 2558 | } |
| 2559 | 2559 | |
| 2560 | 2560 | test "positional" { |