| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | // $G $D/$F.go && $L $F.$A && ./$A.out | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Copyright 2009 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package	main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func | 
					
						
							| 
									
										
										
										
											2008-12-18 20:06:28 -08:00
										 |  |  | setpd(a []int) | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | { | 
					
						
							|  |  |  | //	print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); | 
					
						
							|  |  |  | 	for i:=0; i<len(a); i++ { | 
					
						
							|  |  |  | 		a[i] = i; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func | 
					
						
							| 
									
										
										
										
											2008-12-18 20:06:28 -08:00
										 |  |  | sumpd(a []int) int | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | { | 
					
						
							|  |  |  | //	print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); | 
					
						
							|  |  |  | 	t := 0; | 
					
						
							|  |  |  | 	for i:=0; i<len(a); i++ { | 
					
						
							|  |  |  | 		t += a[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | //	print("sumpd t=", t, "\n"); | 
					
						
							|  |  |  | 	return t; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func | 
					
						
							|  |  |  | setpf(a *[20]int) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //	print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); | 
					
						
							|  |  |  | 	for i:=0; i<len(a); i++ { | 
					
						
							|  |  |  | 		a[i] = i; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func | 
					
						
							|  |  |  | sumpf(a *[20]int) int | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //	print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); | 
					
						
							|  |  |  | 	t := 0; | 
					
						
							|  |  |  | 	for i:=0; i<len(a); i++ { | 
					
						
							|  |  |  | 		t += a[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | //	print("sumpf t=", t, "\n"); | 
					
						
							|  |  |  | 	return t; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | res(t int, lb, hb int) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	sb := (hb-lb)*(hb+lb-1)/2; | 
					
						
							|  |  |  | 	if t != sb { | 
					
						
							|  |  |  | 		print(	"lb=", lb, | 
					
						
							|  |  |  | 			"; hb=", hb, | 
					
						
							|  |  |  | 			"; t=", t, | 
					
						
							|  |  |  | 			"; sb=", sb, | 
					
						
							|  |  |  | 			"\n"); | 
					
						
							|  |  |  | 		panic("res") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // call ptr dynamic with ptr dynamic | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testpdpd() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	a := new([]int, 10, 100); | 
					
						
							|  |  |  | 	if len(a) != 10 && cap(a) != 100 { | 
					
						
							|  |  |  | 		panic("len and cap from new: ", len(a), " ", cap(a), "\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a = a[0:100]; | 
					
						
							|  |  |  | 	setpd(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a = a[0:10]; | 
					
						
							|  |  |  | 	res(sumpd(a), 0, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a = a[5:25]; | 
					
						
							|  |  |  | 	res(sumpd(a), 5, 25); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // call ptr fixed with ptr fixed | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testpfpf() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	var a [20]int; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setpf(&a); | 
					
						
							|  |  |  | 	res(sumpf(&a), 0, 20); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // call ptr dynamic with ptr fixed from new | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testpdpf1() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-19 03:05:37 -08:00
										 |  |  | 	a := new(*[40]int); | 
					
						
							| 
									
										
										
										
											2008-12-20 13:38:29 -08:00
										 |  |  | 	setpd(*a); | 
					
						
							|  |  |  | 	res(sumpd(*a), 0, 40); | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-20 13:38:29 -08:00
										 |  |  | 	b := (*a)[5:30]; | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | 	res(sumpd(b), 5, 30); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // call ptr dynamic with ptr fixed from var | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testpdpf2() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	var a [80]int; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-18 20:06:28 -08:00
										 |  |  | 	setpd(a); | 
					
						
							|  |  |  | 	res(sumpd(a), 0, 80); | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // generate bounds error with ptr dynamic | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testpdfault() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	a := new([]int, 100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print("good\n"); | 
					
						
							|  |  |  | 	for i:=0; i<100; i++ { | 
					
						
							|  |  |  | 		a[i] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	print("should fault\n"); | 
					
						
							|  |  |  | 	a[100] = 0; | 
					
						
							|  |  |  | 	print("bad\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // generate bounds error with ptr fixed | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | testfdfault() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	var a [80]int; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print("good\n"); | 
					
						
							|  |  |  | 	for i:=0; i<80; i++ { | 
					
						
							|  |  |  | 		a[i] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	print("should fault\n"); | 
					
						
							| 
									
										
										
										
											2008-11-24 16:23:49 -08:00
										 |  |  | 	x := 80; | 
					
						
							|  |  |  | 	a[x] = 0; | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | 	print("bad\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | main() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-03 13:21:05 -07:00
										 |  |  | 	testpdpd(); | 
					
						
							|  |  |  | 	testpfpf(); | 
					
						
							|  |  |  | 	testpdpf1(); | 
					
						
							|  |  |  | 	testpdpf2(); | 
					
						
							| 
									
										
										
										
											2008-08-29 13:24:53 -07:00
										 |  |  | //	print("testpdfault\n");	testpdfault(); | 
					
						
							|  |  |  | //	print("testfdfault\n");	testfdfault(); | 
					
						
							|  |  |  | } |