v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/hash/wyhash/wyhash_test.go (about)

     1  package wyhash
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  	"unsafe"
     7  
     8  	"v8.run/go/exp/fastrand/alg/splitmix64"
     9  )
    10  
    11  func Test_wyhash(t *testing.T) {
    12  	type args struct {
    13  		key    unsafe.Pointer
    14  		len    uintptr
    15  		seed   uint64
    16  		secret *[4]uint64
    17  	}
    18  
    19  	var a = unsafe.Pointer(&[]byte("a")[0])
    20  	var abc = unsafe.Pointer(&[]byte("abc")[0])
    21  	var message_digest = unsafe.Pointer(&[]byte("message digest")[0])
    22  	var abcdefghijklmnopqrstuvwxyz = unsafe.Pointer(&[]byte("abcdefghijklmnopqrstuvwxyz")[0])
    23  	var ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 = unsafe.Pointer(&[]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")[0])
    24  	tests := []struct {
    25  		name string
    26  		args args
    27  		want uint64
    28  	}{
    29  		{
    30  			"len = 0",
    31  			args{
    32  				nil,
    33  				0,
    34  				0,
    35  				&_wyp,
    36  			},
    37  			uint64(0x42bc986dc5eec4d3),
    38  		},
    39  		{
    40  			"len = 1, msg=\"a\"",
    41  			args{
    42  				a,
    43  				1,
    44  				1,
    45  				&_wyp,
    46  			},
    47  			uint64(0x84508dc903c31551),
    48  		},
    49  		{
    50  			"len = 3, msg=\"abc\"",
    51  			args{
    52  				abc,
    53  				3,
    54  				2,
    55  				&_wyp,
    56  			},
    57  			uint64(0x0bc54887cfc9ecb1),
    58  		},
    59  		{
    60  			"len = 14, msg=\"message_digest\"",
    61  			args{
    62  				message_digest,
    63  				14,
    64  				3,
    65  				&_wyp,
    66  			},
    67  			uint64(0x6e2ff3298208a67c),
    68  		},
    69  		{
    70  			"len = 26, msg=\"abcdefghijklmnopqrstuvwxyz\"",
    71  			args{
    72  				abcdefghijklmnopqrstuvwxyz,
    73  				26,
    74  				4,
    75  				&_wyp,
    76  			},
    77  			uint64(0x9a64e42e897195b9),
    78  		},
    79  		{
    80  			"len = 62, msg=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"",
    81  			args{
    82  				ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,
    83  				62,
    84  				5,
    85  				&_wyp,
    86  			},
    87  			uint64(0x9199383239c32554),
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			if got := wyhash(tt.args.key, tt.args.len, tt.args.seed, tt.args.secret); got != tt.want {
    93  				t.Errorf("wyhash() = %v, want %v", got, tt.want)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func BenchmarkWyHash64(b *testing.B) {
   100  	var ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__ = unsafe.Pointer(&[]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__")[0])
   101  	b.SetBytes(64)
   102  	b.RunParallel(
   103  		func(p *testing.PB) {
   104  			for p.Next() {
   105  				_ = wyhash(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__, 64, 42, &_wyp)
   106  			}
   107  		},
   108  	)
   109  }
   110  
   111  const seed = 42
   112  
   113  func BenchmarkWyHash128(b *testing.B) {
   114  	data := make([]byte, 128)
   115  	rand.Read(data)
   116  	b.SetBytes(int64(len(data)))
   117  	b.RunParallel(func(p *testing.PB) {
   118  		for p.Next() {
   119  			Hash(data, seed)
   120  		}
   121  	})
   122  }
   123  
   124  func BenchmarkWyHash256(b *testing.B) {
   125  	data := make([]byte, 256)
   126  	rand.Read(data)
   127  	b.SetBytes(int64(len(data)))
   128  	b.RunParallel(func(p *testing.PB) {
   129  		for p.Next() {
   130  			Hash(data, seed)
   131  		}
   132  	})
   133  }
   134  
   135  func BenchmarkWyHash512(b *testing.B) {
   136  	data := make([]byte, 512)
   137  	rand.Read(data)
   138  	b.SetBytes(int64(len(data)))
   139  	b.RunParallel(func(p *testing.PB) {
   140  		for p.Next() {
   141  			Hash(data, seed)
   142  		}
   143  	})
   144  }
   145  
   146  func BenchmarkWyHash1024(b *testing.B) {
   147  	data := make([]byte, 1024)
   148  	rand.Read(data)
   149  	b.SetBytes(int64(len(data)))
   150  	b.RunParallel(func(p *testing.PB) {
   151  		for p.Next() {
   152  			Hash(data, seed)
   153  		}
   154  	})
   155  }
   156  
   157  func BenchmarkWyHash2048(b *testing.B) {
   158  	data := make([]byte, 2048)
   159  	rand.Read(data)
   160  	b.SetBytes(int64(len(data)))
   161  	b.RunParallel(func(p *testing.PB) {
   162  		for p.Next() {
   163  			Hash(data, seed)
   164  		}
   165  	})
   166  }
   167  
   168  func BenchmarkWyHash4096(b *testing.B) {
   169  	data := make([]byte, 4096)
   170  	rand.Read(data)
   171  	b.SetBytes(int64(len(data)))
   172  	b.RunParallel(func(p *testing.PB) {
   173  		for p.Next() {
   174  			Hash(data, seed)
   175  		}
   176  	})
   177  }
   178  
   179  func BenchmarkWyHash8192(b *testing.B) {
   180  	data := make([]byte, 8192)
   181  	rand.Read(data)
   182  	b.SetBytes(int64(len(data)))
   183  	b.RunParallel(func(p *testing.PB) {
   184  		for p.Next() {
   185  			Hash(data, seed)
   186  		}
   187  	})
   188  }
   189  
   190  func BenchmarkWyHash16384(b *testing.B) {
   191  	data := make([]byte, 16384)
   192  	rand.Read(data)
   193  	b.SetBytes(int64(len(data)))
   194  	b.RunParallel(func(p *testing.PB) {
   195  		for p.Next() {
   196  			Hash(data, seed)
   197  		}
   198  	})
   199  }
   200  
   201  func BenchmarkWyHash32768(b *testing.B) {
   202  	data := make([]byte, 32768)
   203  	rand.Read(data)
   204  	b.SetBytes(int64(len(data)))
   205  	b.RunParallel(func(p *testing.PB) {
   206  		for p.Next() {
   207  			Hash(data, seed)
   208  		}
   209  	})
   210  }
   211  
   212  func BenchmarkWyHash65536(b *testing.B) {
   213  	data := make([]byte, 65536)
   214  	rand.Read(data)
   215  	b.SetBytes(int64(len(data)))
   216  	b.RunParallel(func(p *testing.PB) {
   217  		for p.Next() {
   218  			Hash(data, seed)
   219  		}
   220  	})
   221  }
   222  
   223  func BenchmarkWyHash131072(b *testing.B) {
   224  	data := make([]byte, 131072)
   225  	rand.Read(data)
   226  	b.SetBytes(int64(len(data)))
   227  	b.RunParallel(func(p *testing.PB) {
   228  		for p.Next() {
   229  			Hash(data, seed)
   230  		}
   231  	})
   232  }
   233  
   234  func BenchmarkWYRAND(b *testing.B) {
   235  	b.RunParallel(func(p *testing.PB) {
   236  		var seed = splitmix64.Next()
   237  		for p.Next() {
   238  			_ = WYRAND(&seed)
   239  		}
   240  	})
   241  }