diff options
| author | Ondřej Surý <ondrej@sury.org> | 2011-05-23 09:45:29 +0200 |
|---|---|---|
| committer | Ondřej Surý <ondrej@sury.org> | 2011-05-23 09:45:29 +0200 |
| commit | 63d29fefab5290dc96e0a03ff70603aefa995887 (patch) | |
| tree | 95da0105686f9aba568a72e7a8ebd580a4fda20e /src/pkg/reflect | |
| parent | ad811fbb8897a9a3063274e927133915941f1dca (diff) | |
| download | golang-63d29fefab5290dc96e0a03ff70603aefa995887.tar.gz | |
Imported Upstream version 2011.05.22upstream-weekly/2011.05.22
Diffstat (limited to 'src/pkg/reflect')
| -rw-r--r-- | src/pkg/reflect/all_test.go | 30 | ||||
| -rw-r--r-- | src/pkg/reflect/value.go | 9 |
2 files changed, 29 insertions, 10 deletions
diff --git a/src/pkg/reflect/all_test.go b/src/pkg/reflect/all_test.go index 5bf65333c..c83a9b75f 100644 --- a/src/pkg/reflect/all_test.go +++ b/src/pkg/reflect/all_test.go @@ -579,13 +579,7 @@ func TestCopyArray(t *testing.T) { } for i := len(a); i < len(b); i++ { if b[i] != c[i] { - if i < len(a) { - t.Errorf("(ii) a[%d]=%d, b[%d]=%d, c[%d]=%d", - i, a[i], i, b[i], i, c[i]) - } else { - t.Errorf("(iii) b[%d]=%d, c[%d]=%d", - i, b[i], i, c[i]) - } + t.Errorf("(ii) b[%d]=%d, c[%d]=%d", i, b[i], i, c[i]) } else { t.Logf("elem %d is okay\n", i) } @@ -732,6 +726,24 @@ func TestDeepEqualComplexStructInequality(t *testing.T) { } } +type UnexpT struct { + m map[int]int +} + +func TestDeepEqualUnexportedMap(t *testing.T) { + // Check that DeepEqual can look at unexported fields. + x1 := UnexpT{map[int]int{1: 2}} + x2 := UnexpT{map[int]int{1: 2}} + if !DeepEqual(&x1, &x2) { + t.Error("DeepEqual(x1, x2) = false, want true") + } + + y1 := UnexpT{map[int]int{2: 3}} + if DeepEqual(&x1, &y1) { + t.Error("DeepEqual(x1, y1) = true, want false") + } +} + func check2ndField(x interface{}, offs uintptr, t *testing.T) { s := ValueOf(x) @@ -1439,7 +1451,9 @@ func noAlloc(t *testing.T, n int, f func(int)) { for j := 0; j < n; j++ { f(j) } - if runtime.MemStats.Mallocs != 0 { + // A few allocs may happen in the testing package when GOMAXPROCS > 1, so don't + // require zero mallocs. + if runtime.MemStats.Mallocs > 5 { t.Fatalf("%d mallocs after %d iterations", runtime.MemStats.Mallocs, n) } } diff --git a/src/pkg/reflect/value.go b/src/pkg/reflect/value.go index 6dffb0783..2c2158a3c 100644 --- a/src/pkg/reflect/value.go +++ b/src/pkg/reflect/value.go @@ -958,14 +958,19 @@ func (v Value) MapIndex(key Value) Value { iv.mustBe(Map) typ := iv.typ.toType() + // Do not require ikey to be exported, so that DeepEqual + // and other programs can use all the keys returned by + // MapKeys as arguments to MapIndex. If either the map + // or the key is unexported, though, the result will be + // considered unexported. + ikey := key.internal() - ikey.mustBeExported() ikey = convertForAssignment("reflect.Value.MapIndex", nil, typ.Key(), ikey) if iv.word == 0 { return Value{} } - flag := iv.flag & flagRO + flag := (iv.flag | ikey.flag) & flagRO elemType := typ.Elem() elemWord, ok := mapaccess(iv.word, ikey.word) if !ok { |
