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

     1  package parquet
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/segmentio/parquet-go/internal/quick"
     9  )
    10  
    11  func TestMaxInt32(t *testing.T) {
    12  	err := quick.Check(func(values []int32) bool {
    13  		max := int32(0)
    14  		if len(values) > 0 {
    15  			max = values[0]
    16  			for _, v := range values[1:] {
    17  				if v > max {
    18  					max = v
    19  				}
    20  			}
    21  		}
    22  		return max == maxInt32(values)
    23  	})
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  }
    28  
    29  func TestMaxInt64(t *testing.T) {
    30  	err := quick.Check(func(values []int64) bool {
    31  		max := int64(0)
    32  		if len(values) > 0 {
    33  			max = values[0]
    34  			for _, v := range values[1:] {
    35  				if v > max {
    36  					max = v
    37  				}
    38  			}
    39  		}
    40  		return max == maxInt64(values)
    41  	})
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  }
    46  
    47  func TestMaxUint32(t *testing.T) {
    48  	err := quick.Check(func(values []uint32) bool {
    49  		max := uint32(0)
    50  		if len(values) > 0 {
    51  			max = values[0]
    52  			for _, v := range values[1:] {
    53  				if v > max {
    54  					max = v
    55  				}
    56  			}
    57  		}
    58  		return max == maxUint32(values)
    59  	})
    60  	if err != nil {
    61  		t.Error(err)
    62  	}
    63  }
    64  
    65  func TestMaxUint64(t *testing.T) {
    66  	err := quick.Check(func(values []uint64) bool {
    67  		max := uint64(0)
    68  		if len(values) > 0 {
    69  			max = values[0]
    70  			for _, v := range values[1:] {
    71  				if v > max {
    72  					max = v
    73  				}
    74  			}
    75  		}
    76  		return max == maxUint64(values)
    77  	})
    78  	if err != nil {
    79  		t.Error(err)
    80  	}
    81  }
    82  
    83  func TestMaxFloat32(t *testing.T) {
    84  	err := quick.Check(func(values []float32) bool {
    85  		max := float32(0)
    86  		if len(values) > 0 {
    87  			max = values[0]
    88  			for _, v := range values[1:] {
    89  				if v > max {
    90  					max = v
    91  				}
    92  			}
    93  		}
    94  		return max == maxFloat32(values)
    95  	})
    96  	if err != nil {
    97  		t.Error(err)
    98  	}
    99  }
   100  
   101  func TestMaxFloat64(t *testing.T) {
   102  	err := quick.Check(func(values []float64) bool {
   103  		max := float64(0)
   104  		if len(values) > 0 {
   105  			max = values[0]
   106  			for _, v := range values[1:] {
   107  				if v > max {
   108  					max = v
   109  				}
   110  			}
   111  		}
   112  		return max == maxFloat64(values)
   113  	})
   114  	if err != nil {
   115  		t.Error(err)
   116  	}
   117  }
   118  
   119  func TestMaxBE128(t *testing.T) {
   120  	err := quick.Check(func(values [][16]byte) bool {
   121  		max := [16]byte{}
   122  		if len(values) > 0 {
   123  			max = values[0]
   124  			for _, v := range values[1:] {
   125  				if bytes.Compare(v[:], max[:]) > 0 {
   126  					max = v
   127  				}
   128  			}
   129  		}
   130  		ret := maxBE128(values)
   131  		return (len(values) == 0 && ret == nil) || bytes.Equal(max[:], ret)
   132  	})
   133  	if err != nil {
   134  		t.Error(err)
   135  	}
   136  }
   137  
   138  func TestMaxFixedLenByteArray(t *testing.T) {
   139  	err := quick.Check(func(values []byte) bool {
   140  		max := [1]byte{}
   141  		if len(values) > 0 {
   142  			max[0] = values[0]
   143  			for _, v := range values[1:] {
   144  				if v > max[0] {
   145  					max[0] = v
   146  				}
   147  			}
   148  		}
   149  		ret := maxFixedLenByteArray(values, 1)
   150  		return (len(values) == 0 && ret == nil) || bytes.Equal(max[:], ret)
   151  	})
   152  	if err != nil {
   153  		t.Error(err)
   154  	}
   155  }
   156  
   157  func BenchmarkMaxInt32(b *testing.B) {
   158  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   159  		values := make([]int32, bufferSize/4)
   160  		prng := rand.New(rand.NewSource(1))
   161  		for i := range values {
   162  			values[i] = prng.Int31()
   163  		}
   164  		for i := 0; i < b.N; i++ {
   165  			maxInt32(values)
   166  		}
   167  	})
   168  }
   169  
   170  func BenchmarkMaxInt64(b *testing.B) {
   171  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   172  		values := make([]int64, bufferSize/8)
   173  		prng := rand.New(rand.NewSource(1))
   174  		for i := range values {
   175  			values[i] = prng.Int63()
   176  		}
   177  		for i := 0; i < b.N; i++ {
   178  			maxInt64(values)
   179  		}
   180  	})
   181  }
   182  
   183  func BenchmarkMaxUint32(b *testing.B) {
   184  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   185  		values := make([]uint32, bufferSize/4)
   186  		prng := rand.New(rand.NewSource(1))
   187  		for i := range values {
   188  			values[i] = prng.Uint32()
   189  		}
   190  		for i := 0; i < b.N; i++ {
   191  			maxUint32(values)
   192  		}
   193  	})
   194  }
   195  
   196  func BenchmarkMaxUint64(b *testing.B) {
   197  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   198  		values := make([]uint64, bufferSize/8)
   199  		prng := rand.New(rand.NewSource(1))
   200  		for i := range values {
   201  			values[i] = prng.Uint64()
   202  		}
   203  		for i := 0; i < b.N; i++ {
   204  			maxUint64(values)
   205  		}
   206  	})
   207  }
   208  
   209  func BenchmarkMaxFloat32(b *testing.B) {
   210  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   211  		values := make([]float32, bufferSize/4)
   212  		prng := rand.New(rand.NewSource(1))
   213  		for i := range values {
   214  			values[i] = prng.Float32()
   215  		}
   216  		for i := 0; i < b.N; i++ {
   217  			maxFloat32(values)
   218  		}
   219  	})
   220  }
   221  
   222  func BenchmarkMaxFloat64(b *testing.B) {
   223  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   224  		values := make([]float64, bufferSize/8)
   225  		prng := rand.New(rand.NewSource(1))
   226  		for i := range values {
   227  			values[i] = prng.Float64()
   228  		}
   229  		for i := 0; i < b.N; i++ {
   230  			maxFloat64(values)
   231  		}
   232  	})
   233  }
   234  
   235  func BenchmarkMaxBE128(b *testing.B) {
   236  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   237  		values := make([][16]byte, bufferSize)
   238  		prng := rand.New(rand.NewSource(1))
   239  		for i := range values {
   240  			prng.Read(values[i][:])
   241  		}
   242  		for i := 0; i < b.N; i++ {
   243  			maxBE128(values)
   244  		}
   245  	})
   246  }
   247  
   248  func BenchmarkMaxFixedLenByteArray(b *testing.B) {
   249  	forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
   250  		values := make([]byte, bufferSize)
   251  		prng := rand.New(rand.NewSource(1))
   252  		prng.Read(values)
   253  		for i := 0; i < b.N; i++ {
   254  			maxFixedLenByteArray(values, 32)
   255  		}
   256  	})
   257  }