initial commits

master
Charles Iliya Krempeaux 2023-10-04 17:37:18 +09:00
parent fce9b28f6d
commit fb416591af
2 changed files with 101 additions and 0 deletions

8
hexdigit.go 100644
View File

@ -0,0 +1,8 @@
package rfc2234
// IsHexDigit returns true if value of 'r' matches 'ALPHA' as defined in IETF RFC-2234:
//
// HEXDIGIT = %30-%39 / %41-%46 / %61-%66 ; 0-9 / A-F / a-f
func IsHexDigit(r rune) bool {
return ('0' <= r && r <= '9') || ('A' <= r && r <= 'F') || ('a' <= r && r <= 'f')
}

93
hexdigit_test.go 100644
View File

@ -0,0 +1,93 @@
package rfc2234_test
import (
"testing"
"sourcecode.social/reiver/go-rfc2234"
)
func TestIsHexDigit(t *testing.T) {
tests := []struct{
Rune rune
Expected bool
}{
}
for r:=rune(0); r < '0'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: false,
})
}
for r:='0'; r <= '9'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: true,
})
}
for r:='9'+1; r < 'A'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: false,
})
}
for r:='A'; r <= 'F'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: true,
})
}
for r:='F'+1; r < 'a'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: false,
})
}
for r:='a'; r <= 'f'; r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: true,
})
}
for r:='f'+1; r < rune(1024); r++ {
tests = append(tests, struct{
Rune rune
Expected bool
}{
Rune: r,
Expected: false,
})
}
for testNumber, test := range tests {
actual := rfc2234.IsHexDigit(test.Rune)
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value for rfc2234.IsHexDigit() is not what was expected.", testNumber)
t.Logf("EXPECTED: %t", expected)
t.Logf("ACTUAL: %t", actual)
t.Logf("RUNE: (%U) %q", test.Rune, string(test.Rune))
continue
}
}
}