modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/ken/string.go (about) 1 // cmpout 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 operations including printing. 8 9 package main 10 11 func main() { 12 var c string 13 14 a := `abc` 15 b := `xyz` 16 17 /* print a literal */ 18 print(`abc`) 19 20 /* print a variable */ 21 print(b, "-") 22 23 /* catenate literals */ 24 print(`abc`+`xyz`, "-") 25 26 /* catenate variables */ 27 print(a+b, "-") 28 29 /* compare literals */ 30 if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` { 31 panic("compare literals") 32 } 33 34 /* compare variables */ 35 if a == b || a != a || a > b { 36 panic("compare variables") 37 } 38 39 /* cat */ 40 c = a + b 41 print(c, "-") 42 43 /* catequal */ 44 c = a 45 c += b 46 print(c, "-") 47 48 /* clumsy evaluation */ 49 c = b 50 c = a + c 51 print(c, "-") 52 53 /* len */ 54 if len(c) != 6 { 55 print("len ", len(c)) 56 panic("fail") 57 } 58 59 /* index strings */ 60 for i := 0; i < len(c); i = i + 1 { 61 if c[i] != (a + b)[i] { 62 print("index ", i, " ", c[i], " ", (a + b)[i]) 63 panic("fail") 64 } 65 } 66 67 /* slice strings */ 68 print(c[0:3], c[3:]) 69 70 print("\n") 71 72 /* create string with integer constant */ 73 c = string('x') 74 if c != "x" { 75 panic("create int " + c) 76 } 77 78 /* create string with integer variable */ 79 v := 'x' 80 c = string(v) 81 if c != "x" { 82 panic("create int " + c) 83 } 84 85 /* create string with byte array */ 86 var z1 [3]byte 87 z1[0] = 'a' 88 z1[1] = 'b' 89 z1[2] = 'c' 90 c = string(z1[0:]) 91 if c != "abc" { 92 panic("create byte array " + c) 93 } 94 95 /* create string with int array */ 96 var z2 [3]rune 97 z2[0] = 'a' 98 z2[1] = '\u1234' 99 z2[2] = 'c' 100 c = string(z2[0:]) 101 if c != "a\u1234c" { 102 panic("create int array " + c) 103 } 104 105 /* create string with byte array pointer */ 106 z3 := new([3]byte) 107 z3[0] = 'a' 108 z3[1] = 'b' 109 z3[2] = 'c' 110 c = string(z3[0:]) 111 if c != "abc" { 112 panic("create array pointer " + c) 113 } 114 }