| 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 simple boolean and numeric constants. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import "os" |
| 12 |
|
| 13 |
const ( |
| 14 |
c0 = 0 |
| 15 |
cm1 = -1 |
| 16 |
chuge = 1 << 100 |
| 17 |
chuge_1 = chuge - 1 |
| 18 |
c1 = chuge >> 100 |
| 19 |
c3div2 = 3 / 2 |
| 20 |
c1e3 = 1e3 |
| 21 |
|
| 22 |
rsh1 = 1e100 >> 1000 |
| 23 |
rsh2 = 1e302 >> 1000 |
| 24 |
|
| 25 |
ctrue = true |
| 26 |
cfalse = !ctrue |
| 27 |
|
| 28 |
// Issue #34563 |
| 29 |
_ = string(int(123)) |
| 30 |
_ = string(rune(456)) |
| 31 |
) |
| 32 |
|
| 33 |
const ( |
| 34 |
f0 = 0.0 |
| 35 |
fm1 = -1. |
| 36 |
fhuge float64 = 1 << 100 |
| 37 |
fhuge_1 float64 = chuge - 1 |
| 38 |
f1 float64 = chuge >> 100 |
| 39 |
f3div2 = 3. / 2. |
| 40 |
f1e3 float64 = 1e3 |
| 41 |
) |
| 42 |
|
| 43 |
func assert(t bool, s string) { |
| 44 |
if !t { |
| 45 |
panic(s) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func ints() { |
| 50 |
assert(c0 == 0, "c0") |
| 51 |
assert(c1 == 1, "c1") |
| 52 |
assert(chuge > chuge_1, "chuge") |
| 53 |
assert(chuge_1+1 == chuge, "chuge 1") |
| 54 |
assert(chuge+cm1+1 == chuge, "cm1") |
| 55 |
assert(c3div2 == 1, "3/2") |
| 56 |
assert(c1e3 == 1000, "c1e3 int") |
| 57 |
assert(c1e3 == 1e3, "c1e3 float") |
| 58 |
assert(rsh1 == 0, "rsh1") |
| 59 |
assert(rsh2 == 9, "rsh2") |
| 60 |
|
| 61 |
// verify that all (in range) are assignable as ints |
| 62 |
var i int |
| 63 |
i = c0 |
| 64 |
assert(i == c0, "i == c0") |
| 65 |
i = cm1 |
| 66 |
assert(i == cm1, "i == cm1") |
| 67 |
i = c1 |
| 68 |
assert(i == c1, "i == c1") |
| 69 |
i = c3div2 |
| 70 |
assert(i == c3div2, "i == c3div2") |
| 71 |
i = c1e3 |
| 72 |
assert(i == c1e3, "i == c1e3") |
| 73 |
|
| 74 |
// verify that all are assignable as floats |
| 75 |
var f float64 |
| 76 |
f = c0 |
| 77 |
assert(f == c0, "f == c0") |
| 78 |
f = cm1 |
| 79 |
assert(f == cm1, "f == cm1") |
| 80 |
f = chuge |
| 81 |
assert(f == chuge, "f == chuge") |
| 82 |
f = chuge_1 |
| 83 |
assert(f == chuge_1, "f == chuge_1") |
| 84 |
f = c1 |
| 85 |
assert(f == c1, "f == c1") |
| 86 |
f = c3div2 |
| 87 |
assert(f == c3div2, "f == c3div2") |
| 88 |
f = c1e3 |
| 89 |
assert(f == c1e3, "f == c1e3") |
| 90 |
} |
| 91 |
|
| 92 |
func floats() { |
| 93 |
assert(f0 == c0, "f0") |
| 94 |
assert(f1 == c1, "f1") |
| 95 |
// TODO(gri): exp/ssa/interp constant folding is incorrect. |
| 96 |
if os.Getenv("GOSSAINTERP") == "" { |
| 97 |
assert(fhuge == fhuge_1, "fhuge") // float64 can't distinguish fhuge, fhuge_1. |
| 98 |
} |
| 99 |
assert(fhuge_1+1 == fhuge, "fhuge 1") |
| 100 |
assert(fhuge+fm1+1 == fhuge, "fm1") |
| 101 |
assert(f3div2 == 1.5, "3./2.") |
| 102 |
assert(f1e3 == 1000, "f1e3 int") |
| 103 |
assert(f1e3 == 1.e3, "f1e3 float") |
| 104 |
|
| 105 |
// verify that all (in range) are assignable as ints |
| 106 |
var i int |
| 107 |
i = f0 |
| 108 |
assert(i == f0, "i == f0") |
| 109 |
i = fm1 |
| 110 |
assert(i == fm1, "i == fm1") |
| 111 |
|
| 112 |
// verify that all are assignable as floats |
| 113 |
var f float64 |
| 114 |
f = f0 |
| 115 |
assert(f == f0, "f == f0") |
| 116 |
f = fm1 |
| 117 |
assert(f == fm1, "f == fm1") |
| 118 |
f = fhuge |
| 119 |
assert(f == fhuge, "f == fhuge") |
| 120 |
f = fhuge_1 |
| 121 |
assert(f == fhuge_1, "f == fhuge_1") |
| 122 |
f = f1 |
| 123 |
assert(f == f1, "f == f1") |
| 124 |
f = f3div2 |
| 125 |
assert(f == f3div2, "f == f3div2") |
| 126 |
f = f1e3 |
| 127 |
assert(f == f1e3, "f == f1e3") |
| 128 |
} |
| 129 |
|
| 130 |
func interfaces() { |
| 131 |
var ( |
| 132 |
nilN interface{} |
| 133 |
nilI *int |
| 134 |
five = 5 |
| 135 |
|
| 136 |
_ = nil == interface{}(nil) |
| 137 |
_ = interface{}(nil) == nil |
| 138 |
) |
| 139 |
ii := func(i1 interface{}, i2 interface{}) bool { return i1 == i2 } |
| 140 |
ni := func(n interface{}, i int) bool { return n == i } |
| 141 |
in := func(i int, n interface{}) bool { return i == n } |
| 142 |
pi := func(p *int, i interface{}) bool { return p == i } |
| 143 |
ip := func(i interface{}, p *int) bool { return i == p } |
| 144 |
|
| 145 |
assert((interface{}(nil) == interface{}(nil)) == ii(nilN, nilN), |
| 146 |
"for interface{}==interface{} compiler == runtime") |
| 147 |
|
| 148 |
assert(((*int)(nil) == interface{}(nil)) == pi(nilI, nilN), |
| 149 |
"for *int==interface{} compiler == runtime") |
| 150 |
assert((interface{}(nil) == (*int)(nil)) == ip(nilN, nilI), |
| 151 |
"for interface{}==*int compiler == runtime") |
| 152 |
|
| 153 |
assert((&five == interface{}(nil)) == pi(&five, nilN), |
| 154 |
"for interface{}==*int compiler == runtime") |
| 155 |
assert((interface{}(nil) == &five) == ip(nilN, &five), |
| 156 |
"for interface{}==*int compiler == runtime") |
| 157 |
|
| 158 |
assert((5 == interface{}(5)) == ni(five, five), |
| 159 |
"for int==interface{} compiler == runtime") |
| 160 |
assert((interface{}(5) == 5) == in(five, five), |
| 161 |
"for interface{}==int comipiler == runtime") |
| 162 |
} |
| 163 |
|
| 164 |
// Test that typed floating-point and complex arithmetic |
| 165 |
// is computed with correct precision. |
| 166 |
func truncate() { |
| 167 |
const ( |
| 168 |
x30 = 1 << 30 |
| 169 |
x60 = 1 << 60 |
| 170 |
|
| 171 |
staticF32 = float32(x30) + 1 - x30 |
| 172 |
staticF64 = float64(x60) + 1 - x60 |
| 173 |
staticC64 = complex64(x30) + 1 - x30 |
| 174 |
staticC128 = complex128(x60) + 1 - x60 |
| 175 |
) |
| 176 |
dynamicF32 := float32(x30) |
| 177 |
dynamicF32 += 1 |
| 178 |
dynamicF32 -= x30 |
| 179 |
|
| 180 |
dynamicF64 := float64(x60) |
| 181 |
dynamicF64 += 1 |
| 182 |
dynamicF64 -= x60 |
| 183 |
|
| 184 |
dynamicC64 := complex64(x30) |
| 185 |
dynamicC64 += 1 |
| 186 |
dynamicC64 -= x30 |
| 187 |
|
| 188 |
dynamicC128 := complex128(x60) |
| 189 |
dynamicC128 += 1 |
| 190 |
dynamicC128 -= x60 |
| 191 |
|
| 192 |
assert(staticF32 == 0, "staticF32 == 0") |
| 193 |
assert(staticF64 == 0, "staticF64 == 0") |
| 194 |
assert(dynamicF32 == 0, "dynamicF32 == 0") |
| 195 |
assert(dynamicF64 == 0, "dynamicF64 == 0") |
| 196 |
assert(staticC64 == 0, "staticC64 == 0") |
| 197 |
assert(staticC128 == 0, "staticC128 == 0") |
| 198 |
assert(dynamicC64 == 0, "dynamicC64 == 0") |
| 199 |
assert(dynamicC128 == 0, "dynamicC128 == 0") |
| 200 |
} |
| 201 |
|
| 202 |
func main() { |
| 203 |
ints() |
| 204 |
floats() |
| 205 |
interfaces() |
| 206 |
truncate() |
| 207 |
|
| 208 |
assert(ctrue == true, "ctrue == true") |
| 209 |
assert(cfalse == false, "cfalse == false") |
| 210 |
} |