aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/bytes/bytes_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-11-15 12:07:27 -0800
committerRob Pike <r@golang.org>2009-11-15 12:07:27 -0800
commit27779dd6cb20786b645dcf1a33169f9c51d26b6c (patch)
tree5eb1d990e3bd345c255af44850325fb4065daf57 /src/pkg/bytes/bytes_test.go
parent13ad5d40c49f10a760c907db6dd09425580310f4 (diff)
downloadgo-27779dd6cb20786b645dcf1a33169f9c51d26b6c.tar.xz
fix bug in bytes.Map and add test cases for Map in both strings and bytes packages.
thanks to ulrik.sverdrup for the test case. Fixes #191. R=rsc CC=golang-dev https://golang.org/cl/155056
Diffstat (limited to 'src/pkg/bytes/bytes_test.go')
-rw-r--r--src/pkg/bytes/bytes_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go
index 20d6b25f7c..dddaf5064e 100644
--- a/src/pkg/bytes/bytes_test.go
+++ b/src/pkg/bytes/bytes_test.go
@@ -268,9 +268,22 @@ func tenRunes(rune int) string {
return string(r);
}
+// User-defined self-inverse mapping function
+func rot13(rune int) int {
+ step := 13;
+ if rune >= 'a' && rune <= 'z' {
+ return ((rune - 'a' + step) % 26) + 'a'
+ }
+ if rune >= 'A' && rune <= 'Z' {
+ return ((rune - 'A' + step) % 26) + 'A'
+ }
+ return rune;
+}
+
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));
@@ -278,6 +291,7 @@ func TestMap(t *testing.T) {
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)));
@@ -285,6 +299,20 @@ func TestMap(t *testing.T) {
if string(m) != expect {
t.Errorf("shrinking: expected %q got %q", expect, m)
}
+
+ // 3. Rot13
+ m = Map(rot13, Bytes("a to zed"));
+ expect = "n gb mrq";
+ if string(m) != expect {
+ t.Errorf("rot13: expected %q got %q", expect, m)
+ }
+
+ // 4. Rot13^2
+ m = Map(rot13, Map(rot13, Bytes("a to zed")));
+ expect = "a to zed";
+ if string(m) != expect {
+ t.Errorf("rot13: expected %q got %q", expect, m)
+ }
}
func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }