summaryrefslogtreecommitdiff
path: root/test/stringrange.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-04-12 17:01:17 -0700
committerRob Pike <r@golang.org>2009-04-12 17:01:17 -0700
commit54ec719391f1516a8bcbe8e5a8ac6ba2764a646d (patch)
treed32d2a8598ce38c4d0b60ae2b367a3a520d063ac /test/stringrange.go
parent9ddeb2105f6c6b87a15bc020684aa2b8f97678d4 (diff)
downloadgo-git-54ec719391f1516a8bcbe8e5a8ac6ba2764a646d.tar.gz
fix string range to have full unicode range (up to 10FFFF).
add test for string range. test has minor failure: after loop the index == len(s); should be len(s)-1 in this case. according to spec, vars are left at position at last iteration. R=ken,rsc DELTA=259 (161 added, 96 deleted, 2 changed) OCL=27343 CL=27343
Diffstat (limited to 'test/stringrange.go')
-rw-r--r--test/stringrange.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/stringrange.go b/test/stringrange.go
new file mode 100644
index 0000000000..66d25e1f4f
--- /dev/null
+++ b/test/stringrange.go
@@ -0,0 +1,47 @@
+// $G $F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import(
+ "fmt";
+ "utf8";
+)
+
+func main() {
+ s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx";
+ expect := []int{ 0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x' };
+ var rune, size int;
+ offset := 0;
+ var i, c int;
+ ok := true;
+ cnum := 0;
+ for i, c = range s {
+ rune, size := utf8.DecodeRuneInString(s, i); // check it another way
+ if i != offset {
+ fmt.Printf("unexpected offset %d not %d\n", i, offset);
+ ok = false;
+ }
+ if rune != expect[cnum] {
+ fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum]);
+ ok = false;
+ }
+ if c != expect[cnum] {
+ fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum]);
+ ok = false;
+ }
+ offset += size;
+ cnum++;
+ }
+ if i != len(s)-1 {
+ fmt.Println("after loop i is", i, "not", len(s)-1);
+ ok = false;
+ }
+ if !ok {
+ fmt.Println("FAIL");
+ sys.Exit(1)
+ }
+}