github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/tests/arrays_test.go (about) 1 package tests 2 3 import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 ) 8 9 func TestArrayPointer(t *testing.T) { 10 t.Run("nil", func(t *testing.T) { 11 var p1 *[1]int 12 if p1 != nil { 13 t.Errorf("Zero-value array pointer is not equal to nil: %v", p1) 14 } 15 16 var p2 *[1]int = nil 17 if p2 != nil { 18 t.Errorf("Nil array pointer is not equal to nil: %v", p2) 19 } 20 21 p3 := func() *[1]int { return nil }() 22 if p3 != nil { 23 t.Errorf("Nil array pointer returned from function is not equal to nil: %v", p3) 24 } 25 26 if p1 != p3 || p1 != p2 || p2 != p3 { 27 t.Errorf("Nil pointers are not equal to each other: %v %v %v", p1, p2, p3) 28 } 29 30 if v := reflect.ValueOf(p1); !v.IsNil() { 31 t.Errorf("reflect.Value.IsNil() is false for a nil pointer: %v %v", p1, v) 32 } 33 34 type arr *[1]int 35 var p4 arr = nil 36 37 if v := reflect.ValueOf(p4); !v.IsNil() { 38 t.Errorf("reflect.Value.IsNil() is false for a nil pointer: %v %v", p4, v) 39 } 40 }) 41 42 t.Run("pointer-dereference", func(t *testing.T) { 43 a1 := [1]int{42} 44 aPtr := &a1 45 a2 := *aPtr 46 if !reflect.DeepEqual(a1, a2) { 47 t.Errorf("Array after pointer dereferencing is not equal to the original: %v != %v", a1, a2) 48 t.Logf("Pointer: %v", aPtr) 49 } 50 }) 51 52 t.Run("interface-and-back", func(t *testing.T) { 53 type arr *[1]int 54 tests := []struct { 55 name string 56 a arr 57 }{{ 58 name: "not nil", 59 a: &[1]int{42}, 60 }, { 61 name: "nil", 62 a: nil, 63 }} 64 for _, test := range tests { 65 a1 := test.a 66 i := interface{}(a1) 67 a2 := i.(arr) 68 69 if a1 != a2 { 70 t.Errorf("Array pointer is not equal to itself after interface conversion: %v != %v", a1, a2) 71 println(a1, a2) 72 } 73 } 74 }) 75 76 t.Run("reflect.IsNil", func(t *testing.T) { 77 }) 78 } 79 80 func TestReflectArraySize(t *testing.T) { 81 want := unsafe.Sizeof(int(0)) * 8 82 if got := reflect.TypeOf([8]int{}).Size(); got != want { 83 t.Errorf("array type size gave %v, want %v", got, want) 84 } 85 }