Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 1 | // $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 | |
| 7 | package main |
| 8 | |
Rob Pike | 74dd0ab | 2009-08-17 13:30:22 -0700 | [diff] [blame] | 9 | import ( |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 10 | "fmt" |
| 11 | "os" |
| 12 | "utf8" |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 16 | 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 Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 22 | for i, c = range s { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 23 | rune, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 24 | if i != offset { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 25 | fmt.Printf("unexpected offset %d not %d\n", i, offset) |
| 26 | ok = false |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 27 | } |
| 28 | if rune != expect[cnum] { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 29 | fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum]) |
| 30 | ok = false |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 31 | } |
| 32 | if c != expect[cnum] { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 33 | fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum]) |
| 34 | ok = false |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 35 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 36 | offset += size |
| 37 | cnum++ |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 38 | } |
| 39 | if i != len(s)-1 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 40 | fmt.Println("after loop i is", i, "not", len(s)-1) |
| 41 | ok = false |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 42 | } |
Russ Cox | a62467a | 2009-04-13 05:31:44 -0700 | [diff] [blame] | 43 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 44 | i = 12345 |
| 45 | c = 23456 |
Russ Cox | a62467a | 2009-04-13 05:31:44 -0700 | [diff] [blame] | 46 | for i, c = range "" { |
| 47 | } |
| 48 | if i != 12345 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 49 | fmt.Println("range empty string assigned to index:", i) |
| 50 | ok = false |
Russ Cox | a62467a | 2009-04-13 05:31:44 -0700 | [diff] [blame] | 51 | } |
| 52 | if c != 23456 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 53 | fmt.Println("range empty string assigned to value:", c) |
| 54 | ok = false |
Russ Cox | a62467a | 2009-04-13 05:31:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 57 | if !ok { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 58 | fmt.Println("BUG: stringrange") |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 59 | os.Exit(1) |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 60 | } |
| 61 | } |