modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/literal.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 literal syntax for basic types.
     8  
     9  package main
    10  
    11  var nbad int
    12  
    13  func assert(cond bool, msg string) {
    14  	if !cond {
    15  		if nbad == 0 {
    16  			print("BUG")
    17  		}
    18  		nbad++
    19  		print(" ", msg)
    20  	}
    21  }
    22  
    23  func equal(a, b float32) bool {
    24  	return a == b
    25  }
    26  
    27  func main() {
    28  	// bool
    29  	var t bool = true
    30  	var f bool = false
    31  	assert(t == !f, "bool")
    32  
    33  	// int8
    34  	var i00 int8 = 0
    35  	var i01 int8 = 1
    36  	var i02 int8 = -1
    37  	var i03 int8 = 127
    38  	var i04 int8 = -127
    39  	var i05 int8 = -128
    40  	var i06 int8 = +127
    41  	assert(i01 == i00+1, "i01")
    42  	assert(i02 == -i01, "i02")
    43  	assert(i03 == -i04, "i03")
    44  	assert(-(i05+1) == i06, "i05")
    45  
    46  	// int16
    47  	var i10 int16 = 0
    48  	var i11 int16 = 1
    49  	var i12 int16 = -1
    50  	var i13 int16 = 32767
    51  	var i14 int16 = -32767
    52  	var i15 int16 = -32768
    53  	var i16 int16 = +32767
    54  	assert(i11 == i10+1, "i11")
    55  	assert(i12 == -i11, "i12")
    56  	assert(i13 == -i14, "i13")
    57  	assert(-(i15+1) == i16, "i15")
    58  
    59  	// int32
    60  	var i20 int32 = 0
    61  	var i21 int32 = 1
    62  	var i22 int32 = -1
    63  	var i23 int32 = 2147483647
    64  	var i24 int32 = -2147483647
    65  	var i25 int32 = -2147483648
    66  	var i26 int32 = +2147483647
    67  	assert(i21 == i20+1, "i21")
    68  	assert(i22 == -i21, "i22")
    69  	assert(i23 == -i24, "i23")
    70  	assert(-(i25+1) == i26, "i25")
    71  	assert(i23 == (1<<31)-1, "i23 size")
    72  
    73  	// int64
    74  	var i30 int64 = 0
    75  	var i31 int64 = 1
    76  	var i32 int64 = -1
    77  	var i33 int64 = 9223372036854775807
    78  	var i34 int64 = -9223372036854775807
    79  	var i35 int64 = -9223372036854775808
    80  	var i36 int64 = +9223372036854775807
    81  	assert(i31 == i30+1, "i31")
    82  	assert(i32 == -i31, "i32")
    83  	assert(i33 == -i34, "i33")
    84  	assert(-(i35+1) == i36, "i35")
    85  	assert(i33 == (1<<63)-1, "i33 size")
    86  
    87  	// uint8
    88  	var u00 uint8 = 0
    89  	var u01 uint8 = 1
    90  	var u02 uint8 = 255
    91  	var u03 uint8 = +255
    92  	assert(u01 == u00+1, "u01")
    93  	assert(u02 == u03, "u02")
    94  	assert(u03 == (1<<8)-1, "u03 size")
    95  
    96  	// uint16
    97  	var u10 uint16 = 0
    98  	var u11 uint16 = 1
    99  	var u12 uint16 = 65535
   100  	var u13 uint16 = +65535
   101  	assert(u11 == u10+1, "u11")
   102  	assert(u12 == u13, "u12")
   103  
   104  	// uint32
   105  	var u20 uint32 = 0
   106  	var u21 uint32 = 1
   107  	var u22 uint32 = 4294967295
   108  	var u23 uint32 = +4294967295
   109  	assert(u21 == u20+1, "u21")
   110  	assert(u22 == u23, "u22")
   111  
   112  	// uint64
   113  	var u30 uint64 = 0
   114  	var u31 uint64 = 1
   115  	var u32 uint64 = 18446744073709551615
   116  	var u33 uint64 = +18446744073709551615
   117  	_, _, _, _ = u30, u31, u32, u33
   118  
   119  	// float
   120  	var f00 float32 = 3.14159
   121  	var f01 float32 = -3.14159
   122  	var f02 float32 = +3.14159
   123  	var f03 float32 = 0.0
   124  	var f04 float32 = .0
   125  	var f05 float32 = 0.
   126  	var f06 float32 = -0.0
   127  	var f07 float32 = 1e10
   128  	var f08 float32 = -1e10
   129  	var f09 float32 = 1e-10
   130  	var f10 float32 = 1e+10
   131  	var f11 float32 = 1.e-10
   132  	var f12 float32 = 1.e+10
   133  	var f13 float32 = .1e-10
   134  	var f14 float32 = .1e+10
   135  	var f15 float32 = 1.1e-10
   136  	var f16 float32 = 1.1e+10
   137  	assert(f01 == -f00, "f01")
   138  	assert(f02 == -f01, "f02")
   139  	assert(f03 == f04, "f03")
   140  	assert(f04 == f05, "f04")
   141  	assert(f05 == f06, "f05")
   142  	assert(f07 == -f08, "f07")
   143  	assert(equal(f09, 1/f10), "f09")
   144  	assert(f11 == f09, "f11")
   145  	assert(f12 == f10, "f12")
   146  	assert(equal(f13, f09/10.0), "f13")
   147  	assert(equal(f14, f12/10.0), "f14")
   148  	assert(equal(f15, f16/1e20), "f15")
   149  
   150  	// character
   151  	var c0 uint8 = 'a'
   152  	var c1 uint8 = 'ä'
   153  	var c2 uint8 = '\a'
   154  	var c3 uint8 = '\b'
   155  	var c4 uint8 = '\f'
   156  	var c5 uint8 = '\n'
   157  	var c6 uint8 = '\r'
   158  	var c7 uint8 = '\t'
   159  	var c8 uint8 = '\v'
   160  	// var c9 uint8 = '本' // correctly caught as error
   161  	var c9 uint16 = '本'
   162  	assert(c0 == 0x61, "c0")
   163  	assert(c1 == 0xe4, "c1")
   164  	assert(c2 == 0x07, "c2")
   165  	assert(c3 == 0x08, "c3")
   166  	assert(c4 == 0x0c, "c4")
   167  	assert(c5 == 0x0a, "c4")
   168  	assert(c6 == 0x0d, "c6")
   169  	assert(c7 == 0x09, "c7")
   170  	assert(c8 == 0x0b, "c8")
   171  	assert(c9 == 0x672c, "c9")
   172  
   173  	var c00 uint8 = '\000'
   174  	var c01 uint8 = '\007'
   175  	var c02 uint8 = '\177'
   176  	var c03 uint8 = '\377'
   177  	assert(c00 == 0, "c00")
   178  	assert(c01 == 7, "c01")
   179  	assert(c02 == 127, "c02")
   180  	assert(c03 == 255, "c03")
   181  
   182  	var cx0 uint8 = '\x00'
   183  	var cx1 uint8 = '\x0f'
   184  	var cx2 uint8 = '\xff'
   185  	assert(cx0 == 0, "cx0")
   186  	assert(cx1 == 15, "cx1")
   187  	assert(cx2 == 255, "cx2")
   188  
   189  	var cu0 uint16 = '\u1234'
   190  	var cu1 uint32 = '\U00101234'
   191  	assert(cu0 == 0x1234, "cu0")
   192  	assert(cu1 == 0x101234, "cu1")
   193  
   194  	// string
   195  	var s0 string = ""
   196  	var s1 string = "hellô"
   197  	assert(s1[0] == 'h', "s1-0")
   198  	assert(s1[4] == 0xc3, "s1-4")
   199  	assert(s1[5] == 0xb4, "s1-5")
   200  	var s2 string = "\a\b\f\n\r\t\v"
   201  	_, _ = s0, s2
   202  
   203  	var s00 string = "\000"
   204  	var s01 string = "\007"
   205  	var s02 string = "\377"
   206  	assert(s00[0] == 0, "s00")
   207  	assert(s01[0] == 7, "s01")
   208  	assert(s02[0] == 255, "s02")
   209  
   210  	var x00 string = "\x00"
   211  	var x01 string = "\x0f"
   212  	var x02 string = "\xff"
   213  	assert(x00[0] == 0, "x00")
   214  	assert(x01[0] == 15, "x01")
   215  	assert(x02[0] == 255, "x02")
   216  
   217  	// these are all the same string
   218  	var sj0 string = "日本語"
   219  	var sj1 string = "\u65e5\u672c\u8a9e"
   220  	var sj2 string = "\U000065e5\U0000672c\U00008a9e"
   221  	var sj3 string = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
   222  	assert(sj0 == sj1, "sj1")
   223  	assert(sj0 == sj2, "sj2")
   224  	assert(sj0 == sj3, "sj3")
   225  
   226  	if nbad > 0 {
   227  		panic("literal failed")
   228  	}
   229  }