From d1077fe542a902a09f86e43f974cc53fab4aa889 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 27 Oct 2023 11:50:51 -0700 Subject: [PATCH] correction --- symbols.go | 29 +++-------- symbols/byte/lowercase.go | 21 ++++++++ symbols/byte/lowercase_test.go | 95 ++++++++++++++++++++++++++++++++++ symbols/byte/uppercase.go | 21 ++++++++ symbols/byte/uppercase_test.go | 95 ++++++++++++++++++++++++++++++++++ 5 files changed, 238 insertions(+), 23 deletions(-) create mode 100644 symbols/byte/lowercase.go create mode 100644 symbols/byte/lowercase_test.go create mode 100644 symbols/byte/uppercase.go create mode 100644 symbols/byte/uppercase_test.go diff --git a/symbols.go b/symbols.go index 9158e4f..b9e3d20 100644 --- a/symbols.go +++ b/symbols.go @@ -1,5 +1,9 @@ package hexadeca +import ( + "sourcecode.social/reiver/go-hexadeca/symbols/byte" +) + // SymbolLowerCase is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using lower-case symbols from ASCII / Unicode UTF-8. // I.e., // @@ -11,20 +15,9 @@ package hexadeca // // If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'f' and and ‘leastSignificant’ would be 'e'. func SymbolLowerCase(value byte) rune { - return rune(symbolLowerCase(value)) + return rune(bytesymbols.LowerCase(value)) } -func symbolLowerCase(value byte) byte { - const table string = "0123456789abcdef" - - var index int = int(value) % len(table) - - var result byte = table[index] - - return result -} - - // SymbolPersian is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using Persian symbols from Unicode UTF-8. // I.e., // @@ -73,15 +66,5 @@ func SymbolPersian(value byte) rune { // // If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'F' and and ‘leastSignificant’ would be 'E'. func SymbolUpperCase(value byte) rune { - return rune(symbolUpperCase(value)) -} - -func symbolUpperCase(value byte) byte { - const table string = "0123456789ABCDEF" - - var index int = int(value) % len(table) - - var result byte = table[index] - - return result + return rune(bytesymbols.UpperCase(value)) } diff --git a/symbols/byte/lowercase.go b/symbols/byte/lowercase.go new file mode 100644 index 0000000..5c59af2 --- /dev/null +++ b/symbols/byte/lowercase.go @@ -0,0 +1,21 @@ +package bytesymbols + +// LowerCase is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using lower-case symbols from ASCII / Unicode UTF-8. +// I.e., +// +// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' +// +// Example usage: +// +// mostSignificiant, leastSignificant := hexadeca.EncodeByte(value, bytesymbols.LowerCase) +// +// If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'f' and and ‘leastSignificant’ would be 'e'. +func LowerCase(value byte) byte { + const table string = "0123456789abcdef" + + var index int = int(value) % len(table) + + var result byte = table[index] + + return result +} diff --git a/symbols/byte/lowercase_test.go b/symbols/byte/lowercase_test.go new file mode 100644 index 0000000..6644ffe --- /dev/null +++ b/symbols/byte/lowercase_test.go @@ -0,0 +1,95 @@ +package bytesymbols_test + +import ( + "testing" + + "sourcecode.social/reiver/go-hexadeca/symbols/byte" +) + +func TestSymbolLowerCase(t *testing.T) { + + tests := []struct{ + Value byte + Expected byte + }{ + { + Value: 0, + Expected: '0', + }, + { + Value: 1, + Expected: '1', + }, + { + Value: 2, + Expected: '2', + }, + { + Value: 3, + Expected: '3', + }, + { + Value: 4, + Expected: '4', + }, + { + Value: 5, + Expected: '5', + }, + { + Value: 6, + Expected: '6', + }, + { + Value: 7, + Expected: '7', + }, + { + Value: 8, + Expected: '8', + }, + { + Value: 9, + Expected: '9', + }, + { + Value: 10, + Expected: 'a', + }, + { + Value: 11, + Expected: 'b', + }, + { + Value: 12, + Expected: 'c', + }, + { + Value: 13, + Expected: 'd', + }, + { + Value: 14, + Expected: 'e', + }, + { + Value: 15, + Expected: 'f', + }, + } + + for testNumber, test := range tests { + + actual := bytesymbols.LowerCase(test.Value) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %q (%d)", expected, expected) + t.Logf("ACTUAL: %q (%d)", actual, actual) + t.Logf("VALUE: %x (%d)", test.Value, test.Value) + continue + } + + } +} diff --git a/symbols/byte/uppercase.go b/symbols/byte/uppercase.go new file mode 100644 index 0000000..ae143ae --- /dev/null +++ b/symbols/byte/uppercase.go @@ -0,0 +1,21 @@ +package bytesymbols + +// UpperCase is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using upper-case symbols from ASCII / Unicode UTF-8. +// I.e., +// +// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +// +// Example usage: +// +// mostSignificiant, leastSignificant := hexadeca.EncodeByte(value, bytesymbols.UpperCase) +// +// If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'f' and and ‘leastSignificant’ would be 'e'. +func UpperCase(value byte) byte { + const table string = "0123456789ABCDEF" + + var index int = int(value) % len(table) + + var result byte = table[index] + + return result +} diff --git a/symbols/byte/uppercase_test.go b/symbols/byte/uppercase_test.go new file mode 100644 index 0000000..8de0bd6 --- /dev/null +++ b/symbols/byte/uppercase_test.go @@ -0,0 +1,95 @@ +package bytesymbols_test + +import ( + "testing" + + "sourcecode.social/reiver/go-hexadeca/symbols/byte" +) + +func TestSymbolUpperCase(t *testing.T) { + + tests := []struct{ + Value byte + Expected byte + }{ + { + Value: 0, + Expected: '0', + }, + { + Value: 1, + Expected: '1', + }, + { + Value: 2, + Expected: '2', + }, + { + Value: 3, + Expected: '3', + }, + { + Value: 4, + Expected: '4', + }, + { + Value: 5, + Expected: '5', + }, + { + Value: 6, + Expected: '6', + }, + { + Value: 7, + Expected: '7', + }, + { + Value: 8, + Expected: '8', + }, + { + Value: 9, + Expected: '9', + }, + { + Value: 10, + Expected: 'A', + }, + { + Value: 11, + Expected: 'B', + }, + { + Value: 12, + Expected: 'C', + }, + { + Value: 13, + Expected: 'D', + }, + { + Value: 14, + Expected: 'E', + }, + { + Value: 15, + Expected: 'F', + }, + } + + for testNumber, test := range tests { + + actual := bytesymbols.UpperCase(test.Value) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %q (%d)", expected, expected) + t.Logf("ACTUAL: %q (%d)", actual, actual) + t.Logf("VALUE: %x (%d)", test.Value, test.Value) + continue + } + + } +}