github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/compile/internal/gc/testdata/compound.go (about) 1 // run 2 3 // Copyright 2015 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 compound objects 8 9 package main 10 11 import "fmt" 12 13 func string_ssa(a, b string, x bool) string { 14 s := "" 15 if x { 16 s = a 17 } else { 18 s = b 19 } 20 return s 21 } 22 23 func testString() { 24 a := "foo" 25 b := "barz" 26 if want, got := a, string_ssa(a, b, true); got != want { 27 fmt.Printf("string_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 28 failed = true 29 } 30 if want, got := b, string_ssa(a, b, false); got != want { 31 fmt.Printf("string_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want) 32 failed = true 33 } 34 } 35 36 //go:noinline 37 func complex64_ssa(a, b complex64, x bool) complex64 { 38 var c complex64 39 if x { 40 c = a 41 } else { 42 c = b 43 } 44 return c 45 } 46 47 //go:noinline 48 func complex128_ssa(a, b complex128, x bool) complex128 { 49 var c complex128 50 if x { 51 c = a 52 } else { 53 c = b 54 } 55 return c 56 } 57 58 func testComplex64() { 59 var a complex64 = 1 + 2i 60 var b complex64 = 3 + 4i 61 62 if want, got := a, complex64_ssa(a, b, true); got != want { 63 fmt.Printf("complex64_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 64 failed = true 65 } 66 if want, got := b, complex64_ssa(a, b, false); got != want { 67 fmt.Printf("complex64_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 68 failed = true 69 } 70 } 71 72 func testComplex128() { 73 var a complex128 = 1 + 2i 74 var b complex128 = 3 + 4i 75 76 if want, got := a, complex128_ssa(a, b, true); got != want { 77 fmt.Printf("complex128_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 78 failed = true 79 } 80 if want, got := b, complex128_ssa(a, b, false); got != want { 81 fmt.Printf("complex128_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 82 failed = true 83 } 84 } 85 86 func slice_ssa(a, b []byte, x bool) []byte { 87 var s []byte 88 if x { 89 s = a 90 } else { 91 s = b 92 } 93 return s 94 } 95 96 func testSlice() { 97 a := []byte{3, 4, 5} 98 b := []byte{7, 8, 9} 99 if want, got := byte(3), slice_ssa(a, b, true)[0]; got != want { 100 fmt.Printf("slice_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 101 failed = true 102 } 103 if want, got := byte(7), slice_ssa(a, b, false)[0]; got != want { 104 fmt.Printf("slice_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want) 105 failed = true 106 } 107 } 108 109 func interface_ssa(a, b interface{}, x bool) interface{} { 110 var s interface{} 111 if x { 112 s = a 113 } else { 114 s = b 115 } 116 return s 117 } 118 119 func testInterface() { 120 a := interface{}(3) 121 b := interface{}(4) 122 if want, got := 3, interface_ssa(a, b, true).(int); got != want { 123 fmt.Printf("interface_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want) 124 failed = true 125 } 126 if want, got := 4, interface_ssa(a, b, false).(int); got != want { 127 fmt.Printf("interface_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want) 128 failed = true 129 } 130 } 131 132 var failed = false 133 134 func main() { 135 testString() 136 testSlice() 137 testInterface() 138 testComplex64() 139 testComplex128() 140 if failed { 141 panic("failed") 142 } 143 }