From 5e3b70c75dd4fc4e8b41058595ca53fb4ba859ef Mon Sep 17 00:00:00 2001 From: Andreas Rohner Date: Wed, 8 Jan 2025 11:38:48 +0100 Subject: [PATCH] Add test cases for `string.format` covering various edge cases 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. --- ext/strings_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/ext/strings_test.go b/ext/strings_test.go index 37bbc352..23a945eb 100644 --- a/ext/strings_test.go +++ b/ext/strings_test.go @@ -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", @@ -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", @@ -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}, }, @@ -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", @@ -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", @@ -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", @@ -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", @@ -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",