github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/util/xxhash3/correctness_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package xxhash3
    16  
    17  import (
    18  	"math/rand"
    19  	"testing"
    20  
    21  	"github.com/bytedance/gopkg/util/xxhash3/internal/xxh3_raw"
    22  	"golang.org/x/sys/cpu"
    23  )
    24  
    25  var dat []byte
    26  
    27  const capacity = 1<<25 + 100000
    28  
    29  func init() {
    30  	dat = make([]byte, capacity)
    31  	for i := 0; i < capacity; i++ {
    32  		dat[i] = byte(rand.Int31())
    33  	}
    34  }
    35  
    36  func TestLen0_16(t *testing.T) {
    37  	for i := 0; i <= 16; i++ {
    38  		input := dat[:i]
    39  		res1 := xxh3_raw.Hash(input)
    40  		res2 := Hash(input)
    41  
    42  		if res1 != res2 {
    43  			t.Fatal("Wrong answer")
    44  		}
    45  	}
    46  }
    47  
    48  func TestLen17_128(t *testing.T) {
    49  	for i := 17; i <= 128; i++ {
    50  		input := dat[:i]
    51  		res1 := xxh3_raw.Hash(input)
    52  		res2 := Hash(input)
    53  
    54  		if res1 != res2 {
    55  			t.Fatal("Wrong answer")
    56  		}
    57  	}
    58  }
    59  
    60  func TestLen129_240(t *testing.T) {
    61  
    62  	for i := 129; i <= 240; i++ {
    63  		input := dat[:i]
    64  		res1 := xxh3_raw.Hash(input)
    65  		res2 := Hash(input)
    66  
    67  		if res1 != res2 {
    68  			t.Fatal("Wrong answer")
    69  		}
    70  	}
    71  }
    72  
    73  func TestLen240_1024(t *testing.T) {
    74  	avx2, sse2 = false, false
    75  
    76  	for i := 240; i <= 1024; i++ {
    77  		input := dat[:i]
    78  		res1 := xxh3_raw.Hash(input)
    79  		res2 := Hash(input)
    80  
    81  		if res1 != res2 {
    82  			t.Fatal("Wrong answer")
    83  		}
    84  	}
    85  }
    86  
    87  func TestLen1025_1048576_scalar(t *testing.T) {
    88  	avx2, sse2 = false, false
    89  	for i := 1025; i < 1048576; i = i << 1 {
    90  		input := dat[:i]
    91  		res1 := xxh3_raw.Hash(input)
    92  		res2 := Hash(input)
    93  
    94  		if res1 != res2 {
    95  			t.Fatal("Wrong answer", i)
    96  		}
    97  	}
    98  }
    99  
   100  func TestLen1024_1048576_AVX2(t *testing.T) {
   101  	avx2, sse2 = cpu.X86.HasAVX2, false
   102  	for i := 1024; i < 1048576; i = i << 1 {
   103  		input := dat[:i]
   104  		res1 := xxh3_raw.Hash(input)
   105  		res2 := Hash(input)
   106  
   107  		if res1 != res2 {
   108  			t.Fatal("Wrong answer", i)
   109  		}
   110  	}
   111  }
   112  
   113  func TestLen1024_1048576_SSE2(t *testing.T) {
   114  	avx2, sse2 = false, cpu.X86.HasSSE2
   115  	for i := 1024; i < 1048576; i = i << 1 {
   116  		input := dat[:i]
   117  		res1 := xxh3_raw.Hash(input)
   118  		res2 := Hash(input)
   119  
   120  		if res1 != res2 {
   121  			t.Fatal("Wrong answer")
   122  		}
   123  	}
   124  }
   125  
   126  func TestLen128_0_16(t *testing.T) {
   127  	for i := 0; i <= 16; i++ {
   128  		input := dat[:i]
   129  		res1 := xxh3_raw.Hash128(input)
   130  		res2 := Hash128(input)
   131  
   132  		if res1 != res2 {
   133  			t.Fatal("Wrong answer")
   134  		}
   135  	}
   136  }
   137  
   138  func TestLen128_17_128(t *testing.T) {
   139  	for i := 17; i <= 128; i++ {
   140  		input := dat[:i]
   141  		res1 := xxh3_raw.Hash128(input)
   142  		res2 := Hash128(input)
   143  
   144  		if res1 != res2 {
   145  			t.Fatal("Wrong answer")
   146  		}
   147  	}
   148  }
   149  
   150  func TestLen128_129_240(t *testing.T) {
   151  
   152  	for i := 129; i <= 240; i++ {
   153  		input := dat[:i]
   154  		res1 := xxh3_raw.Hash128(input)
   155  		res2 := Hash128(input)
   156  
   157  		if res1 != res2 {
   158  			t.Fatal("Wrong answer")
   159  		}
   160  	}
   161  }
   162  
   163  func TestLen128_240_1024(t *testing.T) {
   164  	avx2, sse2 = false, false
   165  
   166  	for i := 240; i <= 1024; i++ {
   167  		input := dat[:i]
   168  		res1 := xxh3_raw.Hash128(input)
   169  		res2 := Hash128(input)
   170  
   171  		if res1 != res2 {
   172  			t.Fatal("Wrong answer")
   173  		}
   174  	}
   175  }
   176  
   177  func TestLen128_1025_1048576_scalar(t *testing.T) {
   178  	avx2, sse2 = false, false
   179  	for i := 1025; i < 1048576; i = i << 1 {
   180  		input := dat[:i]
   181  		res1 := xxh3_raw.Hash128(input)
   182  		res2 := Hash128(input)
   183  
   184  		if res1 != res2 {
   185  			t.Fatal("Wrong answer", i)
   186  		}
   187  	}
   188  }
   189  
   190  func TestLen128_1024_1048576_AVX2(t *testing.T) {
   191  	avx2, sse2 = cpu.X86.HasAVX2, false
   192  
   193  	for i := 1024; i < 1048576; i = i << 1 {
   194  		input := dat[:i]
   195  		res1 := xxh3_raw.Hash128(input)
   196  		res2 := Hash128(input)
   197  
   198  		if res1 != res2 {
   199  			t.Fatal("Wrong answer", i)
   200  		}
   201  	}
   202  }
   203  
   204  func TestLen128_1024_1048576_SSE2(t *testing.T) {
   205  	avx2, sse2 = false, cpu.X86.HasSSE2
   206  
   207  	for i := 1024; i < 1048576; i = i << 1 {
   208  		input := dat[:i]
   209  		res1 := xxh3_raw.Hash128(input)
   210  		res2 := Hash128(input)
   211  
   212  		if res1 != res2 {
   213  			t.Fatal("Wrong answer")
   214  		}
   215  	}
   216  }