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

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

Issue 4281055: code review 4281055: reflect: new Type and Value definitions (Closed)
Left Patch Set: Created 13 years ago
Right Patch Set: diff -r ce9962e29e4b https://go.googlecode.com/hg Created 12 years, 11 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 | « no previous file | src/pkg/reflect/deepequal.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 "container/vector" 8 "container/vector"
9 "fmt" 9 "fmt"
10 "io" 10 "io"
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 200
201 func testType(t *testing.T, i int, typ Type, want string) { 201 func testType(t *testing.T, i int, typ Type, want string) {
202 s := typ.String() 202 s := typ.String()
203 if s != want { 203 if s != want {
204 t.Errorf("#%d: have %#q, want %#q", i, s, want) 204 t.Errorf("#%d: have %#q, want %#q", i, s, want)
205 } 205 }
206 } 206 }
207 207
208 func TestTypes(t *testing.T) { 208 func TestTypes(t *testing.T) {
209 for i, tt := range typeTests { 209 for i, tt := range typeTests {
210 » » testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt .s) 210 » » testType(t, i, NewValue(tt.i).Field(0).Type(), tt.s)
211 } 211 }
212 } 212 }
213 213
214 func TestSet(t *testing.T) { 214 func TestSet(t *testing.T) {
215 for i, tt := range valueTests { 215 for i, tt := range valueTests {
216 v := NewValue(tt.i) 216 v := NewValue(tt.i)
217 » » switch v := v.(type) { 217 » » switch v.Kind() {
218 » » case *IntValue: 218 » » case Int:
219 » » » switch v.Type().Kind() { 219 » » » v.SetInt(132)
220 » » » case Int: 220 » » case Int8:
221 » » » » v.Set(132) 221 » » » v.SetInt(8)
222 » » » case Int8: 222 » » case Int16:
223 » » » » v.Set(8) 223 » » » v.SetInt(16)
224 » » » case Int16: 224 » » case Int32:
225 » » » » v.Set(16) 225 » » » v.SetInt(32)
226 » » » case Int32: 226 » » case Int64:
227 » » » » v.Set(32) 227 » » » v.SetInt(64)
228 » » » case Int64: 228 » » case Uint:
229 » » » » v.Set(64) 229 » » » v.SetUint(132)
230 » » » } 230 » » case Uint8:
231 » » case *UintValue: 231 » » » v.SetUint(8)
232 » » » switch v.Type().Kind() { 232 » » case Uint16:
233 » » » case Uint: 233 » » » v.SetUint(16)
234 » » » » v.Set(132) 234 » » case Uint32:
235 » » » case Uint8: 235 » » » v.SetUint(32)
236 » » » » v.Set(8) 236 » » case Uint64:
237 » » » case Uint16: 237 » » » v.SetUint(64)
238 » » » » v.Set(16) 238 » » case Float32:
239 » » » case Uint32: 239 » » » v.SetFloat(256.25)
240 » » » » v.Set(32) 240 » » case Float64:
241 » » » case Uint64: 241 » » » v.SetFloat(512.125)
242 » » » » v.Set(64) 242 » » case Complex64:
243 » » » } 243 » » » v.SetComplex(532.125 + 10i)
244 » » case *FloatValue: 244 » » case Complex128:
245 » » » switch v.Type().Kind() { 245 » » » v.SetComplex(564.25 + 1i)
246 » » » case Float32: 246 » » case String:
247 » » » » v.Set(256.25) 247 » » » v.SetString("stringy cheese")
248 » » » case Float64: 248 » » case Bool:
249 » » » » v.Set(512.125) 249 » » » v.SetBool(true)
250 » » » }
251 » » case *ComplexValue:
252 » » » switch v.Type().Kind() {
253 » » » case Complex64:
254 » » » » v.Set(532.125 + 10i)
255 » » » case Complex128:
256 » » » » v.Set(564.25 + 1i)
257 » » » }
258 » » case *StringValue:
259 » » » v.Set("stringy cheese")
260 » » case *BoolValue:
261 » » » v.Set(true)
262 } 250 }
263 s := valueToString(v) 251 s := valueToString(v)
264 if s != tt.s { 252 if s != tt.s {
265 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s) 253 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
266 } 254 }
267 } 255 }
268 } 256 }
269 257
270 func TestSetValue(t *testing.T) { 258 func TestSetValue(t *testing.T) {
271 for i, tt := range valueTests { 259 for i, tt := range valueTests {
272 v := NewValue(tt.i) 260 v := NewValue(tt.i)
273 » » switch v := v.(type) { 261 » » switch v.Kind() {
274 » » case *IntValue: 262 » » case Int:
275 » » » switch v.Type().Kind() { 263 » » » v.Set(NewValue(int(132)))
276 » » » case Int: 264 » » case Int8:
277 » » » » v.SetValue(NewValue(int(132))) 265 » » » v.Set(NewValue(int8(8)))
278 » » » case Int8: 266 » » case Int16:
279 » » » » v.SetValue(NewValue(int8(8))) 267 » » » v.Set(NewValue(int16(16)))
280 » » » case Int16: 268 » » case Int32:
281 » » » » v.SetValue(NewValue(int16(16))) 269 » » » v.Set(NewValue(int32(32)))
282 » » » case Int32: 270 » » case Int64:
283 » » » » v.SetValue(NewValue(int32(32))) 271 » » » v.Set(NewValue(int64(64)))
284 » » » case Int64: 272 » » case Uint:
285 » » » » v.SetValue(NewValue(int64(64))) 273 » » » v.Set(NewValue(uint(132)))
286 » » » } 274 » » case Uint8:
287 » » case *UintValue: 275 » » » v.Set(NewValue(uint8(8)))
288 » » » switch v.Type().Kind() { 276 » » case Uint16:
289 » » » case Uint: 277 » » » v.Set(NewValue(uint16(16)))
290 » » » » v.SetValue(NewValue(uint(132))) 278 » » case Uint32:
291 » » » case Uint8: 279 » » » v.Set(NewValue(uint32(32)))
292 » » » » v.SetValue(NewValue(uint8(8))) 280 » » case Uint64:
293 » » » case Uint16: 281 » » » v.Set(NewValue(uint64(64)))
294 » » » » v.SetValue(NewValue(uint16(16))) 282 » » case Float32:
295 » » » case Uint32: 283 » » » v.Set(NewValue(float32(256.25)))
296 » » » » v.SetValue(NewValue(uint32(32))) 284 » » case Float64:
297 » » » case Uint64: 285 » » » v.Set(NewValue(512.125))
298 » » » » v.SetValue(NewValue(uint64(64))) 286 » » case Complex64:
299 » » » } 287 » » » v.Set(NewValue(complex64(532.125 + 10i)))
300 » » case *FloatValue: 288 » » case Complex128:
301 » » » switch v.Type().Kind() { 289 » » » v.Set(NewValue(complex128(564.25 + 1i)))
302 » » » case Float32: 290 » » case String:
303 » » » » v.SetValue(NewValue(float32(256.25))) 291 » » » v.Set(NewValue("stringy cheese"))
304 » » » case Float64: 292 » » case Bool:
305 » » » » v.SetValue(NewValue(512.125)) 293 » » » v.Set(NewValue(true))
306 » » » }
307 » » case *ComplexValue:
308 » » » switch v.Type().Kind() {
309 » » » case Complex64:
310 » » » » v.SetValue(NewValue(complex64(532.125 + 10i)))
311 » » » case Complex128:
312 » » » » v.SetValue(NewValue(complex128(564.25 + 1i)))
313 » » » }
314
315 » » case *StringValue:
316 » » » v.SetValue(NewValue("stringy cheese"))
317 » » case *BoolValue:
318 » » » v.SetValue(NewValue(true))
319 } 294 }
320 s := valueToString(v) 295 s := valueToString(v)
321 if s != tt.s { 296 if s != tt.s {
322 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s) 297 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
323 } 298 }
324 } 299 }
325 } 300 }
326 301
327 var _i = 7 302 var _i = 7
328 303
(...skipping 14 matching lines...) Expand all
343 for i, test := range valueToStringTests { 318 for i, test := range valueToStringTests {
344 s := valueToString(NewValue(test.i)) 319 s := valueToString(NewValue(test.i))
345 if s != test.s { 320 if s != test.s {
346 t.Errorf("#%d: have %#q, want %#q", i, s, test.s) 321 t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
347 } 322 }
348 } 323 }
349 } 324 }
350 325
351 func TestArrayElemSet(t *testing.T) { 326 func TestArrayElemSet(t *testing.T) {
352 v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) 327 v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
353 » v.(*ArrayValue).Elem(4).(*IntValue).Set(123) 328 » v.Index(4).SetInt(123)
354 s := valueToString(v) 329 s := valueToString(v)
355 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}" 330 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
356 if s != want { 331 if s != want {
357 t.Errorf("[10]int: have %#q want %#q", s, want) 332 t.Errorf("[10]int: have %#q want %#q", s, want)
358 } 333 }
359 334
360 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) 335 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
361 » v.(*SliceValue).Elem(4).(*IntValue).Set(123) 336 » v.Index(4).SetInt(123)
362 s = valueToString(v) 337 s = valueToString(v)
363 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}" 338 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
364 if s != want1 { 339 if s != want1 {
365 t.Errorf("[]int: have %#q want %#q", s, want1) 340 t.Errorf("[]int: have %#q want %#q", s, want1)
366 } 341 }
367 } 342 }
368 343
369 func TestPtrPointTo(t *testing.T) { 344 func TestPtrPointTo(t *testing.T) {
370 var ip *int32 345 var ip *int32
371 var i int32 = 1234 346 var i int32 = 1234
372 vip := NewValue(&ip) 347 vip := NewValue(&ip)
373 vi := NewValue(i) 348 vi := NewValue(i)
374 » vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi) 349 » vip.Elem().Set(vi.Addr())
375 if *ip != 1234 { 350 if *ip != 1234 {
376 t.Errorf("got %d, want 1234", *ip) 351 t.Errorf("got %d, want 1234", *ip)
377 } 352 }
378 353
379 ip = nil 354 ip = nil
380 » vp := NewValue(ip).(*PtrValue) 355 » vp := NewValue(ip)
381 » vp.PointTo(vp.Elem()) 356 » vp.Set(Zero(vp.Type()))
382 if ip != nil { 357 if ip != nil {
383 t.Errorf("got non-nil (%p), want nil", ip) 358 t.Errorf("got non-nil (%p), want nil", ip)
384 } 359 }
385 } 360 }
386 361
387 func TestPtrSetNil(t *testing.T) { 362 func TestPtrSetNil(t *testing.T) {
388 var i int32 = 1234 363 var i int32 = 1234
389 ip := &i 364 ip := &i
390 vip := NewValue(&ip) 365 vip := NewValue(&ip)
391 » vip.(*PtrValue).Elem().(*PtrValue).Set(nil) 366 » vip.Elem().Set(Zero(vip.Elem().Type()))
392 if ip != nil { 367 if ip != nil {
393 t.Errorf("got non-nil (%d), want nil", *ip) 368 t.Errorf("got non-nil (%d), want nil", *ip)
394 } 369 }
395 } 370 }
396 371
397 func TestMapSetNil(t *testing.T) { 372 func TestMapSetNil(t *testing.T) {
398 m := make(map[string]int) 373 m := make(map[string]int)
399 vm := NewValue(&m) 374 vm := NewValue(&m)
400 » vm.(*PtrValue).Elem().(*MapValue).Set(nil) 375 » vm.Elem().Set(Zero(vm.Elem().Type()))
401 if m != nil { 376 if m != nil {
402 t.Errorf("got non-nil (%p), want nil", m) 377 t.Errorf("got non-nil (%p), want nil", m)
403 } 378 }
404 } 379 }
405 380
406 381
407 func TestAll(t *testing.T) { 382 func TestAll(t *testing.T) {
408 testType(t, 1, Typeof((int8)(0)), "int8") 383 testType(t, 1, Typeof((int8)(0)), "int8")
409 » testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8") 384 » testType(t, 2, Typeof((*int8)(nil)).Elem(), "int8")
410 385
411 typ := Typeof((*struct { 386 typ := Typeof((*struct {
412 c chan *int32 387 c chan *int32
413 d float32 388 d float32
414 })(nil)) 389 })(nil))
415 testType(t, 3, typ, "*struct { c chan *int32; d float32 }") 390 testType(t, 3, typ, "*struct { c chan *int32; d float32 }")
416 » etyp := typ.(*PtrType).Elem() 391 » etyp := typ.Elem()
417 testType(t, 4, etyp, "struct { c chan *int32; d float32 }") 392 testType(t, 4, etyp, "struct { c chan *int32; d float32 }")
418 » styp := etyp.(*StructType) 393 » styp := etyp
419 f := styp.Field(0) 394 f := styp.Field(0)
420 testType(t, 5, f.Type, "chan *int32") 395 testType(t, 5, f.Type, "chan *int32")
421 396
422 f, present := styp.FieldByName("d") 397 f, present := styp.FieldByName("d")
423 if !present { 398 if !present {
424 t.Errorf("FieldByName says present field is absent") 399 t.Errorf("FieldByName says present field is absent")
425 } 400 }
426 testType(t, 6, f.Type, "float32") 401 testType(t, 6, f.Type, "float32")
427 402
428 f, present = styp.FieldByName("absent") 403 f, present = styp.FieldByName("absent")
429 if present { 404 if present {
430 t.Errorf("FieldByName says absent field is present") 405 t.Errorf("FieldByName says absent field is present")
431 } 406 }
432 407
433 typ = Typeof([32]int32{}) 408 typ = Typeof([32]int32{})
434 testType(t, 7, typ, "[32]int32") 409 testType(t, 7, typ, "[32]int32")
435 » testType(t, 8, typ.(*ArrayType).Elem(), "int32") 410 » testType(t, 8, typ.Elem(), "int32")
436 411
437 typ = Typeof((map[string]*int32)(nil)) 412 typ = Typeof((map[string]*int32)(nil))
438 testType(t, 9, typ, "map[string] *int32") 413 testType(t, 9, typ, "map[string] *int32")
439 » mtyp := typ.(*MapType) 414 » mtyp := typ
440 testType(t, 10, mtyp.Key(), "string") 415 testType(t, 10, mtyp.Key(), "string")
441 testType(t, 11, mtyp.Elem(), "*int32") 416 testType(t, 11, mtyp.Elem(), "*int32")
442 417
443 typ = Typeof((chan<- string)(nil)) 418 typ = Typeof((chan<- string)(nil))
444 testType(t, 12, typ, "chan<- string") 419 testType(t, 12, typ, "chan<- string")
445 » testType(t, 13, typ.(*ChanType).Elem(), "string") 420 » testType(t, 13, typ.Elem(), "string")
446 421
447 // make sure tag strings are not part of element type 422 // make sure tag strings are not part of element type
448 typ = Typeof(struct { 423 typ = Typeof(struct {
449 d []uint32 "TAG" 424 d []uint32 "TAG"
450 » }{}).(*StructType).Field(0).Type 425 » }{}).Field(0).Type
451 testType(t, 14, typ, "[]uint32") 426 testType(t, 14, typ, "[]uint32")
452 } 427 }
453 428
454 func TestInterfaceGet(t *testing.T) { 429 func TestInterfaceGet(t *testing.T) {
455 var inter struct { 430 var inter struct {
456 e interface{} 431 e interface{}
457 } 432 }
458 inter.e = 123.456 433 inter.e = 123.456
459 v1 := NewValue(&inter) 434 v1 := NewValue(&inter)
460 » v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0) 435 » v2 := v1.Elem().Field(0)
461 assert(t, v2.Type().String(), "interface { }") 436 assert(t, v2.Type().String(), "interface { }")
462 » i2 := v2.(*InterfaceValue).Interface() 437 » i2 := v2.Interface()
463 v3 := NewValue(i2) 438 v3 := NewValue(i2)
464 assert(t, v3.Type().String(), "float64") 439 assert(t, v3.Type().String(), "float64")
465 } 440 }
466 441
467 func TestInterfaceValue(t *testing.T) { 442 func TestInterfaceValue(t *testing.T) {
468 var inter struct { 443 var inter struct {
469 e interface{} 444 e interface{}
470 } 445 }
471 inter.e = 123.456 446 inter.e = 123.456
472 v1 := NewValue(&inter) 447 v1 := NewValue(&inter)
473 » v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0) 448 » v2 := v1.Elem().Field(0)
474 assert(t, v2.Type().String(), "interface { }") 449 assert(t, v2.Type().String(), "interface { }")
475 » v3 := v2.(*InterfaceValue).Elem() 450 » v3 := v2.Elem()
476 assert(t, v3.Type().String(), "float64") 451 assert(t, v3.Type().String(), "float64")
477 452
478 i3 := v2.Interface() 453 i3 := v2.Interface()
479 if _, ok := i3.(float64); !ok { 454 if _, ok := i3.(float64); !ok {
480 t.Error("v2.Interface() did not return float64, got ", Typeof(i3 )) 455 t.Error("v2.Interface() did not return float64, got ", Typeof(i3 ))
481 } 456 }
482 } 457 }
483 458
484 func TestFunctionValue(t *testing.T) { 459 func TestFunctionValue(t *testing.T) {
485 v := NewValue(func() {}) 460 v := NewValue(func() {})
(...skipping 13 matching lines...) Expand all
499 func TestAppend(t *testing.T) { 474 func TestAppend(t *testing.T) {
500 for i, test := range appendTests { 475 for i, test := range appendTests {
501 origLen, extraLen := len(test.orig), len(test.extra) 476 origLen, extraLen := len(test.orig), len(test.extra)
502 want := append(test.orig, test.extra...) 477 want := append(test.orig, test.extra...)
503 // Convert extra from []int to []Value. 478 // Convert extra from []int to []Value.
504 e0 := make([]Value, len(test.extra)) 479 e0 := make([]Value, len(test.extra))
505 for j, e := range test.extra { 480 for j, e := range test.extra {
506 e0[j] = NewValue(e) 481 e0[j] = NewValue(e)
507 } 482 }
508 // Convert extra from []int to *SliceValue. 483 // Convert extra from []int to *SliceValue.
509 » » e1 := NewValue(test.extra).(*SliceValue) 484 » » e1 := NewValue(test.extra)
510 // Test Append. 485 // Test Append.
511 » » a0 := NewValue(test.orig).(*SliceValue) 486 » » a0 := NewValue(test.orig)
512 have0 := Append(a0, e0...).Interface().([]int) 487 have0 := Append(a0, e0...).Interface().([]int)
513 if !DeepEqual(have0, want) { 488 if !DeepEqual(have0, want) {
514 t.Errorf("Append #%d: have %v, want %v", i, have0, want) 489 t.Errorf("Append #%d: have %v, want %v", i, have0, want)
515 } 490 }
516 // Check that the orig and extra slices were not modified. 491 // Check that the orig and extra slices were not modified.
517 if len(test.orig) != origLen { 492 if len(test.orig) != origLen {
518 t.Errorf("Append #%d origLen: have %v, want %v", i, len( test.orig), origLen) 493 t.Errorf("Append #%d origLen: have %v, want %v", i, len( test.orig), origLen)
519 } 494 }
520 if len(test.extra) != extraLen { 495 if len(test.extra) != extraLen {
521 t.Errorf("Append #%d extraLen: have %v, want %v", i, len (test.extra), extraLen) 496 t.Errorf("Append #%d extraLen: have %v, want %v", i, len (test.extra), extraLen)
522 } 497 }
523 // Test AppendSlice. 498 // Test AppendSlice.
524 » » a1 := NewValue(test.orig).(*SliceValue) 499 » » a1 := NewValue(test.orig)
525 have1 := AppendSlice(a1, e1).Interface().([]int) 500 have1 := AppendSlice(a1, e1).Interface().([]int)
526 if !DeepEqual(have1, want) { 501 if !DeepEqual(have1, want) {
527 t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want) 502 t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want)
528 } 503 }
529 // Check that the orig and extra slices were not modified. 504 // Check that the orig and extra slices were not modified.
530 if len(test.orig) != origLen { 505 if len(test.orig) != origLen {
531 t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen) 506 t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen)
532 } 507 }
533 if len(test.extra) != extraLen { 508 if len(test.extra) != extraLen {
534 t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i , len(test.extra), extraLen) 509 t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i , len(test.extra), extraLen)
535 } 510 }
536 } 511 }
537 } 512 }
538 513
539 func TestCopy(t *testing.T) { 514 func TestCopy(t *testing.T) {
540 a := []int{1, 2, 3, 4, 10, 9, 8, 7} 515 a := []int{1, 2, 3, 4, 10, 9, 8, 7}
541 b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44} 516 b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
542 c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44} 517 c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
543 for i := 0; i < len(b); i++ { 518 for i := 0; i < len(b); i++ {
544 if b[i] != c[i] { 519 if b[i] != c[i] {
545 t.Fatalf("b != c before test") 520 t.Fatalf("b != c before test")
546 } 521 }
547 } 522 }
548 » aa := NewValue(a).(*SliceValue) 523 » aa := NewValue(a)
549 » ab := NewValue(b).(*SliceValue) 524 » ab := NewValue(b)
550 for tocopy := 1; tocopy <= 7; tocopy++ { 525 for tocopy := 1; tocopy <= 7; tocopy++ {
551 aa.SetLen(tocopy) 526 aa.SetLen(tocopy)
552 Copy(ab, aa) 527 Copy(ab, aa)
553 aa.SetLen(8) 528 aa.SetLen(8)
554 for i := 0; i < tocopy; i++ { 529 for i := 0; i < tocopy; i++ {
555 if a[i] != b[i] { 530 if a[i] != b[i] {
556 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d", 531 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
557 tocopy, i, a[i], i, b[i]) 532 tocopy, i, a[i], i, b[i])
558 } 533 }
559 } 534 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 for _, test := range deepEqualTests { 628 for _, test := range deepEqualTests {
654 if r := DeepEqual(test.a, test.b); r != test.eq { 629 if r := DeepEqual(test.a, test.b); r != test.eq {
655 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test .b, r, test.eq) 630 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test .b, r, test.eq)
656 } 631 }
657 } 632 }
658 } 633 }
659 634
660 func TestTypeof(t *testing.T) { 635 func TestTypeof(t *testing.T) {
661 for _, test := range deepEqualTests { 636 for _, test := range deepEqualTests {
662 v := NewValue(test.a) 637 v := NewValue(test.a)
663 » » if v == nil { 638 » » if !v.IsValid() {
664 continue 639 continue
665 } 640 }
666 typ := Typeof(test.a) 641 typ := Typeof(test.a)
667 if typ != v.Type() { 642 if typ != v.Type() {
668 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v" , test.a, typ, test.a, v.Type()) 643 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v" , test.a, typ, test.a, v.Type())
669 } 644 }
670 } 645 }
671 } 646 }
672 647
673 type Recursive struct { 648 type Recursive struct {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 a, b := new(_Complex), new(_Complex) 683 a, b := new(_Complex), new(_Complex)
709 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m} 684 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
710 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m} 685 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
711 if DeepEqual(a, b) { 686 if DeepEqual(a, b) {
712 t.Error("DeepEqual(complex different) = true, want false") 687 t.Error("DeepEqual(complex different) = true, want false")
713 } 688 }
714 } 689 }
715 690
716 691
717 func check2ndField(x interface{}, offs uintptr, t *testing.T) { 692 func check2ndField(x interface{}, offs uintptr, t *testing.T) {
718 » s := NewValue(x).(*StructValue) 693 » s := NewValue(x)
719 » f := s.Type().(*StructType).Field(1) 694 » f := s.Type().Field(1)
720 if f.Offset != offs { 695 if f.Offset != offs {
721 t.Error("mismatched offsets in structure alignment:", f.Offset, offs) 696 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
722 } 697 }
723 } 698 }
724 699
725 // Check that structure alignment & offsets viewed through reflect agree with th ose 700 // Check that structure alignment & offsets viewed through reflect agree with th ose
726 // from the compiler itself. 701 // from the compiler itself.
727 func TestAlignment(t *testing.T) { 702 func TestAlignment(t *testing.T) {
728 type T1inner struct { 703 type T1inner struct {
729 a int 704 a int
(...skipping 10 matching lines...) Expand all
740 f int 715 f int
741 } 716 }
742 717
743 x := T1{T1inner{2}, 17} 718 x := T1{T1inner{2}, 17}
744 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x )), t) 719 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x )), t)
745 720
746 x1 := T2{T2inner{2, 3}, 17} 721 x1 := T2{T2inner{2, 3}, 17}
747 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer( &x1)), t) 722 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer( &x1)), t)
748 } 723 }
749 724
750 type IsNiller interface {
751 IsNil() bool
752 }
753
754 func Nil(a interface{}, t *testing.T) { 725 func Nil(a interface{}, t *testing.T) {
755 » n := NewValue(a).(*StructValue).Field(0).(IsNiller) 726 » n := NewValue(a).Field(0)
756 if !n.IsNil() { 727 if !n.IsNil() {
757 t.Errorf("%v should be nil", a) 728 t.Errorf("%v should be nil", a)
758 } 729 }
759 } 730 }
760 731
761 func NotNil(a interface{}, t *testing.T) { 732 func NotNil(a interface{}, t *testing.T) {
762 » n := NewValue(a).(*StructValue).Field(0).(IsNiller) 733 » n := NewValue(a).Field(0)
763 if n.IsNil() { 734 if n.IsNil() {
764 t.Errorf("value of type %v should not be nil", NewValue(a).Type( ).String()) 735 t.Errorf("value of type %v should not be nil", NewValue(a).Type( ).String())
765 } 736 }
766 } 737 }
767 738
768 func TestIsNil(t *testing.T) { 739 func TestIsNil(t *testing.T) {
769 » // These do not implement IsNil 740 » // These implement IsNil.
770 » doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
771 » for _, ts := range doNotNil {
772 » » ty := Typeof(ts)
773 » » v := MakeZero(ty)
774 » » if _, ok := v.(IsNiller); ok {
775 » » » t.Errorf("%s is nilable; should not be", ts)
776 » » }
777 » }
778
779 » // These do implement IsNil.
780 // Wrap in extra struct to hide interface type. 741 // Wrap in extra struct to hide interface type.
781 doNil := []interface{}{ 742 doNil := []interface{}{
782 struct{ x *int }{}, 743 struct{ x *int }{},
783 struct{ x interface{} }{}, 744 struct{ x interface{} }{},
784 struct{ x map[string]int }{}, 745 struct{ x map[string]int }{},
785 struct{ x func() bool }{}, 746 struct{ x func() bool }{},
786 struct{ x chan int }{}, 747 struct{ x chan int }{},
787 struct{ x []string }{}, 748 struct{ x []string }{},
788 } 749 }
789 for _, ts := range doNil { 750 for _, ts := range doNil {
790 » » ty := Typeof(ts).(*StructType).Field(0).Type 751 » » ty := Typeof(ts).Field(0).Type
791 » » v := MakeZero(ty) 752 » » v := Zero(ty)
792 » » if _, ok := v.(IsNiller); !ok { 753 » » v.IsNil() // panics if not okay to call
793 » » » t.Errorf("%s %T is not nilable; should be", ts, v)
794 » » }
795 } 754 }
796 755
797 // Check the implementations 756 // Check the implementations
798 var pi struct { 757 var pi struct {
799 x *int 758 x *int
800 } 759 }
801 Nil(pi, t) 760 Nil(pi, t)
802 pi.x = new(int) 761 pi.x = new(int)
803 NotNil(pi, t) 762 NotNil(pi, t)
804 763
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 fi.x = TestIsNil 796 fi.x = TestIsNil
838 NotNil(fi, t) 797 NotNil(fi, t)
839 } 798 }
840 799
841 func TestInterfaceExtraction(t *testing.T) { 800 func TestInterfaceExtraction(t *testing.T) {
842 var s struct { 801 var s struct {
843 w io.Writer 802 w io.Writer
844 } 803 }
845 804
846 s.w = os.Stdout 805 s.w = os.Stdout
847 » v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface() 806 » v := Indirect(NewValue(&s)).Field(0).Interface()
848 if v != s.w.(interface{}) { 807 if v != s.w.(interface{}) {
849 t.Error("Interface() on interface: ", v, s.w) 808 t.Error("Interface() on interface: ", v, s.w)
850 } 809 }
851 } 810 }
852 811
853 func TestInterfaceEditing(t *testing.T) { 812 func TestInterfaceEditing(t *testing.T) {
854 // strings are bigger than one word, 813 // strings are bigger than one word,
855 // so the interface conversion allocates 814 // so the interface conversion allocates
856 // memory to hold a string and puts that 815 // memory to hold a string and puts that
857 // pointer in the interface. 816 // pointer in the interface.
858 var i interface{} = "hello" 817 var i interface{} = "hello"
859 818
860 // if i pass the interface value by value 819 // if i pass the interface value by value
861 // to NewValue, i should get a fresh copy 820 // to NewValue, i should get a fresh copy
862 // of the value. 821 // of the value.
863 v := NewValue(i) 822 v := NewValue(i)
864 823
865 // and setting that copy to "bye" should 824 // and setting that copy to "bye" should
866 // not change the value stored in i. 825 // not change the value stored in i.
867 » v.(*StringValue).Set("bye") 826 » v.SetString("bye")
868 if i.(string) != "hello" { 827 if i.(string) != "hello" {
869 t.Errorf(`Set("bye") changed i to %s`, i.(string)) 828 t.Errorf(`Set("bye") changed i to %s`, i.(string))
870 } 829 }
871 830
872 // the same should be true of smaller items. 831 // the same should be true of smaller items.
873 i = 123 832 i = 123
874 v = NewValue(i) 833 v = NewValue(i)
875 » v.(*IntValue).Set(234) 834 » v.SetInt(234)
876 if i.(int) != 123 { 835 if i.(int) != 123 {
877 t.Errorf("Set(234) changed i to %d", i.(int)) 836 t.Errorf("Set(234) changed i to %d", i.(int))
878 } 837 }
879 } 838 }
880 839
881 func TestNilPtrValueSub(t *testing.T) { 840 func TestNilPtrValueSub(t *testing.T) {
882 var pi *int 841 var pi *int
883 » if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil { 842 » if pv := NewValue(pi); pv.Elem().IsValid() {
884 » » t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil") 843 » » t.Error("NewValue((*int)(nil)).Elem().IsValid()")
885 } 844 }
886 } 845 }
887 846
888 func TestMap(t *testing.T) { 847 func TestMap(t *testing.T) {
889 m := map[string]int{"a": 1, "b": 2} 848 m := map[string]int{"a": 1, "b": 2}
890 » mv := NewValue(m).(*MapValue) 849 » mv := NewValue(m)
891 if n := mv.Len(); n != len(m) { 850 if n := mv.Len(); n != len(m) {
892 t.Errorf("Len = %d, want %d", n, len(m)) 851 t.Errorf("Len = %d, want %d", n, len(m))
893 } 852 }
894 » keys := mv.Keys() 853 » keys := mv.MapKeys()
895 i := 0 854 i := 0
896 » newmap := MakeMap(mv.Type().(*MapType)) 855 » newmap := MakeMap(mv.Type())
897 for k, v := range m { 856 for k, v := range m {
898 // Check that returned Keys match keys in range. 857 // Check that returned Keys match keys in range.
899 // These aren't required to be in the same order, 858 // These aren't required to be in the same order,
900 // but they are in this implementation, which makes 859 // but they are in this implementation, which makes
901 // the test easier. 860 // the test easier.
902 if i >= len(keys) { 861 if i >= len(keys) {
903 t.Errorf("Missing key #%d %q", i, k) 862 t.Errorf("Missing key #%d %q", i, k)
904 » » } else if kv := keys[i].(*StringValue); kv.Get() != k { 863 » » } else if kv := keys[i]; kv.String() != k {
905 » » » t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k) 864 » » » t.Errorf("Keys[%q] = %d, want %d", i, kv.Int(), k)
906 } 865 }
907 i++ 866 i++
908 867
909 // Check that value lookup is correct. 868 // Check that value lookup is correct.
910 » » vv := mv.Elem(NewValue(k)) 869 » » vv := mv.MapIndex(NewValue(k))
911 » » if vi := vv.(*IntValue).Get(); vi != int64(v) { 870 » » if vi := vv.Int(); vi != int64(v) {
912 t.Errorf("Key %q: have value %d, want %d", k, vi, v) 871 t.Errorf("Key %q: have value %d, want %d", k, vi, v)
913 } 872 }
914 873
915 // Copy into new map. 874 // Copy into new map.
916 » » newmap.SetElem(NewValue(k), NewValue(v)) 875 » » newmap.SetMapIndex(NewValue(k), NewValue(v))
917 » } 876 » }
918 » vv := mv.Elem(NewValue("not-present")) 877 » vv := mv.MapIndex(NewValue("not-present"))
919 » if vv != nil { 878 » if vv.IsValid() {
920 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv)) 879 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
921 } 880 }
922 881
923 newm := newmap.Interface().(map[string]int) 882 newm := newmap.Interface().(map[string]int)
924 if len(newm) != len(m) { 883 if len(newm) != len(m) {
925 t.Errorf("length after copy: newm=%d, m=%d", newm, m) 884 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
926 } 885 }
927 886
928 for k, v := range newm { 887 for k, v := range newm {
929 mv, ok := m[k] 888 mv, ok := m[k]
930 if mv != v { 889 if mv != v {
931 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, m v, ok) 890 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, m v, ok)
932 } 891 }
933 } 892 }
934 893
935 » newmap.SetElem(NewValue("a"), nil) 894 » newmap.SetMapIndex(NewValue("a"), Value{})
936 v, ok := newm["a"] 895 v, ok := newm["a"]
937 if ok { 896 if ok {
938 t.Errorf("newm[\"a\"] = %d after delete", v) 897 t.Errorf("newm[\"a\"] = %d after delete", v)
939 } 898 }
940 899
941 » mv = NewValue(&m).(*PtrValue).Elem().(*MapValue) 900 » mv = NewValue(&m).Elem()
942 » mv.Set(nil) 901 » mv.Set(Zero(mv.Type()))
943 if m != nil { 902 if m != nil {
944 t.Errorf("mv.Set(nil) failed") 903 t.Errorf("mv.Set(nil) failed")
945 } 904 }
946 } 905 }
947 906
948 func TestChan(t *testing.T) { 907 func TestChan(t *testing.T) {
949 for loop := 0; loop < 2; loop++ { 908 for loop := 0; loop < 2; loop++ {
950 var c chan int 909 var c chan int
951 » » var cv *ChanValue 910 » » var cv Value
952 911
953 // check both ways to allocate channels 912 // check both ways to allocate channels
954 switch loop { 913 switch loop {
955 case 1: 914 case 1:
956 c = make(chan int, 1) 915 c = make(chan int, 1)
957 » » » cv = NewValue(c).(*ChanValue) 916 » » » cv = NewValue(c)
958 case 0: 917 case 0:
959 » » » cv = MakeChan(Typeof(c).(*ChanType), 1) 918 » » » cv = MakeChan(Typeof(c), 1)
960 c = cv.Interface().(chan int) 919 c = cv.Interface().(chan int)
961 } 920 }
962 921
963 // Send 922 // Send
964 cv.Send(NewValue(2)) 923 cv.Send(NewValue(2))
965 if i := <-c; i != 2 { 924 if i := <-c; i != 2 {
966 t.Errorf("reflect Send 2, native recv %d", i) 925 t.Errorf("reflect Send 2, native recv %d", i)
967 } 926 }
968 927
969 // Recv 928 // Recv
970 c <- 3 929 c <- 3
971 » » if i, ok := cv.Recv(); i.(*IntValue).Get() != 3 || !ok { 930 » » if i, ok := cv.Recv(); i.Int() != 3 || !ok {
972 » » » t.Errorf("native send 3, reflect Recv %d, %t", i.(*IntVa lue).Get(), ok) 931 » » » t.Errorf("native send 3, reflect Recv %d, %t", i.Int(), ok)
973 } 932 }
974 933
975 // TryRecv fail 934 // TryRecv fail
976 val, ok := cv.TryRecv() 935 val, ok := cv.TryRecv()
977 » » if val != nil || ok { 936 » » if val.IsValid() || ok {
978 t.Errorf("TryRecv on empty chan: %s, %t", valueToString( val), ok) 937 t.Errorf("TryRecv on empty chan: %s, %t", valueToString( val), ok)
979 } 938 }
980 939
981 // TryRecv success 940 // TryRecv success
982 c <- 4 941 c <- 4
983 val, ok = cv.TryRecv() 942 val, ok = cv.TryRecv()
984 » » if val == nil { 943 » » if !val.IsValid() {
985 t.Errorf("TryRecv on ready chan got nil") 944 t.Errorf("TryRecv on ready chan got nil")
986 » » } else if i := val.(*IntValue).Get(); i != 4 || !ok { 945 » » } else if i := val.Int(); i != 4 || !ok {
987 t.Errorf("native send 4, TryRecv %d, %t", i, ok) 946 t.Errorf("native send 4, TryRecv %d, %t", i, ok)
988 } 947 }
989 948
990 // TrySend fail 949 // TrySend fail
991 c <- 100 950 c <- 100
992 ok = cv.TrySend(NewValue(5)) 951 ok = cv.TrySend(NewValue(5))
993 i := <-c 952 i := <-c
994 if ok { 953 if ok {
995 t.Errorf("TrySend on full chan succeeded: value %d", i) 954 t.Errorf("TrySend on full chan succeeded: value %d", i)
996 } 955 }
997 956
998 // TrySend success 957 // TrySend success
999 ok = cv.TrySend(NewValue(6)) 958 ok = cv.TrySend(NewValue(6))
1000 if !ok { 959 if !ok {
1001 t.Errorf("TrySend on empty chan failed") 960 t.Errorf("TrySend on empty chan failed")
1002 } else { 961 } else {
1003 if i = <-c; i != 6 { 962 if i = <-c; i != 6 {
1004 t.Errorf("TrySend 6, recv %d", i) 963 t.Errorf("TrySend 6, recv %d", i)
1005 } 964 }
1006 } 965 }
1007 966
1008 // Close 967 // Close
1009 c <- 123 968 c <- 123
1010 cv.Close() 969 cv.Close()
1011 » » if i, ok := cv.Recv(); i.(*IntValue).Get() != 123 || !ok { 970 » » if i, ok := cv.Recv(); i.Int() != 123 || !ok {
1012 » » » t.Errorf("send 123 then close; Recv %d, %t", i.(*IntValu e).Get(), ok) 971 » » » t.Errorf("send 123 then close; Recv %d, %t", i.Int(), ok )
1013 » » } 972 » » }
1014 » » if i, ok := cv.Recv(); i.(*IntValue).Get() != 0 || ok { 973 » » if i, ok := cv.Recv(); i.Int() != 0 || ok {
1015 » » » t.Errorf("after close Recv %d, %t", i.(*IntValue).Get(), ok) 974 » » » t.Errorf("after close Recv %d, %t", i.Int(), ok)
1016 } 975 }
1017 } 976 }
1018 977
1019 // check creation of unbuffered channel 978 // check creation of unbuffered channel
1020 var c chan int 979 var c chan int
1021 » cv := MakeChan(Typeof(c).(*ChanType), 0) 980 » cv := MakeChan(Typeof(c), 0)
1022 c = cv.Interface().(chan int) 981 c = cv.Interface().(chan int)
1023 if cv.TrySend(NewValue(7)) { 982 if cv.TrySend(NewValue(7)) {
1024 t.Errorf("TrySend on sync chan succeeded") 983 t.Errorf("TrySend on sync chan succeeded")
1025 } 984 }
1026 » if v, ok := cv.TryRecv(); v != nil || ok { 985 » if v, ok := cv.TryRecv(); v.IsValid() || ok {
1027 t.Errorf("TryRecv on sync chan succeeded") 986 t.Errorf("TryRecv on sync chan succeeded")
1028 } 987 }
1029 988
1030 // len/cap 989 // len/cap
1031 » cv = MakeChan(Typeof(c).(*ChanType), 10) 990 » cv = MakeChan(Typeof(c), 10)
1032 c = cv.Interface().(chan int) 991 c = cv.Interface().(chan int)
1033 for i := 0; i < 3; i++ { 992 for i := 0; i < 3; i++ {
1034 c <- i 993 c <- i
1035 } 994 }
1036 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) { 995 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
1037 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c)) 996 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
1038 } 997 }
1039 998
1040 } 999 }
1041 1000
1042 // Difficult test for function call because of 1001 // Difficult test for function call because of
1043 // implicit padding between arguments. 1002 // implicit padding between arguments.
1044 func dummy(b byte, c int, d byte) (i byte, j int, k byte) { 1003 func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
1045 return b, c, d 1004 return b, c, d
1046 } 1005 }
1047 1006
1048 func TestFunc(t *testing.T) { 1007 func TestFunc(t *testing.T) {
1049 » ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), New Value(20), NewValue(byte(30))}) 1008 » ret := NewValue(dummy).Call([]Value{NewValue(byte(10)), NewValue(20), Ne wValue(byte(30))})
1050 if len(ret) != 3 { 1009 if len(ret) != 3 {
1051 t.Fatalf("Call returned %d values, want 3", len(ret)) 1010 t.Fatalf("Call returned %d values, want 3", len(ret))
1052 } 1011 }
1053 1012
1054 » i := ret[0].(*UintValue).Get() 1013 » i := byte(ret[0].Uint())
1055 » j := ret[1].(*IntValue).Get() 1014 » j := int(ret[1].Int())
1056 » k := ret[2].(*UintValue).Get() 1015 » k := byte(ret[2].Uint())
1057 if i != 10 || j != 20 || k != 30 { 1016 if i != 10 || j != 20 || k != 30 {
1058 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k) 1017 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
1059 } 1018 }
1060 } 1019 }
1061 1020
1062 type Point struct { 1021 type Point struct {
1063 x, y int 1022 x, y int
1064 } 1023 }
1065 1024
1066 func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale } 1025 func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
1067 1026
1068 func TestMethod(t *testing.T) { 1027 func TestMethod(t *testing.T) {
1069 // Non-curried method of type. 1028 // Non-curried method of type.
1070 p := Point{3, 4} 1029 p := Point{3, 4}
1071 » i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0 ].(*IntValue).Get() 1030 » i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0 ].Int()
1072 if i != 250 { 1031 if i != 250 {
1073 t.Errorf("Type Method returned %d; want 250", i) 1032 t.Errorf("Type Method returned %d; want 250", i)
1074 } 1033 }
1075 1034
1076 » i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[ 0].(*IntValue).Get() 1035 » i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[ 0].Int()
1077 if i != 250 { 1036 if i != 250 {
1078 t.Errorf("Pointer Type Method returned %d; want 250", i) 1037 t.Errorf("Pointer Type Method returned %d; want 250", i)
1079 } 1038 }
1080 1039
1081 // Curried method of value. 1040 // Curried method of value.
1082 » i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get () 1041 » i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].Int()
1083 if i != 250 { 1042 if i != 250 {
1084 t.Errorf("Value Method returned %d; want 250", i) 1043 t.Errorf("Value Method returned %d; want 250", i)
1085 } 1044 }
1086 1045
1087 // Curried method of pointer. 1046 // Curried method of pointer.
1088 » i = NewValue(&p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Ge t() 1047 » i = NewValue(&p).Method(0).Call([]Value{NewValue(10)})[0].Int()
1089 if i != 250 { 1048 if i != 250 {
1090 t.Errorf("Value Method returned %d; want 250", i) 1049 t.Errorf("Value Method returned %d; want 250", i)
1091 } 1050 }
1092 1051
1093 // Curried method of pointer to value. 1052 // Curried method of pointer to value.
1094 » i = NewValue(p).Addr().Method(0).Call([]Value{NewValue(10)})[0].(*IntVal ue).Get() 1053 » i = NewValue(p).Addr().Method(0).Call([]Value{NewValue(10)})[0].Int()
1095 if i != 250 { 1054 if i != 250 {
1096 t.Errorf("Value Method returned %d; want 250", i) 1055 t.Errorf("Value Method returned %d; want 250", i)
1097 } 1056 }
1098 1057
1099 // Curried method of interface value. 1058 // Curried method of interface value.
1100 // Have to wrap interface value in a struct to get at it. 1059 // Have to wrap interface value in a struct to get at it.
1101 // Passing it to NewValue directly would 1060 // Passing it to NewValue directly would
1102 // access the underlying Point, not the interface. 1061 // access the underlying Point, not the interface.
1103 var s = struct { 1062 var s = struct {
1104 x interface { 1063 x interface {
1105 Dist(int) int 1064 Dist(int) int
1106 } 1065 }
1107 }{p} 1066 }{p}
1108 » pv := NewValue(s).(*StructValue).Field(0) 1067 » pv := NewValue(s).Field(0)
1109 » i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get() 1068 » i = pv.Method(0).Call([]Value{NewValue(10)})[0].Int()
1110 if i != 250 { 1069 if i != 250 {
1111 t.Errorf("Interface Method returned %d; want 250", i) 1070 t.Errorf("Interface Method returned %d; want 250", i)
1112 } 1071 }
1113 } 1072 }
1114 1073
1115 func TestInterfaceSet(t *testing.T) { 1074 func TestInterfaceSet(t *testing.T) {
1116 p := &Point{3, 4} 1075 p := &Point{3, 4}
1117 1076
1118 var s struct { 1077 var s struct {
1119 I interface{} 1078 I interface{}
1120 P interface { 1079 P interface {
1121 Dist(int) int 1080 Dist(int) int
1122 } 1081 }
1123 } 1082 }
1124 » sv := NewValue(&s).(*PtrValue).Elem().(*StructValue) 1083 » sv := NewValue(&s).Elem()
1125 » sv.Field(0).(*InterfaceValue).Set(NewValue(p)) 1084 » sv.Field(0).Set(NewValue(p))
1126 if q := s.I.(*Point); q != p { 1085 if q := s.I.(*Point); q != p {
1127 t.Errorf("i: have %p want %p", q, p) 1086 t.Errorf("i: have %p want %p", q, p)
1128 } 1087 }
1129 1088
1130 » pv := sv.Field(1).(*InterfaceValue) 1089 » pv := sv.Field(1)
1131 pv.Set(NewValue(p)) 1090 pv.Set(NewValue(p))
1132 if q := s.P.(*Point); q != p { 1091 if q := s.P.(*Point); q != p {
1133 t.Errorf("i: have %p want %p", q, p) 1092 t.Errorf("i: have %p want %p", q, p)
1134 } 1093 }
1135 1094
1136 » i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get() 1095 » i := pv.Method(0).Call([]Value{NewValue(10)})[0].Int()
1137 if i != 250 { 1096 if i != 250 {
1138 t.Errorf("Interface Method returned %d; want 250", i) 1097 t.Errorf("Interface Method returned %d; want 250", i)
1139 } 1098 }
1140 } 1099 }
1141 1100
1142 type T1 struct { 1101 type T1 struct {
1143 a string 1102 a string
1144 int 1103 int
1145 } 1104 }
1146 1105
1147 func TestAnonymousFields(t *testing.T) { 1106 func TestAnonymousFields(t *testing.T) {
1148 var field StructField 1107 var field StructField
1149 var ok bool 1108 var ok bool
1150 var t1 T1 1109 var t1 T1
1151 » type1 := Typeof(t1).(*StructType) 1110 » type1 := Typeof(t1)
1152 if field, ok = type1.FieldByName("int"); !ok { 1111 if field, ok = type1.FieldByName("int"); !ok {
1153 t.Error("no field 'int'") 1112 t.Error("no field 'int'")
1154 } 1113 }
1155 if field.Index[0] != 1 { 1114 if field.Index[0] != 1 {
1156 t.Error("field index should be 1; is", field.Index) 1115 t.Error("field index should be 1; is", field.Index)
1157 } 1116 }
1158 } 1117 }
1159 1118
1160 type FTest struct { 1119 type FTest struct {
1161 s interface{} 1120 s interface{}
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 {S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'}, 1184 {S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'},
1226 {S3{}, "b", nil, 0}, 1185 {S3{}, "b", nil, 0},
1227 {S3{d: 'd'}, "d", []int{2}, 0}, 1186 {S3{d: 'd'}, "d", []int{2}, 0},
1228 {S3{e: 'e'}, "e", []int{3}, 'e'}, 1187 {S3{e: 'e'}, "e", []int{3}, 'e'},
1229 {S4{a: 'a'}, "a", []int{1}, 'a'}, 1188 {S4{a: 'a'}, "a", []int{1}, 'a'},
1230 {S4{}, "b", nil, 0}, 1189 {S4{}, "b", nil, 0},
1231 } 1190 }
1232 1191
1233 func TestFieldByIndex(t *testing.T) { 1192 func TestFieldByIndex(t *testing.T) {
1234 for _, test := range fieldTests { 1193 for _, test := range fieldTests {
1235 » » s := Typeof(test.s).(*StructType) 1194 » » s := Typeof(test.s)
1236 f := s.FieldByIndex(test.index) 1195 f := s.FieldByIndex(test.index)
1237 if f.Name != "" { 1196 if f.Name != "" {
1238 if test.index != nil { 1197 if test.index != nil {
1239 if f.Name != test.name { 1198 if f.Name != test.name {
1240 t.Errorf("%s.%s found; want %s", s.Name( ), f.Name, test.name) 1199 t.Errorf("%s.%s found; want %s", s.Name( ), f.Name, test.name)
1241 } 1200 }
1242 } else { 1201 } else {
1243 t.Errorf("%s.%s found", s.Name(), f.Name) 1202 t.Errorf("%s.%s found", s.Name(), f.Name)
1244 } 1203 }
1245 } else if len(test.index) > 0 { 1204 } else if len(test.index) > 0 {
1246 t.Errorf("%s.%s not found", s.Name(), test.name) 1205 t.Errorf("%s.%s not found", s.Name(), test.name)
1247 } 1206 }
1248 1207
1249 if test.value != 0 { 1208 if test.value != 0 {
1250 » » » v := NewValue(test.s).(*StructValue).FieldByIndex(test.i ndex) 1209 » » » v := NewValue(test.s).FieldByIndex(test.index)
1251 » » » if v != nil { 1210 » » » if v.IsValid() {
1252 if x, ok := v.Interface().(int); ok { 1211 if x, ok := v.Interface().(int); ok {
1253 if x != test.value { 1212 if x != test.value {
1254 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value) 1213 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
1255 } 1214 }
1256 } else { 1215 } else {
1257 t.Errorf("%s%v value not an int", s.Name (), test.index) 1216 t.Errorf("%s%v value not an int", s.Name (), test.index)
1258 } 1217 }
1259 } else { 1218 } else {
1260 t.Errorf("%s%v value not found", s.Name(), test. index) 1219 t.Errorf("%s%v value not found", s.Name(), test. index)
1261 } 1220 }
1262 } 1221 }
1263 } 1222 }
1264 } 1223 }
1265 1224
1266 func TestFieldByName(t *testing.T) { 1225 func TestFieldByName(t *testing.T) {
1267 for _, test := range fieldTests { 1226 for _, test := range fieldTests {
1268 » » s := Typeof(test.s).(*StructType) 1227 » » s := Typeof(test.s)
1269 f, found := s.FieldByName(test.name) 1228 f, found := s.FieldByName(test.name)
1270 if found { 1229 if found {
1271 if test.index != nil { 1230 if test.index != nil {
1272 // Verify field depth and index. 1231 // Verify field depth and index.
1273 if len(f.Index) != len(test.index) { 1232 if len(f.Index) != len(test.index) {
1274 t.Errorf("%s.%s depth %d; want %d", s.Na me(), test.name, len(f.Index), len(test.index)) 1233 t.Errorf("%s.%s depth %d; want %d", s.Na me(), test.name, len(f.Index), len(test.index))
1275 } else { 1234 } else {
1276 for i, x := range f.Index { 1235 for i, x := range f.Index {
1277 if x != test.index[i] { 1236 if x != test.index[i] {
1278 t.Errorf("%s.%s.Index[%d ] is %d; want %d", s.Name(), test.name, i, x, test.index[i]) 1237 t.Errorf("%s.%s.Index[%d ] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
1279 } 1238 }
1280 } 1239 }
1281 } 1240 }
1282 } else { 1241 } else {
1283 t.Errorf("%s.%s found", s.Name(), f.Name) 1242 t.Errorf("%s.%s found", s.Name(), f.Name)
1284 } 1243 }
1285 } else if len(test.index) > 0 { 1244 } else if len(test.index) > 0 {
1286 t.Errorf("%s.%s not found", s.Name(), test.name) 1245 t.Errorf("%s.%s not found", s.Name(), test.name)
1287 } 1246 }
1288 1247
1289 if test.value != 0 { 1248 if test.value != 0 {
1290 » » » v := NewValue(test.s).(*StructValue).FieldByName(test.na me) 1249 » » » v := NewValue(test.s).FieldByName(test.name)
1291 » » » if v != nil { 1250 » » » if v.IsValid() {
1292 if x, ok := v.Interface().(int); ok { 1251 if x, ok := v.Interface().(int); ok {
1293 if x != test.value { 1252 if x != test.value {
1294 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value) 1253 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
1295 } 1254 }
1296 } else { 1255 } else {
1297 t.Errorf("%s.%s value not an int", s.Nam e(), test.name) 1256 t.Errorf("%s.%s value not an int", s.Nam e(), test.name)
1298 } 1257 }
1299 } else { 1258 } else {
1300 t.Errorf("%s.%s value not found", s.Name(), test .name) 1259 t.Errorf("%s.%s value not found", s.Name(), test .name)
1301 } 1260 }
1302 } 1261 }
1303 } 1262 }
1304 } 1263 }
1305 1264
1306 func TestImportPath(t *testing.T) { 1265 func TestImportPath(t *testing.T) {
1307 if path := Typeof(vector.Vector{}).PkgPath(); path != "container/vector" { 1266 if path := Typeof(vector.Vector{}).PkgPath(); path != "container/vector" {
1308 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"contain er/vector\"", path) 1267 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"contain er/vector\"", path)
1309 } 1268 }
1310 } 1269 }
1311 1270
1312 func TestDotDotDot(t *testing.T) { 1271 func TestDotDotDot(t *testing.T) {
1313 // Test example from FuncType.DotDotDot documentation. 1272 // Test example from FuncType.DotDotDot documentation.
1314 var f func(x int, y ...float64) 1273 var f func(x int, y ...float64)
1315 » typ := Typeof(f).(*FuncType) 1274 » typ := Typeof(f)
1316 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) { 1275 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1317 » » sl, ok := typ.In(1).(*SliceType) 1276 » » sl := typ.In(1)
1318 » » if ok { 1277 » » if sl.Kind() == Slice {
1319 if sl.Elem() == Typeof(0.0) { 1278 if sl.Elem() == Typeof(0.0) {
1320 // ok 1279 // ok
1321 return 1280 return
1322 } 1281 }
1323 } 1282 }
1324 } 1283 }
1325 1284
1326 // Failed 1285 // Failed
1327 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64") 1286 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
1328 s := fmt.Sprintf("have NumIn() = %d", typ.NumIn()) 1287 s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
(...skipping 10 matching lines...) Expand all
1339 type outer struct { 1298 type outer struct {
1340 y int 1299 y int
1341 inner 1300 inner
1342 } 1301 }
1343 1302
1344 func (*inner) m() {} 1303 func (*inner) m() {}
1345 func (*outer) m() {} 1304 func (*outer) m() {}
1346 1305
1347 func TestNestedMethods(t *testing.T) { 1306 func TestNestedMethods(t *testing.T) {
1348 typ := Typeof((*outer)(nil)) 1307 typ := Typeof((*outer)(nil))
1349 » if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outer) .m).(*FuncValue).Get() { 1308 » if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != NewValue((*ou ter).m).Pointer() {
1350 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m) 1309 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m)
1351 for i := 0; i < typ.NumMethod(); i++ { 1310 for i := 0; i < typ.NumMethod(); i++ {
1352 m := typ.Method(i) 1311 m := typ.Method(i)
1353 » » » t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get()) 1312 » » » t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer())
1354 } 1313 }
1355 } 1314 }
1356 } 1315 }
1357 1316
1358 type innerInt struct { 1317 type innerInt struct {
1359 x int 1318 x int
1360 } 1319 }
1361 1320
1362 type outerInt struct { 1321 type outerInt struct {
1363 y int 1322 y int
1364 innerInt 1323 innerInt
1365 } 1324 }
1366 1325
1367 func (i *innerInt) m() int { 1326 func (i *innerInt) m() int {
1368 return i.x 1327 return i.x
1369 } 1328 }
1370 1329
1371 func TestEmbeddedMethods(t *testing.T) { 1330 func TestEmbeddedMethods(t *testing.T) {
1372 typ := Typeof((*outerInt)(nil)) 1331 typ := Typeof((*outerInt)(nil))
1373 » if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outerI nt).m).(*FuncValue).Get() { 1332 » if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != NewValue((*ou terInt).m).Pointer() {
1374 t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt). m) 1333 t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt). m)
1375 for i := 0; i < typ.NumMethod(); i++ { 1334 for i := 0; i < typ.NumMethod(); i++ {
1376 m := typ.Method(i) 1335 m := typ.Method(i)
1377 » » » t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get()) 1336 » » » t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer())
1378 } 1337 }
1379 } 1338 }
1380 1339
1381 i := &innerInt{3} 1340 i := &innerInt{3}
1382 » if v := NewValue(i).Method(0).Call(nil)[0].(*IntValue).Get(); v != 3 { 1341 » if v := NewValue(i).Method(0).Call(nil)[0].Int(); v != 3 {
1383 t.Errorf("i.m() = %d, want 3", v) 1342 t.Errorf("i.m() = %d, want 3", v)
1384 } 1343 }
1385 1344
1386 o := &outerInt{1, innerInt{2}} 1345 o := &outerInt{1, innerInt{2}}
1387 » if v := NewValue(o).Method(0).Call(nil)[0].(*IntValue).Get(); v != 2 { 1346 » if v := NewValue(o).Method(0).Call(nil)[0].Int(); v != 2 {
1388 t.Errorf("i.m() = %d, want 2", v) 1347 t.Errorf("i.m() = %d, want 2", v)
1389 } 1348 }
1390 1349
1391 f := (*outerInt).m 1350 f := (*outerInt).m
1392 if v := f(o); v != 2 { 1351 if v := f(o); v != 2 {
1393 t.Errorf("f(o) = %d, want 2", v) 1352 t.Errorf("f(o) = %d, want 2", v)
1394 } 1353 }
1395 } 1354 }
1396 1355
1397 func TestPtrTo(t *testing.T) { 1356 func TestPtrTo(t *testing.T) {
1398 var i int 1357 var i int
1399 1358
1400 typ := Typeof(i) 1359 typ := Typeof(i)
1401 for i = 0; i < 100; i++ { 1360 for i = 0; i < 100; i++ {
1402 typ = PtrTo(typ) 1361 typ = PtrTo(typ)
1403 } 1362 }
1404 for i = 0; i < 100; i++ { 1363 for i = 0; i < 100; i++ {
1405 » » typ = typ.(*PtrType).Elem() 1364 » » typ = typ.Elem()
1406 } 1365 }
1407 if typ != Typeof(i) { 1366 if typ != Typeof(i) {
1408 t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, Type of(i)) 1367 t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, Type of(i))
1409 } 1368 }
1410 } 1369 }
1411 1370
1412 func TestAddr(t *testing.T) { 1371 func TestAddr(t *testing.T) {
1413 var p struct { 1372 var p struct {
1414 X, Y int 1373 X, Y int
1415 } 1374 }
1416 1375
1417 v := NewValue(&p) 1376 v := NewValue(&p)
1418 » v = v.(*PtrValue).Elem() 1377 » v = v.Elem()
1419 v = v.Addr() 1378 v = v.Addr()
1420 » v = v.(*PtrValue).Elem() 1379 » v = v.Elem()
1421 » v = v.(*StructValue).Field(0) 1380 » v = v.Field(0)
1422 » v.(*IntValue).Set(2) 1381 » v.SetInt(2)
1423 if p.X != 2 { 1382 if p.X != 2 {
1424 t.Errorf("Addr.Elem.Set failed to set value") 1383 t.Errorf("Addr.Elem.Set failed to set value")
1425 } 1384 }
1426 1385
1427 // Again but take address of the NewValue value. 1386 // Again but take address of the NewValue value.
1428 // Exercises generation of PtrTypes not present in the binary. 1387 // Exercises generation of PtrTypes not present in the binary.
1429 v = NewValue(&p) 1388 v = NewValue(&p)
1430 v = v.Addr() 1389 v = v.Addr()
1431 » v = v.(*PtrValue).Elem() 1390 » v = v.Elem()
1432 » v = v.(*PtrValue).Elem() 1391 » v = v.Elem()
1433 v = v.Addr() 1392 v = v.Addr()
1434 » v = v.(*PtrValue).Elem() 1393 » v = v.Elem()
1435 » v = v.(*StructValue).Field(0) 1394 » v = v.Field(0)
1436 » v.(*IntValue).Set(3) 1395 » v.SetInt(3)
1437 if p.X != 3 { 1396 if p.X != 3 {
1438 t.Errorf("Addr.Elem.Set failed to set value") 1397 t.Errorf("Addr.Elem.Set failed to set value")
1439 } 1398 }
1440 1399
1441 // Starting without pointer we should get changed value 1400 // Starting without pointer we should get changed value
1442 // in interface. 1401 // in interface.
1443 v = NewValue(p) 1402 v = NewValue(p)
1444 v0 := v 1403 v0 := v
1445 v = v.Addr() 1404 v = v.Addr()
1446 » v = v.(*PtrValue).Elem() 1405 » v = v.Elem()
1447 » v = v.(*StructValue).Field(0) 1406 » v = v.Field(0)
1448 » v.(*IntValue).Set(4) 1407 » v.SetInt(4)
1449 if p.X != 3 { // should be unchanged from last time 1408 if p.X != 3 { // should be unchanged from last time
1450 t.Errorf("somehow value Set changed original p") 1409 t.Errorf("somehow value Set changed original p")
1451 } 1410 }
1452 p = v0.Interface().(struct { 1411 p = v0.Interface().(struct {
1453 X, Y int 1412 X, Y int
1454 }) 1413 })
1455 if p.X != 4 { 1414 if p.X != 4 {
1456 t.Errorf("Addr.Elem.Set valued to set value in top value") 1415 t.Errorf("Addr.Elem.Set valued to set value in top value")
1457 } 1416 }
1458 } 1417 }
LEFTRIGHT

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