summaryrefslogtreecommitdiffstats
path: root/test/intcvt.go
blob: 3920528a403bfad15b4f5faee8c63ceab3356846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// run

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test implicit and explicit conversions of constants.

package main

const (
	ci8  = -1 << 7
	ci16 = -1<<15 + 100
	ci32 = -1<<31 + 100000
	ci64 = -1<<63 + 10000000001

	cu8  = 1<<8 - 1
	cu16 = 1<<16 - 1234
	cu32 = 1<<32 - 1234567
	cu64 = 1<<64 - 1234567890123

	cf32 = 1e8 + 0.5
	cf64 = -1e8 + 0.5
)

var (
	i8  int8  = ci8
	i16 int16 = ci16
	i32 int32 = ci32
	i64 int64 = ci64

	u8  uint8  = cu8
	u16 uint16 = cu16
	u32 uint32 = cu32
	u64 uint64 = cu64

	//	f32 float32 = 1e8 + 0.5
	//	f64 float64 = -1e8 + 0.5
)

func chki8(i, v int8) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chki16(i, v int16) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chki32(i, v int32) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chki64(i, v int64) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chku8(i, v uint8) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chku16(i, v uint16) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chku32(i, v uint32) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
func chku64(i, v uint64) {
	if i != v {
		println(i, "!=", v)
		panic("fail")
	}
}
//func chkf32(f, v float32) { if f != v { println(f, "!=", v); panic("fail") } }
//func chkf64(f, v float64) { if f != v { println(f, "!=", v); panic("fail") } }

func main() {
	chki8(int8(i8), ci8&0xff-1<<8)
	chki8(int8(i16), ci16&0xff)
	chki8(int8(i32), ci32&0xff-1<<8)
	chki8(int8(i64), ci64&0xff)
	chki8(int8(u8), cu8&0xff-1<<8)
	chki8(int8(u16), cu16&0xff)
	chki8(int8(u32), cu32&0xff)
	chki8(int8(u64), cu64&0xff)
	//	chki8(int8(f32), 0)
	//	chki8(int8(f64), 0)

	chki16(int16(i8), ci8&0xffff-1<<16)
	chki16(int16(i16), ci16&0xffff-1<<16)
	chki16(int16(i32), ci32&0xffff-1<<16)
	chki16(int16(i64), ci64&0xffff-1<<16)
	chki16(int16(u8), cu8&0xffff)
	chki16(int16(u16), cu16&0xffff-1<<16)
	chki16(int16(u32), cu32&0xffff)
	chki16(int16(u64), cu64&0xffff-1<<16)
	//	chki16(int16(f32), 0)
	//	chki16(int16(f64), 0)

	chki32(int32(i8), ci8&0xffffffff-1<<32)
	chki32(int32(i16), ci16&0xffffffff-1<<32)
	chki32(int32(i32), ci32&0xffffffff-1<<32)
	chki32(int32(i64), ci64&0xffffffff)
	chki32(int32(u8), cu8&0xffffffff)
	chki32(int32(u16), cu16&0xffffffff)
	chki32(int32(u32), cu32&0xffffffff-1<<32)
	chki32(int32(u64), cu64&0xffffffff-1<<32)
	//	chki32(int32(f32), 0)
	//	chki32(int32(f64), 0)

	chki64(int64(i8), ci8&0xffffffffffffffff-1<<64)
	chki64(int64(i16), ci16&0xffffffffffffffff-1<<64)
	chki64(int64(i32), ci32&0xffffffffffffffff-1<<64)
	chki64(int64(i64), ci64&0xffffffffffffffff-1<<64)
	chki64(int64(u8), cu8&0xffffffffffffffff)
	chki64(int64(u16), cu16&0xffffffffffffffff)
	chki64(int64(u32), cu32&0xffffffffffffffff)
	chki64(int64(u64), cu64&0xffffffffffffffff-1<<64)
	//	chki64(int64(f32), 0)
	//	chki64(int64(f64), 0)


	chku8(uint8(i8), ci8&0xff)
	chku8(uint8(i16), ci16&0xff)
	chku8(uint8(i32), ci32&0xff)
	chku8(uint8(i64), ci64&0xff)
	chku8(uint8(u8), cu8&0xff)
	chku8(uint8(u16), cu16&0xff)
	chku8(uint8(u32), cu32&0xff)
	chku8(uint8(u64), cu64&0xff)
	//	chku8(uint8(f32), 0)
	//	chku8(uint8(f64), 0)

	chku16(uint16(i8), ci8&0xffff)
	chku16(uint16(i16), ci16&0xffff)
	chku16(uint16(i32), ci32&0xffff)
	chku16(uint16(i64), ci64&0xffff)
	chku16(uint16(u8), cu8&0xffff)
	chku16(uint16(u16), cu16&0xffff)
	chku16(uint16(u32), cu32&0xffff)
	chku16(uint16(u64), cu64&0xffff)
	//	chku16(uint16(f32), 0)
	//	chku16(uint16(f64), 0)

	chku32(uint32(i8), ci8&0xffffffff)
	chku32(uint32(i16), ci16&0xffffffff)
	chku32(uint32(i32), ci32&0xffffffff)
	chku32(uint32(i64), ci64&0xffffffff)
	chku32(uint32(u8), cu8&0xffffffff)
	chku32(uint32(u16), cu16&0xffffffff)
	chku32(uint32(u32), cu32&0xffffffff)
	chku32(uint32(u64), cu64&0xffffffff)
	//	chku32(uint32(f32), 0)
	//	chku32(uint32(f64), 0)

	chku64(uint64(i8), ci8&0xffffffffffffffff)
	chku64(uint64(i16), ci16&0xffffffffffffffff)
	chku64(uint64(i32), ci32&0xffffffffffffffff)
	chku64(uint64(i64), ci64&0xffffffffffffffff)
	chku64(uint64(u8), cu8&0xffffffffffffffff)
	chku64(uint64(u16), cu16&0xffffffffffffffff)
	chku64(uint64(u32), cu32&0xffffffffffffffff)
	chku64(uint64(u64), cu64&0xffffffffffffffff)
	//	chku64(uint64(f32), 0)
	//	chku64(uint64(f64), 0)
}