gcc/libgo/go/utf8/string_test.go
Ian Lance Taylor 7a9389330e Add Go frontend, libgo library, and Go testsuite.
gcc/:
	* gcc.c (default_compilers): Add entry for ".go".
	* common.opt: Add -static-libgo as a driver option.
	* doc/install.texi (Configuration): Mention libgo as an option for
	--enable-shared.  Mention go as an option for --enable-languages.
	* doc/invoke.texi (Overall Options): Mention .go as a file name
	suffix.  Mention go as a -x option.
	* doc/frontends.texi (G++ and GCC): Mention Go as a supported
	language.
	* doc/sourcebuild.texi (Top Level): Mention libgo.
	* doc/standards.texi (Standards): Add section on Go language.
	Move references for other languages into their own section.
	* doc/contrib.texi (Contributors): Mention that I contributed the
	Go frontend.
gcc/testsuite/:
	* lib/go.exp: New file.
	* lib/go-dg.exp: New file.
	* lib/go-torture.exp: New file.
	* lib/target-supports.exp (check_compile): Match // Go.

From-SVN: r167407
2010-12-03 04:34:57 +00:00

110 lines
2.5 KiB
Go

// 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 utf8_test
import (
"rand"
"testing"
. "utf8"
)
func TestScanForwards(t *testing.T) {
for _, s := range testStrings {
runes := []int(s)
str := NewString(s)
if str.RuneCount() != len(runes) {
t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
break
}
for i, expect := range runes {
got := str.At(i)
if got != expect {
t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
}
}
}
}
func TestScanBackwards(t *testing.T) {
for _, s := range testStrings {
runes := []int(s)
str := NewString(s)
if str.RuneCount() != len(runes) {
t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
break
}
for i := len(runes) - 1; i >= 0; i-- {
expect := runes[i]
got := str.At(i)
if got != expect {
t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
}
}
}
}
const randCount = 100000
func TestRandomAccess(t *testing.T) {
for _, s := range testStrings {
if len(s) == 0 {
continue
}
runes := []int(s)
str := NewString(s)
if str.RuneCount() != len(runes) {
t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
break
}
for j := 0; j < randCount; j++ {
i := rand.Intn(len(runes))
expect := runes[i]
got := str.At(i)
if got != expect {
t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
}
}
}
}
func TestRandomSliceAccess(t *testing.T) {
for _, s := range testStrings {
if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test
continue
}
runes := []int(s)
str := NewString(s)
if str.RuneCount() != len(runes) {
t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
break
}
for k := 0; k < randCount; k++ {
i := rand.Intn(len(runes))
j := rand.Intn(len(runes) + 1)
if i > j { // include empty strings
continue
}
expect := string(runes[i:j])
got := str.Slice(i, j)
if got != expect {
t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
}
}
}
}
func TestLimitSliceAccess(t *testing.T) {
for _, s := range testStrings {
str := NewString(s)
if str.Slice(0, 0) != "" {
t.Error("failure with empty slice at beginning")
}
nr := RuneCountInString(s)
if str.Slice(nr, nr) != "" {
t.Error("failure with empty slice at end")
}
}
}