1// run
2
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.
6
7// Test string literal syntax.
8
9package main
10
11import "os"
12
13var ecode int
14
15func assert(a, b, c string) {
16	if a != b {
17		ecode = 1
18		print("FAIL: ", c, ": ", a, "!=", b, "\n")
19		var max int = len(a)
20		if len(b) > max {
21			max = len(b)
22		}
23		for i := 0; i < max; i++ {
24			ac := 0
25			bc := 0
26			if i < len(a) {
27				ac = int(a[i])
28			}
29			if i < len(b) {
30				bc = int(b[i])
31			}
32			if ac != bc {
33				print("\ta[", i, "] = ", ac, "; b[", i, "] =", bc, "\n")
34			}
35		}
36	}
37}
38
39const (
40	gx1    = "aä本☺"
41	gx2    = "aä\xFF\xFF本☺"
42	gx2fix = "aä\uFFFD\uFFFD本☺"
43)
44
45var (
46	gr1 = []rune(gx1)
47	gr2 = []rune(gx2)
48	gb1 = []byte(gx1)
49	gb2 = []byte(gx2)
50)
51
52func main() {
53	ecode = 0
54	s :=
55		"" +
56			" " +
57			"'`" +
58			"a" +
59			"ä" +
60			"本" +
61			"\a\b\f\n\r\t\v\\\"" +
62			"\000\123\x00\xca\xFE\u0123\ubabe\U0000babe" +
63
64			`` +
65			` ` +
66			`'"` +
67			`a` +
68			`ä` +
69			`本` +
70			`\a\b\f\n\r\t\v\\\'` +
71			`\000\123\x00\xca\xFE\u0123\ubabe\U0000babe` +
72			`\x\u\U\`
73
74	assert("", ``, "empty")
75	assert(" ", " ", "blank")
76	assert("\x61", "a", "lowercase a")
77	assert("\x61", `a`, "lowercase a (backquote)")
78	assert("\u00e4", "ä", "a umlaut")
79	assert("\u00e4", `ä`, "a umlaut (backquote)")
80	assert("\u672c", "本", "nihon")
81	assert("\u672c", `本`, "nihon (backquote)")
82	assert("\x07\x08\x0c\x0a\x0d\x09\x0b\x5c\x22",
83		"\a\b\f\n\r\t\v\\\"",
84		"backslashes")
85	assert("\\a\\b\\f\\n\\r\\t\\v\\\\\\\"",
86		`\a\b\f\n\r\t\v\\\"`,
87		"backslashes (backquote)")
88	assert("\x00\x53\000\xca\376S몾몾",
89		"\000\123\x00\312\xFE\u0053\ubabe\U0000babe",
90		"backslashes 2")
91	assert("\\000\\123\\x00\\312\\xFE\\u0123\\ubabe\\U0000babe",
92		`\000\123\x00\312\xFE\u0123\ubabe\U0000babe`,
93		"backslashes 2 (backquote)")
94	assert("\\x\\u\\U\\", `\x\u\U\`, "backslash 3 (backquote)")
95
96	// test large and surrogate-half runes. perhaps not the most logical place for these tests.
97	var r int32
98	r = 0x10ffff // largest rune value
99	s = string(r)
100	assert(s, "\xf4\x8f\xbf\xbf", "largest rune")
101	r = 0x10ffff + 1
102	s = string(r)
103	assert(s, "\xef\xbf\xbd", "too-large rune")
104	r = 0xD800
105	s = string(r)
106	assert(s, "\xef\xbf\xbd", "surrogate rune min")
107	r = 0xDFFF
108	s = string(r)
109	assert(s, "\xef\xbf\xbd", "surrogate rune max")
110	r = -1
111	s = string(r)
112	assert(s, "\xef\xbf\xbd", "negative rune")
113
114	// the large rune tests again, this time using constants instead of a variable.
115	// these conversions will be done at compile time.
116	s = string(0x10ffff) // largest rune value
117	assert(s, "\xf4\x8f\xbf\xbf", "largest rune constant")
118	s = string(0x10ffff + 1)
119	assert(s, "\xef\xbf\xbd", "too-large rune constant")
120	s = string(0xD800)
121	assert(s, "\xef\xbf\xbd", "surrogate rune min constant")
122	s = string(0xDFFF)
123	assert(s, "\xef\xbf\xbd", "surrogate rune max constant")
124	s = string(-1)
125	assert(s, "\xef\xbf\xbd", "negative rune")
126
127	assert(string(gr1), gx1, "global ->[]rune")
128	assert(string(gr2), gx2fix, "global invalid ->[]rune")
129	assert(string(gb1), gx1, "->[]byte")
130	assert(string(gb2), gx2, "global invalid ->[]byte")
131
132	var (
133		r1 = []rune(gx1)
134		r2 = []rune(gx2)
135		b1 = []byte(gx1)
136		b2 = []byte(gx2)
137	)
138	assert(string(r1), gx1, "->[]rune")
139	assert(string(r2), gx2fix, "invalid ->[]rune")
140	assert(string(b1), gx1, "->[]byte")
141	assert(string(b2), gx2, "invalid ->[]byte")
142
143	os.Exit(ecode)
144}
145