@@ -57,3 +57,53 @@ func TestSanitizeValueNaN(t *testing.T) {
5757 t .Fatalf ("Value sanitization test for %f fails unexpectedly." , math .NaN ())
5858 }
5959}
60+
61+ func TestSanitizeIntValue (t * testing.T ) {
62+ tests := []struct {
63+ Input string
64+ ExpectedOutput int64
65+ ShouldSucceed bool
66+ }{
67+ // Baseline int64 parsing.
68+ {"1234" , 1234 , true },
69+ {"0" , 0 , true },
70+ {"-1234" , - 1234 , true },
71+ {"9223372036854775807" , 9223372036854775807 , true }, // math.MaxInt64
72+ {"-9223372036854775808" , - 9223372036854775808 , true },
73+
74+ // Rust-style "u64" suffix should be stripped before parsing.
75+ {"1234u64" , 1234 , true },
76+ {"0u64" , 0 , true },
77+ {"9223372036854775807u64" , 9223372036854775807 , true },
78+
79+ // Values exceeding int64 range must still fail even after stripping.
80+ {"9223372036854775808u64" , 0 , false }, // math.MaxInt64 + 1
81+ {"18446744073709551615u64" , 0 , false }, // math.MaxUint64
82+
83+ // Suffix stripping is exact: only trailing "u64", case-sensitive, whole suffix.
84+ {"1234U64" , 0 , false }, // uppercase not stripped
85+ {"1234u32" , 0 , false }, // only u64 handled
86+ {"u641234" , 0 , false }, // suffix must be at end
87+ {"u64" , 0 , false }, // empty after stripping
88+
89+ // Non-numeric and float inputs are not integers.
90+ {"abcd" , 0 , false },
91+ {"1234.5" , 0 , false },
92+ {"1234.5u64" , 0 , false }, // stripped to "1234.5" - still not an int
93+ {"" , 0 , false },
94+ {"true" , 0 , false },
95+ }
96+
97+ for i , test := range tests {
98+ actualOutput , err := SanitizeIntValue (test .Input )
99+ if err != nil && test .ShouldSucceed {
100+ t .Fatalf ("Int value sanitization test %d failed with an unexpected error.\n INPUT:\n %q\n ERR:\n %s" , i , test .Input , err )
101+ }
102+ if err == nil && ! test .ShouldSucceed {
103+ t .Fatalf ("Int value sanitization test %d succeeded unexpectedly.\n INPUT:\n %q\n GOT:\n %d" , i , test .Input , actualOutput )
104+ }
105+ if test .ShouldSucceed && actualOutput != test .ExpectedOutput {
106+ t .Fatalf ("Int value sanitization test %d fails unexpectedly.\n INPUT:\n %q\n GOT:\n %d\n EXPECTED:\n %d" , i , test .Input , actualOutput , test .ExpectedOutput )
107+ }
108+ }
109+ }
0 commit comments