github.com/parquet-go/parquet-go@v0.21.1-0.20240501160520-b3c3a0c3ed6f/column_buffer_test.go (about)

     1  package parquet
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestBroadcastValueInt32(t *testing.T) {
     8  	buf := make([]int32, 123)
     9  	broadcastValueInt32(buf, 0x0A)
    10  
    11  	for i, v := range buf {
    12  		if v != 0x0A0A0A0A {
    13  			t.Fatalf("wrong value at index %d: %v", i, v)
    14  		}
    15  	}
    16  }
    17  
    18  func TestBroadcastRangeInt32(t *testing.T) {
    19  	buf := make([]int32, 123)
    20  	broadcastRangeInt32(buf, 1)
    21  
    22  	for i, v := range buf {
    23  		if v != int32(1+i) {
    24  			t.Fatalf("wrong value at index %d: %v", i, v)
    25  		}
    26  	}
    27  }
    28  
    29  func BenchmarkBroadcastValueInt32(b *testing.B) {
    30  	buf := make([]int32, 1000)
    31  	for i := 0; i < b.N; i++ {
    32  		broadcastValueInt32(buf, -1)
    33  	}
    34  	b.SetBytes(4 * int64(len(buf)))
    35  }
    36  
    37  func BenchmarkBroadcastRangeInt32(b *testing.B) {
    38  	buf := make([]int32, 1000)
    39  	for i := 0; i < b.N; i++ {
    40  		broadcastRangeInt32(buf, 0)
    41  	}
    42  	b.SetBytes(4 * int64(len(buf)))
    43  }
    44  
    45  // https://github.com/segmentio/parquet-go/issues/501
    46  func TestIssue501(t *testing.T) {
    47  	col := newBooleanColumnBuffer(BooleanType, 0, 2055208)
    48  
    49  	// write all trues and then flush the buffer
    50  	_, err := col.WriteBooleans([]bool{true, true, true, true, true, true, true, true})
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	col.Reset()
    55  
    56  	// write a single false, we are trying to trip a certain line of code in WriteBooleans
    57  	_, err = col.WriteBooleans([]bool{false})
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	// now write 7 booleans at once, this will cause WriteBooleans to attempt its "alignment" logic
    62  	_, err = col.WriteBooleans([]bool{false, false, false, false, false, false, false})
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  
    67  	for i := 0; i < 8; i++ {
    68  		read := make([]Value, 1)
    69  		_, err = col.ReadValuesAt(read, int64(i))
    70  		if err != nil {
    71  			t.Fatal(err)
    72  		}
    73  		if read[0].Boolean() {
    74  			t.Fatalf("expected false at index %d", i)
    75  		}
    76  	}
    77  }