Skip to content

Commit 0e40322

Browse files
author
James Cor
committed
add unit tests for truncate functions
1 parent 7de04eb commit 0e40322

File tree

2 files changed

+205
-2
lines changed

2 files changed

+205
-2
lines changed

sql/types/number.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,19 +1482,21 @@ const (
14821482
// TruncateStringToInt trims any whitespace from s, then truncates the string to the left most characters that make
14831483
// up a valid integer. Empty strings are converted "0". Additionally, returns a flag indicating if truncation occurred.
14841484
func TruncateStringToInt(s string) (string, bool) {
1485+
var seenDigit bool
14851486
s = strings.Trim(s, IntCutSet)
14861487
i, n := 0, len(s)
14871488
for ; i < n; i++ {
14881489
c := rune(s[i])
14891490
if unicode.IsDigit(c) {
1491+
seenDigit = true
14901492
continue
14911493
}
14921494
if i == 0 && (c == '-' || c == '+') {
14931495
continue
14941496
}
14951497
break
14961498
}
1497-
if i == 0 {
1499+
if !seenDigit {
14981500
return "0", i != n
14991501
}
15001502
return s[:i], i != n
@@ -1527,7 +1529,7 @@ func TruncateStringToDouble(s string) (string, bool) {
15271529
}
15281530
break
15291531
}
1530-
if i == 0 {
1532+
if !seenDigit {
15311533
return "0", i != n
15321534
}
15331535
return s[:i], i != n

sql/types/number_test.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,204 @@ func TestNumberString(t *testing.T) {
369369
})
370370
}
371371
}
372+
373+
func TestTruncateStringToInt(t *testing.T) {
374+
tests := []struct {
375+
input string
376+
exp string
377+
expTrunc bool
378+
}{
379+
{
380+
input: "1",
381+
exp: "1",
382+
expTrunc: false,
383+
},
384+
{
385+
// Whitespace does not count as truncation
386+
input: " \t 1 \t ",
387+
exp: "1",
388+
expTrunc: false,
389+
},
390+
{
391+
// Newlines do count as part of truncation
392+
input: " \t\n1",
393+
exp: "0",
394+
expTrunc: true,
395+
},
396+
{
397+
input: "123abc",
398+
exp: "123",
399+
expTrunc: true,
400+
},
401+
{
402+
input: "abc",
403+
exp: "0",
404+
expTrunc: true,
405+
},
406+
{
407+
// Leading sign is fine
408+
input: "+123",
409+
exp: "+123",
410+
expTrunc: false,
411+
},
412+
{
413+
// Leading sign is fine
414+
input: "-123",
415+
exp: "-123",
416+
expTrunc: false,
417+
},
418+
{
419+
// Repeated signs
420+
input: "+-+-+-123",
421+
exp: "0",
422+
expTrunc: true,
423+
},
424+
{
425+
// Space after sign
426+
input: "+ 123",
427+
exp: "0",
428+
expTrunc: true,
429+
},
430+
{
431+
// Valid float strings are not valid ints
432+
input: "1.23",
433+
exp: "1",
434+
expTrunc: true,
435+
},
436+
{
437+
// Scientific float notation is not valid
438+
input: "123.456e10",
439+
exp: "123",
440+
expTrunc: true,
441+
},
442+
{
443+
// Scientific notation is not valid
444+
input: "123e10",
445+
exp: "123",
446+
expTrunc: true,
447+
},
448+
}
449+
450+
for _, test := range tests {
451+
t.Run(fmt.Sprintf("%v", test.input), func(t *testing.T) {
452+
truncStr, didTrunc := TruncateStringToInt(test.input)
453+
assert.Equal(t, test.exp, truncStr)
454+
assert.Equal(t, test.expTrunc, didTrunc)
455+
})
456+
}
457+
}
458+
459+
func TestTruncateStringToDouble(t *testing.T) {
460+
tests := []struct {
461+
input string
462+
exp string
463+
expTrunc bool
464+
}{
465+
{
466+
input: "1",
467+
exp: "1",
468+
expTrunc: false,
469+
},
470+
{
471+
// Whitespace does not count as truncation
472+
input: " \t\n 1 \t\n ",
473+
exp: "1",
474+
expTrunc: false,
475+
},
476+
{
477+
input: "123abc",
478+
exp: "123",
479+
expTrunc: true,
480+
},
481+
{
482+
input: "abc",
483+
exp: "0",
484+
expTrunc: true,
485+
},
486+
{
487+
// Leading sign is fine
488+
input: "+123",
489+
exp: "+123",
490+
expTrunc: false,
491+
},
492+
{
493+
// Leading sign is fine
494+
input: "-123",
495+
exp: "-123",
496+
expTrunc: false,
497+
},
498+
{
499+
// Repeated signs
500+
input: "+-+-+-123",
501+
exp: "0",
502+
expTrunc: true,
503+
},
504+
{
505+
// Space after sign
506+
input: "+ 123",
507+
exp: "0",
508+
expTrunc: true,
509+
},
510+
{
511+
// Valid float strings are not valid ints
512+
input: "1.23",
513+
exp: "1.23",
514+
expTrunc: false,
515+
},
516+
{
517+
// Scientific notation
518+
input: "123.456e10",
519+
exp: "123.456e10",
520+
expTrunc: false,
521+
},
522+
{
523+
// Scientific notation
524+
input: "123e10",
525+
exp: "123e10",
526+
expTrunc: false,
527+
},
528+
{
529+
// Scientific notation
530+
input: "+123.456e-10",
531+
exp: "+123.456e-10",
532+
expTrunc: false,
533+
},
534+
{
535+
// Scientific notation truncates
536+
input: "+123.456e-10notaumber",
537+
exp: "+123.456e-10",
538+
expTrunc: true,
539+
},
540+
{
541+
// Invalid Scientific notation
542+
input: "e123",
543+
exp: "0",
544+
expTrunc: true,
545+
},
546+
{
547+
// Invalid Scientific notation
548+
input: ".e1",
549+
exp: "0",
550+
expTrunc: true,
551+
},
552+
{
553+
// Invalid Scientific notation
554+
input: "1e2e3",
555+
exp: "1e2",
556+
expTrunc: true,
557+
},
558+
{
559+
input: ".0e123",
560+
exp: ".0e123",
561+
expTrunc: false,
562+
},
563+
}
564+
565+
for _, test := range tests {
566+
t.Run(fmt.Sprintf("%v", test.input), func(t *testing.T) {
567+
truncStr, didTrunc := TruncateStringToDouble(test.input)
568+
assert.Equal(t, test.exp, truncStr)
569+
assert.Equal(t, test.expTrunc, didTrunc)
570+
})
571+
}
572+
}

0 commit comments

Comments
 (0)