Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1167)

Delta Between Two Patch Sets: src/pkg/reflect/all_test.go

Issue 6572043: code review 6572043: reflect: add ArrayOf, ChanOf, MapOf, SliceOf (Closed)
Left Patch Set: diff -r 0a3866d6cc6b https://code.google.com/p/go/ Created 11 years, 6 months ago
Right Patch Set: diff -r 6e9d872ffc66 https://code.google.com/p/go/ Created 11 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/cmd/ld/symtab.c ('k') | src/pkg/reflect/export_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package reflect_test 5 package reflect_test
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/base64" 9 "encoding/base64"
10 "flag" 10 "flag"
(...skipping 2685 matching lines...) Expand 10 before | Expand all | Expand 10 after
2696 maxUint32 := uint64(0xffffffff) 2696 maxUint32 := uint64(0xffffffff)
2697 if ovf := V(uint32(0)).OverflowUint(maxUint32); ovf { 2697 if ovf := V(uint32(0)).OverflowUint(maxUint32); ovf {
2698 t.Errorf("%v wrongly overflows uint32", maxUint32) 2698 t.Errorf("%v wrongly overflows uint32", maxUint32)
2699 } 2699 }
2700 ovfUint32 := uint64(1 << 32) 2700 ovfUint32 := uint64(1 << 32)
2701 if ovf := V(uint32(0)).OverflowUint(ovfUint32); !ovf { 2701 if ovf := V(uint32(0)).OverflowUint(ovfUint32); !ovf {
2702 t.Errorf("%v should overflow uint32", ovfUint32) 2702 t.Errorf("%v should overflow uint32", ovfUint32)
2703 } 2703 }
2704 } 2704 }
2705 2705
2706 func checkSameType(t *testing.T, x, y interface{}) {
2707 if TypeOf(x) != TypeOf(y) {
2708 t.Errorf("did not find preexisting type for %s (vs %s)", TypeOf( x), TypeOf(y))
2709 }
2710 }
2711
2712 func TestArrayOf(t *testing.T) {
2713 // check construction and use of type not in binary
2714 type T int
2715 at := ArrayOf(10, TypeOf(T(1)))
2716 v := New(at).Elem()
2717 for i := 0; i < v.Len(); i++ {
2718 v.Index(i).Set(ValueOf(T(i)))
2719 }
2720 s := fmt.Sprint(v.Interface())
2721 want := "[0 1 2 3 4 5 6 7 8 9]"
2722 if s != want {
2723 t.Errorf("constructed array = %s, want %s", s, want)
2724 }
2725
2726 // check that type already in binary is found
2727 checkSameType(t, Zero(ArrayOf(5, TypeOf(T(1)))).Interface(), [5]T{})
2728 }
2729
2730 func TestSliceOf(t *testing.T) {
2731 // check construction and use of type not in binary
2732 type T int
2733 st := SliceOf(TypeOf(T(1)))
2734 v := MakeSlice(st, 10, 10)
2735 for i := 0; i < v.Len(); i++ {
2736 v.Index(i).Set(ValueOf(T(i)))
2737 }
2738 s := fmt.Sprint(v.Interface())
2739 want := "[0 1 2 3 4 5 6 7 8 9]"
2740 if s != want {
2741 t.Errorf("constructed slice = %s, want %s", s, want)
2742 }
2743
2744 // check that type already in binary is found
2745 type T1 int
2746 checkSameType(t, Zero(SliceOf(TypeOf(T1(1)))).Interface(), []T1{})
2747 }
2748
2749 func TestChanOf(t *testing.T) {
2750 // check construction and use of type not in binary
2751 type T string
2752 ct := ChanOf(BothDir, TypeOf(T("")))
2753 v := MakeChan(ct, 2)
2754 v.Send(ValueOf(T("hello")))
2755 v.Send(ValueOf(T("world")))
2756
2757 sv1, _ := v.Recv()
2758 sv2, _ := v.Recv()
2759 s1 := sv1.String()
2760 s2 := sv2.String()
2761 if s1 != "hello" || s2 != "world" {
2762 t.Errorf("constructed chan: have %q, %q, want %q, %q", s1, s2, " hello", "world")
2763 }
2764
2765 // check that type already in binary is found
2766 type T1 int
2767 checkSameType(t, Zero(ChanOf(BothDir, TypeOf(T1(1)))).Interface(), (chan T1)(nil))
2768 }
2769
2770 func TestMapOf(t *testing.T) {
2771 // check construction and use of type not in binary
2772 type K string
2773 type V float64
2774
2775 v := MakeMap(MapOf(TypeOf(K("")), TypeOf(V(0))))
2776 v.SetMapIndex(ValueOf(K("a")), ValueOf(V(1)))
2777
2778 s := fmt.Sprint(v.Interface())
2779 want := "map[a:1]"
2780 if s != want {
2781 t.Errorf("constructed map = %s, want %s", s, want)
2782 }
2783
2784 // check that type already in binary is found
2785 checkSameType(t, Zero(MapOf(TypeOf(V(0)), TypeOf(K("")))).Interface(), m ap[V]K(nil))
2786 }
2787
2706 type B1 struct { 2788 type B1 struct {
2707 X int 2789 X int
2708 Y int 2790 Y int
2709 Z int 2791 Z int
2710 } 2792 }
2711 2793
2712 func BenchmarkFieldByName1(b *testing.B) { 2794 func BenchmarkFieldByName1(b *testing.B) {
2713 t := TypeOf(B1{}) 2795 t := TypeOf(B1{})
2714 for i := 0; i < b.N; i++ { 2796 for i := 0; i < b.N; i++ {
2715 t.FieldByName("Z") 2797 t.FieldByName("Z")
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
2891 x.pos++ 2973 x.pos++
2892 if c.max != max { 2974 if c.max != max {
2893 panic("inconsistent use of exhaustive tester") 2975 panic("inconsistent use of exhaustive tester")
2894 } 2976 }
2895 return (c.n + c.off) % max 2977 return (c.n + c.off) % max
2896 } 2978 }
2897 2979
2898 func (x *exhaustive) Maybe() bool { 2980 func (x *exhaustive) Maybe() bool {
2899 return x.Choose(2) == 1 2981 return x.Choose(2) == 1
2900 } 2982 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b