github.com/segmentio/parquet-go@v0.0.0-20230712180008-5d42db8f0d47/encoding/delta/byte_array_test.go (about)

     1  package delta
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func TestLinearSearchPrefixLength(t *testing.T) {
    10  	testSearchPrefixLength(t, linearSearchPrefixLength)
    11  }
    12  
    13  func TestBinarySearchPrefixLength(t *testing.T) {
    14  	testSearchPrefixLength(t, func(base, data []byte) int {
    15  		return binarySearchPrefixLength(base, data)
    16  	})
    17  }
    18  
    19  func testSearchPrefixLength(t *testing.T, prefixLength func(base, data []byte) int) {
    20  	tests := []struct {
    21  		base string
    22  		data string
    23  		len  int
    24  	}{
    25  		{
    26  			base: "",
    27  			data: "",
    28  			len:  0,
    29  		},
    30  
    31  		{
    32  			base: "A",
    33  			data: "B",
    34  			len:  0,
    35  		},
    36  
    37  		{
    38  			base: "",
    39  			data: "Hello World!",
    40  			len:  0,
    41  		},
    42  
    43  		{
    44  			base: "H",
    45  			data: "Hello World!",
    46  			len:  1,
    47  		},
    48  
    49  		{
    50  			base: "He",
    51  			data: "Hello World!",
    52  			len:  2,
    53  		},
    54  
    55  		{
    56  			base: "Hel",
    57  			data: "Hello World!",
    58  			len:  3,
    59  		},
    60  
    61  		{
    62  			base: "Hell",
    63  			data: "Hello World!",
    64  			len:  4,
    65  		},
    66  
    67  		{
    68  			base: "Hello",
    69  			data: "Hello World!",
    70  			len:  5,
    71  		},
    72  
    73  		{
    74  			base: "Hello ",
    75  			data: "Hello World!",
    76  			len:  6,
    77  		},
    78  
    79  		{
    80  			base: "Hello W",
    81  			data: "Hello World!",
    82  			len:  7,
    83  		},
    84  
    85  		{
    86  			base: "Hello Wo",
    87  			data: "Hello World!",
    88  			len:  8,
    89  		},
    90  
    91  		{
    92  			base: "Hello Wor",
    93  			data: "Hello World!",
    94  			len:  9,
    95  		},
    96  
    97  		{
    98  			base: "Hello Worl",
    99  			data: "Hello World!",
   100  			len:  10,
   101  		},
   102  
   103  		{
   104  			base: "Hello World",
   105  			data: "Hello World!",
   106  			len:  11,
   107  		},
   108  
   109  		{
   110  			base: "Hello World!",
   111  			data: "Hello World!",
   112  			len:  12,
   113  		},
   114  
   115  		{
   116  			base: "Hell.......",
   117  			data: "Hello World!",
   118  			len:  4,
   119  		},
   120  	}
   121  
   122  	for _, test := range tests {
   123  		t.Run("", func(t *testing.T) {
   124  			n := prefixLength([]byte(test.base), []byte(test.data))
   125  			if n != test.len {
   126  				t.Errorf("prefixLength(%q,%q): want=%d got=%d", test.base, test.data, test.len, n)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func BenchmarkLinearSearchPrefixLength(b *testing.B) {
   133  	benchmarkSearchPrefixLength(b, linearSearchPrefixLength)
   134  }
   135  
   136  func BenchmarkBinarySearchPrefixLength(b *testing.B) {
   137  	benchmarkSearchPrefixLength(b, func(base, data []byte) int {
   138  		return binarySearchPrefixLength(base, data)
   139  	})
   140  }
   141  
   142  func benchmarkSearchPrefixLength(b *testing.B, prefixLength func(base, data []byte) int) {
   143  	buffer := bytes.Repeat([]byte("0123456789"), 100)
   144  
   145  	for _, size := range []int{10, 100, 1000} {
   146  		b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
   147  			base := buffer[:size]
   148  			data := buffer[:size/2]
   149  
   150  			for i := 0; i < b.N; i++ {
   151  				_ = prefixLength(base, data)
   152  			}
   153  		})
   154  	}
   155  }