]> Cypherpunks.ru repositories - gostls13.git/blob - test/literal2.go
cmd/compile: accept new Go2 number literals
[gostls13.git] / test / literal2.go
1 // run
2
3 // Copyright 2019 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 Go2 literal syntax for basic types.
8 // TODO add more tests
9
10 package main
11
12 import "fmt"
13
14 func assert(cond bool) {
15         if !cond {
16                 panic("assertion failed")
17         }
18 }
19
20 func equal(x, y float64) bool {
21         if x != y {
22                 fmt.Printf("%g != %g\n", x, y)
23                 return false
24         }
25         return true
26 }
27
28 func main() {
29         // 0-octals
30         assert(0_1 == 01)
31         assert(012 == 012)
32         assert(0_1_2 == 012)
33
34         // decimals
35         assert(1_000_000 == 1000000)
36
37         // hexadecimals
38         assert(0x_1 == 0x1)
39         assert(0x1_2 == 0x12)
40         assert(0X_cafe_f00d == 0xcafef00d)
41
42         // octals
43         assert(0o_1 == 01)
44         assert(0o12 == 012)
45         assert(0O_1_2 == 012)
46
47         // binaries
48         assert(0b_1 == 1)
49         assert(0b10 == 2)
50         assert(0b_1_0 == 2)
51
52         // decimal floats
53         assert(0. == 0.0)
54         assert(.0 == 0.0)
55         assert(1_0. == 10.0)
56         assert(.0_1 == 0.01)
57         assert(1_0.0_1 == 10.01)
58
59         assert(0.e1_0 == 0.0e10)
60         assert(.0e1_0 == 0.0e10)
61         assert(1_0.e1_0 == 10.0e10)
62         assert(.0_1e1_0 == 0.01e10)
63         assert(1_0.0_1e1_0 == 10.01e10)
64
65         // hexadecimal floats
66         assert(equal(0x1p-2, 0.25))
67         assert(equal(0x2.p10, 2048.0))
68         assert(equal(0x1.Fp+0, 1.9375))
69         assert(equal(0X.8p-0, 0.5))
70         assert(equal(0X1FFFP-16, 0.1249847412109375))
71         assert(equal(0x1.fffffffffffffp1023, 1.7976931348623157e308))
72
73         assert(equal(0x_1p-2, 0.25))
74         assert(equal(0x2.p1_0, 2048.0))
75         assert(equal(0x1_0.Fp+0, 16.9375))
76         assert(equal(0X_0.8p-0, 0.5))
77         assert(equal(0X_1FF_FP-16, 0.1249847412109375))
78         assert(equal(0x1.f_ffff_ffff_ffffP1_023, 1.7976931348623157e308))
79
80         // imaginaries
81         assert(0i == complex(0, 0))
82         assert(09i == complex(0, 9)) // "09i" is a decimal int followed by "i"
83         assert(1.2e+3i == complex(0, 1.2e+3))
84
85         assert(0_0i == complex(0, 0))
86         assert(0_9i == complex(0, 9)) // "0_9i" is a decimal int followed by "i"
87         assert(1.2_0e+0_3i == complex(0, 1.2e+3))
88 }