Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for string.format covering various edge cases #1101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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