github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/test/iota.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 iota.
     8  
     9  package main
    10  
    11  func assert(cond bool, msg string) {
    12  	if !cond {
    13  		print("assertion fail: ", msg, "\n")
    14  		panic(1)
    15  	}
    16  }
    17  
    18  const (
    19  	x int = iota
    20  	y = iota
    21  	z = 1 << iota
    22  	f float32 = 2 * iota
    23  	g float32 = 4.5 * float32(iota)
    24  )
    25  
    26  const (
    27  	X = 0
    28  	Y
    29  	Z
    30  )
    31  
    32  const (
    33  	A = 1 << iota
    34  	B
    35  	C
    36  	D
    37  	E = iota * iota
    38  	F
    39  	G
    40  )
    41  
    42  const (
    43  	a = 1
    44  	b = iota << a
    45  	c = iota << b
    46  	d
    47  )
    48  
    49  const (
    50  	i = (a << iota) + (b * iota)
    51  	j
    52  	k
    53  	l
    54  )
    55  
    56  const (
    57  	m = iota == 0
    58  	n
    59  )
    60  
    61  const (
    62  	p = float32(iota)
    63  	q
    64  	r
    65  )
    66  
    67  const (
    68  	s = string(iota + 'a')
    69  	t
    70  )
    71  
    72  const (
    73  	abit, amask = 1 << iota, 1<<iota - 1
    74  	bbit, bmask = 1 << iota, 1<<iota - 1
    75  	cbit, cmask = 1 << iota, 1<<iota - 1
    76  )
    77  
    78  func main() {
    79  	assert(x == 0, "x")
    80  	assert(y == 1, "y")
    81  	assert(z == 4, "z")
    82  	assert(f == 6.0, "f")
    83  	assert(g == 18.0, "g")
    84  
    85  	assert(X == 0, "X")
    86  	assert(Y == 0, "Y")
    87  	assert(Z == 0, "Z")
    88  
    89  	assert(A == 1, "A")
    90  	assert(B == 2, "B")
    91  	assert(C == 4, "C")
    92  	assert(D == 8, "D")
    93  	assert(E == 16, "E")
    94  	assert(F == 25, "F")
    95  
    96  	assert(a == 1, "a")
    97  	assert(b == 2, "b")
    98  	assert(c == 8, "c")
    99  	assert(d == 12, "d")
   100  
   101  	assert(i == 1, "i")
   102  	assert(j == 4, "j")
   103  	assert(k == 8, "k")
   104  	assert(l == 14, "l")
   105  
   106  	assert(m, "m")
   107  	assert(!n, "n")
   108  
   109  	assert(p == 0.0, "p")
   110  	assert(q == 1.0, "q")
   111  	assert(r == 2.0, "r")
   112  
   113  	assert(s == "a", "s")
   114  	assert(t == "b", "t")
   115  
   116  	assert(abit == 1, "abit")
   117  	assert(amask == 0, "amask")
   118  	assert(bbit == 2, "bbit")
   119  	assert(bmask == 1, "bmask")
   120  	assert(cbit == 4, "cbit")
   121  	assert(cmask == 3, "cmask")
   122  }