Home | History | Annotate | Download | only in ken
      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 arrays and slices.
      8 
      9 package main
     10 
     11 func setpd(a []int) {
     12 	//	print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
     13 	for i := 0; i < len(a); i++ {
     14 		a[i] = i
     15 	}
     16 }
     17 
     18 func sumpd(a []int) int {
     19 	//	print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
     20 	t := 0
     21 	for i := 0; i < len(a); i++ {
     22 		t += a[i]
     23 	}
     24 	//	print("sumpd t=", t, "\n");
     25 	return t
     26 }
     27 
     28 func setpf(a *[20]int) {
     29 	//	print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
     30 	for i := 0; i < len(a); i++ {
     31 		a[i] = i
     32 	}
     33 }
     34 
     35 func sumpf(a *[20]int) int {
     36 	//	print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
     37 	t := 0
     38 	for i := 0; i < len(a); i++ {
     39 		t += a[i]
     40 	}
     41 	//	print("sumpf t=", t, "\n");
     42 	return t
     43 }
     44 
     45 func res(t int, lb, hb int) {
     46 	sb := (hb - lb) * (hb + lb - 1) / 2
     47 	if t != sb {
     48 		print("lb=", lb,
     49 			"; hb=", hb,
     50 			"; t=", t,
     51 			"; sb=", sb,
     52 			"\n")
     53 		panic("res")
     54 	}
     55 }
     56 
     57 // call ptr dynamic with ptr dynamic
     58 func testpdpd() {
     59 	a := make([]int, 10, 100)
     60 	if len(a) != 10 && cap(a) != 100 {
     61 		print("len and cap from new: ", len(a), " ", cap(a), "\n")
     62 		panic("fail")
     63 	}
     64 
     65 	a = a[0:100]
     66 	setpd(a)
     67 
     68 	a = a[0:10]
     69 	res(sumpd(a), 0, 10)
     70 
     71 	a = a[5:25]
     72 	res(sumpd(a), 5, 25)
     73 
     74 	a = a[30:95]
     75 	res(sumpd(a), 35, 100)
     76 }
     77 
     78 // call ptr fixed with ptr fixed
     79 func testpfpf() {
     80 	var a [20]int
     81 
     82 	setpf(&a)
     83 	res(sumpf(&a), 0, 20)
     84 }
     85 
     86 // call ptr dynamic with ptr fixed from new
     87 func testpdpf1() {
     88 	a := new([40]int)
     89 	setpd(a[0:])
     90 	res(sumpd(a[0:]), 0, 40)
     91 
     92 	b := (*a)[5:30]
     93 	res(sumpd(b), 5, 30)
     94 }
     95 
     96 // call ptr dynamic with ptr fixed from var
     97 func testpdpf2() {
     98 	var a [80]int
     99 
    100 	setpd(a[0:])
    101 	res(sumpd(a[0:]), 0, 80)
    102 }
    103 
    104 // generate bounds error with ptr dynamic
    105 func testpdfault() {
    106 	a := make([]int, 100)
    107 
    108 	print("good\n")
    109 	for i := 0; i < 100; i++ {
    110 		a[i] = 0
    111 	}
    112 	print("should fault\n")
    113 	a[100] = 0
    114 	print("bad\n")
    115 }
    116 
    117 // generate bounds error with ptr fixed
    118 func testfdfault() {
    119 	var a [80]int
    120 
    121 	print("good\n")
    122 	for i := 0; i < 80; i++ {
    123 		a[i] = 0
    124 	}
    125 	print("should fault\n")
    126 	x := 80
    127 	a[x] = 0
    128 	print("bad\n")
    129 }
    130 
    131 func main() {
    132 	testpdpd()
    133 	testpfpf()
    134 	testpdpf1()
    135 	testpdpf2()
    136 	//	print("testpdfault\n");	testpdfault();
    137 	//	print("testfdfault\n");	testfdfault();
    138 }
    139