Skip to content

Commit 43bc483

Browse files
authored
Add test cases for string.format covering various edge cases (#1101)
These test cases cover formatting for negative integers for %b, %o, and %x as well as leading and trailing zeros for %x when the argument is a string. Furthermore it covers how NaN, +Inf, and -Inf are printed. The discrepancy between the fixed point formatting for %s for lists should be fixed in a future commit.
1 parent 628543b commit 43bc483

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

ext/strings_test.go

+102-2
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,14 @@ func TestStringFormat(t *testing.T) {
537537
expectedRuntimeCost: 13,
538538
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
539539
},
540+
{
541+
name: "negative binary formatting clause",
542+
format: "this is -5 in binary: %b",
543+
formatArgs: "-5",
544+
expectedOutput: "this is -5 in binary: -101",
545+
expectedRuntimeCost: 13,
546+
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
547+
},
540548
{
541549
name: "uint support for binary formatting",
542550
format: "unsigned 64 in binary: %b",
@@ -561,6 +569,14 @@ func TestStringFormat(t *testing.T) {
561569
expectedRuntimeCost: 11,
562570
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
563571
},
572+
{
573+
name: "negative octal formatting clause",
574+
format: "%o",
575+
formatArgs: "-11",
576+
expectedOutput: "-13",
577+
expectedRuntimeCost: 11,
578+
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
579+
},
564580
{
565581
name: "uint support for octal formatting clause",
566582
format: "this is an unsigned octal: %o",
@@ -571,9 +587,9 @@ func TestStringFormat(t *testing.T) {
571587
},
572588
{
573589
name: "lowercase hexadecimal formatting clause",
574-
format: "%x is 20 in hexadecimal",
590+
format: "%x is 30 in hexadecimal",
575591
formatArgs: "30",
576-
expectedOutput: "1e is 20 in hexadecimal",
592+
expectedOutput: "1e is 30 in hexadecimal",
577593
expectedRuntimeCost: 13,
578594
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
579595
},
@@ -585,6 +601,14 @@ func TestStringFormat(t *testing.T) {
585601
expectedRuntimeCost: 13,
586602
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
587603
},
604+
{
605+
name: "negative hexadecimal formatting clause",
606+
format: "%x is -30 in hexadecimal",
607+
formatArgs: "-30",
608+
expectedOutput: "-1e is -30 in hexadecimal",
609+
expectedRuntimeCost: 13,
610+
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
611+
},
588612
{
589613
name: "unsigned support for hexadecimal formatting clause",
590614
format: "%X is 6000 in hexadecimal",
@@ -617,6 +641,14 @@ func TestStringFormat(t *testing.T) {
617641
expectedRuntimeCost: 11,
618642
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
619643
},
644+
{
645+
name: "byte support with hexadecimal formatting clause leading zero",
646+
format: "%x",
647+
formatArgs: `b"\x00\x00byte string\x00"`,
648+
expectedOutput: "00006279746520737472696e6700",
649+
expectedRuntimeCost: 11,
650+
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
651+
},
620652
{
621653
name: "byte support with uppercase hexadecimal formatting clause",
622654
format: "%X",
@@ -661,6 +693,42 @@ func TestStringFormat(t *testing.T) {
661693
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
662694
locale: "en_US",
663695
},
696+
{
697+
name: "default precision for string",
698+
format: "%s",
699+
formatArgs: "2.71",
700+
expectedOutput: "2.71",
701+
expectedRuntimeCost: 11,
702+
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
703+
locale: "en_US",
704+
},
705+
{
706+
name: "default list precision for string",
707+
format: "%s",
708+
formatArgs: "[2.71]",
709+
expectedOutput: "[2.710000]",
710+
expectedRuntimeCost: 21,
711+
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
712+
locale: "en_US",
713+
},
714+
{
715+
name: "default scientific notation for string",
716+
format: "%s",
717+
formatArgs: "0.000000002",
718+
expectedOutput: "2e-09",
719+
expectedRuntimeCost: 11,
720+
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
721+
locale: "en_US",
722+
},
723+
{
724+
name: "default list scientific notation for string",
725+
format: "%s",
726+
formatArgs: "[0.000000002]",
727+
expectedOutput: "[0.000000]",
728+
expectedRuntimeCost: 21,
729+
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
730+
locale: "en_US",
731+
},
664732
{
665733
name: "unicode output for scientific notation",
666734
format: "unescaped unicode: %e, escaped unicode: %e",
@@ -697,6 +765,30 @@ func TestStringFormat(t *testing.T) {
697765
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
698766
locale: "en_US",
699767
},
768+
{
769+
name: "NaN support for string",
770+
format: "%s",
771+
formatArgs: `double("NaN")`,
772+
expectedOutput: "NaN",
773+
},
774+
{
775+
name: "positive infinity support for string",
776+
format: "%s",
777+
formatArgs: `double("Inf")`,
778+
expectedOutput: "+Inf",
779+
},
780+
{
781+
name: "negative infinity support for string",
782+
format: "%s",
783+
formatArgs: `double("-Inf")`,
784+
expectedOutput: "-Inf",
785+
},
786+
{
787+
name: "infinity list support for string",
788+
format: "%s",
789+
formatArgs: `[double("NaN"),double("+Inf"), double("-Inf")]`,
790+
expectedOutput: `["NaN", "+Inf", "-Inf"]`,
791+
},
700792
{
701793
name: "uint support for decimal clause",
702794
format: "%d",
@@ -753,6 +845,14 @@ func TestStringFormat(t *testing.T) {
753845
expectedRuntimeCost: 12,
754846
expectedEstimatedCost: checker.CostEstimate{Min: 12, Max: 12},
755847
},
848+
{
849+
name: "small duration support for string",
850+
format: "%s",
851+
formatArgs: `duration("2ns")`,
852+
expectedOutput: "0.000000002s",
853+
expectedRuntimeCost: 12,
854+
expectedEstimatedCost: checker.CostEstimate{Min: 12, Max: 12},
855+
},
756856
{
757857
name: "list support for string",
758858
format: "%s",

0 commit comments

Comments
 (0)