Source file 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  // Avoid running gofmt on this file to preserve the
     9  // test cases with upper-case prefixes (0B, 0O, 0X).
    10  
    11  package main
    12  
    13  import "fmt"
    14  
    15  func assert(cond bool) {
    16  	if !cond {
    17  		panic("assertion failed")
    18  	}
    19  }
    20  
    21  func equal(x, y interface{}) bool {
    22  	if x != y {
    23  		fmt.Printf("%g != %g\n", x, y)
    24  		return false
    25  	}
    26  	return true
    27  }
    28  
    29  func main() {
    30  	// 0-octals
    31  	assert(0_1 == 01)
    32  	assert(012 == 012)
    33  	assert(0_1_2 == 012)
    34  	assert(0_1_2i == complex(0, 12)) // decimal digits despite leading 0 for backward-compatibility
    35  	assert(00089i == complex(0, 89)) // decimal digits despite leading 0 for backward-compatibility
    36  
    37  	// decimals
    38  	assert(1_000_000 == 1000000)
    39  	assert(1_000i == complex(0, 1000))
    40  
    41  	// hexadecimals
    42  	assert(0x_1 == 0x1)
    43  	assert(0x1_2 == 0x12)
    44  	assert(0x_cafe_f00d == 0xcafef00d)
    45  	assert(0x_cafei == complex(0, 0xcafe))
    46  
    47  	// octals
    48  	assert(0o_1 == 01)
    49  	assert(0o12 == 012)
    50  	assert(0o_1_2 == 012)
    51  	assert(0o_1_2i == complex(0, 0o12))
    52  
    53  	// binaries
    54  	assert(0b_1 == 1)
    55  	assert(0b10 == 2)
    56  	assert(0b_1_0 == 2)
    57  	assert(0b_1_0i == complex(0, 2))
    58  
    59  	// decimal floats
    60  	assert(0. == 0.0)
    61  	assert(.0 == 0.0)
    62  	assert(1_0. == 10.0)
    63  	assert(.0_1 == 0.01)
    64  	assert(1_0.0_1 == 10.01)
    65  	assert(1_0.0_1i == complex(0, 10.01))
    66  
    67  	assert(0.e1_0 == 0.0e10)
    68  	assert(.0e1_0 == 0.0e10)
    69  	assert(1_0.e1_0 == 10.0e10)
    70  	assert(.0_1e1_0 == 0.01e10)
    71  	assert(1_0.0_1e1_0 == 10.01e10)
    72  	assert(1_0.0_1e1_0i == complex(0, 10.01e10))
    73  
    74  	// hexadecimal floats
    75  	assert(equal(0x1p-2, 0.25))
    76  	assert(equal(0x2.p10, 2048.0))
    77  	assert(equal(0x1.Fp+0, 1.9375))
    78  	assert(equal(0x.8p-0, 0.5))
    79  	assert(equal(0x1FFFp-16, 0.1249847412109375))
    80  	assert(equal(0x1.fffffffffffffp1023, 1.7976931348623157e308))
    81  	assert(equal(0x1.fffffffffffffp1023i, complex(0, 1.7976931348623157e308)))
    82  
    83  	assert(equal(0x_1p-2, 0.25))
    84  	assert(equal(0x2.p1_0, 2048.0))
    85  	assert(equal(0x1_0.Fp+0, 16.9375))
    86  	assert(equal(0x_0.8p-0, 0.5))
    87  	assert(equal(0x_1FF_Fp-16, 0.1249847412109375))
    88  	assert(equal(0x1.f_ffff_ffff_ffffp1_023, 1.7976931348623157e308))
    89  	assert(equal(0x1.f_ffff_ffff_ffffp1_023i, complex(0, 1.7976931348623157e308)))
    90  }
    91  

View as plain text