github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/tests/copy_test.go (about) 1 package tests 2 3 import ( 4 "testing" 5 ) 6 7 type S struct { 8 x int 9 } 10 11 func (a S) test(b S) { 12 a.x = 0 13 b.x = 0 14 } 15 16 type A [1]int 17 18 func (a A) test(b A) { 19 a[0] = 0 20 b[0] = 0 21 } 22 23 func TestCopyOnCall(t *testing.T) { 24 { 25 a := S{1} 26 b := S{2} 27 28 a.test(b) 29 func() { 30 defer a.test(b) 31 }() 32 33 if a.x != 1 { 34 t.Error("a.x != 1") 35 } 36 if b.x != 2 { 37 t.Error("b.x != 2") 38 } 39 } 40 { 41 a := A{1} 42 b := A{2} 43 44 a.test(b) 45 func() { 46 defer a.test(b) 47 }() 48 49 if a[0] != 1 { 50 t.Error("a[0] != 1") 51 } 52 if b[0] != 2 { 53 t.Error("b[0] != 2") 54 } 55 } 56 } 57 58 func TestSwap(t *testing.T) { 59 { 60 a := S{1} 61 b := S{2} 62 a, b = b, a 63 if a.x != 2 || b.x != 1 { 64 t.Fail() 65 } 66 } 67 { 68 a := A{1} 69 b := A{2} 70 a, b = b, a 71 if a[0] != 2 || b[0] != 1 { 72 t.Fail() 73 } 74 } 75 } 76 77 func TestComposite(t *testing.T) { 78 { 79 a := S{1} 80 s := []S{a} 81 s[0].x = 0 82 if a.x != 1 { 83 t.Fail() 84 } 85 } 86 { 87 a := A{1} 88 s := []A{a} 89 s[0][0] = 0 90 if a[0] != 1 { 91 t.Fail() 92 } 93 } 94 } 95 96 func TestAppend(t *testing.T) { 97 { 98 s := append(make([]S, 3), S{}) // cap(s) == 6 99 s = s[:6] 100 if s[5].x != 0 { 101 t.Fail() 102 } 103 } 104 105 { 106 a := S{1} 107 b := []S{{2}} 108 s := append([]S{}, b...) 109 s[0].x = 0 110 if a.x != 1 || b[0].x != 2 { 111 t.Fail() 112 } 113 } 114 } 115 116 type I interface { 117 M() int 118 } 119 120 type T S 121 122 func (t T) M() int { 123 return t.x 124 } 125 126 func TestExplicitConversion(t *testing.T) { 127 var coolGuy = S{x: 42} 128 var i I 129 i = T(coolGuy) 130 if i.M() != 42 { 131 t.Fail() 132 } 133 }