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

Side by Side Diff: src/pkg/reflect/all_test.go

Issue 6498078: code review 6498078: reflect: add Select (Closed)
Patch Set: diff -r df382f6986cf https://code.google.com/p/go/ Created 11 years, 6 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:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pkg/reflect/value.go » ('j') | src/pkg/reflect/value.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "fmt" 10 "fmt"
11 "io" 11 "io"
12 "os" 12 "os"
13 . "reflect" 13 . "reflect"
14 "runtime" 14 "runtime"
15 "testing" 15 "testing"
16 "time"
16 "unsafe" 17 "unsafe"
17 ) 18 )
18 19
19 func TestBool(t *testing.T) { 20 func TestBool(t *testing.T) {
20 v := ValueOf(true) 21 v := ValueOf(true)
21 if v.Bool() != true { 22 if v.Bool() != true {
22 t.Fatal("ValueOf(true).Bool() = false") 23 t.Fatal("ValueOf(true).Bool() = false")
23 } 24 }
24 } 25 }
25 26
(...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 1047
1047 // len/cap 1048 // len/cap
1048 cv = MakeChan(TypeOf(c), 10) 1049 cv = MakeChan(TypeOf(c), 10)
1049 c = cv.Interface().(chan int) 1050 c = cv.Interface().(chan int)
1050 for i := 0; i < 3; i++ { 1051 for i := 0; i < 3; i++ {
1051 c <- i 1052 c <- i
1052 } 1053 }
1053 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) { 1054 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
1054 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c)) 1055 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
1055 } 1056 }
1057
1058 // select
1059 // six cases:
1060 // 0 non-blocking send, 1 recv, 2 default,
1061 // 3 blocking send, 4 recv,
1062 // 5 recv from closed channel
r 2012/09/05 15:07:04 probably want a nil channel in here.
iant 2012/09/05 15:53:10 A nil channel makes sense in the language, but her
rsc 2012/09/13 21:26:55 Done.
1063 for loop := 0; loop < 6; loop++ {
1064 var cases []SelectCase
1065 sendBuf := 0
1066 if loop == 0 {
1067 // set up non-blocking send
1068 sendBuf = 1
1069 }
1070 sendch := make(chan int, sendBuf)
1071 recvch := make(chan int, 1)
1072 cases = append(cases, SelectCase{
1073 Dir: SendDir,
1074 Chan: ValueOf(sendch),
1075 Send: ValueOf(1),
1076 })
1077 cases = append(cases, SelectCase{
1078 Dir: RecvDir,
1079 Chan: ValueOf(recvch),
1080 })
1081 if loop < 3 {
1082 cases = append(cases, SelectCase{Dir: 0})
1083 }
1084 expect := loop % 3
1085 switch loop {
1086 case 1:
1087 // set up non-blocking recv
1088 recvch <- 1
1089 case 3:
1090 // set up eventual completion of blocking send
1091 go func() {
1092 time.Sleep(10 * time.Millisecond)
1093 <-sendch
1094 }()
1095 case 4:
1096 // set up eventual completion of blocking recv
1097 go func() {
1098 time.Sleep(10 * time.Millisecond)
1099 recvch <- loop
1100 }()
1101 case 5:
1102 // set up eventual completion of blocking recv
1103 go func() {
1104 time.Sleep(10 * time.Millisecond)
1105 close(recvch)
1106 }()
1107 expect = 1 // recv should complete
1108 }
1109
1110 i, recv, recvOK := Select(cases)
1111 if i != expect {
1112 t.Errorf("Select#%d: selected case %d, expected case %d" , loop, i, expect)
1113 continue
1114 }
1115
1116 if loop%3 == 1 { // recv
1117 if v := recv.Interface(); v != loop || !recvOK {
1118 t.Errorf("Select#%d: received %v, %v, expected % v, %v", loop, v, recvOK, loop, true)
1119 }
1120 } else if loop == 5 { // recv from closed
1121 if v := recv.Interface(); v != 0 || recvOK {
1122 t.Errorf("Select#%d: received %v, %v, expected % v, %v", loop, v, recvOK, 0, false)
1123 }
1124 } else if recv.IsValid() || recvOK {
1125 t.Errorf("Select#%d: selected non-recv case but got recv =%v ok=%v", loop, recv, recvOK)
1126 continue
1127 }
1128 }
1056 } 1129 }
1057 1130
1058 // Difficult test for function call because of 1131 // Difficult test for function call because of
1059 // implicit padding between arguments. 1132 // implicit padding between arguments.
1060 func dummy(b byte, c int, d byte) (i byte, j int, k byte) { 1133 func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
1061 return b, c, d 1134 return b, c, d
1062 } 1135 }
1063 1136
1064 func TestFunc(t *testing.T) { 1137 func TestFunc(t *testing.T) {
1065 ret := ValueOf(dummy).Call([]Value{ValueOf(byte(10)), ValueOf(20), Value Of(byte(30))}) 1138 ret := ValueOf(dummy).Call([]Value{ValueOf(byte(10)), ValueOf(20), Value Of(byte(30))})
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after
1926 t.Fatalf(`FieldByName("X") should fail, returned %v`, f.Index) 1999 t.Fatalf(`FieldByName("X") should fail, returned %v`, f.Index)
1927 } 2000 }
1928 } 2001 }
1929 2002
1930 func BenchmarkFieldByName3(b *testing.B) { 2003 func BenchmarkFieldByName3(b *testing.B) {
1931 t := TypeOf(R0{}) 2004 t := TypeOf(R0{})
1932 for i := 0; i < b.N; i++ { 2005 for i := 0; i < b.N; i++ {
1933 t.FieldByName("X") 2006 t.FieldByName("X")
1934 } 2007 }
1935 } 2008 }
OLDNEW
« no previous file with comments | « no previous file | src/pkg/reflect/value.go » ('j') | src/pkg/reflect/value.go » ('J')

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