1 /*
2 Text comparisons through every spelling of text.
3 4 `string` and `[]byte` are one type, but the legacy type checker keeps the two
5 spellings distinct while it types a package, so `string(a) != string(b)` reached
6 the SSA builder as a comparison of two []byte operands. The conversion is
7 identical-type and folds away, so the builder saw a bare `a != b` on two slice
8 parameters: legacy rejected it as "[]byte is not comparable" while stage4
9 accepted it. A switch on `string(x)` failed the same way, one level down, in the
10 case comparisons.
11 12 The rewrite layer classifies an operand by the text predicate now, not by
13 []byte, so both spellings lower to the same content comparison. These cases pin
14 the equality AND the ordering, and every "different" pair has the SAME LENGTH on
15 purpose: a header comparison would call them equal, a content comparison does
16 not.
17 18 usage: text_compare_forms exit 0 when every form compares by content
19 */
20 package main
21 22 type label string
23 24 func eqConv(a []byte, b []byte) (ok bool) {
25 return string(a) == string(b)
26 }
27 28 func neConv(a []byte, b []byte) (ok bool) {
29 return string(a) != string(b)
30 }
31 32 func ltConv(a []byte, b []byte) (ok bool) {
33 return string(a) < string(b)
34 }
35 36 func bareEq(a []byte, b []byte) (ok bool) {
37 return a == b
38 }
39 40 func litEq(a []byte) (ok bool) {
41 return a == "x"
42 }
43 44 func pick(tag []byte) (n int32) {
45 switch string(tag) {
46 case "one":
47 return 1
48 case "two":
49 return 2
50 case "":
51 return 3
52 }
53 return 0
54 }
55 56 func main() {
57 // Termination: a batch program must still be stoppable, so the entry
58 // handles the channel the runtime closes on SIGINT/SIGTERM.
59 select {
60 case <-shutdown:
61 return
62 default:
63 }
64 65 // Distinct buffers, equal content: header identity must not decide this.
66 x := []byte("padded")
67 y := []byte("padded")
68 z := []byte("qadded")
69 70 if !eqConv(x, y) {
71 panic("string()== string() says unequal for equal content")
72 }
73 if neConv(x, y) {
74 panic("string()!= string() says unequal for equal content")
75 }
76 // Same length, different content. A header compare calls these equal.
77 if !neConv(x, z) {
78 panic("string()!= string() missed a same-length difference")
79 }
80 if eqConv(x, z) {
81 panic("string()== string() matched a same-length difference")
82 }
83 if !ltConv(x, z) {
84 panic("string()< string() is wrong for 'padded' < 'qadded'")
85 }
86 if ltConv(z, x) {
87 panic("string()< string() is wrong for 'qadded' < 'padded'")
88 }
89 if !bareEq(x, y) {
90 panic("a == b says unequal for equal content")
91 }
92 if litEq(x) {
93 panic("a == \"x\" matched a literal the text does not hold")
94 }
95 if !litEq([]byte("x")) {
96 panic("a == \"x\" missed a literal the text does hold")
97 }
98 99 // Prefix ordering: shorter sorts first with equal leading bytes.
100 short := []byte("pad")
101 if !ltConv(short, x) {
102 panic("prefix must sort before the longer text")
103 }
104 // Empty is a defined value, not a missing one.
105 var empty []byte
106 if !eqConv(empty, []byte("")) {
107 panic("empty text is not equal to empty text")
108 }
109 if !neConv(empty, short) {
110 panic("empty text compared equal to non-empty text")
111 }
112 113 // A named text type compares the same way.
114 var l label = "padded"
115 if string(l) != string(x) {
116 panic("named string type compared unequal for equal content")
117 }
118 119 // Switch tags go through the same path.
120 if pick([]byte("one")) != 1 {
121 panic("switch on string(tag) missed case one")
122 }
123 if pick([]byte("two")) != 2 {
124 panic("switch on string(tag) missed case two")
125 }
126 if pick([]byte("")) != 3 {
127 panic("switch on string(tag) missed the empty case")
128 }
129 if pick([]byte("three")) != 0 {
130 panic("switch on string(tag) matched a case it should not")
131 }
132 133 println("text compare forms ok")
134 }
135