summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-02-25 15:11:07 -0800
committerRuss Cox <rsc@golang.org>2010-02-25 15:11:07 -0800
commit915a2eae7fb61cbb338c985c3e80b2ab95fa6696 (patch)
tree1711df77712c4674453f3a5f66ee1c089dbe63d6 /test
parente3039013d966d1a9e9f905af61d6583615c40c28 (diff)
downloadgo-915a2eae7fb61cbb338c985c3e80b2ab95fa6696.tar.gz
gc: implement []int(string) and []byte(string)
R=ken2 CC=golang-dev http://codereview.appspot.com/224060
Diffstat (limited to 'test')
-rw-r--r--test/convlit.go27
-rw-r--r--test/string_lit.go30
2 files changed, 57 insertions, 0 deletions
diff --git a/test/convlit.go b/test/convlit.go
index e65dad3df..22415bb32 100644
--- a/test/convlit.go
+++ b/test/convlit.go
@@ -35,3 +35,30 @@ var good2 int = 1.0;
var good3 int = 1e9;
var good4 float = 1e20;
+// explicit conversion of string is okay
+var _ = []int("abc")
+var _ = []byte("abc")
+
+// implicit is not
+var _ []int = "abc" // ERROR "cannot use|incompatible|invalid"
+var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
+
+// named string is okay
+type Tstring string
+var ss Tstring = "abc"
+var _ = []int(ss)
+var _ = []byte(ss)
+
+// implicit is still not
+var _ []int = ss // ERROR "cannot use|incompatible|invalid"
+var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
+
+// named slice is not
+type Tint []int
+type Tbyte []byte
+var _ = Tint("abc") // ERROR "convert|incompatible|invalid"
+var _ = Tbyte("abc") // ERROR "convert|incompatible|invalid"
+
+// implicit is still not
+var _ Tint = "abc" // ERROR "cannot use|incompatible|invalid"
+var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"
diff --git a/test/string_lit.go b/test/string_lit.go
index 547be8003..88b5d251f 100644
--- a/test/string_lit.go
+++ b/test/string_lit.go
@@ -34,6 +34,19 @@ func assert(a, b, c string) {
}
}
+const (
+ gx1 = "aä本☺"
+ gx2 = "aä\xFF\xFF本☺"
+ gx2fix = "aä\uFFFD\uFFFD本☺"
+)
+
+var (
+ gr1 = []int(gx1)
+ gr2 = []int(gx2)
+ gb1 = []byte(gx1)
+ gb2 = []byte(gx2)
+)
+
func main() {
ecode = 0;
s :=
@@ -86,5 +99,22 @@ func main() {
r = 0x10ffff + 1;
s = string(r);
assert(s, "\xef\xbf\xbd", "too-large rune");
+
+ assert(string(gr1), gx1, "global ->[]int")
+ assert(string(gr2), gx2fix, "global invalid ->[]int")
+ assert(string(gb1), gx1, "->[]byte")
+ assert(string(gb2), gx2, "global invalid ->[]byte")
+
+ var (
+ r1 = []int(gx1)
+ r2 = []int(gx2)
+ b1 = []byte(gx1)
+ b2 = []byte(gx2)
+ )
+ assert(string(r1), gx1, "->[]int")
+ assert(string(r2), gx2fix, "invalid ->[]int")
+ assert(string(b1), gx1, "->[]byte")
+ assert(string(b2), gx2, "invalid ->[]byte")
+
os.Exit(ecode);
}