2012-02-16 23:51:04 -05:00
// run
2009-04-12 17:01:17 -07:00
// 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.
2012-02-24 11:48:19 +11:00
// Test range over strings.
2009-04-12 17:01:17 -07:00
package main
2009-08-17 13:30:22 -07:00
import (
2010-09-04 10:36:13 +10:00
"fmt"
"os"
2011-11-08 15:43:02 -08:00
"unicode/utf8"
2009-04-12 17:01:17 -07:00
)
func main ( ) {
2010-09-04 10:36:13 +10:00
s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-25 22:20:02 -07:00
expect := [ ] rune { 0 , 0123 , 0 , 0xFFFD , 0xFFFD , 0x123 , 0xbabe , 0xbabe , 0x10FFFF , 'x' }
2010-09-04 10:36:13 +10:00
offset := 0
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-25 22:20:02 -07:00
var i int
var c rune
2010-09-04 10:36:13 +10:00
ok := true
cnum := 0
2009-04-12 17:01:17 -07:00
for i , c = range s {
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-25 22:20:02 -07:00
r , size := utf8 . DecodeRuneInString ( s [ i : len ( s ) ] ) // check it another way
2009-04-12 17:01:17 -07:00
if i != offset {
2010-09-04 10:36:13 +10:00
fmt . Printf ( "unexpected offset %d not %d\n" , i , offset )
ok = false
2009-04-12 17:01:17 -07:00
}
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-25 22:20:02 -07:00
if r != expect [ cnum ] {
fmt . Printf ( "unexpected rune %d from DecodeRuneInString: %x not %x\n" , i , r , expect [ cnum ] )
2010-09-04 10:36:13 +10:00
ok = false
2009-04-12 17:01:17 -07:00
}
if c != expect [ cnum ] {
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-25 22:20:02 -07:00
fmt . Printf ( "unexpected rune %d from range: %x not %x\n" , i , r , expect [ cnum ] )
2010-09-04 10:36:13 +10:00
ok = false
2009-04-12 17:01:17 -07:00
}
2010-09-04 10:36:13 +10:00
offset += size
cnum ++
2009-04-12 17:01:17 -07:00
}
if i != len ( s ) - 1 {
2010-09-04 10:36:13 +10:00
fmt . Println ( "after loop i is" , i , "not" , len ( s ) - 1 )
ok = false
2009-04-12 17:01:17 -07:00
}
2009-04-13 05:31:44 -07:00
2010-09-04 10:36:13 +10:00
i = 12345
c = 23456
2009-04-13 05:31:44 -07:00
for i , c = range "" {
}
if i != 12345 {
2010-09-04 10:36:13 +10:00
fmt . Println ( "range empty string assigned to index:" , i )
ok = false
2009-04-13 05:31:44 -07:00
}
if c != 23456 {
2010-09-04 10:36:13 +10:00
fmt . Println ( "range empty string assigned to value:" , c )
ok = false
2009-04-13 05:31:44 -07:00
}
2012-08-08 14:01:23 -07:00
for _ , c := range "a\xed\xa0\x80a" {
if c != 'a' && c != utf8 . RuneError {
fmt . Printf ( "surrogate UTF-8 does not error: %U\n" , c )
ok = false
}
}
2009-04-12 17:01:17 -07:00
if ! ok {
2010-09-04 10:36:13 +10:00
fmt . Println ( "BUG: stringrange" )
2009-05-08 15:21:41 -07:00
os . Exit ( 1 )
2009-04-12 17:01:17 -07:00
}
}