github.com/segmentio/parquet-go@v0.0.0-20230712180008-5d42db8f0d47/internal/unsafecast/unsafecast_go18_test.go (about)

     1  //go:build go1.18
     2  
     3  package unsafecast_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/segmentio/parquet-go/internal/unsafecast"
     9  )
    10  
    11  func TestUnsafeCastSlice(t *testing.T) {
    12  	a := make([]uint32, 4, 13)
    13  	a[0] = 1
    14  	a[1] = 0
    15  	a[2] = 2
    16  	a[3] = 0
    17  
    18  	b := unsafecast.Slice[int64](a)
    19  	if len(b) != 2 { // (4 * sizeof(uint32)) / sizeof(int64)
    20  		t.Fatalf("length mismatch: want=2 got=%d", len(b))
    21  	}
    22  	if cap(b) != 6 { // (13 * sizeof(uint32)) / sizeof(int64)
    23  		t.Fatalf("capacity mismatch: want=7 got=%d", cap(b))
    24  	}
    25  	if b[0] != 1 {
    26  		t.Errorf("wrong value at index 0: want=1 got=%d", b[0])
    27  	}
    28  	if b[1] != 2 {
    29  		t.Errorf("wrong value at index 1: want=2 got=%d", b[1])
    30  	}
    31  
    32  	c := unsafecast.Slice[uint32](b)
    33  	if len(c) != 4 {
    34  		t.Fatalf("length mismatch: want=2 got=%d", len(b))
    35  	}
    36  	if cap(c) != 12 {
    37  		t.Fatalf("capacity mismatch: want=7 got=%d", cap(b))
    38  	}
    39  	for i := range c {
    40  		if c[i] != a[i] {
    41  			t.Errorf("wrong value at index %d: want=%d got=%d", i, a[i], c[i])
    42  		}
    43  	}
    44  }