Skip to content

Commit

Permalink
Add test cases for string.format covering various edge cases (#1101)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zeitgeist87 authored Jan 14, 2025
1 parent 628543b commit 43bc483
Showing 1 changed file with 102 additions and 2 deletions.
104 changes: 102 additions & 2 deletions ext/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
{
name: "negative binary formatting clause",
format: "this is -5 in binary: %b",
formatArgs: "-5",
expectedOutput: "this is -5 in binary: -101",
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
{
name: "uint support for binary formatting",
format: "unsigned 64 in binary: %b",
Expand All @@ -561,6 +569,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "negative octal formatting clause",
format: "%o",
formatArgs: "-11",
expectedOutput: "-13",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "uint support for octal formatting clause",
format: "this is an unsigned octal: %o",
Expand All @@ -571,9 +587,9 @@ func TestStringFormat(t *testing.T) {
},
{
name: "lowercase hexadecimal formatting clause",
format: "%x is 20 in hexadecimal",
format: "%x is 30 in hexadecimal",
formatArgs: "30",
expectedOutput: "1e is 20 in hexadecimal",
expectedOutput: "1e is 30 in hexadecimal",
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
Expand All @@ -585,6 +601,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
{
name: "negative hexadecimal formatting clause",
format: "%x is -30 in hexadecimal",
formatArgs: "-30",
expectedOutput: "-1e is -30 in hexadecimal",
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
{
name: "unsigned support for hexadecimal formatting clause",
format: "%X is 6000 in hexadecimal",
Expand Down Expand Up @@ -617,6 +641,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "byte support with hexadecimal formatting clause leading zero",
format: "%x",
formatArgs: `b"\x00\x00byte string\x00"`,
expectedOutput: "00006279746520737472696e6700",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "byte support with uppercase hexadecimal formatting clause",
format: "%X",
Expand Down Expand Up @@ -661,6 +693,42 @@ func TestStringFormat(t *testing.T) {
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
},
{
name: "default precision for string",
format: "%s",
formatArgs: "2.71",
expectedOutput: "2.71",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
},
{
name: "default list precision for string",
format: "%s",
formatArgs: "[2.71]",
expectedOutput: "[2.710000]",
expectedRuntimeCost: 21,
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
locale: "en_US",
},
{
name: "default scientific notation for string",
format: "%s",
formatArgs: "0.000000002",
expectedOutput: "2e-09",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
},
{
name: "default list scientific notation for string",
format: "%s",
formatArgs: "[0.000000002]",
expectedOutput: "[0.000000]",
expectedRuntimeCost: 21,
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
locale: "en_US",
},
{
name: "unicode output for scientific notation",
format: "unescaped unicode: %e, escaped unicode: %e",
Expand Down Expand Up @@ -697,6 +765,30 @@ func TestStringFormat(t *testing.T) {
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
},
{
name: "NaN support for string",
format: "%s",
formatArgs: `double("NaN")`,
expectedOutput: "NaN",
},
{
name: "positive infinity support for string",
format: "%s",
formatArgs: `double("Inf")`,
expectedOutput: "+Inf",
},
{
name: "negative infinity support for string",
format: "%s",
formatArgs: `double("-Inf")`,
expectedOutput: "-Inf",
},
{
name: "infinity list support for string",
format: "%s",
formatArgs: `[double("NaN"),double("+Inf"), double("-Inf")]`,
expectedOutput: `["NaN", "+Inf", "-Inf"]`,
},
{
name: "uint support for decimal clause",
format: "%d",
Expand Down Expand Up @@ -753,6 +845,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 12,
expectedEstimatedCost: checker.CostEstimate{Min: 12, Max: 12},
},
{
name: "small duration support for string",
format: "%s",
formatArgs: `duration("2ns")`,
expectedOutput: "0.000000002s",
expectedRuntimeCost: 12,
expectedEstimatedCost: checker.CostEstimate{Min: 12, Max: 12},
},
{
name: "list support for string",
format: "%s",
Expand Down

0 comments on commit 43bc483

Please sign in to comment.