github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/test/named.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 that basic operations on named types are valid
     8  // and preserve the type.
     9  
    10  package main
    11  
    12  type Array [10]byte
    13  type Bool bool
    14  type Chan chan int
    15  type Float float32
    16  type Int int
    17  type Map map[int]byte
    18  type Slice []byte
    19  type String string
    20  
    21  // Calling these functions checks at compile time that the argument
    22  // can be converted implicitly to (used as) the given type.
    23  func asArray(Array)   {}
    24  func asBool(Bool)     {}
    25  func asChan(Chan)     {}
    26  func asFloat(Float)   {}
    27  func asInt(Int)       {}
    28  func asMap(Map)       {}
    29  func asSlice(Slice)   {}
    30  func asString(String) {}
    31  
    32  func (Map) M() {}
    33  
    34  
    35  // These functions check at run time that the default type
    36  // (in the absence of any implicit conversion hints)
    37  // is the given type.
    38  func isArray(x interface{})  { _ = x.(Array) }
    39  func isBool(x interface{})   { _ = x.(Bool) }
    40  func isChan(x interface{})   { _ = x.(Chan) }
    41  func isFloat(x interface{})  { _ = x.(Float) }
    42  func isInt(x interface{})    { _ = x.(Int) }
    43  func isMap(x interface{})    { _ = x.(Map) }
    44  func isSlice(x interface{})  { _ = x.(Slice) }
    45  func isString(x interface{}) { _ = x.(String) }
    46  
    47  func main() {
    48  	var (
    49  		a     Array
    50  		b     Bool   = true
    51  		c     Chan   = make(Chan)
    52  		f     Float  = 1
    53  		i     Int    = 1
    54  		m     Map    = make(Map)
    55  		slice Slice  = make(Slice, 10)
    56  		str   String = "hello"
    57  	)
    58  
    59  	asArray(a)
    60  	isArray(a)
    61  	asArray(*&a)
    62  	isArray(*&a)
    63  	asArray(Array{})
    64  	isArray(Array{})
    65  
    66  	asBool(b)
    67  	isBool(b)
    68  	asBool(!b)
    69  	isBool(!b)
    70  	asBool(true)
    71  	asBool(*&b)
    72  	isBool(*&b)
    73  	asBool(Bool(true))
    74  	isBool(Bool(true))
    75  
    76  	asChan(c)
    77  	isChan(c)
    78  	asChan(make(Chan))
    79  	isChan(make(Chan))
    80  	asChan(*&c)
    81  	isChan(*&c)
    82  	asChan(Chan(nil))
    83  	isChan(Chan(nil))
    84  
    85  	asFloat(f)
    86  	isFloat(f)
    87  	asFloat(-f)
    88  	isFloat(-f)
    89  	asFloat(+f)
    90  	isFloat(+f)
    91  	asFloat(f + 1)
    92  	isFloat(f + 1)
    93  	asFloat(1 + f)
    94  	isFloat(1 + f)
    95  	asFloat(f + f)
    96  	isFloat(f + f)
    97  	f++
    98  	f += 2
    99  	asFloat(f - 1)
   100  	isFloat(f - 1)
   101  	asFloat(1 - f)
   102  	isFloat(1 - f)
   103  	asFloat(f - f)
   104  	isFloat(f - f)
   105  	f--
   106  	f -= 2
   107  	asFloat(f * 2.5)
   108  	isFloat(f * 2.5)
   109  	asFloat(2.5 * f)
   110  	isFloat(2.5 * f)
   111  	asFloat(f * f)
   112  	isFloat(f * f)
   113  	f *= 4
   114  	asFloat(f / 2.5)
   115  	isFloat(f / 2.5)
   116  	asFloat(2.5 / f)
   117  	isFloat(2.5 / f)
   118  	asFloat(f / f)
   119  	isFloat(f / f)
   120  	f /= 4
   121  	asFloat(f)
   122  	isFloat(f)
   123  	f = 5
   124  	asFloat(*&f)
   125  	isFloat(*&f)
   126  	asFloat(234)
   127  	asFloat(Float(234))
   128  	isFloat(Float(234))
   129  	asFloat(1.2)
   130  	asFloat(Float(i))
   131  	isFloat(Float(i))
   132  
   133  	asInt(i)
   134  	isInt(i)
   135  	asInt(-i)
   136  	isInt(-i)
   137  	asInt(^i)
   138  	isInt(^i)
   139  	asInt(+i)
   140  	isInt(+i)
   141  	asInt(i + 1)
   142  	isInt(i + 1)
   143  	asInt(1 + i)
   144  	isInt(1 + i)
   145  	asInt(i + i)
   146  	isInt(i + i)
   147  	i++
   148  	i += 1
   149  	asInt(i - 1)
   150  	isInt(i - 1)
   151  	asInt(1 - i)
   152  	isInt(1 - i)
   153  	asInt(i - i)
   154  	isInt(i - i)
   155  	i--
   156  	i -= 1
   157  	asInt(i * 2)
   158  	isInt(i * 2)
   159  	asInt(2 * i)
   160  	isInt(2 * i)
   161  	asInt(i * i)
   162  	isInt(i * i)
   163  	i *= 2
   164  	asInt(i / 5)
   165  	isInt(i / 5)
   166  	asInt(5 / i)
   167  	isInt(5 / i)
   168  	asInt(i / i)
   169  	isInt(i / i)
   170  	i /= 2
   171  	asInt(i % 5)
   172  	isInt(i % 5)
   173  	asInt(5 % i)
   174  	isInt(5 % i)
   175  	asInt(i % i)
   176  	isInt(i % i)
   177  	i %= 2
   178  	asInt(i & 5)
   179  	isInt(i & 5)
   180  	asInt(5 & i)
   181  	isInt(5 & i)
   182  	asInt(i & i)
   183  	isInt(i & i)
   184  	i &= 2
   185  	asInt(i &^ 5)
   186  	isInt(i &^ 5)
   187  	asInt(5 &^ i)
   188  	isInt(5 &^ i)
   189  	asInt(i &^ i)
   190  	isInt(i &^ i)
   191  	i &^= 2
   192  	asInt(i | 5)
   193  	isInt(i | 5)
   194  	asInt(5 | i)
   195  	isInt(5 | i)
   196  	asInt(i | i)
   197  	isInt(i | i)
   198  	i |= 2
   199  	asInt(i ^ 5)
   200  	isInt(i ^ 5)
   201  	asInt(5 ^ i)
   202  	isInt(5 ^ i)
   203  	asInt(i ^ i)
   204  	isInt(i ^ i)
   205  	i ^= 2
   206  	asInt(i << 4)
   207  	isInt(i << 4)
   208  	i <<= 2
   209  	asInt(i >> 4)
   210  	isInt(i >> 4)
   211  	i >>= 2
   212  	asInt(i)
   213  	isInt(i)
   214  	asInt(0)
   215  	asInt(Int(0))
   216  	isInt(Int(0))
   217  	i = 10
   218  	asInt(*&i)
   219  	isInt(*&i)
   220  	asInt(23)
   221  	asInt(Int(f))
   222  	isInt(Int(f))
   223  
   224  	asMap(m)
   225  	isMap(m)
   226  	asMap(nil)
   227  	m = nil
   228  	asMap(make(Map))
   229  	isMap(make(Map))
   230  	asMap(*&m)
   231  	isMap(*&m)
   232  	asMap(Map(nil))
   233  	isMap(Map(nil))
   234  	asMap(Map{})
   235  	isMap(Map{})
   236  
   237  	asSlice(slice)
   238  	isSlice(slice)
   239  	asSlice(make(Slice, 5))
   240  	isSlice(make(Slice, 5))
   241  	asSlice([]byte{1, 2, 3})
   242  	asSlice([]byte{1, 2, 3}[0:2])
   243  	asSlice(slice[0:4])
   244  	isSlice(slice[0:4])
   245  	asSlice(slice[3:8])
   246  	isSlice(slice[3:8])
   247  	asSlice(nil)
   248  	asSlice(Slice(nil))
   249  	isSlice(Slice(nil))
   250  	slice = nil
   251  	asSlice(Slice{1, 2, 3})
   252  	isSlice(Slice{1, 2, 3})
   253  	asSlice(Slice{})
   254  	isSlice(Slice{})
   255  	asSlice(*&slice)
   256  	isSlice(*&slice)
   257  
   258  	asString(str)
   259  	isString(str)
   260  	asString(str + "a")
   261  	isString(str + "a")
   262  	asString("a" + str)
   263  	isString("a" + str)
   264  	asString(str + str)
   265  	isString(str + str)
   266  	str += "a"
   267  	str += str
   268  	asString(String('a'))
   269  	isString(String('a'))
   270  	asString(String([]byte(slice)))
   271  	isString(String([]byte(slice)))
   272  	asString(String([]byte(nil)))
   273  	isString(String([]byte(nil)))
   274  	asString("hello")
   275  	asString(String("hello"))
   276  	isString(String("hello"))
   277  	str = "hello"
   278  	isString(str)
   279  	asString(*&str)
   280  	isString(*&str)
   281  }