github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/sys/xxhash3/correctness_test.go (about)

     1  package xxhash3
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/songzhibin97/go-baseutils/sys/cpu"
     8  	"github.com/songzhibin97/go-baseutils/sys/xxhash3/internal/xxh3_raw"
     9  )
    10  
    11  var dat []byte
    12  
    13  const capacity = 1<<25 + 100000
    14  
    15  func init() {
    16  	dat = make([]byte, capacity)
    17  	for i := 0; i < capacity; i++ {
    18  		dat[i] = byte(rand.Int31())
    19  	}
    20  }
    21  
    22  func TestLen0_16(t *testing.T) {
    23  	for i := 0; i <= 16; i++ {
    24  		input := dat[:i]
    25  		res1 := xxh3_raw.Hash(input)
    26  		res2 := Hash(input)
    27  
    28  		if res1 != res2 {
    29  			t.Fatal("Wrong answer")
    30  		}
    31  	}
    32  }
    33  
    34  func TestLen17_128(t *testing.T) {
    35  	for i := 17; i <= 128; i++ {
    36  		input := dat[:i]
    37  		res1 := xxh3_raw.Hash(input)
    38  		res2 := Hash(input)
    39  
    40  		if res1 != res2 {
    41  			t.Fatal("Wrong answer")
    42  		}
    43  	}
    44  }
    45  
    46  func TestLen129_240(t *testing.T) {
    47  
    48  	for i := 129; i <= 240; i++ {
    49  		input := dat[:i]
    50  		res1 := xxh3_raw.Hash(input)
    51  		res2 := Hash(input)
    52  
    53  		if res1 != res2 {
    54  			t.Fatal("Wrong answer")
    55  		}
    56  	}
    57  }
    58  
    59  func TestLen240_1024(t *testing.T) {
    60  	avx2, sse2 = false, false
    61  
    62  	for i := 240; i <= 1024; 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 TestLen1025_1048576_scalar(t *testing.T) {
    74  	avx2, sse2 = false, false
    75  	for i := 1025; i < 1048576; i = i << 1 {
    76  		input := dat[:i]
    77  		res1 := xxh3_raw.Hash(input)
    78  		res2 := Hash(input)
    79  
    80  		if res1 != res2 {
    81  			t.Fatal("Wrong answer", i)
    82  		}
    83  	}
    84  }
    85  
    86  func TestLen1024_1048576_AVX2(t *testing.T) {
    87  	avx2, sse2 = cpu.X86.HasAVX2, false
    88  	for i := 1024; i < 1048576; i = i << 1 {
    89  		input := dat[:i]
    90  		res1 := xxh3_raw.Hash(input)
    91  		res2 := Hash(input)
    92  
    93  		if res1 != res2 {
    94  			t.Fatal("Wrong answer", i)
    95  		}
    96  	}
    97  }
    98  
    99  func TestLen1024_1048576_SSE2(t *testing.T) {
   100  	avx2, sse2 = false, cpu.X86.HasSSE2
   101  	for i := 1024; i < 1048576; i = i << 1 {
   102  		input := dat[:i]
   103  		res1 := xxh3_raw.Hash(input)
   104  		res2 := Hash(input)
   105  
   106  		if res1 != res2 {
   107  			t.Fatal("Wrong answer")
   108  		}
   109  	}
   110  }
   111  
   112  func TestLen128_0_16(t *testing.T) {
   113  	for i := 0; i <= 16; i++ {
   114  		input := dat[:i]
   115  		res1 := xxh3_raw.Hash128(input)
   116  		res2 := Hash128(input)
   117  
   118  		if res1 != res2 {
   119  			t.Fatal("Wrong answer")
   120  		}
   121  	}
   122  }
   123  
   124  func TestLen128_17_128(t *testing.T) {
   125  	for i := 17; i <= 128; i++ {
   126  		input := dat[:i]
   127  		res1 := xxh3_raw.Hash128(input)
   128  		res2 := Hash128(input)
   129  
   130  		if res1 != res2 {
   131  			t.Fatal("Wrong answer")
   132  		}
   133  	}
   134  }
   135  
   136  func TestLen128_129_240(t *testing.T) {
   137  
   138  	for i := 129; i <= 240; i++ {
   139  		input := dat[:i]
   140  		res1 := xxh3_raw.Hash128(input)
   141  		res2 := Hash128(input)
   142  
   143  		if res1 != res2 {
   144  			t.Fatal("Wrong answer")
   145  		}
   146  	}
   147  }
   148  
   149  func TestLen128_240_1024(t *testing.T) {
   150  	avx2, sse2 = false, false
   151  
   152  	for i := 240; i <= 1024; 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_1025_1048576_scalar(t *testing.T) {
   164  	avx2, sse2 = false, false
   165  	for i := 1025; i < 1048576; i = i << 1 {
   166  		input := dat[:i]
   167  		res1 := xxh3_raw.Hash128(input)
   168  		res2 := Hash128(input)
   169  
   170  		if res1 != res2 {
   171  			t.Fatal("Wrong answer", i)
   172  		}
   173  	}
   174  }
   175  
   176  func TestLen128_1024_1048576_AVX2(t *testing.T) {
   177  	avx2, sse2 = cpu.X86.HasAVX2, false
   178  
   179  	for i := 1024; 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_SSE2(t *testing.T) {
   191  	avx2, sse2 = false, cpu.X86.HasSSE2
   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")
   200  		}
   201  	}
   202  }