github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/frame/unsafe_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package frame
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  /*
    13  
    14  func TestUnsafeAssign(t *testing.T) {
    15  	var src, dst int
    16  
    17  	src = 123
    18  	srcp, dstp := unsafe.Pointer(&src), unsafe.Pointer(&dst)
    19  	assign(type2rtype(typeOfInt), dstp, srcp)
    20  	if got, want := dst, src; got != want {
    21  		t.Errorf("got %v, want %v", got, want)
    22  	}
    23  }
    24  
    25  func TestUnsafeAssignBig(t *testing.T) {
    26  	var src, dst struct {
    27  		a, b, c int
    28  		d       [128]byte
    29  	}
    30  	fz := fuzz.New()
    31  	fz.Fuzz(&src.a)
    32  	fz.Fuzz(&src.b)
    33  	fz.Fuzz(&src.c)
    34  	fz.Fuzz(&src.d)
    35  	if got, want := src, dst; got == want {
    36  		t.Fatal("fuzzing broken")
    37  	}
    38  
    39  	assign(type2rtype(reflect.TypeOf(dst)), unsafe.Pointer(&dst), unsafe.Pointer(&src))
    40  	if got, want := src, dst; got != want {
    41  		t.Errorf("got %+v, want %+v", got, want)
    42  	}
    43  }
    44  */
    45  
    46  func TestPointers(t *testing.T) {
    47  	types := []struct {
    48  		val      interface{}
    49  		pointers bool
    50  	}{
    51  		{struct{ x, y int }{}, false},
    52  		{0, false},
    53  		{new(int), true},
    54  		{struct {
    55  			x int
    56  			y string
    57  		}{}, true},
    58  		{[]int{}, true},
    59  		{123, false},
    60  		{"okay", true},
    61  		{[123]int{}, false},
    62  		{[123]string{}, true},
    63  		{map[int]int{}, true},
    64  		{make(chan int), true},
    65  	}
    66  	for i, typ := range types {
    67  		if got, want := pointers(reflect.TypeOf(typ.val)), typ.pointers; got != want {
    68  			t.Errorf("test %d: %v: got %v, want %v", i, typ.val, got, want)
    69  		}
    70  	}
    71  }