Source file test/ken/sliceslice.go

     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 slicing and re-slicing.
     8  
     9  package main
    10  
    11  var bx []byte
    12  var by []byte
    13  var fx []float64
    14  var fy []float64
    15  var lb, hb int
    16  var t int
    17  
    18  func main() {
    19  
    20  	// width 1 (byte)
    21  	lb = 0
    22  	hb = 10
    23  	by = bx[lb:hb]
    24  	tstb()
    25  	by = bx[lb:10]
    26  	tstb()
    27  	by = bx[lb:]
    28  	tstb()
    29  	by = bx[:hb]
    30  	tstb()
    31  	by = bx[0:hb]
    32  	tstb()
    33  	by = bx[0:10]
    34  	tstb()
    35  	by = bx[0:]
    36  	tstb()
    37  	by = bx[:10]
    38  	tstb()
    39  	by = bx[:]
    40  	tstb()
    41  
    42  	lb = 2
    43  	hb = 10
    44  	by = bx[lb:hb]
    45  	tstb()
    46  	by = bx[lb:10]
    47  	tstb()
    48  	by = bx[lb:]
    49  	tstb()
    50  	by = bx[2:hb]
    51  	tstb()
    52  	by = bx[2:10]
    53  	tstb()
    54  	by = bx[2:]
    55  	tstb()
    56  
    57  	lb = 0
    58  	hb = 8
    59  	by = bx[lb:hb]
    60  	tstb()
    61  	by = bx[lb:8]
    62  	tstb()
    63  	by = bx[0:hb]
    64  	tstb()
    65  	by = bx[0:8]
    66  	tstb()
    67  	by = bx[:8]
    68  	tstb()
    69  	by = bx[:hb]
    70  	tstb()
    71  
    72  	lb = 2
    73  	hb = 8
    74  	by = bx[lb:hb]
    75  	tstb()
    76  	by = bx[lb:8]
    77  	tstb()
    78  	by = bx[2:hb]
    79  	tstb()
    80  	by = bx[2:8]
    81  	tstb()
    82  
    83  	// width 4 (float64)
    84  	lb = 0
    85  	hb = 10
    86  	fy = fx[lb:hb]
    87  	tstf()
    88  	fy = fx[lb:10]
    89  	tstf()
    90  	fy = fx[lb:]
    91  	tstf()
    92  	fy = fx[:hb]
    93  	tstf()
    94  	fy = fx[0:hb]
    95  	tstf()
    96  	fy = fx[0:10]
    97  	tstf()
    98  	fy = fx[0:]
    99  	tstf()
   100  	fy = fx[:10]
   101  	tstf()
   102  	fy = fx[:]
   103  	tstf()
   104  
   105  	lb = 2
   106  	hb = 10
   107  	fy = fx[lb:hb]
   108  	tstf()
   109  	fy = fx[lb:10]
   110  	tstf()
   111  	fy = fx[lb:]
   112  	tstf()
   113  	fy = fx[2:hb]
   114  	tstf()
   115  	fy = fx[2:10]
   116  	tstf()
   117  	fy = fx[2:]
   118  	tstf()
   119  
   120  	lb = 0
   121  	hb = 8
   122  	fy = fx[lb:hb]
   123  	tstf()
   124  	fy = fx[lb:8]
   125  	tstf()
   126  	fy = fx[:hb]
   127  	tstf()
   128  	fy = fx[0:hb]
   129  	tstf()
   130  	fy = fx[0:8]
   131  	tstf()
   132  	fy = fx[:8]
   133  	tstf()
   134  
   135  	lb = 2
   136  	hb = 8
   137  	fy = fx[lb:hb]
   138  	tstf()
   139  	fy = fx[lb:8]
   140  	tstf()
   141  	fy = fx[2:hb]
   142  	tstf()
   143  	fy = fx[2:8]
   144  	tstf()
   145  }
   146  
   147  func tstb() {
   148  	t++
   149  	if len(by) != hb-lb {
   150  		println("t=", t, "lb=", lb, "hb=", hb,
   151  			"len=", len(by), "hb-lb=", hb-lb)
   152  		panic("fail")
   153  	}
   154  	if cap(by) != len(bx)-lb {
   155  		println("t=", t, "lb=", lb, "hb=", hb,
   156  			"cap=", cap(by), "len(bx)-lb=", len(bx)-lb)
   157  		panic("fail")
   158  	}
   159  	for i := lb; i < hb; i++ {
   160  		if bx[i] != by[i-lb] {
   161  			println("t=", t, "lb=", lb, "hb=", hb,
   162  				"bx[", i, "]=", bx[i],
   163  				"by[", i-lb, "]=", by[i-lb])
   164  			panic("fail")
   165  		}
   166  	}
   167  	by = nil
   168  }
   169  
   170  func tstf() {
   171  	t++
   172  	if len(fy) != hb-lb {
   173  		println("t=", t, "lb=", lb, "hb=", hb,
   174  			"len=", len(fy), "hb-lb=", hb-lb)
   175  		panic("fail")
   176  	}
   177  	if cap(fy) != len(fx)-lb {
   178  		println("t=", t, "lb=", lb, "hb=", hb,
   179  			"cap=", cap(fy), "len(fx)-lb=", len(fx)-lb)
   180  		panic("fail")
   181  	}
   182  	for i := lb; i < hb; i++ {
   183  		if fx[i] != fy[i-lb] {
   184  			println("t=", t, "lb=", lb, "hb=", hb,
   185  				"fx[", i, "]=", fx[i],
   186  				"fy[", i-lb, "]=", fy[i-lb])
   187  			panic("fail")
   188  		}
   189  	}
   190  	fy = nil
   191  }
   192  
   193  func init() {
   194  	bx = make([]byte, 10)
   195  	for i := 0; i < len(bx); i++ {
   196  		bx[i] = byte(i + 20)
   197  	}
   198  	by = nil
   199  
   200  	fx = make([]float64, 10)
   201  	for i := 0; i < len(fx); i++ {
   202  		fx[i] = float64(i + 20)
   203  	}
   204  	fy = nil
   205  }
   206  

View as plain text