github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/test/const.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 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  	ctrue  = true
    23  	cfalse = !ctrue
    24  )
    25  
    26  const (
    27  	f0              = 0.0
    28  	fm1             = -1.
    29  	fhuge   float64 = 1 << 100
    30  	fhuge_1 float64 = chuge - 1
    31  	f1      float64 = chuge >> 100
    32  	f3div2          = 3. / 2.
    33  	f1e3    float64 = 1e3
    34  )
    35  
    36  func assert(t bool, s string) {
    37  	if !t {
    38  		panic(s)
    39  	}
    40  }
    41  
    42  func ints() {
    43  	assert(c0 == 0, "c0")
    44  	assert(c1 == 1, "c1")
    45  	assert(chuge > chuge_1, "chuge")
    46  	assert(chuge_1+1 == chuge, "chuge 1")
    47  	assert(chuge+cm1+1 == chuge, "cm1")
    48  	assert(c3div2 == 1, "3/2")
    49  	assert(c1e3 == 1000, "c1e3 int")
    50  	assert(c1e3 == 1e3, "c1e3 float")
    51  
    52  	// verify that all (in range) are assignable as ints
    53  	var i int
    54  	i = c0
    55  	assert(i == c0, "i == c0")
    56  	i = cm1
    57  	assert(i == cm1, "i == cm1")
    58  	i = c1
    59  	assert(i == c1, "i == c1")
    60  	i = c3div2
    61  	assert(i == c3div2, "i == c3div2")
    62  	i = c1e3
    63  	assert(i == c1e3, "i == c1e3")
    64  
    65  	// verify that all are assignable as floats
    66  	var f float64
    67  	f = c0
    68  	assert(f == c0, "f == c0")
    69  	f = cm1
    70  	assert(f == cm1, "f == cm1")
    71  	f = chuge
    72  	assert(f == chuge, "f == chuge")
    73  	f = chuge_1
    74  	assert(f == chuge_1, "f == chuge_1")
    75  	f = c1
    76  	assert(f == c1, "f == c1")
    77  	f = c3div2
    78  	assert(f == c3div2, "f == c3div2")
    79  	f = c1e3
    80  	assert(f == c1e3, "f == c1e3")
    81  }
    82  
    83  func floats() {
    84  	assert(f0 == c0, "f0")
    85  	assert(f1 == c1, "f1")
    86  	// TODO(gri): exp/ssa/interp constant folding is incorrect.
    87  	if os.Getenv("GOSSAINTERP") == "" {
    88  		assert(fhuge == fhuge_1, "fhuge") // float64 can't distinguish fhuge, fhuge_1.
    89  	}
    90  	assert(fhuge_1+1 == fhuge, "fhuge 1")
    91  	assert(fhuge+fm1+1 == fhuge, "fm1")
    92  	assert(f3div2 == 1.5, "3./2.")
    93  	assert(f1e3 == 1000, "f1e3 int")
    94  	assert(f1e3 == 1.e3, "f1e3 float")
    95  
    96  	// verify that all (in range) are assignable as ints
    97  	var i int
    98  	i = f0
    99  	assert(i == f0, "i == f0")
   100  	i = fm1
   101  	assert(i == fm1, "i == fm1")
   102  
   103  	// verify that all are assignable as floats
   104  	var f float64
   105  	f = f0
   106  	assert(f == f0, "f == f0")
   107  	f = fm1
   108  	assert(f == fm1, "f == fm1")
   109  	f = fhuge
   110  	assert(f == fhuge, "f == fhuge")
   111  	f = fhuge_1
   112  	assert(f == fhuge_1, "f == fhuge_1")
   113  	f = f1
   114  	assert(f == f1, "f == f1")
   115  	f = f3div2
   116  	assert(f == f3div2, "f == f3div2")
   117  	f = f1e3
   118  	assert(f == f1e3, "f == f1e3")
   119  }
   120  
   121  func main() {
   122  	ints()
   123  	floats()
   124  
   125  	assert(ctrue == true, "ctrue == true")
   126  	assert(cfalse == false, "cfalse == false")
   127  }