Source file test/fixedbugs/issue30116u.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  // This test makes sure the text output for bounds check failures is as expected.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"os"
    14  	"runtime"
    15  	"text/tabwriter"
    16  )
    17  
    18  // Testing with length 3 slices, arrays, and strings.
    19  // A large (>1<<32) value is included to test 32-bit platforms.
    20  var indexes = []uint64{0, 2, 3, 1<<32 - 1, 1<<64 - 1}
    21  var slices = []uint64{0, 3, 4, 1<<32 - 1, 1<<64 - 1}
    22  
    23  var w *tabwriter.Writer
    24  
    25  func main() {
    26  	w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
    27  	defer w.Flush()
    28  	doIndex()
    29  	doSlice()
    30  	doSlice3()
    31  }
    32  func doIndex() {
    33  	a := []int{1, 2, 3}
    34  	for _, i := range indexes {
    35  		printPanic(fmt.Sprintf("slice[%d]", i), func() {
    36  			_ = a[i]
    37  		})
    38  	}
    39  	b := [3]int{1, 2, 3}
    40  	for _, i := range indexes {
    41  		printPanic(fmt.Sprintf("array[%d]", i), func() {
    42  			_ = b[i]
    43  		})
    44  	}
    45  	c := "123"
    46  	for _, i := range indexes {
    47  		printPanic(fmt.Sprintf("string[%d]", i), func() {
    48  			_ = c[i]
    49  		})
    50  	}
    51  }
    52  
    53  func doSlice() {
    54  	a := []int{1, 2, 3}
    55  	for _, i := range slices {
    56  		for _, j := range slices {
    57  			printPanic(fmt.Sprintf("slice[%d:%d]", i, j), func() {
    58  				_ = a[i:j]
    59  			})
    60  		}
    61  	}
    62  	b := [3]int{1, 2, 3}
    63  	for _, i := range slices {
    64  		for _, j := range slices {
    65  			printPanic(fmt.Sprintf("array[%d:%d]", i, j), func() {
    66  				_ = b[i:j]
    67  			})
    68  		}
    69  	}
    70  	c := "123"
    71  	for _, i := range slices {
    72  		for _, j := range slices {
    73  			printPanic(fmt.Sprintf("string[%d:%d]", i, j), func() {
    74  				_ = c[i:j]
    75  			})
    76  		}
    77  	}
    78  }
    79  
    80  func doSlice3() {
    81  	a := []int{1, 2, 3}
    82  	for _, i := range slices {
    83  		for _, j := range slices {
    84  			for _, k := range slices {
    85  				printPanic(fmt.Sprintf("slice[%d:%d:%d]", i, j, k), func() {
    86  					_ = a[i:j:k]
    87  				})
    88  			}
    89  		}
    90  	}
    91  	b := [3]int{1, 2, 3}
    92  	for _, i := range slices {
    93  		for _, j := range slices {
    94  			for _, k := range slices {
    95  				printPanic(fmt.Sprintf("array[%d:%d:%d]", i, j, k), func() {
    96  					_ = b[i:j:k]
    97  				})
    98  			}
    99  		}
   100  	}
   101  }
   102  
   103  func printPanic(msg string, f func()) {
   104  	defer func() {
   105  		res := "no panic"
   106  		if e := recover(); e != nil {
   107  			res = e.(runtime.Error).Error()
   108  		}
   109  		fmt.Fprintf(w, "%s\t %s\n", msg, res)
   110  	}()
   111  	f()
   112  }
   113  

View as plain text