github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/intcvt.go (about) 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 implicit and explicit conversions of constants. 8 9 package main 10 11 const ( 12 ci8 = -1 << 7 13 ci16 = -1<<15 + 100 14 ci32 = -1<<31 + 100000 15 ci64 = -1<<63 + 10000000001 16 17 cu8 = 1<<8 - 1 18 cu16 = 1<<16 - 1234 19 cu32 = 1<<32 - 1234567 20 cu64 = 1<<64 - 1234567890123 21 22 cf32 = 1e8 + 0.5 23 cf64 = -1e8 + 0.5 24 ) 25 26 var ( 27 i8 int8 = ci8 28 i16 int16 = ci16 29 i32 int32 = ci32 30 i64 int64 = ci64 31 32 u8 uint8 = cu8 33 u16 uint16 = cu16 34 u32 uint32 = cu32 35 u64 uint64 = cu64 36 37 // f32 float32 = 1e8 + 0.5 38 // f64 float64 = -1e8 + 0.5 39 ) 40 41 func chki8(i, v int8) { 42 if i != v { 43 println(i, "!=", v) 44 panic("fail") 45 } 46 } 47 func chki16(i, v int16) { 48 if i != v { 49 println(i, "!=", v) 50 panic("fail") 51 } 52 } 53 func chki32(i, v int32) { 54 if i != v { 55 println(i, "!=", v) 56 panic("fail") 57 } 58 } 59 func chki64(i, v int64) { 60 if i != v { 61 println(i, "!=", v) 62 panic("fail") 63 } 64 } 65 func chku8(i, v uint8) { 66 if i != v { 67 println(i, "!=", v) 68 panic("fail") 69 } 70 } 71 func chku16(i, v uint16) { 72 if i != v { 73 println(i, "!=", v) 74 panic("fail") 75 } 76 } 77 func chku32(i, v uint32) { 78 if i != v { 79 println(i, "!=", v) 80 panic("fail") 81 } 82 } 83 func chku64(i, v uint64) { 84 if i != v { 85 println(i, "!=", v) 86 panic("fail") 87 } 88 } 89 //func chkf32(f, v float32) { if f != v { println(f, "!=", v); panic("fail") } } 90 //func chkf64(f, v float64) { if f != v { println(f, "!=", v); panic("fail") } } 91 92 func main() { 93 chki8(int8(i8), ci8&0xff-1<<8) 94 chki8(int8(i16), ci16&0xff) 95 chki8(int8(i32), ci32&0xff-1<<8) 96 chki8(int8(i64), ci64&0xff) 97 chki8(int8(u8), cu8&0xff-1<<8) 98 chki8(int8(u16), cu16&0xff) 99 chki8(int8(u32), cu32&0xff) 100 chki8(int8(u64), cu64&0xff) 101 // chki8(int8(f32), 0) 102 // chki8(int8(f64), 0) 103 104 chki16(int16(i8), ci8&0xffff-1<<16) 105 chki16(int16(i16), ci16&0xffff-1<<16) 106 chki16(int16(i32), ci32&0xffff-1<<16) 107 chki16(int16(i64), ci64&0xffff-1<<16) 108 chki16(int16(u8), cu8&0xffff) 109 chki16(int16(u16), cu16&0xffff-1<<16) 110 chki16(int16(u32), cu32&0xffff) 111 chki16(int16(u64), cu64&0xffff-1<<16) 112 // chki16(int16(f32), 0) 113 // chki16(int16(f64), 0) 114 115 chki32(int32(i8), ci8&0xffffffff-1<<32) 116 chki32(int32(i16), ci16&0xffffffff-1<<32) 117 chki32(int32(i32), ci32&0xffffffff-1<<32) 118 chki32(int32(i64), ci64&0xffffffff) 119 chki32(int32(u8), cu8&0xffffffff) 120 chki32(int32(u16), cu16&0xffffffff) 121 chki32(int32(u32), cu32&0xffffffff-1<<32) 122 chki32(int32(u64), cu64&0xffffffff-1<<32) 123 // chki32(int32(f32), 0) 124 // chki32(int32(f64), 0) 125 126 chki64(int64(i8), ci8&0xffffffffffffffff-1<<64) 127 chki64(int64(i16), ci16&0xffffffffffffffff-1<<64) 128 chki64(int64(i32), ci32&0xffffffffffffffff-1<<64) 129 chki64(int64(i64), ci64&0xffffffffffffffff-1<<64) 130 chki64(int64(u8), cu8&0xffffffffffffffff) 131 chki64(int64(u16), cu16&0xffffffffffffffff) 132 chki64(int64(u32), cu32&0xffffffffffffffff) 133 chki64(int64(u64), cu64&0xffffffffffffffff-1<<64) 134 // chki64(int64(f32), 0) 135 // chki64(int64(f64), 0) 136 137 138 chku8(uint8(i8), ci8&0xff) 139 chku8(uint8(i16), ci16&0xff) 140 chku8(uint8(i32), ci32&0xff) 141 chku8(uint8(i64), ci64&0xff) 142 chku8(uint8(u8), cu8&0xff) 143 chku8(uint8(u16), cu16&0xff) 144 chku8(uint8(u32), cu32&0xff) 145 chku8(uint8(u64), cu64&0xff) 146 // chku8(uint8(f32), 0) 147 // chku8(uint8(f64), 0) 148 149 chku16(uint16(i8), ci8&0xffff) 150 chku16(uint16(i16), ci16&0xffff) 151 chku16(uint16(i32), ci32&0xffff) 152 chku16(uint16(i64), ci64&0xffff) 153 chku16(uint16(u8), cu8&0xffff) 154 chku16(uint16(u16), cu16&0xffff) 155 chku16(uint16(u32), cu32&0xffff) 156 chku16(uint16(u64), cu64&0xffff) 157 // chku16(uint16(f32), 0) 158 // chku16(uint16(f64), 0) 159 160 chku32(uint32(i8), ci8&0xffffffff) 161 chku32(uint32(i16), ci16&0xffffffff) 162 chku32(uint32(i32), ci32&0xffffffff) 163 chku32(uint32(i64), ci64&0xffffffff) 164 chku32(uint32(u8), cu8&0xffffffff) 165 chku32(uint32(u16), cu16&0xffffffff) 166 chku32(uint32(u32), cu32&0xffffffff) 167 chku32(uint32(u64), cu64&0xffffffff) 168 // chku32(uint32(f32), 0) 169 // chku32(uint32(f64), 0) 170 171 chku64(uint64(i8), ci8&0xffffffffffffffff) 172 chku64(uint64(i16), ci16&0xffffffffffffffff) 173 chku64(uint64(i32), ci32&0xffffffffffffffff) 174 chku64(uint64(i64), ci64&0xffffffffffffffff) 175 chku64(uint64(u8), cu8&0xffffffffffffffff) 176 chku64(uint64(u16), cu16&0xffffffffffffffff) 177 chku64(uint64(u32), cu32&0xffffffffffffffff) 178 chku64(uint64(u64), cu64&0xffffffffffffffff) 179 // chku64(uint64(f32), 0) 180 // chku64(uint64(f64), 0) 181 }