blob: d5ada2628dfbf8930cc0b61cc593f2f241bb7ea1 [file] [log] [blame]
Rob Pike54ec7192009-04-12 17:01:17 -07001// $G $F.go && $L $F.$A && ./$A.out
2
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
Rob Pike74dd0ab2009-08-17 13:30:22 -07009import (
Rob Pike4f61fc92010-09-04 10:36:13 +100010 "fmt"
11 "os"
12 "utf8"
Rob Pike54ec7192009-04-12 17:01:17 -070013)
14
15func main() {
Rob Pike4f61fc92010-09-04 10:36:13 +100016 s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
17 expect := []int{ 0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x' }
18 offset := 0
19 var i, c int
20 ok := true
21 cnum := 0
Rob Pike54ec7192009-04-12 17:01:17 -070022 for i, c = range s {
Rob Pike4f61fc92010-09-04 10:36:13 +100023 rune, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
Rob Pike54ec7192009-04-12 17:01:17 -070024 if i != offset {
Rob Pike4f61fc92010-09-04 10:36:13 +100025 fmt.Printf("unexpected offset %d not %d\n", i, offset)
26 ok = false
Rob Pike54ec7192009-04-12 17:01:17 -070027 }
28 if rune != expect[cnum] {
Rob Pike4f61fc92010-09-04 10:36:13 +100029 fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum])
30 ok = false
Rob Pike54ec7192009-04-12 17:01:17 -070031 }
32 if c != expect[cnum] {
Rob Pike4f61fc92010-09-04 10:36:13 +100033 fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum])
34 ok = false
Rob Pike54ec7192009-04-12 17:01:17 -070035 }
Rob Pike4f61fc92010-09-04 10:36:13 +100036 offset += size
37 cnum++
Rob Pike54ec7192009-04-12 17:01:17 -070038 }
39 if i != len(s)-1 {
Rob Pike4f61fc92010-09-04 10:36:13 +100040 fmt.Println("after loop i is", i, "not", len(s)-1)
41 ok = false
Rob Pike54ec7192009-04-12 17:01:17 -070042 }
Russ Coxa62467a2009-04-13 05:31:44 -070043
Rob Pike4f61fc92010-09-04 10:36:13 +100044 i = 12345
45 c = 23456
Russ Coxa62467a2009-04-13 05:31:44 -070046 for i, c = range "" {
47 }
48 if i != 12345 {
Rob Pike4f61fc92010-09-04 10:36:13 +100049 fmt.Println("range empty string assigned to index:", i)
50 ok = false
Russ Coxa62467a2009-04-13 05:31:44 -070051 }
52 if c != 23456 {
Rob Pike4f61fc92010-09-04 10:36:13 +100053 fmt.Println("range empty string assigned to value:", c)
54 ok = false
Russ Coxa62467a2009-04-13 05:31:44 -070055 }
56
Rob Pike54ec7192009-04-12 17:01:17 -070057 if !ok {
Rob Pike4f61fc92010-09-04 10:36:13 +100058 fmt.Println("BUG: stringrange")
Russ Cox918afd942009-05-08 15:21:41 -070059 os.Exit(1)
Rob Pike54ec7192009-04-12 17:01:17 -070060 }
61}