...
Run Format

Source file test/const.go

Documentation: test

     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 simple boolean and numeric constants.
     8  
     9  package main
    10  
    11  import "os"
    12  
    13  const (
    14  	c0      = 0
    15  	cm1     = -1
    16  	chuge   = 1 << 100
    17  	chuge_1 = chuge - 1
    18  	c1      = chuge >> 100
    19  	c3div2  = 3 / 2
    20  	c1e3    = 1e3
    21  
    22  	rsh1 = 1e100 >> 1000
    23  	rsh2 = 1e302 >> 1000
    24  
    25  	ctrue  = true
    26  	cfalse = !ctrue
    27  )
    28  
    29  const (
    30  	f0              = 0.0
    31  	fm1             = -1.
    32  	fhuge   float64 = 1 << 100
    33  	fhuge_1 float64 = chuge - 1
    34  	f1      float64 = chuge >> 100
    35  	f3div2          = 3. / 2.
    36  	f1e3    float64 = 1e3
    37  )
    38  
    39  func assert(t bool, s string) {
    40  	if !t {
    41  		panic(s)
    42  	}
    43  }
    44  
    45  func ints() {
    46  	assert(c0 == 0, "c0")
    47  	assert(c1 == 1, "c1")
    48  	assert(chuge > chuge_1, "chuge")
    49  	assert(chuge_1+1 == chuge, "chuge 1")
    50  	assert(chuge+cm1+1 == chuge, "cm1")
    51  	assert(c3div2 == 1, "3/2")
    52  	assert(c1e3 == 1000, "c1e3 int")
    53  	assert(c1e3 == 1e3, "c1e3 float")
    54  	assert(rsh1 == 0, "rsh1")
    55  	assert(rsh2 == 9, "rsh2")
    56  
    57  	// verify that all (in range) are assignable as ints
    58  	var i int
    59  	i = c0
    60  	assert(i == c0, "i == c0")
    61  	i = cm1
    62  	assert(i == cm1, "i == cm1")
    63  	i = c1
    64  	assert(i == c1, "i == c1")
    65  	i = c3div2
    66  	assert(i == c3div2, "i == c3div2")
    67  	i = c1e3
    68  	assert(i == c1e3, "i == c1e3")
    69  
    70  	// verify that all are assignable as floats
    71  	var f float64
    72  	f = c0
    73  	assert(f == c0, "f == c0")
    74  	f = cm1
    75  	assert(f == cm1, "f == cm1")
    76  	f = chuge
    77  	assert(f == chuge, "f == chuge")
    78  	f = chuge_1
    79  	assert(f == chuge_1, "f == chuge_1")
    80  	f = c1
    81  	assert(f == c1, "f == c1")
    82  	f = c3div2
    83  	assert(f == c3div2, "f == c3div2")
    84  	f = c1e3
    85  	assert(f == c1e3, "f == c1e3")
    86  }
    87  
    88  func floats() {
    89  	assert(f0 == c0, "f0")
    90  	assert(f1 == c1, "f1")
    91  	// TODO(gri): exp/ssa/interp constant folding is incorrect.
    92  	if os.Getenv("GOSSAINTERP") == "" {
    93  		assert(fhuge == fhuge_1, "fhuge") // float64 can't distinguish fhuge, fhuge_1.
    94  	}
    95  	assert(fhuge_1+1 == fhuge, "fhuge 1")
    96  	assert(fhuge+fm1+1 == fhuge, "fm1")
    97  	assert(f3div2 == 1.5, "3./2.")
    98  	assert(f1e3 == 1000, "f1e3 int")
    99  	assert(f1e3 == 1.e3, "f1e3 float")
   100  
   101  	// verify that all (in range) are assignable as ints
   102  	var i int
   103  	i = f0
   104  	assert(i == f0, "i == f0")
   105  	i = fm1
   106  	assert(i == fm1, "i == fm1")
   107  
   108  	// verify that all are assignable as floats
   109  	var f float64
   110  	f = f0
   111  	assert(f == f0, "f == f0")
   112  	f = fm1
   113  	assert(f == fm1, "f == fm1")
   114  	f = fhuge
   115  	assert(f == fhuge, "f == fhuge")
   116  	f = fhuge_1
   117  	assert(f == fhuge_1, "f == fhuge_1")
   118  	f = f1
   119  	assert(f == f1, "f == f1")
   120  	f = f3div2
   121  	assert(f == f3div2, "f == f3div2")
   122  	f = f1e3
   123  	assert(f == f1e3, "f == f1e3")
   124  }
   125  
   126  func interfaces() {
   127  	var (
   128  		nilN interface{}
   129  		nilI *int
   130  		five = 5
   131  
   132  		_ = nil == interface{}(nil)
   133  		_ = interface{}(nil) == nil
   134  	)
   135  	ii := func(i1 interface{}, i2 interface{}) bool { return i1 == i2 }
   136  	ni := func(n interface{}, i int) bool { return n == i }
   137  	in := func(i int, n interface{}) bool { return i == n }
   138  	pi := func(p *int, i interface{}) bool { return p == i }
   139  	ip := func(i interface{}, p *int) bool { return i == p }
   140  
   141  	assert((interface{}(nil) == interface{}(nil)) == ii(nilN, nilN),
   142  		"for interface{}==interface{} compiler == runtime")
   143  
   144  	assert(((*int)(nil) == interface{}(nil)) == pi(nilI, nilN),
   145  		"for *int==interface{} compiler == runtime")
   146  	assert((interface{}(nil) == (*int)(nil)) == ip(nilN, nilI),
   147  		"for interface{}==*int compiler == runtime")
   148  
   149  	assert((&five == interface{}(nil)) == pi(&five, nilN),
   150  		"for interface{}==*int compiler == runtime")
   151  	assert((interface{}(nil) == &five) == ip(nilN, &five),
   152  		"for interface{}==*int compiler == runtime")
   153  
   154  	assert((5 == interface{}(5)) == ni(five, five),
   155  		"for int==interface{} compiler == runtime")
   156  	assert((interface{}(5) == 5) == in(five, five),
   157  		"for interface{}==int comipiler == runtime")
   158  }
   159  
   160  func main() {
   161  	ints()
   162  	floats()
   163  	interfaces()
   164  
   165  	assert(ctrue == true, "ctrue == true")
   166  	assert(cfalse == false, "cfalse == false")
   167  }
   168  

View as plain text