PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: set_test.go Size: 5.71 KB
/proc/thread-self/root/opt/golang/1.22.0/src/reflect/set_test.go

// Copyright 2011 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.

package reflect_test

import (
	"bytes"
	"go/ast"
	"go/token"
	"io"
	. "reflect"
	"strings"
	"testing"
	"unsafe"
)

func TestImplicitMapConversion(t *testing.T) {
	// Test implicit conversions in MapIndex and SetMapIndex.
	{
		// direct
		m := make(map[int]int)
		mv := ValueOf(m)
		mv.SetMapIndex(ValueOf(1), ValueOf(2))
		x, ok := m[1]
		if x != 2 {
			t.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
		}
		if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
			t.Errorf("#1 MapIndex(1) = %d", n)
		}
	}
	{
		// convert interface key
		m := make(map[any]int)
		mv := ValueOf(m)
		mv.SetMapIndex(ValueOf(1), ValueOf(2))
		x, ok := m[1]
		if x != 2 {
			t.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
		}
		if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
			t.Errorf("#2 MapIndex(1) = %d", n)
		}
	}
	{
		// convert interface value
		m := make(map[int]any)
		mv := ValueOf(m)
		mv.SetMapIndex(ValueOf(1), ValueOf(2))
		x, ok := m[1]
		if x != 2 {
			t.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
		}
		if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
			t.Errorf("#3 MapIndex(1) = %d", n)
		}
	}
	{
		// convert both interface key and interface value
		m := make(map[any]any)
		mv := ValueOf(m)
		mv.SetMapIndex(ValueOf(1), ValueOf(2))
		x, ok := m[1]
		if x != 2 {
			t.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
		}
		if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
			t.Errorf("#4 MapIndex(1) = %d", n)
		}
	}
	{
		// convert both, with non-empty interfaces
		m := make(map[io.Reader]io.Writer)
		mv := ValueOf(m)
		b1 := new(bytes.Buffer)
		b2 := new(bytes.Buffer)
		mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
		x, ok := m[b1]
		if x != b2 {
			t.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
		}
		if p := mv.MapIndex(ValueOf(b1)).Elem().UnsafePointer(); p != unsafe.Pointer(b2) {
			t.Errorf("#5 MapIndex(b1) = %#x want %p", p, b2)
		}
	}
	{
		// convert channel direction
		m := make(map[<-chan int]chan int)
		mv := ValueOf(m)
		c1 := make(chan int)
		c2 := make(chan int)
		mv.SetMapIndex(ValueOf(c1), ValueOf(c2))
		x, ok := m[c1]
		if x != c2 {
			t.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x, c2, ok, m)
		}
		if p := mv.MapIndex(ValueOf(c1)).UnsafePointer(); p != ValueOf(c2).UnsafePointer() {
			t.Errorf("#6 MapIndex(c1) = %#x want %p", p, c2)
		}
	}
	{
		// convert identical underlying types
		type MyBuffer bytes.Buffer
		m := make(map[*MyBuffer]*bytes.Buffer)
		mv := ValueOf(m)
		b1 := new(MyBuffer)
		b2 := new(bytes.Buffer)
		mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
		x, ok := m[b1]
		if x != b2 {
			t.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
		}
		if p := mv.MapIndex(ValueOf(b1)).UnsafePointer(); p != unsafe.Pointer(b2) {
			t.Errorf("#7 MapIndex(b1) = %#x want %p", p, b2)
		}
	}

}

func TestImplicitSetConversion(t *testing.T) {
	// Assume TestImplicitMapConversion covered the basics.
	// Just make sure conversions are being applied at all.
	var r io.Reader
	b := new(bytes.Buffer)
	rv := ValueOf(&r).Elem()
	rv.Set(ValueOf(b))
	if r != b {
		t.Errorf("after Set: r=%T(%v)", r, r)
	}
}

func TestImplicitSendConversion(t *testing.T) {
	c := make(chan io.Reader, 10)
	b := new(bytes.Buffer)
	ValueOf(c).Send(ValueOf(b))
	if bb := <-c; bb != b {
		t.Errorf("Received %p != %p", bb, b)
	}
}

func TestImplicitCallConversion(t *testing.T) {
	// Arguments must be assignable to parameter types.
	fv := ValueOf(io.WriteString)
	b := new(strings.Builder)
	fv.Call([]Value{ValueOf(b), ValueOf("hello world")})
	if b.String() != "hello world" {
		t.Errorf("After call: string=%q want %q", b.String(), "hello world")
	}
}

func TestImplicitAppendConversion(t *testing.T) {
	// Arguments must be assignable to the slice's element type.
	s := []io.Reader{}
	sv := ValueOf(&s).Elem()
	b := new(bytes.Buffer)
	sv.Set(Append(sv, ValueOf(b)))
	if len(s) != 1 || s[0] != b {
		t.Errorf("after append: s=%v want [%p]", s, b)
	}
}

var implementsTests = []struct {
	x any
	t any
	b bool
}{
	{new(*bytes.Buffer), new(io.Reader), true},
	{new(bytes.Buffer), new(io.Reader), false},
	{new(*bytes.Buffer), new(io.ReaderAt), false},
	{new(*ast.Ident), new(ast.Expr), true},
	{new(*notAnExpr), new(ast.Expr), false},
	{new(*ast.Ident), new(notASTExpr), false},
	{new(notASTExpr), new(ast.Expr), false},
	{new(ast.Expr), new(notASTExpr), false},
	{new(*notAnExpr), new(notASTExpr), true},
}

type notAnExpr struct{}

func (notAnExpr) Pos() token.Pos { return token.NoPos }
func (notAnExpr) End() token.Pos { return token.NoPos }
func (notAnExpr) exprNode()      {}

type notASTExpr interface {
	Pos() token.Pos
	End() token.Pos
	exprNode()
}

func TestImplements(t *testing.T) {
	for _, tt := range implementsTests {
		xv := TypeOf(tt.x).Elem()
		xt := TypeOf(tt.t).Elem()
		if b := xv.Implements(xt); b != tt.b {
			t.Errorf("(%s).Implements(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
		}
	}
}

var assignableTests = []struct {
	x any
	t any
	b bool
}{
	{new(chan int), new(<-chan int), true},
	{new(<-chan int), new(chan int), false},
	{new(*int), new(IntPtr), true},
	{new(IntPtr), new(*int), true},
	{new(IntPtr), new(IntPtr1), false},
	{new(Ch), new(<-chan any), true},
	// test runs implementsTests too
}

type IntPtr *int
type IntPtr1 *int
type Ch <-chan any

func TestAssignableTo(t *testing.T) {
	for _, tt := range append(assignableTests, implementsTests...) {
		xv := TypeOf(tt.x).Elem()
		xt := TypeOf(tt.t).Elem()
		if b := xv.AssignableTo(xt); b != tt.b {
			t.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
		}
	}
}

Directory Contents

Dirs: 1 × Files: 34

Name Size Perms Modified Actions
internal DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
15.02 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
26.39 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
214.57 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
570 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.07 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.76 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.13 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.77 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.87 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.16 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.16 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.31 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.58 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.10 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.15 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
8.82 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.42 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.53 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.73 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
681 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
838 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
794 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.91 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1004 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.71 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
292 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
263 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.00 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.14 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
80.47 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.26 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
118.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.97 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.27 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`