]> Cypherpunks repositories - gostls13.git/commitdiff
add reflect to build, clean up test
authorRob Pike <r@golang.org>
Fri, 24 Oct 2008 00:13:34 +0000 (17:13 -0700)
committerRob Pike <r@golang.org>
Fri, 24 Oct 2008 00:13:34 +0000 (17:13 -0700)
R=rsc
DELTA=341  (188 added, 151 deleted, 2 changed)
OCL=17678
CL=17750

src/lib/clean.bash
src/lib/make.bash
src/lib/reflect/main.go [deleted file]
src/lib/reflect/test.bash [new file with mode: 0755]
src/lib/reflect/test.go [new file with mode: 0644]
src/run.bash

index e3d7b612f300abbb9fa1a9678c3ff4757d9635bb..cc9b09d1b959aca8cccecfc50caf3c6ec1063734 100755 (executable)
@@ -6,7 +6,7 @@
 
 rm -f $GOROOT/pkg/*
 
-for i in syscall os math net time http regexp
+for i in syscall os math net time http reflect regexp
 do
        cd $i
        make nuke
index 5cc976262cb32b0134d1b84734b31630e1657ed4..db056a371706326fca505b5beadfc0a119bfaf8a 100755 (executable)
@@ -33,7 +33,7 @@ do
        6g -o $GOROOT/pkg/$base.6 $i
 done
 
-for i in net time http regexp
+for i in net time http reflect regexp
 do
        echo; echo; echo %%%% making lib/$i %%%%; echo
        cd $i
diff --git a/src/lib/reflect/main.go b/src/lib/reflect/main.go
deleted file mode 100644 (file)
index c00f2b9..0000000
+++ /dev/null
@@ -1,155 +0,0 @@
-// 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
-
-import (
-       "reflect"
-)
-
-func typedump(s string) {
-       t := reflect.ParseTypeString("", s);
-       print(reflect.TypeToString(t, true),"; size = ", t.Size(), "\n");
-}
-
-func valuedump(s string) {
-       t := reflect.ParseTypeString("", s);
-       v := reflect.NewInitValue(t);
-       switch v.Kind() {
-       case reflect.Int8Kind:
-               v.(reflect.Int8Value).Put(8);
-       case reflect.Int16Kind:
-               v.(reflect.Int16Value).Put(16);
-       case reflect.Int32Kind:
-               v.(reflect.Int32Value).Put(32);
-       case reflect.Int64Kind:
-               v.(reflect.Int64Value).Put(64);
-       case reflect.Uint8Kind:
-               v.(reflect.Uint8Value).Put(8);
-       case reflect.Uint16Kind:
-               v.(reflect.Uint16Value).Put(16);
-       case reflect.Uint32Kind:
-               v.(reflect.Uint32Value).Put(32);
-       case reflect.Uint64Kind:
-               v.(reflect.Uint64Value).Put(64);
-       case reflect.Float32Kind:
-               v.(reflect.Float32Value).Put(32.0);
-       case reflect.Float64Kind:
-               v.(reflect.Float64Value).Put(64.0);
-       case reflect.StringKind:
-               v.(reflect.StringValue).Put("stringy cheese");
-       }
-       print(s, " value = ", reflect.ValueToString(v), "\n");
-}
-
-export type empty interface {}
-
-export type T struct { a int; b float64; c string; d *int }
-
-func main() {
-       var s string;
-       var t reflect.Type;
-
-if false{
-       typedump("int8");
-       typedump("int16");
-       typedump("int32");
-       typedump("int64");
-       typedump("uint8");
-       typedump("uint16");
-       typedump("uint32");
-       typedump("uint64");
-       typedump("float32");
-       typedump("float64");
-       typedump("float80");
-       typedump("int8");
-       typedump("**int8");
-       typedump("**P.integer");
-       typedump("[32]int32");
-       typedump("[]int8");
-       typedump("*map[string]int32");
-       typedump("*chan<-string");
-       typedump("struct {c *chan *int32; d float32}");
-       typedump("*(a int8, b int32)");
-       typedump("struct {c *(? *chan *P.integer, ? *int8)}");
-       typedump("struct {a int8; b int32}");
-       typedump("struct {a int8; b int8; b int32}");
-       typedump("struct {a int8; b int8; c int8; b int32}");
-       typedump("struct {a int8; b int8; c int8; d int8; b int32}");
-       typedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
-
-       valuedump("int8");
-       valuedump("int16");
-       valuedump("int32");
-       valuedump("int64");
-       valuedump("uint8");
-       valuedump("uint16");
-       valuedump("uint32");
-       valuedump("uint64");
-       valuedump("float32");
-       valuedump("float64");
-       valuedump("string");
-       valuedump("*int8");
-       valuedump("**int8");
-       valuedump("[32]int32");
-       valuedump("**P.integer");
-       valuedump("[32]int32");
-       valuedump("[]int8");
-       valuedump("*map[string]int32");
-       valuedump("*chan<-string");
-       valuedump("struct {c *chan *int32; d float32}");
-       valuedump("*(a int8, b int32)");
-       valuedump("struct {c *(? *chan *P.integer, ? *int8)}");
-       valuedump("struct {a int8; b int32}");
-       valuedump("struct {a int8; b int8; b int32}");
-       valuedump("struct {a int8; b int8; c int8; b int32}");
-       valuedump("struct {a int8; b int8; c int8; d int8; b int32}");
-       valuedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
-}
-{      var tmp = 123;
-       value := reflect.NewValue(tmp);
-       println(reflect.ValueToString(value));
-}
-{      var tmp = 123.4;
-       value := reflect.NewValue(tmp);
-       println(reflect.ValueToString(value));
-}
-{      var tmp = "abc";
-       value := reflect.NewValue(tmp);
-       println(reflect.ValueToString(value));
-}
-{
-       var i int = 7;
-       var tmp = &T{123, 456.0, "hello", &i};
-       value := reflect.NewValue(tmp);
-       println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
-}
-{
-       type C chan *T; // TODO: should not be necessary
-       var tmp = new(C);
-       value := reflect.NewValue(tmp);
-       println(reflect.ValueToString(value));
-}
-{
-       type A [10]int;
-       var tmp A = A{1,2,3,4,5,6,7,8,9,10};
-       value := reflect.NewValue(&tmp);
-       println(reflect.TypeToString(value.Type().(reflect.PtrType).Sub(), true));
-       println(reflect.TypeToString(value.(reflect.PtrValue).Sub().Type(), true));
-       println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
-       value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
-       println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
-}
-{
-       type AA []int;
-       tmp1 := [10]int{1,2,3,4,5,6,7,8,9,10};
-       var tmp *AA = &tmp1;
-       value := reflect.NewValue(tmp);
-       println(reflect.TypeToString(value.Type().(reflect.PtrType).Sub(), true));
-       println(reflect.TypeToString(value.(reflect.PtrValue).Sub().Type(), true));
-       println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
-       value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
-       println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
-}
-}
diff --git a/src/lib/reflect/test.bash b/src/lib/reflect/test.bash
new file mode 100755 (executable)
index 0000000..55862e6
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/bash
+# 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.
+
+set -e
+
+make
+6g test.go
+6l test.6
+6.out
+rm -f *.6 6.out
diff --git a/src/lib/reflect/test.go b/src/lib/reflect/test.go
new file mode 100644 (file)
index 0000000..615dda8
--- /dev/null
@@ -0,0 +1,177 @@
+// 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
+
+import (
+       "reflect"
+)
+
+var doprint bool = false
+
+func is_digit(c uint8) bool {
+       return '0' <= c && c <= '9'
+}
+
+// streq, but '@' in t matches a string of digits
+func match(s, t string) bool {
+       for i, j := 0, 0; i < len(s) && j < len(t); i, j = i+1, j+1 {
+               if s[i] == t[j] {
+                       continue
+               }
+               if is_digit(s[i]) && t[j] == '@' {
+                       for is_digit(s[i+1]) {
+                               i++
+                       }
+               } else {
+                       return false
+               }
+       }
+       return true;
+}
+
+func assert(s, t string) {
+       if doprint {
+               println(t)
+       }
+       if !match(s, t) {
+               panicln(s, t)
+       }
+}
+
+func typedump(s, t string) {
+       typ := reflect.ParseTypeString("", s);
+       assert(reflect.TypeToString(typ, true), t);
+}
+
+func valuedump(s, t string) {
+       typ := reflect.ParseTypeString("", s);
+       v := reflect.NewInitValue(typ);
+       switch v.Kind() {
+       case reflect.Int8Kind:
+               v.(reflect.Int8Value).Put(8);
+       case reflect.Int16Kind:
+               v.(reflect.Int16Value).Put(16);
+       case reflect.Int32Kind:
+               v.(reflect.Int32Value).Put(32);
+       case reflect.Int64Kind:
+               v.(reflect.Int64Value).Put(64);
+       case reflect.Uint8Kind:
+               v.(reflect.Uint8Value).Put(8);
+       case reflect.Uint16Kind:
+               v.(reflect.Uint16Value).Put(16);
+       case reflect.Uint32Kind:
+               v.(reflect.Uint32Value).Put(32);
+       case reflect.Uint64Kind:
+               v.(reflect.Uint64Value).Put(64);
+       case reflect.Float32Kind:
+               v.(reflect.Float32Value).Put(32.0);
+       case reflect.Float64Kind:
+               v.(reflect.Float64Value).Put(64.0);
+       case reflect.StringKind:
+               v.(reflect.StringValue).Put("stringy cheese");
+       }
+       assert(reflect.ValueToString(v), t);
+}
+
+export type empty interface {}
+
+export type T struct { a int; b float64; c string; d *int }
+
+func main() {
+       var s string;
+       var t reflect.Type;
+
+       typedump("int8", "int8");
+       typedump("int16", "int16");
+       typedump("int32", "int32");
+       typedump("int64", "int64");
+       typedump("uint8", "uint8");
+       typedump("uint16", "uint16");
+       typedump("uint32", "uint32");
+       typedump("uint64", "uint64");
+       typedump("float32", "float32");
+       typedump("float64", "float64");
+       typedump("float80", "float80");
+       typedump("int8", "int8");
+       typedump("**int8", "**int8");
+       typedump("**P.integer", "**P.integer");
+       typedump("[32]int32", "[32]int32");
+       typedump("[]int8", "[]int8");
+       typedump("*map[string]int32", "*map[string]int32");
+       typedump("*chan<-string", "*chan<-string");
+       typedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}");
+       typedump("*(a int8, b int32)", "*(a int8, b int32)");
+       typedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(? *chan*P.integer, ? *int8)}");
+       typedump("struct {a int8; b int32}", "struct{a int8; b int32}");
+       typedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}");
+       typedump("struct {a int8; b int8; c int8; b int32}", "struct{a int8; b int8; c int8; b int32}");
+       typedump("struct {a int8; b int8; c int8; d int8; b int32}", "struct{a int8; b int8; c int8; d int8; b int32}");
+       typedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}", "struct{a int8; b int8; c int8; d int8; e int8; b int32}");
+
+       valuedump("int8", "8");
+       valuedump("int16", "16");
+       valuedump("int32", "32");
+       valuedump("int64", "64");
+       valuedump("uint8", "8");
+       valuedump("uint16", "16");
+       valuedump("uint32", "32");
+       valuedump("uint64", "64");
+       valuedump("float32", "+3.200000e+01");
+       valuedump("float64", "+6.400000e+01");
+       valuedump("string", "stringy cheese");
+       valuedump("*int8", "*int8(0)");
+       valuedump("**int8", "**int8(0)");
+       valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}");
+       valuedump("**P.integer", "**P.integer(0)");
+       valuedump("*map[string]int32", "*map[string]int32(0)");
+       valuedump("*chan<-string", "*chan<-string(0)");
+       valuedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}{*chan*int32(0), +0.000000e+00}");
+       valuedump("*(a int8, b int32)", "*(a int8, b int32)(0)");
+       valuedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(? *chan*P.integer, ? *int8)}{*(? *chan*P.integer, ? *int8)(0)}");
+       valuedump("struct {a int8; b int32}", "struct{a int8; b int32}{0, 0}");
+       valuedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}{0, 0, 0}");
+
+       {       var tmp = 123;
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value), "123");
+       }
+       {       var tmp = 123.4;
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value), "+1.234000e+02");
+       }
+       {       var tmp = "abc";
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value), "abc");
+       }
+       {
+               var i int = 7;
+               var tmp = &T{123, 456.0, "hello", &i};
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.T{123, +4.560000e+02, hello, *int32(134980)}");
+       }
+       {
+               type C chan *T; // TODO: should not be necessary
+               var tmp = new(C);
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value), "*main.C_test(@)");
+       }
+       {
+               type A [10]int;
+               var tmp A = A{1,2,3,4,5,6,7,8,9,10};
+               value := reflect.NewValue(&tmp);
+               assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
+               value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
+               assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
+       }
+       {
+               type AA []int;
+               tmp1 := [10]int{1,2,3,4,5,6,7,8,9,10};  // TODO: should not be necessary to use tmp1
+               var tmp *AA = &tmp1;
+               value := reflect.NewValue(tmp);
+               assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
+               value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
+               assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
+       }
+}
index bd3067e35c243f0b6da636c972ec3eb557da0b1b..45154e04905a5bbc950147b1a1b5365d608e7b47 100755 (executable)
@@ -10,6 +10,12 @@ xcd() {
        echo --- cd $1
 }
 
+(xcd lib/reflect
+make clean
+time make
+bash test.bash
+)
+
 (xcd lib/regexp
 make clean
 time make