2018-01-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / go.test / test / stringrange.go
blob99e5edb5a429d1ce4dc0ca30e4bc77385018c8ef
1 // run
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.
7 // Test range over strings.
9 package main
11 import (
12 "fmt"
13 "os"
14 "unicode/utf8"
17 func main() {
18 s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
19 expect := []rune{0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x'}
20 offset := 0
21 var i int
22 var c rune
23 ok := true
24 cnum := 0
25 for i, c = range s {
26 r, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
27 if i != offset {
28 fmt.Printf("unexpected offset %d not %d\n", i, offset)
29 ok = false
31 if r != expect[cnum] {
32 fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, r, expect[cnum])
33 ok = false
35 if c != expect[cnum] {
36 fmt.Printf("unexpected rune %d from range: %x not %x\n", i, r, expect[cnum])
37 ok = false
39 offset += size
40 cnum++
42 if i != len(s)-1 {
43 fmt.Println("after loop i is", i, "not", len(s)-1)
44 ok = false
47 i = 12345
48 c = 23456
49 for i, c = range "" {
51 if i != 12345 {
52 fmt.Println("range empty string assigned to index:", i)
53 ok = false
55 if c != 23456 {
56 fmt.Println("range empty string assigned to value:", c)
57 ok = false
60 for _, c := range "a\xed\xa0\x80a" {
61 if c != 'a' && c != utf8.RuneError {
62 fmt.Printf("surrogate UTF-8 does not error: %U\n", c)
63 ok = false
67 if !ok {
68 fmt.Println("BUG: stringrange")
69 os.Exit(1)