diff options
author | Rob Pike <r@golang.org> | 2009-09-01 13:46:59 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-09-01 13:46:59 -0700 |
commit | d504196b95d2f87f4796b32148579955a3f2caf2 (patch) | |
tree | d4b6e571edb2c10b0dbe9341187850fa899a7699 /src/pkg/bytes/bytes_test.go | |
parent | a9fec6c6215fb66ec448c996278026436639baca (diff) | |
download | golang-d504196b95d2f87f4796b32148579955a3f2caf2.tar.gz |
casing operations for byte arrays
R=rsc
DELTA=186 (181 added, 0 deleted, 5 changed)
OCL=34203
CL=34203
Diffstat (limited to 'src/pkg/bytes/bytes_test.go')
-rw-r--r-- | src/pkg/bytes/bytes_test.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index e37767d9a..a7667ec21 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -8,6 +8,7 @@ import ( . "bytes"; "strings"; "testing"; + "unicode"; ) func eq(a, b []string) bool { @@ -163,3 +164,100 @@ func TestCopy(t *testing.T) { } } } + +// Test case for any function which accepts and returns a byte array. +// For ease of creation, we write the byte arrays as strings. +type StringTest struct { + in, out string; +} + +var upperTests = []StringTest { + StringTest{"", ""}, + StringTest{"abc", "ABC"}, + StringTest{"AbC123", "ABC123"}, + StringTest{"azAZ09_", "AZAZ09_"}, + StringTest{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char +} + +var lowerTests = []StringTest { + StringTest{"", ""}, + StringTest{"abc", "abc"}, + StringTest{"AbC123", "abc123"}, + StringTest{"azAZ09_", "azaz09_"}, + StringTest{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char +} + +const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000" + +var trimSpaceTests = []StringTest { + StringTest{"", ""}, + StringTest{"abc", "abc"}, + StringTest{space + "abc" + space, "abc"}, + StringTest{" ", ""}, + StringTest{" \t\r\n \t\t\r\r\n\n ", ""}, + StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"}, + StringTest{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"}, + StringTest{"1 \t\r\n2", "1 \t\r\n2"}, + StringTest{" x\x80", "x\x80"}, // invalid UTF-8 on end + StringTest{" x\xc0", "x\xc0"}, // invalid UTF-8 on end +} + +// Bytes returns a new slice containing the bytes in s. +// Borrowed from strings to avoid dependency. +func Bytes(s string) []byte { + b := make([]byte, len(s)); + for i := 0; i < len(s); i++ { + b[i] = s[i]; + } + return b; +} + +// Execute f on each test case. funcName should be the name of f; it's used +// in failure reports. +func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) { + for i, tc := range testCases { + actual := string(f(Bytes(tc.in))); + if actual != tc.out { + t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out); + } + } +} + +func tenRunes(rune int) string { + r := make([]int, 10); + for i := range r { + r[i] = rune + } + return string(r) +} + +func TestMap(t *testing.T) { + // Run a couple of awful growth/shrinkage tests + a := tenRunes('a'); + // 1. Grow. This triggers two reallocations in Map. + maxRune := func(rune int) int { return unicode.MaxRune }; + m := Map(maxRune, Bytes(a)); + expect := tenRunes(unicode.MaxRune); + if string(m) != expect { + t.Errorf("growing: expected %q got %q", expect, m); + } + // 2. Shrink + minRune := func(rune int) int { return 'a' }; + m = Map(minRune, Bytes(tenRunes(unicode.MaxRune))); + expect = a; + if string(m) != expect { + t.Errorf("shrinking: expected %q got %q", expect, m); + } +} + +func TestToUpper(t *testing.T) { + runStringTests(t, ToUpper, "ToUpper", upperTests); +} + +func TestToLower(t *testing.T) { + runStringTests(t, ToLower, "ToLower", lowerTests); +} + +func TestTrimSpace(t *testing.T) { + runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests); +} |