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

View as plain text