@@ -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 \n 1" ,
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