initail commits

master
Charles Iliya Krempeaux 2023-09-25 03:46:24 +09:00
parent 7befe15b0e
commit 69d362c4b8
2 changed files with 317 additions and 0 deletions

29
isnumeric.go 100644
View File

@ -0,0 +1,29 @@
package jsonint
func isNumeric(data []byte) bool {
if len(data) <= 0 {
return false
}
{
datum := data[0]
if '-' == datum || '+' == datum {
data = data[1:]
if len(data) <= 0 {
return false
}
}
}
// This is safe to do even though it is UTF-8 encoded Unicode.
for _, datum := range data {
if datum < '0' || '9' < datum {
return false
}
}
return true
}

288
isnumeric_test.go 100644
View File

@ -0,0 +1,288 @@
package jsonint
import (
"testing"
)
func TestIsNumeric(t *testing.T) {
tests := []struct{
Value []byte
Expected bool
}{
{
Value: nil,
Expected: false,
},
{
Value: []byte(""),
Expected: false,
},
{
Value: []byte(" "),
Expected: false,
},
{
Value: []byte(" "),
Expected: false,
},
{
Value: []byte(" "),
Expected: false,
},
{
Value: []byte(" "),
Expected: false,
},
{
Value: []byte(" "),
Expected: false,
},
{
Value: []byte("apple"),
Expected: false,
},
{
Value: []byte("banana"),
Expected: false,
},
{
Value: []byte("cherry"),
Expected: false,
},
{
Value: []byte("ONCE"),
Expected: false,
},
{
Value: []byte("TWICE"),
Expected: false,
},
{
Value: []byte("THRICE"),
Expected: false,
},
{
Value: []byte("FOURCE"),
Expected: false,
},
{
Value: []byte("one"),
Expected: false,
},
{
Value: []byte("two"),
Expected: false,
},
{
Value: []byte("three"),
Expected: false,
},
{
Value: []byte("four"),
Expected: false,
},
{
Value: []byte("five"),
Expected: false,
},
{
Value: []byte(`"-255"`),
Expected: false,
},
{
Value: []byte(`"-5"`),
Expected: false,
},
{
Value: []byte(`"-4"`),
Expected: false,
},
{
Value: []byte(`"-3"`),
Expected: false,
},
{
Value: []byte(`"-2"`),
Expected: false,
},
{
Value: []byte(`"-1"`),
Expected: false,
},
{
Value: []byte(`"0"`),
Expected: false,
},
{
Value: []byte(`"1"`),
Expected: false,
},
{
Value: []byte(`"2"`),
Expected: false,
},
{
Value: []byte(`"3"`),
Expected: false,
},
{
Value: []byte(`"4"`),
Expected: false,
},
{
Value: []byte(`"5"`),
Expected: false,
},
{
Value: []byte(`"255"`),
Expected: false,
},
{
Value: []byte("-255"),
Expected: true,
},
{
Value: []byte("-5"),
Expected: true,
},
{
Value: []byte("-4"),
Expected: true,
},
{
Value: []byte("-3"),
Expected: true,
},
{
Value: []byte("-2"),
Expected: true,
},
{
Value: []byte("-1"),
Expected: true,
},
{
Value: []byte("0"),
Expected: true,
},
{
Value: []byte("1"),
Expected: true,
},
{
Value: []byte("2"),
Expected: true,
},
{
Value: []byte("3"),
Expected: true,
},
{
Value: []byte("4"),
Expected: true,
},
{
Value: []byte("5"),
Expected: true,
},
{
Value: []byte("255"),
Expected: true,
},
{
Value: []byte("-0"),
Expected: true,
},
{
Value: []byte("+0"),
Expected: true,
},
{
Value: []byte("+0"),
Expected: true,
},
{
Value: []byte("+1"),
Expected: true,
},
{
Value: []byte("+2"),
Expected: true,
},
{
Value: []byte("+3"),
Expected: true,
},
{
Value: []byte("+4"),
Expected: true,
},
{
Value: []byte("+5"),
Expected: true,
},
{
Value: []byte("+255"),
Expected: true,
},
{
Value: []byte("-"),
Expected: false,
},
{
Value: []byte("+"),
Expected: false,
},
}
for testNumber, test := range tests {
actual := isNumeric(test.Value)
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value return from isNumeric() is not what was expected.", testNumber)
t.Logf("EXPECTED: %t", expected)
t.Logf("ACTUAL: %t", actual)
t.Logf("VALUE: %#v", test.Value)
t.Logf("VALUE: %q", test.Value)
continue
}
}
}