2008-10-23 18:13:34 -06:00
|
|
|
// 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.
|
|
|
|
|
2008-11-19 20:11:01 -07:00
|
|
|
package reflect
|
2008-10-23 18:13:34 -06:00
|
|
|
|
|
|
|
import (
|
2009-05-21 12:50:20 -06:00
|
|
|
"io";
|
|
|
|
"os";
|
2008-11-19 20:11:01 -07:00
|
|
|
"reflect";
|
2009-04-29 23:16:53 -06:00
|
|
|
"testing";
|
|
|
|
"unsafe";
|
2008-10-23 18:13:34 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2009-05-21 12:50:20 -06:00
|
|
|
typ := ParseTypeString("", s);
|
|
|
|
assert(typeToString(typ, true), t);
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func valuedump(s, t string) {
|
2009-05-21 12:50:20 -06:00
|
|
|
typ := ParseTypeString("", s);
|
|
|
|
v := NewZeroValue(typ);
|
2008-12-19 04:06:19 -07:00
|
|
|
if v == nil {
|
|
|
|
panicln("valuedump", s);
|
|
|
|
}
|
2008-10-23 18:13:34 -06:00
|
|
|
switch v.Kind() {
|
2009-05-21 12:50:20 -06:00
|
|
|
case IntKind:
|
|
|
|
v.(IntValue).Set(132);
|
|
|
|
case Int8Kind:
|
|
|
|
v.(Int8Value).Set(8);
|
|
|
|
case Int16Kind:
|
|
|
|
v.(Int16Value).Set(16);
|
|
|
|
case Int32Kind:
|
|
|
|
v.(Int32Value).Set(32);
|
|
|
|
case Int64Kind:
|
|
|
|
v.(Int64Value).Set(64);
|
|
|
|
case UintKind:
|
|
|
|
v.(UintValue).Set(132);
|
|
|
|
case Uint8Kind:
|
|
|
|
v.(Uint8Value).Set(8);
|
|
|
|
case Uint16Kind:
|
|
|
|
v.(Uint16Value).Set(16);
|
|
|
|
case Uint32Kind:
|
|
|
|
v.(Uint32Value).Set(32);
|
|
|
|
case Uint64Kind:
|
|
|
|
v.(Uint64Value).Set(64);
|
|
|
|
case FloatKind:
|
|
|
|
v.(FloatValue).Set(3200.0);
|
|
|
|
case Float32Kind:
|
|
|
|
v.(Float32Value).Set(32.1);
|
|
|
|
case Float64Kind:
|
|
|
|
v.(Float64Value).Set(64.2);
|
|
|
|
case StringKind:
|
|
|
|
v.(StringValue).Set("stringy cheese");
|
|
|
|
case BoolKind:
|
|
|
|
v.(BoolValue).Set(true);
|
|
|
|
}
|
|
|
|
assert(valueToString(v), t);
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type T struct { a int; b float64; c string; d *int }
|
2008-10-23 18:13:34 -06:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAll(tt *testing.T) { // TODO(r): wrap up better
|
2008-10-23 18:13:34 -06:00
|
|
|
var s string;
|
2009-05-21 12:50:20 -06:00
|
|
|
var t Type;
|
2008-10-23 18:13:34 -06:00
|
|
|
|
2008-11-04 23:54:11 -07:00
|
|
|
// Types
|
2008-11-02 13:32:14 -07:00
|
|
|
typedump("missing", "$missing$");
|
2008-10-29 16:31:02 -06:00
|
|
|
typedump("int", "int");
|
2008-10-23 18:13:34 -06:00
|
|
|
typedump("int8", "int8");
|
|
|
|
typedump("int16", "int16");
|
|
|
|
typedump("int32", "int32");
|
|
|
|
typedump("int64", "int64");
|
2008-10-29 16:31:02 -06:00
|
|
|
typedump("uint", "uint");
|
2008-10-23 18:13:34 -06:00
|
|
|
typedump("uint8", "uint8");
|
|
|
|
typedump("uint16", "uint16");
|
|
|
|
typedump("uint32", "uint32");
|
|
|
|
typedump("uint64", "uint64");
|
2008-10-29 16:31:02 -06:00
|
|
|
typedump("float", "float");
|
2008-10-23 18:13:34 -06:00
|
|
|
typedump("float32", "float32");
|
|
|
|
typedump("float64", "float64");
|
|
|
|
typedump("int8", "int8");
|
2008-11-02 13:32:14 -07:00
|
|
|
typedump("whoknows.whatsthis", "$missing$");
|
2008-10-23 18:13:34 -06:00
|
|
|
typedump("**int8", "**int8");
|
|
|
|
typedump("**P.integer", "**P.integer");
|
|
|
|
typedump("[32]int32", "[32]int32");
|
|
|
|
typedump("[]int8", "[]int8");
|
2008-12-19 04:06:19 -07:00
|
|
|
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}");
|
2009-04-14 07:46:01 -06:00
|
|
|
typedump("func(a int8, b int32)", "func(a int8, b int32)");
|
|
|
|
typedump("struct {c func(? chan *P.integer, ? *int8)}", "struct{c func(chan*P.integer, *int8)}");
|
2008-10-23 18:13:34 -06:00
|
|
|
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}");
|
2008-10-30 18:29:53 -06:00
|
|
|
typedump("struct {a int8 \"hi there\"; }", "struct{a int8 \"hi there\"}");
|
2008-10-31 16:26:14 -06:00
|
|
|
typedump("struct {a int8 \"hi \\x00there\\t\\n\\\"\\\\\"; }", "struct{a int8 \"hi \\x00there\\t\\n\\\"\\\\\"}");
|
2009-04-14 07:46:01 -06:00
|
|
|
typedump("struct {f func(args ...)}", "struct{f func(args ...)}");
|
|
|
|
typedump("interface { a(? func(? func(? int) int) func(? func(? int)) int); b() }", "interface{a (func(func(int)(int))(func(func(int))(int))); b ()}");
|
2008-10-23 18:13:34 -06:00
|
|
|
|
2008-11-04 23:54:11 -07:00
|
|
|
// Values
|
2008-10-23 18:13:34 -06:00
|
|
|
valuedump("int8", "8");
|
|
|
|
valuedump("int16", "16");
|
|
|
|
valuedump("int32", "32");
|
|
|
|
valuedump("int64", "64");
|
|
|
|
valuedump("uint8", "8");
|
|
|
|
valuedump("uint16", "16");
|
|
|
|
valuedump("uint32", "32");
|
|
|
|
valuedump("uint64", "64");
|
2008-11-17 13:34:03 -07:00
|
|
|
valuedump("float32", "32.1");
|
|
|
|
valuedump("float64", "64.2");
|
2008-10-23 18:13:34 -06:00
|
|
|
valuedump("string", "stringy cheese");
|
2008-10-31 17:34:47 -06:00
|
|
|
valuedump("bool", "true");
|
2008-10-23 18:13:34 -06:00
|
|
|
valuedump("*int8", "*int8(0)");
|
|
|
|
valuedump("**int8", "**int8(0)");
|
|
|
|
valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}");
|
|
|
|
valuedump("**P.integer", "**P.integer(0)");
|
2008-12-19 04:06:19 -07:00
|
|
|
valuedump("map[string]int32", "map[string]int32{<can't iterate on maps>}");
|
|
|
|
valuedump("chan<-string", "chan<-string");
|
|
|
|
valuedump("struct {c chan *int32; d float32}", "struct{c chan*int32; d float32}{chan*int32, 0}");
|
2009-04-14 07:46:01 -06:00
|
|
|
valuedump("func(a int8, b int32)", "func(a int8, b int32)(0)");
|
|
|
|
valuedump("struct {c func(? chan *P.integer, ? *int8)}", "struct{c func(chan*P.integer, *int8)}{func(chan*P.integer, *int8)(0)}");
|
2008-10-23 18:13:34 -06:00
|
|
|
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;
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value), "123");
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
|
|
|
{ var tmp = 123.4;
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value), "123.4");
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
2009-03-27 14:43:50 -06:00
|
|
|
{
|
|
|
|
var tmp = byte(123);
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value), "123");
|
|
|
|
assert(typeToString(value.Type(), false), "uint8");
|
2009-03-27 14:43:50 -06:00
|
|
|
}
|
2008-10-23 18:13:34 -06:00
|
|
|
{ var tmp = "abc";
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value), "abc");
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
|
|
|
{
|
|
|
|
var i int = 7;
|
2009-03-03 09:39:12 -07:00
|
|
|
var tmp = &T{123, 456.75, "hello", &i};
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value.(PtrValue).Sub()), "reflect.T{123, 456.75, hello, *int(@)}");
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
|
|
|
{
|
|
|
|
type C chan *T; // TODO: should not be necessary
|
2009-01-06 16:19:02 -07:00
|
|
|
var tmp = new(C);
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(tmp);
|
|
|
|
assert(valueToString(value), "*reflect.C·all_test(@)");
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
// {
|
|
|
|
// type A [10]int;
|
|
|
|
// var tmp A = A{1,2,3,4,5,6,7,8,9,10};
|
2009-05-21 12:50:20 -06:00
|
|
|
// value := NewValue(&tmp);
|
|
|
|
// assert(valueToString(value.(PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
|
|
|
|
// value.(PtrValue).Sub().(ArrayValue).Elem(4).(IntValue).Set(123);
|
|
|
|
// assert(valueToString(value.(PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
|
2008-12-18 23:37:22 -07:00
|
|
|
// }
|
2008-12-19 04:06:19 -07:00
|
|
|
{
|
|
|
|
type AA []int;
|
2009-03-03 09:39:12 -07:00
|
|
|
var tmp = AA{1,2,3,4,5,6,7,8,9,10};
|
2009-05-21 12:50:20 -06:00
|
|
|
value := NewValue(&tmp); // TODO: NewValue(tmp) too
|
|
|
|
assert(valueToString(value.(PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
|
|
|
|
value.(PtrValue).Sub().(ArrayValue).Elem(4).(IntValue).Set(123);
|
|
|
|
assert(valueToString(value.(PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
|
2008-12-19 04:06:19 -07:00
|
|
|
}
|
2008-11-04 23:54:11 -07:00
|
|
|
|
2008-11-12 20:05:05 -07:00
|
|
|
{
|
|
|
|
var ip *int32;
|
|
|
|
var i int32 = 1234;
|
2009-05-21 12:50:20 -06:00
|
|
|
vip := NewValue(&ip);
|
|
|
|
vi := NewValue(i);
|
|
|
|
vip.(PtrValue).Sub().(PtrValue).SetSub(vi);
|
2008-11-12 20:05:05 -07:00
|
|
|
if *ip != 1234 {
|
|
|
|
panicln("SetSub failure", *ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
var pt PtrType;
|
|
|
|
var st StructType;
|
|
|
|
var mt MapType;
|
|
|
|
var at ArrayType;
|
|
|
|
var ct ChanType;
|
2008-11-04 23:54:11 -07:00
|
|
|
var name string;
|
2009-05-21 12:50:20 -06:00
|
|
|
var typ Type;
|
2008-11-04 23:54:11 -07:00
|
|
|
var tag string;
|
2008-11-05 11:17:38 -07:00
|
|
|
var offset int;
|
2008-11-04 23:54:11 -07:00
|
|
|
|
|
|
|
// Type strings
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "int8");
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(t.String(), "int8");
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "*int8");
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(t.String(), "*int8");
|
2009-05-21 12:50:20 -06:00
|
|
|
pt = t.(PtrType);
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(pt.Sub().String(), "int8");
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "*struct {c chan *int32; d float32}");
|
2008-12-19 04:06:19 -07:00
|
|
|
assert(t.String(), "*struct {c chan *int32; d float32}");
|
2009-05-21 12:50:20 -06:00
|
|
|
pt = t.(PtrType);
|
2008-12-19 04:06:19 -07:00
|
|
|
assert(pt.Sub().String(), "struct {c chan *int32; d float32}");
|
2009-05-21 12:50:20 -06:00
|
|
|
st = pt.Sub().(StructType);
|
2008-11-04 23:54:11 -07:00
|
|
|
name, typ, tag, offset = st.Field(0);
|
2008-12-19 04:06:19 -07:00
|
|
|
assert(typ.String(), "chan *int32");
|
2008-11-04 23:54:11 -07:00
|
|
|
name, typ, tag, offset = st.Field(1);
|
|
|
|
assert(typ.String(), "float32");
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "interface {a() *int}");
|
2008-11-05 09:17:01 -07:00
|
|
|
assert(t.String(), "interface {a() *int}");
|
2008-11-04 23:54:11 -07:00
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "func(a int8, b int32)");
|
2009-04-14 07:46:01 -06:00
|
|
|
assert(t.String(), "func(a int8, b int32)");
|
2008-11-04 23:54:11 -07:00
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "func(a int8, b int32) float");
|
2009-04-14 07:46:01 -06:00
|
|
|
assert(t.String(), "func(a int8, b int32) float");
|
2008-11-05 09:17:01 -07:00
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "func(a int8, b int32) (a float, b float)");
|
2009-04-14 07:46:01 -06:00
|
|
|
assert(t.String(), "func(a int8, b int32) (a float, b float)");
|
2008-11-05 09:17:01 -07:00
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "[32]int32");
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(t.String(), "[32]int32");
|
2009-05-21 12:50:20 -06:00
|
|
|
at = t.(ArrayType);
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(at.Elem().String(), "int32");
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "map[string]*int32");
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(t.String(), "map[string]*int32");
|
2009-05-21 12:50:20 -06:00
|
|
|
mt = t.(MapType);
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(mt.Key().String(), "string");
|
|
|
|
assert(mt.Elem().String(), "*int32");
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "chan<-string");
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(t.String(), "chan<-string");
|
2009-05-21 12:50:20 -06:00
|
|
|
ct = t.(ChanType);
|
2008-11-04 23:54:11 -07:00
|
|
|
assert(ct.Elem().String(), "string");
|
2008-11-05 14:01:33 -07:00
|
|
|
|
|
|
|
// make sure tag strings are not part of element type
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "struct{d []uint32 \"TAG\"}");
|
|
|
|
st = t.(StructType);
|
2008-11-05 14:01:33 -07:00
|
|
|
name, typ, tag, offset = st.Field(0);
|
2008-12-18 23:37:22 -07:00
|
|
|
assert(typ.String(), "[]uint32");
|
2008-11-13 14:42:59 -07:00
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
t = ParseTypeString("", "[]int32");
|
|
|
|
v := NewSliceValue(t.(ArrayType), 5, 10);
|
|
|
|
t1 := ParseTypeString("", "*[]int32");
|
|
|
|
v1 := NewZeroValue(t1);
|
2008-12-18 23:37:22 -07:00
|
|
|
if v1 == nil { panic("V1 is nil"); }
|
2009-05-21 12:50:20 -06:00
|
|
|
v1.(PtrValue).SetSub(v);
|
2009-06-25 16:15:44 -06:00
|
|
|
a := *v1.Interface().(*[]int32);
|
|
|
|
println(a, len(a), cap(a));
|
2008-11-13 14:42:59 -07:00
|
|
|
for i := 0; i < len(a); i++ {
|
2009-05-21 12:50:20 -06:00
|
|
|
v.Elem(i).(Int32Value).Set(int32(i));
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
println(a[i]);
|
|
|
|
}
|
2008-10-23 18:13:34 -06:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestInterfaceGet(t *testing.T) {
|
2008-11-24 15:51:33 -07:00
|
|
|
var inter struct { e interface{ } };
|
|
|
|
inter.e = 123.456;
|
2009-05-21 12:50:20 -06:00
|
|
|
v1 := NewValue(&inter);
|
|
|
|
v2 := v1.(PtrValue).Sub().(StructValue).Field(0);
|
2008-11-24 15:51:33 -07:00
|
|
|
assert(v2.Type().String(), "interface { }");
|
2009-05-21 12:50:20 -06:00
|
|
|
i2 := v2.(InterfaceValue).Get();
|
|
|
|
v3 := NewValue(i2);
|
2008-11-24 15:51:33 -07:00
|
|
|
assert(v3.Type().String(), "float");
|
|
|
|
}
|
2008-12-10 16:55:59 -07:00
|
|
|
|
2009-04-06 22:28:04 -06:00
|
|
|
func TestInterfaceValue(t *testing.T) {
|
|
|
|
var inter struct { e interface{ } };
|
|
|
|
inter.e = 123.456;
|
2009-05-21 12:50:20 -06:00
|
|
|
v1 := NewValue(&inter);
|
|
|
|
v2 := v1.(PtrValue).Sub().(StructValue).Field(0);
|
2009-04-06 22:28:04 -06:00
|
|
|
assert(v2.Type().String(), "interface { }");
|
2009-05-21 12:50:20 -06:00
|
|
|
v3 := v2.(InterfaceValue).Value();
|
2009-04-06 22:28:04 -06:00
|
|
|
assert(v3.Type().String(), "float");
|
2009-04-15 01:55:58 -06:00
|
|
|
|
2009-04-14 20:03:57 -06:00
|
|
|
i3 := v2.Interface();
|
|
|
|
if f, ok := i3.(float); !ok {
|
2009-05-08 15:57:56 -06:00
|
|
|
a, typ, c := unsafe.Reflect(i3);
|
2009-04-14 20:03:57 -06:00
|
|
|
t.Error("v2.Interface() did not return float, got ", typ);
|
|
|
|
}
|
2009-04-06 22:28:04 -06:00
|
|
|
}
|
|
|
|
|
2009-04-14 07:46:01 -06:00
|
|
|
func TestFunctionValue(t *testing.T) {
|
2009-05-21 12:50:20 -06:00
|
|
|
v := NewValue(func() {});
|
2009-04-14 07:46:01 -06:00
|
|
|
if v.Interface() != v.Interface() {
|
|
|
|
t.Fatalf("TestFunction != itself");
|
|
|
|
}
|
|
|
|
assert(v.Type().String(), "func()");
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestCopyArray(t *testing.T) {
|
2009-03-03 09:39:12 -07:00
|
|
|
a := []int{ 1, 2, 3, 4, 10, 9, 8, 7 };
|
|
|
|
b := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
|
|
|
|
c := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
|
2008-12-18 23:37:22 -07:00
|
|
|
va := NewValue(&a);
|
|
|
|
vb := NewValue(&b);
|
2008-12-10 16:55:59 -07:00
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if b[i] != c[i] {
|
|
|
|
t.Fatalf("b != c before test");
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
for tocopy := 1; tocopy <= 7; tocopy++ {
|
2009-02-11 18:55:16 -07:00
|
|
|
vb.(PtrValue).Sub().(ArrayValue).CopyFrom(va.(PtrValue).Sub().(ArrayValue), tocopy);
|
2008-12-10 16:55:59 -07:00
|
|
|
for i := 0; i < tocopy; i++ {
|
|
|
|
if a[i] != b[i] {
|
2008-12-18 23:37:22 -07:00
|
|
|
t.Errorf("1 tocopy=%d a[%d]=%d, b[%d]=%d",
|
2008-12-10 16:55:59 -07:00
|
|
|
tocopy, i, a[i], i, b[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := tocopy; i < len(b); i++ {
|
|
|
|
if b[i] != c[i] {
|
|
|
|
if i < len(a) {
|
2008-12-18 23:37:22 -07:00
|
|
|
t.Errorf("2 tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
|
2008-12-10 16:55:59 -07:00
|
|
|
tocopy, i, a[i], i, b[i], i, c[i]);
|
|
|
|
} else {
|
2008-12-18 23:37:22 -07:00
|
|
|
t.Errorf("3 tocopy=%d b[%d]=%d, c[%d]=%d",
|
2008-12-10 16:55:59 -07:00
|
|
|
tocopy, i, b[i], i, c[i]);
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
} else {
|
|
|
|
t.Logf("tocopy=%d elem %d is okay\n", tocopy, i);
|
2008-12-10 16:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-09 01:17:46 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestBigUnnamedStruct(t *testing.T) {
|
2009-03-03 09:39:12 -07:00
|
|
|
b := struct{a,b,c,d int64}{1, 2, 3, 4};
|
2009-01-09 01:17:46 -07:00
|
|
|
v := NewValue(b);
|
|
|
|
b1 := v.Interface().(struct{a,b,c,d int64});
|
|
|
|
if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
|
|
|
|
t.Errorf("NewValue(%v).Interface().(Big) = %v", b, b1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type big struct {
|
2009-01-09 01:17:46 -07:00
|
|
|
a, b, c, d, e int64
|
|
|
|
}
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestBigStruct(t *testing.T) {
|
2009-03-03 09:39:12 -07:00
|
|
|
b := big{1, 2, 3, 4, 5};
|
2009-01-09 01:17:46 -07:00
|
|
|
v := NewValue(b);
|
2009-01-16 13:48:07 -07:00
|
|
|
b1 := v.Interface().(big);
|
2009-01-09 01:17:46 -07:00
|
|
|
if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
|
2009-01-16 13:48:07 -07:00
|
|
|
t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1);
|
2009-01-09 01:17:46 -07:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 23:20:18 -06:00
|
|
|
|
|
|
|
type Basic struct {
|
|
|
|
x int;
|
|
|
|
y float32
|
|
|
|
}
|
|
|
|
|
2009-04-15 01:55:58 -06:00
|
|
|
type NotBasic Basic
|
|
|
|
|
2009-04-01 23:20:18 -06:00
|
|
|
type Recursive struct {
|
|
|
|
x int;
|
|
|
|
r *Recursive
|
|
|
|
}
|
|
|
|
|
|
|
|
type Complex struct {
|
|
|
|
a int;
|
|
|
|
b [3]*Complex;
|
|
|
|
c *string;
|
|
|
|
d map[float]float
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeepEqualTest struct {
|
|
|
|
a, b interface{};
|
|
|
|
eq bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
var deepEqualTests = []DeepEqualTest {
|
|
|
|
// Equalities
|
|
|
|
DeepEqualTest{ 1, 1, true },
|
|
|
|
DeepEqualTest{ int32(1), int32(1), true },
|
|
|
|
DeepEqualTest{ 0.5, 0.5, true },
|
|
|
|
DeepEqualTest{ float32(0.5), float32(0.5), true },
|
|
|
|
DeepEqualTest{ "hello", "hello", true },
|
|
|
|
DeepEqualTest{ make([]int, 10), make([]int, 10), true },
|
|
|
|
DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true },
|
|
|
|
DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true },
|
2009-06-25 15:25:38 -06:00
|
|
|
DeepEqualTest{ os.Error(nil), os.Error(nil), true },
|
2009-07-01 17:45:09 -06:00
|
|
|
|
2009-04-01 23:20:18 -06:00
|
|
|
// Inequalities
|
|
|
|
DeepEqualTest{ 1, 2, false },
|
|
|
|
DeepEqualTest{ int32(1), int32(2), false },
|
|
|
|
DeepEqualTest{ 0.5, 0.6, false },
|
|
|
|
DeepEqualTest{ float32(0.5), float32(0.6), false },
|
|
|
|
DeepEqualTest{ "hello", "hey", false },
|
|
|
|
DeepEqualTest{ make([]int, 10), make([]int, 11), false },
|
|
|
|
DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false },
|
|
|
|
DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false },
|
2009-07-01 17:45:09 -06:00
|
|
|
DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false },
|
|
|
|
|
2009-04-01 23:20:18 -06:00
|
|
|
// Mismatched types
|
|
|
|
DeepEqualTest{ 1, 1.0, false },
|
|
|
|
DeepEqualTest{ int32(1), int64(1), false },
|
|
|
|
DeepEqualTest{ 0.5, "hello", false },
|
|
|
|
DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false },
|
|
|
|
DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false },
|
2009-04-15 01:55:58 -06:00
|
|
|
DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false },
|
2009-04-01 23:20:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeepEqual(t *testing.T) {
|
|
|
|
for i, test := range deepEqualTests {
|
|
|
|
if r := DeepEqual(test.a, test.b); r != test.eq {
|
|
|
|
t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-25 15:25:38 -06:00
|
|
|
func TestTypeof(t *testing.T) {
|
|
|
|
for i, test := range deepEqualTests {
|
|
|
|
v := NewValue(test.a);
|
|
|
|
typ := Typeof(test.a);
|
|
|
|
if typ != v.Type() {
|
|
|
|
t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-01 23:20:18 -06:00
|
|
|
func TestDeepEqualRecursiveStruct(t *testing.T) {
|
|
|
|
a, b := new(Recursive), new(Recursive);
|
|
|
|
*a = Recursive{ 12, a };
|
|
|
|
*b = Recursive{ 12, b };
|
|
|
|
if !DeepEqual(a, b) {
|
|
|
|
t.Error("DeepEqual(recursive same) = false, want true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeepEqualComplexStruct(t *testing.T) {
|
|
|
|
m := make(map[float]float);
|
|
|
|
stra, strb := "hello", "hello";
|
|
|
|
a, b := new(Complex), new(Complex);
|
|
|
|
*a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
|
|
|
|
*b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
|
|
|
|
if !DeepEqual(a, b) {
|
|
|
|
t.Error("DeepEqual(complex same) = false, want true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeepEqualComplexStructInequality(t *testing.T) {
|
|
|
|
m := make(map[float]float);
|
|
|
|
stra, strb := "hello", "helloo"; // Difference is here
|
|
|
|
a, b := new(Complex), new(Complex);
|
|
|
|
*a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
|
|
|
|
*b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
|
|
|
|
if DeepEqual(a, b) {
|
|
|
|
t.Error("DeepEqual(complex different) = true, want false");
|
|
|
|
}
|
|
|
|
}
|
2009-04-29 23:16:53 -06:00
|
|
|
|
|
|
|
|
|
|
|
func check2ndField(x interface{}, offs uintptr, t *testing.T) {
|
2009-05-21 12:50:20 -06:00
|
|
|
s := NewValue(x).(StructValue);
|
|
|
|
name, ftype, tag, reflect_offset := s.Type().(StructType).Field(1);
|
2009-04-29 23:16:53 -06:00
|
|
|
if uintptr(reflect_offset) != offs {
|
|
|
|
t.Error("mismatched offsets in structure alignment:", reflect_offset, offs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that structure alignment & offsets viewed through reflect agree with those
|
|
|
|
// from the compiler itself.
|
|
|
|
func TestAlignment(t *testing.T) {
|
|
|
|
type T1inner struct {
|
|
|
|
a int
|
|
|
|
}
|
|
|
|
type T1 struct {
|
|
|
|
T1inner;
|
|
|
|
f int;
|
|
|
|
}
|
|
|
|
type T2inner struct {
|
|
|
|
a, b int
|
|
|
|
}
|
|
|
|
type T2 struct {
|
|
|
|
T2inner;
|
|
|
|
f int;
|
|
|
|
}
|
|
|
|
|
|
|
|
x := T1{T1inner{2}, 17};
|
|
|
|
check2ndField(x, uintptr(unsafe.Pointer(&x.f)) - uintptr(unsafe.Pointer(&x)), t);
|
|
|
|
|
|
|
|
x1 := T2{T2inner{2, 3}, 17};
|
|
|
|
check2ndField(x1, uintptr(unsafe.Pointer(&x1.f)) - uintptr(unsafe.Pointer(&x1)), t);
|
|
|
|
}
|
2009-05-12 15:57:44 -06:00
|
|
|
|
|
|
|
type Nillable interface {
|
|
|
|
IsNil() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func Nil(a interface{}, t *testing.T) {
|
|
|
|
n := NewValue(a).(Nillable);
|
|
|
|
if !n.IsNil() {
|
|
|
|
t.Errorf("%v should be nil", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotNil(a interface{}, t *testing.T) {
|
|
|
|
n := NewValue(a).(Nillable);
|
|
|
|
if n.IsNil() {
|
|
|
|
t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsNil(t *testing.T) {
|
|
|
|
// These do not implement IsNil
|
|
|
|
doNotNil := []string{"int", "float32", "struct { a int }"};
|
|
|
|
// These do implement IsNil
|
|
|
|
doNil := []string{"*int", "interface{}", "map[string]int", "func() bool", "chan int", "[]string"};
|
|
|
|
for i, ts := range doNotNil {
|
2009-05-21 12:50:20 -06:00
|
|
|
ty := ParseTypeString("", ts);
|
|
|
|
v := NewZeroValue(ty);
|
2009-05-12 15:57:44 -06:00
|
|
|
if nilable, ok := v.(Nillable); ok {
|
|
|
|
t.Errorf("%s is nilable; should not be", ts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ts := range doNil {
|
2009-05-21 12:50:20 -06:00
|
|
|
ty := ParseTypeString("", ts);
|
|
|
|
v := NewZeroValue(ty);
|
2009-05-12 15:57:44 -06:00
|
|
|
if nilable, ok := v.(Nillable); !ok {
|
2009-05-20 16:42:14 -06:00
|
|
|
t.Errorf("%s %T is not nilable; should be", ts, v)
|
2009-05-12 15:57:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check the implementations
|
|
|
|
var pi *int;
|
|
|
|
Nil(pi, t);
|
|
|
|
pi = new(int);
|
|
|
|
NotNil(pi, t);
|
|
|
|
|
|
|
|
var si []int;
|
|
|
|
Nil(si, t);
|
|
|
|
si = make([]int, 10);
|
|
|
|
NotNil(si, t);
|
|
|
|
|
|
|
|
// TODO: map and chan don't work yet
|
|
|
|
|
|
|
|
var ii interface {};
|
|
|
|
Nil(ii, t);
|
|
|
|
ii = pi;
|
|
|
|
NotNil(ii, t);
|
|
|
|
|
|
|
|
var fi func(t *testing.T);
|
|
|
|
Nil(fi, t);
|
|
|
|
fi = TestIsNil;
|
|
|
|
NotNil(fi, t);
|
|
|
|
}
|
2009-05-21 12:50:20 -06:00
|
|
|
|
|
|
|
func TestInterfaceExtraction(t *testing.T) {
|
|
|
|
var s struct {
|
|
|
|
w io.Writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.w = os.Stdout;
|
|
|
|
v := Indirect(NewValue(&s)).(StructValue).Field(0).Interface();
|
|
|
|
if v != s.w.(interface{}) {
|
|
|
|
t.Errorf("Interface() on interface: ", v, s.w);
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 15:06:43 -06:00
|
|
|
|
|
|
|
func TestInterfaceEditing(t *testing.T) {
|
|
|
|
// strings are bigger than one word,
|
|
|
|
// so the interface conversion allocates
|
|
|
|
// memory to hold a string and puts that
|
|
|
|
// pointer in the interface.
|
|
|
|
var i interface{} = "hello";
|
|
|
|
|
|
|
|
// if i pass the interface value by value
|
|
|
|
// to NewValue, i should get a fresh copy
|
|
|
|
// of the value.
|
|
|
|
v := NewValue(i);
|
|
|
|
|
|
|
|
// and setting that copy to "bye" should
|
|
|
|
// not change the value stored in i.
|
|
|
|
v.(StringValue).Set("bye");
|
|
|
|
if i.(string) != "hello" {
|
|
|
|
t.Errorf(`Set("bye") changed i to %s`, i.(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
// the same should be true of smaller items.
|
|
|
|
i = 123;
|
|
|
|
v = NewValue(i);
|
|
|
|
v.(IntValue).Set(234);
|
|
|
|
if i.(int) != 123 {
|
|
|
|
t.Errorf("Set(234) changed i to %d", i.(int));
|
|
|
|
}
|
|
|
|
}
|
2009-06-15 19:35:04 -06:00
|
|
|
|
|
|
|
func TestNilPtrValueSub(t *testing.T) {
|
|
|
|
var pi *int;
|
|
|
|
if pv := NewValue(pi).(PtrValue); pv.Sub() != nil {
|
|
|
|
t.Error("NewValue((*int)(nil)).(PtrValue).Sub() != nil");
|
|
|
|
}
|
|
|
|
}
|