Source file test/fixedbugs/issue19113.go

     1  // run
     2  
     3  // Copyright 2019 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  package main
     8  
     9  import "reflect"
    10  
    11  var tests = []interface{}{
    12  	func(x int, s int) int {
    13  		return x << s
    14  	},
    15  	func(x int, s int64) int {
    16  		return x << s
    17  	},
    18  	func(x int, s int32) int {
    19  		return x << s
    20  	},
    21  	func(x int, s int16) int {
    22  		return x << s
    23  	},
    24  	func(x int, s int8) int {
    25  		return x << s
    26  	},
    27  	func(x int, s int) int {
    28  		return x >> s
    29  	},
    30  	func(x int, s int64) int {
    31  		return x >> s
    32  	},
    33  	func(x int, s int32) int {
    34  		return x >> s
    35  	},
    36  	func(x int, s int16) int {
    37  		return x >> s
    38  	},
    39  	func(x int, s int8) int {
    40  		return x >> s
    41  	},
    42  	func(x uint, s int) uint {
    43  		return x << s
    44  	},
    45  	func(x uint, s int64) uint {
    46  		return x << s
    47  	},
    48  	func(x uint, s int32) uint {
    49  		return x << s
    50  	},
    51  	func(x uint, s int16) uint {
    52  		return x << s
    53  	},
    54  	func(x uint, s int8) uint {
    55  		return x << s
    56  	},
    57  	func(x uint, s int) uint {
    58  		return x >> s
    59  	},
    60  	func(x uint, s int64) uint {
    61  		return x >> s
    62  	},
    63  	func(x uint, s int32) uint {
    64  		return x >> s
    65  	},
    66  	func(x uint, s int16) uint {
    67  		return x >> s
    68  	},
    69  	func(x uint, s int8) uint {
    70  		return x >> s
    71  	},
    72  }
    73  
    74  func main() {
    75  	for _, t := range tests {
    76  		runTest(reflect.ValueOf(t))
    77  	}
    78  }
    79  
    80  func runTest(f reflect.Value) {
    81  	xt := f.Type().In(0)
    82  	st := f.Type().In(1)
    83  
    84  	for _, x := range []int{1, 0, -1} {
    85  		for _, s := range []int{-99, -64, -63, -32, -31, -16, -15, -8, -7, -1, 0, 1, 7, 8, 15, 16, 31, 32, 63, 64, 99} {
    86  			args := []reflect.Value{
    87  				reflect.ValueOf(x).Convert(xt),
    88  				reflect.ValueOf(s).Convert(st),
    89  			}
    90  			if s < 0 {
    91  				shouldPanic(func() {
    92  					f.Call(args)
    93  				})
    94  			} else {
    95  				f.Call(args) // should not panic
    96  			}
    97  		}
    98  	}
    99  }
   100  
   101  func shouldPanic(f func()) {
   102  	defer func() {
   103  		if recover() == nil {
   104  			panic("did not panic")
   105  		}
   106  	}()
   107  	f()
   108  }
   109  

View as plain text