2008-11-24 15:17:47 -08: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.
|
|
|
|
|
|
|
|
|
|
package strings
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing";
|
|
|
|
|
)
|
|
|
|
|
|
2008-12-18 22:37:22 -08:00
|
|
|
func eq(a, b []string) bool {
|
2008-11-24 15:17:47 -08:00
|
|
|
if len(a) != len(b) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
|
if a[i] != b[i] {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var abcd = "abcd";
|
|
|
|
|
var faces = "☺☻☹";
|
|
|
|
|
var commas = "1,2,3,4";
|
|
|
|
|
var dots = "1....2....3....4";
|
|
|
|
|
|
2009-06-09 10:58:58 -07:00
|
|
|
type IndexTest struct {
|
|
|
|
|
s string;
|
|
|
|
|
sep string;
|
|
|
|
|
out int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var indexTests = []IndexTest {
|
|
|
|
|
IndexTest{"", "", 0},
|
|
|
|
|
IndexTest{"", "a", -1},
|
|
|
|
|
IndexTest{"", "foo", -1},
|
|
|
|
|
IndexTest{"fo", "foo", -1},
|
|
|
|
|
IndexTest{"foo", "foo", 0},
|
|
|
|
|
IndexTest{"oofofoofooo", "f", 2},
|
|
|
|
|
IndexTest{"oofofoofooo", "foo", 4},
|
|
|
|
|
IndexTest{"barfoobarfoo", "foo", 3},
|
|
|
|
|
IndexTest{"foo", "", 0},
|
|
|
|
|
IndexTest{"foo", "o", 1},
|
|
|
|
|
IndexTest{"abcABCabc", "A", 3},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastIndexTests = []IndexTest {
|
|
|
|
|
IndexTest{"", "", 0},
|
|
|
|
|
IndexTest{"", "a", -1},
|
|
|
|
|
IndexTest{"", "foo", -1},
|
|
|
|
|
IndexTest{"fo", "foo", -1},
|
|
|
|
|
IndexTest{"foo", "foo", 0},
|
|
|
|
|
IndexTest{"oofofoofooo", "f", 7},
|
|
|
|
|
IndexTest{"oofofoofooo", "foo", 7},
|
|
|
|
|
IndexTest{"barfoobarfoo", "foo", 9},
|
|
|
|
|
IndexTest{"foo", "", 3},
|
|
|
|
|
IndexTest{"foo", "o", 2},
|
|
|
|
|
IndexTest{"abcABCabc", "A", 3},
|
|
|
|
|
IndexTest{"abcABCabc", "a", 6},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute f on each test case. funcName should be the name of f; it's used
|
|
|
|
|
// in failure reports.
|
|
|
|
|
func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
|
|
|
|
|
for i,test := range testCases {
|
|
|
|
|
actual := f(test.s, test.sep);
|
2009-06-24 20:12:50 -07:00
|
|
|
if actual != test.out {
|
2009-06-09 10:58:58 -07:00
|
|
|
t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIndex(t *testing.T) {
|
|
|
|
|
runIndexTests(t, Index, "Index", indexTests);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLastIndex(t *testing.T) {
|
|
|
|
|
runIndexTests(t, LastIndex, "LastIndex", lastIndexTests);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
type ExplodeTest struct {
|
2008-11-24 15:17:47 -08:00
|
|
|
s string;
|
2009-06-24 19:02:29 -07:00
|
|
|
n int;
|
2008-12-18 22:37:22 -08:00
|
|
|
a []string;
|
2008-11-24 15:17:47 -08:00
|
|
|
}
|
2009-03-03 08:39:12 -08:00
|
|
|
var explodetests = []ExplodeTest {
|
2009-06-24 19:02:29 -07:00
|
|
|
ExplodeTest{ abcd, 4, []string{"a", "b", "c", "d"} },
|
|
|
|
|
ExplodeTest{ faces, 3, []string{"☺", "☻", "☹"} },
|
|
|
|
|
ExplodeTest{ abcd, 2, []string{"a", "bcd"} },
|
2009-03-03 08:39:12 -08:00
|
|
|
}
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestExplode(t *testing.T) {
|
2009-06-24 19:02:29 -07:00
|
|
|
for _, tt := range explodetests {
|
|
|
|
|
a := explode(tt.s, tt.n);
|
2008-11-24 15:17:47 -08:00
|
|
|
if !eq(a, tt.a) {
|
2009-06-24 19:02:29 -07:00
|
|
|
t.Errorf("explode(%q, %d) = %v; want %v", tt.s, tt.n, a, tt.a);
|
2008-11-24 15:17:47 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
2009-01-16 12:47:24 -08:00
|
|
|
s := Join(a, "");
|
2008-11-24 15:17:47 -08:00
|
|
|
if s != tt.s {
|
2009-06-24 19:02:29 -07:00
|
|
|
t.Errorf(`Join(explode(%q, %d), "") = %q`, tt.s, tt.n, s);
|
2008-11-24 15:17:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
type SplitTest struct {
|
2008-11-24 15:17:47 -08:00
|
|
|
s string;
|
|
|
|
|
sep string;
|
2009-06-24 19:02:29 -07:00
|
|
|
n int;
|
2008-12-18 22:37:22 -08:00
|
|
|
a []string;
|
2008-11-24 15:17:47 -08:00
|
|
|
}
|
2009-03-03 08:39:12 -08:00
|
|
|
var splittests = []SplitTest {
|
2009-06-24 19:02:29 -07:00
|
|
|
SplitTest{ abcd, "a", 0, []string{"", "bcd"} },
|
|
|
|
|
SplitTest{ abcd, "z", 0, []string{"abcd"} },
|
|
|
|
|
SplitTest{ abcd, "", 0, []string{"a", "b", "c", "d"} },
|
|
|
|
|
SplitTest{ commas, ",", 0, []string{"1", "2", "3", "4"} },
|
|
|
|
|
SplitTest{ dots, "...", 0, []string{"1", ".2", ".3", ".4"} },
|
|
|
|
|
SplitTest{ faces, "☹", 0, []string{"☺☻", ""} },
|
|
|
|
|
SplitTest{ faces, "~", 0, []string{faces} },
|
|
|
|
|
SplitTest{ faces, "", 0, []string{"☺", "☻", "☹"} },
|
|
|
|
|
SplitTest{ "1 2 3 4", " ", 3, []string{"1", "2", "3 4"} },
|
|
|
|
|
SplitTest{ "1 2", " ", 3, []string{"1", "2"} },
|
|
|
|
|
SplitTest{ "123", "", 2, []string{"1", "23"} },
|
|
|
|
|
SplitTest{ "123", "", 17, []string{"1", "2", "3"} },
|
2009-03-03 08:39:12 -08:00
|
|
|
}
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestSplit(t *testing.T) {
|
2009-06-24 19:02:29 -07:00
|
|
|
for _, tt := range splittests {
|
|
|
|
|
a := Split(tt.s, tt.sep, tt.n);
|
2008-11-24 15:17:47 -08:00
|
|
|
if !eq(a, tt.a) {
|
2009-06-24 19:02:29 -07:00
|
|
|
t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a);
|
2008-11-24 15:17:47 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
2009-01-16 12:47:24 -08:00
|
|
|
s := Join(a, tt.sep);
|
2008-11-24 15:17:47 -08:00
|
|
|
if s != tt.s {
|
2009-06-24 19:02:29 -07:00
|
|
|
t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s);
|
2008-11-24 15:17:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-05 13:09:03 -07:00
|
|
|
// Test case for any function which accepts and returns a single string.
|
|
|
|
|
type StringTest struct {
|
|
|
|
|
in, out string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute f on each test case. funcName should be the name of f; it's used
|
|
|
|
|
// in failure reports.
|
|
|
|
|
func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
|
actual := f(tc.in);
|
2009-06-24 20:12:50 -07:00
|
|
|
if actual != tc.out {
|
2009-06-05 13:09:03 -07:00
|
|
|
t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var upperASCIITests = []StringTest {
|
|
|
|
|
StringTest{"", ""},
|
|
|
|
|
StringTest{"abc", "ABC"},
|
|
|
|
|
StringTest{"AbC123", "ABC123"},
|
|
|
|
|
StringTest{"azAZ09_", "AZAZ09_"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lowerASCIITests = []StringTest {
|
|
|
|
|
StringTest{"", ""},
|
|
|
|
|
StringTest{"abc", "abc"},
|
|
|
|
|
StringTest{"AbC123", "abc123"},
|
|
|
|
|
StringTest{"azAZ09_", "azaz09_"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trimSpaceASCIITests = []StringTest {
|
|
|
|
|
StringTest{"", ""},
|
|
|
|
|
StringTest{"abc", "abc"},
|
|
|
|
|
StringTest{" ", ""},
|
|
|
|
|
StringTest{" \t\r\n \t\t\r\r\n\n ", ""},
|
|
|
|
|
StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"},
|
|
|
|
|
StringTest{" \t\r\n x\t\t\r\r\ny\n ", "x\t\t\r\r\ny"},
|
|
|
|
|
StringTest{"1 \t\r\n2", "1 \t\r\n2"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpperASCII(t *testing.T) {
|
|
|
|
|
runStringTests(t, UpperASCII, "UpperASCII", upperASCIITests);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLowerASCII(t *testing.T) {
|
|
|
|
|
runStringTests(t, LowerASCII, "LowerASCII", lowerASCIITests);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTrimSpaceASCII(t *testing.T) {
|
|
|
|
|
runStringTests(t, TrimSpaceASCII, "TrimSpaceASCII", trimSpaceASCIITests);
|
|
|
|
|
}
|
|
|
|
|
|