From fb416591afdaa9cf69d331b44af705a21b64249d Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 4 Oct 2023 17:37:18 +0900 Subject: [PATCH] initial commits --- hexdigit.go | 8 +++++ hexdigit_test.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 hexdigit.go create mode 100644 hexdigit_test.go diff --git a/hexdigit.go b/hexdigit.go new file mode 100644 index 0000000..78218f5 --- /dev/null +++ b/hexdigit.go @@ -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') +} diff --git a/hexdigit_test.go b/hexdigit_test.go new file mode 100644 index 0000000..5dad505 --- /dev/null +++ b/hexdigit_test.go @@ -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 + } + } +}