github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/test/string_lit.go (about)

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