codekingpro/portable-devtools
114k
1// run2 3// Copyright 2009 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test simple boolean and numeric constants.8 9package main10 11import "os"12 13const (14 c0 = 015 cm1 = -116 chuge = 1 << 10017 chuge_1 = chuge - 118 c1 = chuge >> 10019 c3div2 = 3 / 220 c1e3 = 1e321 22 rsh1 = 1e100 >> 100023 rsh2 = 1e302 >> 100024 25 ctrue = true26 cfalse = !ctrue27 28 // Issue #3456329 _ = string(int(123))30 _ = string(rune(456))31)32 33const (34 f0 = 0.035 fm1 = -1.36 fhuge float64 = 1 << 10037 fhuge_1 float64 = chuge - 138 f1 float64 = chuge >> 10039 f3div2 = 3. / 2.40 f1e3 float64 = 1e341)42 43func assert(t bool, s string) {44 if !t {45 panic(s)46 }47}48 49func 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 ints62 var i int63 i = c064 assert(i == c0, "i == c0")65 i = cm166 assert(i == cm1, "i == cm1")67 i = c168 assert(i == c1, "i == c1")69 i = c3div270 assert(i == c3div2, "i == c3div2")71 i = c1e372 assert(i == c1e3, "i == c1e3")73 74 // verify that all are assignable as floats75 var f float6476 f = c077 assert(f == c0, "f == c0")78 f = cm179 assert(f == cm1, "f == cm1")80 f = chuge81 assert(f == chuge, "f == chuge")82 f = chuge_183 assert(f == chuge_1, "f == chuge_1")84 f = c185 assert(f == c1, "f == c1")86 f = c3div287 assert(f == c3div2, "f == c3div2")88 f = c1e389 assert(f == c1e3, "f == c1e3")90}91 92func 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 ints106 var i int107 i = f0108 assert(i == f0, "i == f0")109 i = fm1110 assert(i == fm1, "i == fm1")111 112 // verify that all are assignable as floats113 var f float64114 f = f0115 assert(f == f0, "f == f0")116 f = fm1117 assert(f == fm1, "f == fm1")118 f = fhuge119 assert(f == fhuge, "f == fhuge")120 f = fhuge_1121 assert(f == fhuge_1, "f == fhuge_1")122 f = f1123 assert(f == f1, "f == f1")124 f = f3div2125 assert(f == f3div2, "f == f3div2")126 f = f1e3127 assert(f == f1e3, "f == f1e3")128}129 130func interfaces() {131 var (132 nilN interface{}133 nilI *int134 five = 5135 136 _ = nil == interface{}(nil)137 _ = interface{}(nil) == nil138 )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 arithmetic165// is computed with correct precision.166func truncate() {167 const (168 x30 = 1 << 30169 x60 = 1 << 60170 171 staticF32 = float32(x30) + 1 - x30172 staticF64 = float64(x60) + 1 - x60173 staticC64 = complex64(x30) + 1 - x30174 staticC128 = complex128(x60) + 1 - x60175 )176 dynamicF32 := float32(x30)177 dynamicF32 += 1178 dynamicF32 -= x30179 180 dynamicF64 := float64(x60)181 dynamicF64 += 1182 dynamicF64 -= x60183 184 dynamicC64 := complex64(x30)185 dynamicC64 += 1186 dynamicC64 -= x30187 188 dynamicC128 := complex128(x60)189 dynamicC128 += 1190 dynamicC128 -= x60191 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 202func main() {203 ints()204 floats()205 interfaces()206 truncate()207 208 assert(ctrue == true, "ctrue == true")209 assert(cfalse == false, "cfalse == false")210}211 