github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/binop.go (about)

     1  package main
     2  
     3  func main() {
     4  	println("string equality")
     5  	println(a == "a")
     6  	println(a == "b")
     7  	println(a != "a")
     8  	println(a != "b")
     9  	println("string inequality")
    10  	println(a > "b")
    11  	println("b" > a)
    12  	println("b" > "b")
    13  	println(a <= "b")
    14  	println("b" <= a)
    15  	println("b" <= "b")
    16  	println(a > "b")
    17  	println("b" > a)
    18  	println("b" > "b")
    19  	println(a <= "b")
    20  	println("b" <= a)
    21  	println("b" <= "b")
    22  	println(a < "aa")
    23  	println("aa" < a)
    24  	println("ab" < "aa")
    25  	println("aa" < "ab")
    26  
    27  	h := "hello"
    28  	println("h < h", h < h)
    29  	println("h <= h", h <= h)
    30  	println("h == h", h == h)
    31  	println("h >= h", h >= h)
    32  	println("h > h", h > h)
    33  
    34  	println("array equality")
    35  	println(a1 == [2]int{1, 2})
    36  	println(a1 != [2]int{1, 2})
    37  	println(a1 == [2]int{1, 3})
    38  	println(a1 == [2]int{2, 2})
    39  	println(a1 == [2]int{2, 1})
    40  	println(a1 != [2]int{2, 1})
    41  
    42  	println("struct equality")
    43  	println(s1 == Struct1{3, true})
    44  	println(s1 == Struct1{4, true})
    45  	println(s1 == Struct1{3, false})
    46  	println(s1 == Struct1{4, false})
    47  	println(s1 != Struct1{3, true})
    48  	println(s1 != Struct1{4, true})
    49  	println(s1 != Struct1{3, false})
    50  	println(s1 != Struct1{4, false})
    51  
    52  	println("blank fields in structs")
    53  	println(s2 == Struct2{"foo", 0.0, 5})
    54  	println(s2 == Struct2{"foo", 0.0, 7})
    55  	println(s2 == Struct2{"foo", 1.0, 5})
    56  	println(s2 == Struct2{"foo", 1.0, 7})
    57  
    58  	println("complex numbers")
    59  	println(c64 == 3+2i)
    60  	println(c64 == 4+2i)
    61  	println(c64 == 3+3i)
    62  	println(c64 != 3+2i)
    63  	println(c64 != 4+2i)
    64  	println(c64 != 3+3i)
    65  	println(c128 == 3+2i)
    66  	println(c128 == 4+2i)
    67  	println(c128 == 3+3i)
    68  	println(c128 != 3+2i)
    69  	println(c128 != 4+2i)
    70  	println(c128 != 3+3i)
    71  
    72  	println("shifts")
    73  	println(shlSimple == 4)
    74  	println(shlOverflow == 0)
    75  	println(shrSimple == 1)
    76  	println(shrOverflow == 0)
    77  	println(ashrNeg == -1)
    78  	println(ashrOverflow == 0)
    79  	println(ashrNegOverflow == -1)
    80  
    81  	// fix for a bug in constant numbers
    82  	println("constant number")
    83  	x := uint32(5)
    84  	println(uint32(x) / (20e0 / 1))
    85  
    86  	// check for signed integer overflow
    87  	println("-2147483648 / -1:", sdiv32(-2147483648, -1))
    88  	println("-2147483648 % -1:", srem32(-2147483648, -1))
    89  
    90  	type foo struct {
    91  		n   int
    92  		itf interface{}
    93  	}
    94  
    95  	var a interface{} = foo{3, float32(5)}
    96  	var b interface{} = foo{3, float32(3)}
    97  	var b2 interface{} = foo{3, float32(3)}
    98  	println("interface equality")
    99  	println("a==b", a == b)
   100  	println("b==b2", b == b2)
   101  }
   102  
   103  var x = true
   104  var y = false
   105  
   106  var a = "a"
   107  var s1 = Struct1{3, true}
   108  var s2 = Struct2{"foo", 0.0, 5}
   109  
   110  var a1 = [2]int{1, 2}
   111  
   112  var c64 = 3 + 2i
   113  var c128 = 4 + 3i
   114  
   115  type Int int
   116  
   117  type Struct1 struct {
   118  	i Int
   119  	b bool
   120  }
   121  
   122  type Struct2 struct {
   123  	s string
   124  	_ float64
   125  	i int
   126  }
   127  
   128  func shl(x uint, y uint) uint {
   129  	return x << y
   130  }
   131  
   132  func shr(x uint, y uint) uint {
   133  	return x >> y
   134  }
   135  
   136  func ashr(x int, y uint) int {
   137  	return x >> y
   138  }
   139  
   140  func sdiv32(x, y int32) int32 {
   141  	return x / y
   142  }
   143  
   144  func srem32(x, y int32) int32 {
   145  	return x % y
   146  }
   147  
   148  var shlSimple = shl(2, 1)
   149  var shlOverflow = shl(2, 1000)
   150  var shrSimple = shr(2, 1)
   151  var shrOverflow = shr(2, 1000000)
   152  var ashrNeg = ashr(-1, 1)
   153  var ashrOverflow = ashr(1, 1000000)
   154  var ashrNegOverflow = ashr(-1, 1000000)