aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes_example_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-14 21:30:49 +0700
committerShulhan <ms@kilabit.info>2021-10-14 21:30:49 +0700
commit2080593f8856863d8819062eede8ea59036b3299 (patch)
tree1d5b302f754c014506c0bdb97f471d4217fcc26e /lib/bytes/bytes_example_test.go
parent8a0352321439e1fe10cfeeebf94372af9c9f7c04 (diff)
downloadpakakeh.go-2080593f8856863d8819062eede8ea59036b3299.tar.xz
lib/bytes: refactoring AppendXxx functions
Previously, we pass pointer to slice on AppendInt16, AppendInt32, AppendUint16, and AppendUint32 functions. This model of function signature is not a Go idiom. It is written when I am still new to Go.
Diffstat (limited to 'lib/bytes/bytes_example_test.go')
-rw-r--r--lib/bytes/bytes_example_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index dcb0e1ec..c1ef70bb 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -6,8 +6,73 @@ package bytes
import (
"fmt"
+ "math"
)
+func ExampleAppendInt16() {
+ for _, v := range []int16{math.MinInt16, 0xab, 0xabc, math.MaxInt16} {
+ out := AppendInt16([]byte{}, v)
+ fmt.Printf("%6d => %#04x => %#02v\n", v, v, out)
+ }
+ // Output:
+ // -32768 => -0x8000 => []byte{0x80, 0x00}
+ // 171 => 0x00ab => []byte{0x00, 0xab}
+ // 2748 => 0x0abc => []byte{0x0a, 0xbc}
+ // 32767 => 0x7fff => []byte{0x7f, 0xff}
+}
+
+func ExampleAppendInt32() {
+ for _, v := range []int32{math.MinInt32, 0xab, 0xabc, math.MaxInt32} {
+ out := AppendInt32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+ }
+ // Output:
+ // -2147483648 => -0x80000000 => []byte{0x80, 0x0, 0x0, 0x0}
+ // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
+ // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
+ // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
+}
+
+func ExampleAppendUint16() {
+ inputs := []uint16{0, 0xab, 0xabc, math.MaxInt16, math.MaxUint16}
+ for _, v := range inputs {
+ out := AppendUint16([]byte{}, v)
+ fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
+ }
+
+ v := inputs[4] + 1 // MaxUint16 + 1
+ out := AppendUint16([]byte{}, v)
+ fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
+
+ // Output:
+ // 0 => 0x0000 => []byte{0x00, 0x00}
+ // 171 => 0x00ab => []byte{0x00, 0xab}
+ // 2748 => 0x0abc => []byte{0x0a, 0xbc}
+ // 32767 => 0x7fff => []byte{0x7f, 0xff}
+ // 65535 => 0xffff => []byte{0xff, 0xff}
+ // 0 => 0x0000 => []byte{0x00, 0x00}
+}
+
+func ExampleAppendUint32() {
+ inputs := []uint32{0, 0xab, 0xabc, math.MaxInt32, math.MaxUint32}
+ for _, v := range inputs {
+ out := AppendUint32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+ }
+
+ v := inputs[4] + 2 // MaxUint32 + 2
+ out := AppendUint32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+
+ // Output:
+ // 0 => 0x0 => []byte{0x0, 0x0, 0x0, 0x0}
+ // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
+ // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
+ // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
+ // 4294967295 => 0xffffffff => []byte{0xff, 0xff, 0xff, 0xff}
+ // 1 => 0x1 => []byte{0x0, 0x0, 0x0, 0x1}
+}
+
func ExampleCutUntilToken() {
line := []byte(`abc \def ghi`)