github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/all_test.go (about)

     1  // Copyright (c) 2014 The mathutil Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mathutil
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"math"
    11  	"math/big"
    12  	"math/rand"
    13  	"os"
    14  	"path"
    15  	"runtime"
    16  	"sort"
    17  	"strings"
    18  	"sync"
    19  	"testing"
    20  )
    21  
    22  func caller(s string, va ...interface{}) {
    23  	_, fn, fl, _ := runtime.Caller(2)
    24  	fmt.Fprintf(os.Stderr, "caller: %s:%d: ", path.Base(fn), fl)
    25  	fmt.Fprintf(os.Stderr, s, va...)
    26  	fmt.Fprintln(os.Stderr)
    27  	_, fn, fl, _ = runtime.Caller(1)
    28  	fmt.Fprintf(os.Stderr, "\tcallee: %s:%d: ", path.Base(fn), fl)
    29  	fmt.Fprintln(os.Stderr)
    30  }
    31  
    32  func dbg(s string, va ...interface{}) {
    33  	if s == "" {
    34  		s = strings.Repeat("%v ", len(va))
    35  	}
    36  	_, fn, fl, _ := runtime.Caller(1)
    37  	fmt.Fprintf(os.Stderr, "dbg %s:%d: ", path.Base(fn), fl)
    38  	fmt.Fprintf(os.Stderr, s, va...)
    39  	fmt.Fprintln(os.Stderr)
    40  }
    41  
    42  func TODO(...interface{}) string {
    43  	_, fn, fl, _ := runtime.Caller(1)
    44  	return fmt.Sprintf("TODO: %s:%d:\n", path.Base(fn), fl)
    45  }
    46  
    47  func use(...interface{}) {}
    48  
    49  func init() {
    50  	use(caller, TODO)
    51  }
    52  
    53  func intPtr(a int) *int {
    54  	return &a
    55  }
    56  
    57  func uIntPtr(a uint) *uint {
    58  	return &a
    59  }
    60  
    61  func bytePtr(a byte) *byte {
    62  	return &a
    63  }
    64  
    65  func int8Ptr(a int8) *int8 {
    66  	return &a
    67  }
    68  
    69  func int16Ptr(a int16) *int16 {
    70  	return &a
    71  }
    72  
    73  func uInt16Ptr(a uint16) *uint16 {
    74  	return &a
    75  }
    76  
    77  func int32Ptr(a int32) *int32 {
    78  	return &a
    79  }
    80  
    81  func uInt32Ptr(a uint32) *uint32 {
    82  	return &a
    83  }
    84  
    85  func int64Ptr(a int64) *int64 {
    86  	return &a
    87  }
    88  
    89  func uInt64Ptr(a uint64) *uint64 {
    90  	return &a
    91  }
    92  
    93  // ============================================================================
    94  
    95  func r32() *FC32 {
    96  	r, err := NewFC32(math.MinInt32, math.MaxInt32, true)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	return r
   102  }
   103  
   104  var (
   105  	r64lo          = big.NewInt(math.MinInt64)
   106  	r64hi          = big.NewInt(math.MaxInt64)
   107  	MinIntM1       = MinInt
   108  	MaxIntP1       = MaxInt
   109  	MaxUintP1 uint = MaxUint
   110  )
   111  
   112  func init() {
   113  	MinIntM1--
   114  	MaxIntP1++
   115  	MaxUintP1++
   116  }
   117  
   118  func r64() *FCBig {
   119  	r, err := NewFCBig(r64lo, r64hi, true)
   120  	if err != nil {
   121  		panic(err)
   122  	}
   123  
   124  	return r
   125  }
   126  
   127  func benchmark1eN(b *testing.B, r *FC32) {
   128  	b.StartTimer()
   129  	for i := 0; i < b.N; i++ {
   130  		r.Next()
   131  	}
   132  }
   133  
   134  func BenchmarkFC1e3(b *testing.B) {
   135  	b.StopTimer()
   136  	r, _ := NewFC32(0, 1e3, false)
   137  	benchmark1eN(b, r)
   138  }
   139  
   140  func BenchmarkFC1e6(b *testing.B) {
   141  	b.StopTimer()
   142  	r, _ := NewFC32(0, 1e6, false)
   143  	benchmark1eN(b, r)
   144  }
   145  
   146  func BenchmarkFC1e9(b *testing.B) {
   147  	b.StopTimer()
   148  	r, _ := NewFC32(0, 1e9, false)
   149  	benchmark1eN(b, r)
   150  }
   151  
   152  func Test0(t *testing.T) {
   153  	const N = 10000
   154  	for n := 1; n < N; n++ {
   155  		lo, hi := 0, n-1
   156  		period := int64(hi) - int64(lo) + 1
   157  		r, err := NewFC32(lo, hi, false)
   158  		if err != nil {
   159  			t.Fatal(err)
   160  		}
   161  		if r.Cycle()-period > period {
   162  			t.Fatalf("Cycle exceeds 2 * period")
   163  		}
   164  	}
   165  	for n := 1; n < N; n++ {
   166  		lo, hi := 0, n-1
   167  		period := int64(hi) - int64(lo) + 1
   168  		r, err := NewFC32(lo, hi, true)
   169  		if err != nil {
   170  			t.Fatal(err)
   171  		}
   172  		if r.Cycle()-2*period > period {
   173  			t.Fatalf("Cycle exceeds 3 * period")
   174  		}
   175  	}
   176  }
   177  
   178  func Test1(t *testing.T) {
   179  	const (
   180  		N = 360
   181  		S = 3
   182  	)
   183  	for hq := 0; hq <= 1; hq++ {
   184  		for n := 1; n < N; n++ {
   185  			for seed := 0; seed < S; seed++ {
   186  				lo, hi := -n, 2*n
   187  				period := int64(hi - lo + 1)
   188  				r, err := NewFC32(lo, hi, hq == 1)
   189  				if err != nil {
   190  					t.Fatal(err)
   191  				}
   192  				r.Seed(int64(seed))
   193  				m := map[int]bool{}
   194  				v := make([]int, period)
   195  				p := make([]int64, period)
   196  				for i := lo; i <= hi; i++ {
   197  					x := r.Next()
   198  					p[i-lo] = r.Pos()
   199  					if x < lo || x > hi {
   200  						t.Fatal("t1.0")
   201  					}
   202  					if m[x] {
   203  						t.Fatal("t1.1")
   204  					}
   205  					m[x] = true
   206  					v[i-lo] = x
   207  				}
   208  				for i := lo; i <= hi; i++ {
   209  					x := r.Next()
   210  					if x < lo || x > hi {
   211  						t.Fatal("t1.2")
   212  					}
   213  					if !m[x] {
   214  						t.Fatal("t1.3")
   215  					}
   216  					if x != v[i-lo] {
   217  						t.Fatal("t1.4")
   218  					}
   219  					if r.Pos() != p[i-lo] {
   220  						t.Fatal("t1.5")
   221  					}
   222  					m[x] = false
   223  				}
   224  				for i := lo; i <= hi; i++ {
   225  					r.Seek(p[i-lo] + 1)
   226  					x := r.Prev()
   227  					if x < lo || x > hi {
   228  						t.Fatal("t1.6")
   229  					}
   230  					if x != v[i-lo] {
   231  						t.Fatal("t1.7")
   232  					}
   233  				}
   234  			}
   235  		}
   236  	}
   237  }
   238  
   239  func Test2(t *testing.T) {
   240  	const (
   241  		N = 370
   242  		S = 3
   243  	)
   244  	for hq := 0; hq <= 1; hq++ {
   245  		for n := 1; n < N; n++ {
   246  			for seed := 0; seed < S; seed++ {
   247  				lo, hi := -n, 2*n
   248  				period := int64(hi - lo + 1)
   249  				r, err := NewFC32(lo, hi, hq == 1)
   250  				if err != nil {
   251  					t.Fatal(err)
   252  				}
   253  				r.Seed(int64(seed))
   254  				m := map[int]bool{}
   255  				v := make([]int, period)
   256  				p := make([]int64, period)
   257  				for i := lo; i <= hi; i++ {
   258  					x := r.Prev()
   259  					p[i-lo] = r.Pos()
   260  					if x < lo || x > hi {
   261  						t.Fatal("t2.0")
   262  					}
   263  					if m[x] {
   264  						t.Fatal("t2.1")
   265  					}
   266  					m[x] = true
   267  					v[i-lo] = x
   268  				}
   269  				for i := lo; i <= hi; i++ {
   270  					x := r.Prev()
   271  					if x < lo || x > hi {
   272  						t.Fatal("t2.2")
   273  					}
   274  					if !m[x] {
   275  						t.Fatal("t2.3")
   276  					}
   277  					if x != v[i-lo] {
   278  						t.Fatal("t2.4")
   279  					}
   280  					if r.Pos() != p[i-lo] {
   281  						t.Fatal("t2.5")
   282  					}
   283  					m[x] = false
   284  				}
   285  				for i := lo; i <= hi; i++ {
   286  					s := p[i-lo] - 1
   287  					if s < 0 {
   288  						s = r.Cycle() - 1
   289  					}
   290  					r.Seek(s)
   291  					x := r.Next()
   292  					if x < lo || x > hi {
   293  						t.Fatal("t2.6")
   294  					}
   295  					if x != v[i-lo] {
   296  						t.Fatal("t2.7")
   297  					}
   298  				}
   299  			}
   300  		}
   301  	}
   302  }
   303  
   304  func benchmarkBig1eN(b *testing.B, r *FCBig) {
   305  	b.StartTimer()
   306  	for i := 0; i < b.N; i++ {
   307  		r.Next()
   308  	}
   309  }
   310  
   311  func BenchmarkFCBig1e3(b *testing.B) {
   312  	b.StopTimer()
   313  	hi := big.NewInt(0).SetInt64(1e3)
   314  	r, _ := NewFCBig(big0, hi, false)
   315  	benchmarkBig1eN(b, r)
   316  }
   317  
   318  func BenchmarkFCBig1e6(b *testing.B) {
   319  	b.StopTimer()
   320  	hi := big.NewInt(0).SetInt64(1e6)
   321  	r, _ := NewFCBig(big0, hi, false)
   322  	benchmarkBig1eN(b, r)
   323  }
   324  
   325  func BenchmarkFCBig1e9(b *testing.B) {
   326  	b.StopTimer()
   327  	hi := big.NewInt(0).SetInt64(1e9)
   328  	r, _ := NewFCBig(big0, hi, false)
   329  	benchmarkBig1eN(b, r)
   330  }
   331  
   332  func BenchmarkFCBig1e12(b *testing.B) {
   333  	b.StopTimer()
   334  	hi := big.NewInt(0).SetInt64(1e12)
   335  	r, _ := NewFCBig(big0, hi, false)
   336  	benchmarkBig1eN(b, r)
   337  }
   338  
   339  func BenchmarkFCBig1e15(b *testing.B) {
   340  	b.StopTimer()
   341  	hi := big.NewInt(0).SetInt64(1e15)
   342  	r, _ := NewFCBig(big0, hi, false)
   343  	benchmarkBig1eN(b, r)
   344  }
   345  
   346  func BenchmarkFCBig1e18(b *testing.B) {
   347  	b.StopTimer()
   348  	hi := big.NewInt(0).SetInt64(1e18)
   349  	r, _ := NewFCBig(big0, hi, false)
   350  	benchmarkBig1eN(b, r)
   351  }
   352  
   353  var (
   354  	big0 = big.NewInt(0)
   355  )
   356  
   357  func TestBig0(t *testing.T) {
   358  	const N = 7400
   359  	lo := big.NewInt(0)
   360  	hi := big.NewInt(0)
   361  	period := big.NewInt(0)
   362  	c := big.NewInt(0)
   363  	for n := int64(1); n < N; n++ {
   364  		hi.SetInt64(n - 1)
   365  		period.Set(hi)
   366  		period.Sub(period, lo)
   367  		period.Add(period, _1)
   368  		r, err := NewFCBig(lo, hi, false)
   369  		if err != nil {
   370  			t.Fatal(err)
   371  		}
   372  		if r.cycle.Cmp(period) < 0 {
   373  			t.Fatalf("Period exceeds cycle")
   374  		}
   375  		c.Set(r.Cycle())
   376  		c.Sub(c, period)
   377  		if c.Cmp(period) > 0 {
   378  			t.Fatalf("Cycle exceeds 2 * period")
   379  		}
   380  	}
   381  	for n := int64(1); n < N; n++ {
   382  		hi.SetInt64(n - 1)
   383  		period.Set(hi)
   384  		period.Sub(period, lo)
   385  		period.Add(period, _1)
   386  		r, err := NewFCBig(lo, hi, true)
   387  		if err != nil {
   388  			t.Fatal(err)
   389  		}
   390  		if r.cycle.Cmp(period) < 0 {
   391  			t.Fatalf("Period exceeds cycle")
   392  		}
   393  		c.Set(r.Cycle())
   394  		c.Sub(c, period)
   395  		c.Sub(c, period)
   396  		if c.Cmp(period) > 0 {
   397  			t.Fatalf("Cycle exceeds 3 * period")
   398  		}
   399  	}
   400  }
   401  
   402  func TestBig1(t *testing.T) {
   403  	const (
   404  		N = 120
   405  		S = 3
   406  	)
   407  	lo := big.NewInt(0)
   408  	hi := big.NewInt(0)
   409  	seek := big.NewInt(0)
   410  	for hq := 0; hq <= 1; hq++ {
   411  		for n := int64(1); n < N; n++ {
   412  			for seed := 0; seed < S; seed++ {
   413  				lo64 := -n
   414  				hi64 := 2 * n
   415  				lo.SetInt64(lo64)
   416  				hi.SetInt64(hi64)
   417  				period := hi64 - lo64 + 1
   418  				r, err := NewFCBig(lo, hi, hq == 1)
   419  				if err != nil {
   420  					t.Fatal(err)
   421  				}
   422  				r.Seed(int64(seed))
   423  				m := map[int64]bool{}
   424  				v := make([]int64, period)
   425  				p := make([]int64, period)
   426  				for i := lo64; i <= hi64; i++ {
   427  					x := r.Next().Int64()
   428  					p[i-lo64] = r.Pos().Int64()
   429  					if x < lo64 || x > hi64 {
   430  						t.Fatal("tb1.0")
   431  					}
   432  					if m[x] {
   433  						t.Fatal("tb1.1")
   434  					}
   435  					m[x] = true
   436  					v[i-lo64] = x
   437  				}
   438  				for i := lo64; i <= hi64; i++ {
   439  					x := r.Next().Int64()
   440  					if x < lo64 || x > hi64 {
   441  						t.Fatal("tb1.2")
   442  					}
   443  					if !m[x] {
   444  						t.Fatal("tb1.3")
   445  					}
   446  					if x != v[i-lo64] {
   447  						t.Fatal("tb1.4")
   448  					}
   449  					if r.Pos().Int64() != p[i-lo64] {
   450  						t.Fatal("tb1.5")
   451  					}
   452  					m[x] = false
   453  				}
   454  				for i := lo64; i <= hi64; i++ {
   455  					r.Seek(seek.SetInt64(p[i-lo64] + 1))
   456  					x := r.Prev().Int64()
   457  					if x < lo64 || x > hi64 {
   458  						t.Fatal("tb1.6")
   459  					}
   460  					if x != v[i-lo64] {
   461  						t.Fatal("tb1.7")
   462  					}
   463  				}
   464  			}
   465  		}
   466  	}
   467  }
   468  
   469  func TestBig2(t *testing.T) {
   470  	const (
   471  		N = 120
   472  		S = 3
   473  	)
   474  	lo := big.NewInt(0)
   475  	hi := big.NewInt(0)
   476  	seek := big.NewInt(0)
   477  	for hq := 0; hq <= 1; hq++ {
   478  		for n := int64(1); n < N; n++ {
   479  			for seed := 0; seed < S; seed++ {
   480  				lo64, hi64 := -n, 2*n
   481  				lo.SetInt64(lo64)
   482  				hi.SetInt64(hi64)
   483  				period := hi64 - lo64 + 1
   484  				r, err := NewFCBig(lo, hi, hq == 1)
   485  				if err != nil {
   486  					t.Fatal(err)
   487  				}
   488  				r.Seed(int64(seed))
   489  				m := map[int64]bool{}
   490  				v := make([]int64, period)
   491  				p := make([]int64, period)
   492  				for i := lo64; i <= hi64; i++ {
   493  					x := r.Prev().Int64()
   494  					p[i-lo64] = r.Pos().Int64()
   495  					if x < lo64 || x > hi64 {
   496  						t.Fatal("tb2.0")
   497  					}
   498  					if m[x] {
   499  						t.Fatal("tb2.1")
   500  					}
   501  					m[x] = true
   502  					v[i-lo64] = x
   503  				}
   504  				for i := lo64; i <= hi64; i++ {
   505  					x := r.Prev().Int64()
   506  					if x < lo64 || x > hi64 {
   507  						t.Fatal("tb2.2")
   508  					}
   509  					if !m[x] {
   510  						t.Fatal("tb2.3")
   511  					}
   512  					if x != v[i-lo64] {
   513  						t.Fatal("tb2.4")
   514  					}
   515  					if r.Pos().Int64() != p[i-lo64] {
   516  						t.Fatal("tb2.5")
   517  					}
   518  					m[x] = false
   519  				}
   520  				for i := lo64; i <= hi64; i++ {
   521  					s := p[i-lo64] - 1
   522  					if s < 0 {
   523  						s = r.Cycle().Int64() - 1
   524  					}
   525  					r.Seek(seek.SetInt64(s))
   526  					x := r.Next().Int64()
   527  					if x < lo64 || x > hi64 {
   528  						t.Fatal("tb2.6")
   529  					}
   530  					if x != v[i-lo64] {
   531  						t.Fatal("tb2.7")
   532  					}
   533  				}
   534  			}
   535  		}
   536  	}
   537  }
   538  
   539  func TestPermutations(t *testing.T) {
   540  	data := sort.IntSlice{3, 2, 1}
   541  	check := [][]int{
   542  		{1, 2, 3},
   543  		{1, 3, 2},
   544  		{2, 1, 3},
   545  		{2, 3, 1},
   546  		{3, 1, 2},
   547  		{3, 2, 1},
   548  	}
   549  	i := 0
   550  	for PermutationFirst(data); ; i++ {
   551  		if i >= len(check) {
   552  			t.Fatalf("too much permutations generated: %d > %d", i+1, len(check))
   553  		}
   554  
   555  		for j, v := range check[i] {
   556  			got := data[j]
   557  			if got != v {
   558  				t.Fatalf("permutation %d:\ndata: %v\ncheck: %v\nexpected data[%d] == %d, got %d", i, data, check[i], j, v, got)
   559  			}
   560  		}
   561  
   562  		if !PermutationNext(data) {
   563  			if i != len(check)-1 {
   564  				t.Fatal("permutations generated", i, "expected", len(check))
   565  			}
   566  			break
   567  		}
   568  	}
   569  }
   570  
   571  func TestIsPrime(t *testing.T) {
   572  	const p4M = 283146 // # of primes < 4e6
   573  	n := 0
   574  	for i := uint32(0); i <= 4e6; i++ {
   575  		if IsPrime(i) {
   576  			n++
   577  		}
   578  	}
   579  	t.Log(n)
   580  	if n != p4M {
   581  		t.Fatal(n)
   582  	}
   583  }
   584  
   585  func BenchmarkIsPrime(b *testing.B) {
   586  	b.StopTimer()
   587  	n := make([]uint32, b.N)
   588  	rng := rand.New(rand.NewSource(1))
   589  	for i := 0; i < b.N; i++ {
   590  		n[i] = rng.Uint32()
   591  	}
   592  	b.StartTimer()
   593  	for _, n := range n {
   594  		IsPrime(n)
   595  	}
   596  }
   597  
   598  func BenchmarkNextPrime(b *testing.B) {
   599  	b.StopTimer()
   600  	n := make([]uint32, b.N)
   601  	rng := rand.New(rand.NewSource(1))
   602  	for i := 0; i < b.N; i++ {
   603  		n[i] = rng.Uint32()
   604  	}
   605  	b.StartTimer()
   606  	for _, n := range n {
   607  		NextPrime(n)
   608  	}
   609  }
   610  
   611  func BenchmarkIsPrimeUint64(b *testing.B) {
   612  	const N = 1 << 16
   613  	b.StopTimer()
   614  	a := make([]uint64, N)
   615  	r := r64()
   616  	for i := range a {
   617  		a[i] = uint64(r.Next().Int64())
   618  	}
   619  	runtime.GC()
   620  	b.StartTimer()
   621  	for i := 0; i < b.N; i++ {
   622  		IsPrimeUint64(a[i&(N-1)])
   623  	}
   624  }
   625  
   626  func BenchmarkNextPrimeUint64(b *testing.B) {
   627  	b.StopTimer()
   628  	n := make([]uint64, b.N)
   629  	rng := rand.New(rand.NewSource(1))
   630  	for i := 0; i < b.N; i++ {
   631  		n[i] = uint64(rng.Int63())
   632  		if i&1 == 0 {
   633  			n[i] ^= 1 << 63
   634  		}
   635  	}
   636  	b.StartTimer()
   637  	for _, n := range n {
   638  		NextPrimeUint64(n)
   639  	}
   640  }
   641  
   642  func TestNextPrime(t *testing.T) {
   643  	const p4M = 283146 // # of primes < 4e6
   644  	n := 0
   645  	var p uint32
   646  	for {
   647  		p, _ = NextPrime(p)
   648  		if p >= 4e6 {
   649  			break
   650  		}
   651  		n++
   652  	}
   653  	t.Log(n)
   654  	if n != p4M {
   655  		t.Fatal(n)
   656  	}
   657  }
   658  
   659  func TestNextPrime2(t *testing.T) {
   660  	type data struct {
   661  		x  uint32
   662  		y  uint32
   663  		ok bool
   664  	}
   665  	tests := []data{
   666  		{0, 2, true},
   667  		{1, 2, true},
   668  		{2, 3, true},
   669  		{3, 5, true},
   670  		{math.MaxUint32, 0, false},
   671  		{math.MaxUint32 - 1, 0, false},
   672  		{math.MaxUint32 - 2, 0, false},
   673  		{math.MaxUint32 - 3, 0, false},
   674  		{math.MaxUint32 - 4, 0, false},
   675  		{math.MaxUint32 - 5, math.MaxUint32 - 4, true},
   676  	}
   677  
   678  	for _, test := range tests {
   679  		y, ok := NextPrime(test.x)
   680  		if ok != test.ok || ok && y != test.y {
   681  			t.Fatalf("x %d, got y %d ok %t, expected y %d ok %t", test.x, y, ok, test.y, test.ok)
   682  		}
   683  	}
   684  }
   685  
   686  func TestNextPrimeUint64(t *testing.T) {
   687  	const (
   688  		lo = 2000000000000000000
   689  		hi = 2000000000000100000
   690  		k  = 2346 // PrimePi(hi)-PrimePi(lo)
   691  	)
   692  	n := 0
   693  	p := uint64(lo) - 1
   694  	var ok bool
   695  	for {
   696  		p0 := p
   697  		p, ok = NextPrimeUint64(p)
   698  		if !ok {
   699  			t.Fatal(p0)
   700  		}
   701  
   702  		if p > hi {
   703  			break
   704  		}
   705  
   706  		n++
   707  	}
   708  	if n != k {
   709  		t.Fatal(n, k)
   710  	}
   711  }
   712  
   713  func TestISqrt(t *testing.T) {
   714  	for n := int64(0); n < 5e6; n++ {
   715  		x := int64(ISqrt(uint32(n)))
   716  		if x2 := x * x; x2 > n {
   717  			t.Fatalf("got ISqrt(%d) == %d, too big", n, x)
   718  		}
   719  		if x2 := x*x + 2*x + 1; x2 < n {
   720  			t.Fatalf("got ISqrt(%d) == %d, too low", n, x)
   721  		}
   722  	}
   723  	for n := int64(math.MaxUint32); n > math.MaxUint32-5e6; n-- {
   724  		x := int64(ISqrt(uint32(n)))
   725  		if x2 := x * x; x2 > n {
   726  			t.Fatalf("got ISqrt(%d) == %d, too big", n, x)
   727  		}
   728  		if x2 := x*x + 2*x + 1; x2 < n {
   729  			t.Fatalf("got ISqrt(%d) == %d, too low", n, x)
   730  		}
   731  	}
   732  	rng := rand.New(rand.NewSource(1))
   733  	for i := 0; i < 5e6; i++ {
   734  		n := int64(rng.Uint32())
   735  		x := int64(ISqrt(uint32(n)))
   736  		if x2 := x * x; x2 > n {
   737  			t.Fatalf("got ISqrt(%d) == %d, too big", n, x)
   738  		}
   739  		if x2 := x*x + 2*x + 1; x2 < n {
   740  			t.Fatalf("got ISqrt(%d) == %d, too low", n, x)
   741  		}
   742  	}
   743  }
   744  
   745  func TestSqrtUint64(t *testing.T) {
   746  	for n := uint64(0); n < 2e6; n++ {
   747  		x := SqrtUint64(n)
   748  		if x > math.MaxUint32 {
   749  			t.Fatalf("got Sqrt(%d) == %d, too big", n, x)
   750  		}
   751  		if x2 := x * x; x2 > n {
   752  			t.Fatalf("got Sqrt(%d) == %d, too big", n, x)
   753  		}
   754  		if x2 := x*x + 2*x + 1; x2 < n {
   755  			t.Fatalf("got Sqrt(%d) == %d, too low", n, x)
   756  		}
   757  	}
   758  	const H = uint64(18446744056529682436)
   759  	for n := H; n > H-2e6; n-- {
   760  		x := SqrtUint64(n)
   761  		if x > math.MaxUint32 {
   762  			t.Fatalf("got Sqrt(%d) == %d, too big", n, x)
   763  		}
   764  		if x2 := x * x; x2 > n {
   765  			t.Fatalf("got Sqrt(%d) == %d, too big", n, x)
   766  		}
   767  		if x2 := x*x + 2*x + 1; x2 < n {
   768  			t.Fatalf("got Sqrt(%d) == %d, too low", n, x)
   769  		}
   770  	}
   771  	rng := rand.New(rand.NewSource(1))
   772  	for i := 0; i < 2e6; i++ {
   773  		n := uint64(rng.Uint32())<<31 | uint64(rng.Uint32())
   774  		x := SqrtUint64(n)
   775  		if x2 := x * x; x2 > n {
   776  			t.Fatalf("got Sqrt(%d) == %d, too big", n, x)
   777  		}
   778  		if x2 := x*x + 2*x + 1; x2 < n {
   779  			t.Fatalf("got Sqrt(%d) == %d, too low", n, x)
   780  		}
   781  	}
   782  }
   783  
   784  func TestSqrtBig(t *testing.T) {
   785  	const N = 3e4
   786  	var n, lim, x2 big.Int
   787  	lim.SetInt64(N)
   788  	for n.Cmp(&lim) != 0 {
   789  		x := SqrtBig(&n)
   790  		x2.Mul(x, x)
   791  		if x.Cmp(&n) > 0 {
   792  			t.Fatalf("got sqrt(%s) == %s, too big", &n, x)
   793  		}
   794  		x2.Add(&x2, x)
   795  		x2.Add(&x2, x)
   796  		x2.Add(&x2, _1)
   797  		if x2.Cmp(&n) < 0 {
   798  			t.Fatalf("got sqrt(%s) == %s, too low", &n, x)
   799  		}
   800  		n.Add(&n, _1)
   801  	}
   802  	rng := rand.New(rand.NewSource(1))
   803  	var h big.Int
   804  	h.SetBit(&h, 1e3, 1)
   805  	for i := 0; i < N; i++ {
   806  		n.Rand(rng, &h)
   807  		x := SqrtBig(&n)
   808  		x2.Mul(x, x)
   809  		if x.Cmp(&n) > 0 {
   810  			t.Fatalf("got sqrt(%s) == %s, too big", &n, x)
   811  		}
   812  		x2.Add(&x2, x)
   813  		x2.Add(&x2, x)
   814  		x2.Add(&x2, _1)
   815  		if x2.Cmp(&n) < 0 {
   816  			t.Fatalf("got sqrt(%s) == %s, too low", &n, x)
   817  		}
   818  	}
   819  }
   820  
   821  func TestFactorInt(t *testing.T) {
   822  	chk := func(n uint64, f []FactorTerm) bool {
   823  		if n < 2 {
   824  			return len(f) == 0
   825  		}
   826  
   827  		for i := 1; i < len(f); i++ { // verify ordering
   828  			if t, u := f[i-1], f[i]; t.Prime >= u.Prime {
   829  				return false
   830  			}
   831  		}
   832  
   833  		x := uint64(1)
   834  		for _, v := range f {
   835  			if p := v.Prime; p < 0 || !IsPrime(v.Prime) {
   836  				return false
   837  			}
   838  
   839  			for i := uint32(0); i < v.Power; i++ {
   840  				x *= uint64(v.Prime)
   841  				if x > math.MaxUint32 {
   842  					return false
   843  				}
   844  			}
   845  		}
   846  		return x == n
   847  	}
   848  
   849  	for n := uint64(0); n < 3e5; n++ {
   850  		f := FactorInt(uint32(n))
   851  		if !chk(n, f) {
   852  			t.Fatalf("bad FactorInt(%d): %v", n, f)
   853  		}
   854  	}
   855  	for n := uint64(math.MaxUint32); n > math.MaxUint32-12e4; n-- {
   856  		f := FactorInt(uint32(n))
   857  		if !chk(n, f) {
   858  			t.Fatalf("bad FactorInt(%d): %v", n, f)
   859  		}
   860  	}
   861  	rng := rand.New(rand.NewSource(1))
   862  	for i := 0; i < 13e4; i++ {
   863  		n := rng.Uint32()
   864  		f := FactorInt(n)
   865  		if !chk(uint64(n), f) {
   866  			t.Fatalf("bad FactorInt(%d): %v", n, f)
   867  		}
   868  	}
   869  }
   870  
   871  func TestFactorIntB(t *testing.T) {
   872  	const N = 3e5 // must be < math.MaxInt32
   873  	factors := make([][]FactorTerm, N+1)
   874  	// set up the divisors
   875  	for prime := uint32(2); prime <= N; prime, _ = NextPrime(prime) {
   876  		for n := int(prime); n <= N; n += int(prime) {
   877  			factors[n] = append(factors[n], FactorTerm{prime, 0})
   878  		}
   879  	}
   880  	// set up the powers
   881  	for n := 2; n <= N; n++ {
   882  		f := factors[n]
   883  		m := uint32(n)
   884  		for i, v := range f {
   885  			for m%v.Prime == 0 {
   886  				m /= v.Prime
   887  				v.Power++
   888  			}
   889  			f[i] = v
   890  		}
   891  		factors[n] = f
   892  	}
   893  	// check equal
   894  	for n, e := range factors {
   895  		g := FactorInt(uint32(n))
   896  		if len(e) != len(g) {
   897  			t.Fatal(n, "len", g, "!=", e)
   898  		}
   899  
   900  		for i, ev := range e {
   901  			gv := g[i]
   902  			if ev.Prime != gv.Prime {
   903  				t.Fatal(n, "prime", gv, ev)
   904  			}
   905  
   906  			if ev.Power != gv.Power {
   907  				t.Fatal(n, "power", gv, ev)
   908  			}
   909  		}
   910  	}
   911  }
   912  
   913  func BenchmarkISqrt(b *testing.B) {
   914  	b.StopTimer()
   915  	n := make([]uint32, b.N)
   916  	rng := rand.New(rand.NewSource(1))
   917  	for i := 0; i < b.N; i++ {
   918  		n[i] = rng.Uint32()
   919  	}
   920  	b.StartTimer()
   921  	for _, n := range n {
   922  		ISqrt(n)
   923  	}
   924  }
   925  
   926  func BenchmarkSqrtUint64(b *testing.B) {
   927  	b.StopTimer()
   928  	n := make([]uint64, b.N)
   929  	rng := rand.New(rand.NewSource(1))
   930  	for i := 0; i < b.N; i++ {
   931  		n[i] = uint64(rng.Uint32())<<32 | uint64(rng.Uint32())
   932  	}
   933  	b.StartTimer()
   934  	for _, n := range n {
   935  		SqrtUint64(n)
   936  	}
   937  }
   938  
   939  func benchmarkSqrtBig(b *testing.B, bits int) {
   940  	b.StopTimer()
   941  	n := make([]*big.Int, b.N)
   942  	rng := rand.New(rand.NewSource(1))
   943  	var nn, h big.Int
   944  	h.SetBit(&h, bits, 1)
   945  	for i := 0; i < b.N; i++ {
   946  		n[i] = nn.Rand(rng, &h)
   947  	}
   948  	runtime.GC()
   949  	b.StartTimer()
   950  	for _, n := range n {
   951  		SqrtBig(n)
   952  	}
   953  }
   954  
   955  func BenchmarkSqrtBig2e1e1(b *testing.B) {
   956  	benchmarkSqrtBig(b, 1e1)
   957  }
   958  
   959  func BenchmarkSqrtBig2e1e2(b *testing.B) {
   960  	benchmarkSqrtBig(b, 1e2)
   961  }
   962  
   963  func BenchmarkSqrtBig2e1e3(b *testing.B) {
   964  	benchmarkSqrtBig(b, 1e3)
   965  }
   966  
   967  func BenchmarkSqrtBig2e1e4(b *testing.B) {
   968  	benchmarkSqrtBig(b, 1e4)
   969  }
   970  
   971  func BenchmarkSqrtBig2e1e5(b *testing.B) {
   972  	benchmarkSqrtBig(b, 1e5)
   973  }
   974  
   975  func BenchmarkFactorInt(b *testing.B) {
   976  	b.StopTimer()
   977  	n := make([]uint32, b.N)
   978  	rng := rand.New(rand.NewSource(1))
   979  	for i := 0; i < b.N; i++ {
   980  		n[i] = rng.Uint32()
   981  	}
   982  	b.StartTimer()
   983  	for _, n := range n {
   984  		FactorInt(n)
   985  	}
   986  }
   987  
   988  func TestIsPrimeUint16(t *testing.T) {
   989  	for n := 0; n <= math.MaxUint16; n++ {
   990  		if IsPrimeUint16(uint16(n)) != IsPrime(uint32(n)) {
   991  			t.Fatal(n)
   992  		}
   993  	}
   994  }
   995  
   996  func BenchmarkIsPrimeUint16(b *testing.B) {
   997  	b.StopTimer()
   998  	n := make([]uint16, b.N)
   999  	rng := rand.New(rand.NewSource(1))
  1000  	for i := 0; i < b.N; i++ {
  1001  		n[i] = uint16(rng.Uint32())
  1002  	}
  1003  	b.StartTimer()
  1004  	for _, n := range n {
  1005  		IsPrimeUint16(n)
  1006  	}
  1007  }
  1008  
  1009  func TestNextPrimeUint16(t *testing.T) {
  1010  	for n := 0; n <= math.MaxUint16; n++ {
  1011  		p, ok := NextPrimeUint16(uint16(n))
  1012  		p2, ok2 := NextPrime(uint32(n))
  1013  		switch {
  1014  		case ok:
  1015  			if !ok2 || uint32(p) != p2 {
  1016  				t.Fatal(n, p, ok)
  1017  			}
  1018  		case !ok && ok2:
  1019  			if p2 < 65536 {
  1020  				t.Fatal(n, p, ok)
  1021  			}
  1022  		}
  1023  	}
  1024  }
  1025  
  1026  func BenchmarkNextPrimeUint16(b *testing.B) {
  1027  	b.StopTimer()
  1028  	n := make([]uint16, b.N)
  1029  	rng := rand.New(rand.NewSource(1))
  1030  	for i := 0; i < b.N; i++ {
  1031  		n[i] = uint16(rng.Uint32())
  1032  	}
  1033  	b.StartTimer()
  1034  	for _, n := range n {
  1035  		NextPrimeUint16(n)
  1036  	}
  1037  }
  1038  
  1039  /*
  1040  
  1041  From: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
  1042  
  1043  Counting bits set, Brian Kernighan's way
  1044  
  1045  unsigned int v; // count the number of bits set in v
  1046  unsigned int c; // c accumulates the total bits set in v
  1047  for (c = 0; v; c++)
  1048  {
  1049    v &= v - 1; // clear the least significant bit set
  1050  }
  1051  
  1052  Brian Kernighan's method goes through as many iterations as there are set bits.
  1053  So if we have a 32-bit word with only the high bit set, then it will only go
  1054  once through the loop.
  1055  
  1056  Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan
  1057  and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don
  1058  Knuth pointed out to me that this method "was first published by Peter Wegner
  1059  in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
  1060  published in 1964 in a book edited by Beckenbach.)"
  1061  */
  1062  func bcnt(v uint64) (c int) {
  1063  	for ; v != 0; c++ {
  1064  		v &= v - 1
  1065  	}
  1066  	return
  1067  }
  1068  
  1069  func TestPopCount(t *testing.T) {
  1070  	const N = 4e5
  1071  	maxUint64 := big.NewInt(0)
  1072  	maxUint64.SetBit(maxUint64, 64, 1)
  1073  	maxUint64.Sub(maxUint64, big.NewInt(1))
  1074  	rng := r64()
  1075  	for i := 0; i < N; i++ {
  1076  		n := uint64(rng.Next().Int64())
  1077  		if g, e := PopCountByte(byte(n)), bcnt(uint64(byte(n))); g != e {
  1078  			t.Fatal(n, g, e)
  1079  		}
  1080  
  1081  		if g, e := PopCountUint16(uint16(n)), bcnt(uint64(uint16(n))); g != e {
  1082  			t.Fatal(n, g, e)
  1083  		}
  1084  
  1085  		if g, e := PopCountUint32(uint32(n)), bcnt(uint64(uint32(n))); g != e {
  1086  			t.Fatal(n, g, e)
  1087  		}
  1088  
  1089  		if g, e := PopCount(int(n)), bcnt(uint64(uint(n))); g != e {
  1090  			t.Fatal(n, g, e)
  1091  		}
  1092  
  1093  		if g, e := PopCountUint(uint(n)), bcnt(uint64(uint(n))); g != e {
  1094  			t.Fatal(n, g, e)
  1095  		}
  1096  
  1097  		if g, e := PopCountUint64(n), bcnt(n); g != e {
  1098  			t.Fatal(n, g, e)
  1099  		}
  1100  
  1101  		if g, e := PopCountUintptr(uintptr(n)), bcnt(uint64(uintptr(n))); g != e {
  1102  			t.Fatal(n, g, e)
  1103  		}
  1104  	}
  1105  }
  1106  
  1107  var gcds = []struct{ a, b, gcd uint64 }{
  1108  	{8, 12, 4},
  1109  	{12, 18, 6},
  1110  	{42, 56, 14},
  1111  	{54, 24, 6},
  1112  	{252, 105, 21},
  1113  	{1989, 867, 51},
  1114  	{1071, 462, 21},
  1115  	{2 * 3 * 5 * 7 * 11, 5 * 7 * 11 * 13 * 17, 5 * 7 * 11},
  1116  	{2 * 3 * 5 * 7 * 7 * 11, 5 * 7 * 7 * 11 * 13 * 17, 5 * 7 * 7 * 11},
  1117  	{2 * 3 * 5 * 7 * 7 * 11, 5 * 7 * 7 * 13 * 17, 5 * 7 * 7},
  1118  	{2 * 3 * 5 * 7 * 11, 13 * 17 * 19, 1},
  1119  }
  1120  
  1121  func TestGCD(t *testing.T) {
  1122  	for i, v := range gcds {
  1123  		if v.a <= math.MaxUint16 && v.b <= math.MaxUint16 {
  1124  			if g, e := uint64(GCDUint16(uint16(v.a), uint16(v.b))), v.gcd; g != e {
  1125  				t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.a, v.b, g, e)
  1126  			}
  1127  			if g, e := uint64(GCDUint16(uint16(v.b), uint16(v.a))), v.gcd; g != e {
  1128  				t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.b, v.a, g, e)
  1129  			}
  1130  		}
  1131  		if v.a <= math.MaxUint32 && v.b <= math.MaxUint32 {
  1132  			if g, e := uint64(GCDUint32(uint32(v.a), uint32(v.b))), v.gcd; g != e {
  1133  				t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.a, v.b, g, e)
  1134  			}
  1135  			if g, e := uint64(GCDUint32(uint32(v.b), uint32(v.a))), v.gcd; g != e {
  1136  				t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.b, v.a, g, e)
  1137  			}
  1138  		}
  1139  		if g, e := GCDUint64(v.a, v.b), v.gcd; g != e {
  1140  			t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.a, v.b, g, e)
  1141  		}
  1142  		if g, e := GCDUint64(v.b, v.a), v.gcd; g != e {
  1143  			t.Errorf("%d: got gcd(%d, %d) %d, exp %d", i, v.b, v.a, g, e)
  1144  		}
  1145  	}
  1146  }
  1147  
  1148  func lg2(n uint64) (lg int) {
  1149  	if n == 0 {
  1150  		return -1
  1151  	}
  1152  
  1153  	for n >>= 1; n != 0; n >>= 1 {
  1154  		lg++
  1155  	}
  1156  	return
  1157  }
  1158  
  1159  func TestLog2(t *testing.T) {
  1160  	if g, e := Log2Byte(0), -1; g != e {
  1161  		t.Error(g, e)
  1162  	}
  1163  	if g, e := Log2Uint16(0), -1; g != e {
  1164  		t.Error(g, e)
  1165  	}
  1166  	if g, e := Log2Uint32(0), -1; g != e {
  1167  		t.Error(g, e)
  1168  	}
  1169  	if g, e := Log2Uint64(0), -1; g != e {
  1170  		t.Error(g, e)
  1171  	}
  1172  	const N = 1e6
  1173  	rng := r64()
  1174  	for i := 0; i < N; i++ {
  1175  		n := uint64(rng.Next().Int64())
  1176  		if g, e := Log2Uint64(n), lg2(n); g != e {
  1177  			t.Fatalf("%b %d %d", n, g, e)
  1178  		}
  1179  		if g, e := Log2Uint32(uint32(n)), lg2(n&0xffffffff); g != e {
  1180  			t.Fatalf("%b %d %d", n, g, e)
  1181  		}
  1182  		if g, e := Log2Uint16(uint16(n)), lg2(n&0xffff); g != e {
  1183  			t.Fatalf("%b %d %d", n, g, e)
  1184  		}
  1185  		if g, e := Log2Byte(byte(n)), lg2(n&0xff); g != e {
  1186  			t.Fatalf("%b %d %d", n, g, e)
  1187  		}
  1188  	}
  1189  }
  1190  
  1191  func TestBitLen(t *testing.T) {
  1192  	if g, e := BitLenByte(0), 0; g != e {
  1193  		t.Error(g, e)
  1194  	}
  1195  	if g, e := BitLenUint16(0), 0; g != e {
  1196  		t.Error(g, e)
  1197  	}
  1198  	if g, e := BitLenUint32(0), 0; g != e {
  1199  		t.Error(g, e)
  1200  	}
  1201  	if g, e := BitLenUint64(0), 0; g != e {
  1202  		t.Error(g, e)
  1203  	}
  1204  	if g, e := BitLenUintptr(0), 0; g != e {
  1205  		t.Error(g, e)
  1206  	}
  1207  	const N = 1e6
  1208  	rng := r64()
  1209  	for i := 0; i < N; i++ {
  1210  		n := uint64(rng.Next().Int64())
  1211  		if g, e := BitLenUintptr(uintptr(n)), lg2(uint64(uintptr(n)))+1; g != e {
  1212  			t.Fatalf("%b %d %d", n, g, e)
  1213  		}
  1214  		if g, e := BitLenUint64(n), lg2(n)+1; g != e {
  1215  			t.Fatalf("%b %d %d", n, g, e)
  1216  		}
  1217  		if g, e := BitLenUint32(uint32(n)), lg2(n&0xffffffff)+1; g != e {
  1218  			t.Fatalf("%b %d %d", n, g, e)
  1219  		}
  1220  		if g, e := BitLen(int(n)), lg2(uint64(uint(n)))+1; g != e {
  1221  			t.Fatalf("%b %d %d", n, g, e)
  1222  		}
  1223  		if g, e := BitLenUint(uint(n)), lg2(uint64(uint(n)))+1; g != e {
  1224  			t.Fatalf("%b %d %d", n, g, e)
  1225  		}
  1226  		if g, e := BitLenUint16(uint16(n)), lg2(n&0xffff)+1; g != e {
  1227  			t.Fatalf("%b %d %d", n, g, e)
  1228  		}
  1229  		if g, e := BitLenByte(byte(n)), lg2(n&0xff)+1; g != e {
  1230  			t.Fatalf("%b %d %d", n, g, e)
  1231  		}
  1232  	}
  1233  }
  1234  
  1235  func BenchmarkGCDByte(b *testing.B) {
  1236  	const N = 1 << 16
  1237  	type t byte
  1238  	type u struct{ a, b t }
  1239  	b.StopTimer()
  1240  	rng := r32()
  1241  	a := make([]u, N)
  1242  	for i := range a {
  1243  		a[i] = u{t(rng.Next()), t(rng.Next())}
  1244  	}
  1245  	b.StartTimer()
  1246  	for i := 0; i < b.N; i++ {
  1247  		v := a[i&(N-1)]
  1248  		GCDByte(byte(v.a), byte(v.b))
  1249  	}
  1250  }
  1251  
  1252  func BenchmarkGCDUint16(b *testing.B) {
  1253  	const N = 1 << 16
  1254  	type t uint16
  1255  	type u struct{ a, b t }
  1256  	b.StopTimer()
  1257  	rng := r32()
  1258  	a := make([]u, N)
  1259  	for i := range a {
  1260  		a[i] = u{t(rng.Next()), t(rng.Next())}
  1261  	}
  1262  	b.StartTimer()
  1263  	for i := 0; i < b.N; i++ {
  1264  		v := a[i&(N-1)]
  1265  		GCDUint16(uint16(v.a), uint16(v.b))
  1266  	}
  1267  }
  1268  
  1269  func BenchmarkGCDUint32(b *testing.B) {
  1270  	const N = 1 << 16
  1271  	type t uint32
  1272  	type u struct{ a, b t }
  1273  	b.StopTimer()
  1274  	rng := r32()
  1275  	a := make([]u, N)
  1276  	for i := range a {
  1277  		a[i] = u{t(rng.Next()), t(rng.Next())}
  1278  	}
  1279  	b.StartTimer()
  1280  	for i := 0; i < b.N; i++ {
  1281  		v := a[i&(N-1)]
  1282  		GCDUint32(uint32(v.a), uint32(v.b))
  1283  	}
  1284  }
  1285  
  1286  func BenchmarkGCDUint64(b *testing.B) {
  1287  	const N = 1 << 16
  1288  	type t uint64
  1289  	type u struct{ a, b t }
  1290  	b.StopTimer()
  1291  	rng := r64()
  1292  	a := make([]u, N)
  1293  	for i := range a {
  1294  		a[i] = u{t(rng.Next().Int64()), t(rng.Next().Int64())}
  1295  	}
  1296  	b.StartTimer()
  1297  	for i := 0; i < b.N; i++ {
  1298  		v := a[i&(N-1)]
  1299  		GCDUint64(uint64(v.a), uint64(v.b))
  1300  	}
  1301  }
  1302  
  1303  func BenchmarkLog2Byte(b *testing.B) {
  1304  	const N = 1 << 16
  1305  	type t byte
  1306  	b.StopTimer()
  1307  	rng := r32()
  1308  	a := make([]t, N)
  1309  	for i := range a {
  1310  		a[i] = t(rng.Next())
  1311  	}
  1312  	b.StartTimer()
  1313  	for i := 0; i < b.N; i++ {
  1314  		Log2Byte(byte(a[i&(N-1)]))
  1315  	}
  1316  }
  1317  
  1318  func BenchmarkLog2Uint16(b *testing.B) {
  1319  	const N = 1 << 16
  1320  	type t uint16
  1321  	b.StopTimer()
  1322  	rng := r32()
  1323  	a := make([]t, N)
  1324  	for i := range a {
  1325  		a[i] = t(rng.Next())
  1326  	}
  1327  	b.StartTimer()
  1328  	for i := 0; i < b.N; i++ {
  1329  		Log2Uint16(uint16(a[i&(N-1)]))
  1330  	}
  1331  }
  1332  
  1333  func BenchmarkLog2Uint32(b *testing.B) {
  1334  	const N = 1 << 16
  1335  	type t uint32
  1336  	b.StopTimer()
  1337  	rng := r32()
  1338  	a := make([]t, N)
  1339  	for i := range a {
  1340  		a[i] = t(rng.Next())
  1341  	}
  1342  	b.StartTimer()
  1343  	for i := 0; i < b.N; i++ {
  1344  		Log2Uint32(uint32(a[i&(N-1)]))
  1345  	}
  1346  }
  1347  
  1348  func BenchmarkLog2Uint64(b *testing.B) {
  1349  	const N = 1 << 16
  1350  	type t uint64
  1351  	b.StopTimer()
  1352  	rng := r64()
  1353  	a := make([]t, N)
  1354  	for i := range a {
  1355  		a[i] = t(rng.Next().Int64())
  1356  	}
  1357  	b.StartTimer()
  1358  	for i := 0; i < b.N; i++ {
  1359  		Log2Uint64(uint64(a[i&(N-1)]))
  1360  	}
  1361  }
  1362  func BenchmarkBitLenByte(b *testing.B) {
  1363  	const N = 1 << 16
  1364  	type t byte
  1365  	b.StopTimer()
  1366  	rng := r32()
  1367  	a := make([]t, N)
  1368  	for i := range a {
  1369  		a[i] = t(rng.Next())
  1370  	}
  1371  	b.StartTimer()
  1372  	for i := 0; i < b.N; i++ {
  1373  		BitLenByte(byte(a[i&(N-1)]))
  1374  	}
  1375  }
  1376  
  1377  func BenchmarkBitLenUint16(b *testing.B) {
  1378  	const N = 1 << 16
  1379  	type t uint16
  1380  	b.StopTimer()
  1381  	rng := r32()
  1382  	a := make([]t, N)
  1383  	for i := range a {
  1384  		a[i] = t(rng.Next())
  1385  	}
  1386  	b.StartTimer()
  1387  	for i := 0; i < b.N; i++ {
  1388  		BitLenUint16(uint16(a[i&(N-1)]))
  1389  	}
  1390  }
  1391  
  1392  func BenchmarkBitLenUint32(b *testing.B) {
  1393  	const N = 1 << 16
  1394  	type t uint32
  1395  	b.StopTimer()
  1396  	rng := r32()
  1397  	a := make([]t, N)
  1398  	for i := range a {
  1399  		a[i] = t(rng.Next())
  1400  	}
  1401  	b.StartTimer()
  1402  	for i := 0; i < b.N; i++ {
  1403  		BitLenUint32(uint32(a[i&(N-1)]))
  1404  	}
  1405  }
  1406  
  1407  func BenchmarkBitLen(b *testing.B) {
  1408  	const N = 1 << 16
  1409  	type t int
  1410  	b.StopTimer()
  1411  	rng := r64()
  1412  	a := make([]t, N)
  1413  	for i := range a {
  1414  		a[i] = t(rng.Next().Int64())
  1415  	}
  1416  	b.StartTimer()
  1417  	for i := 0; i < b.N; i++ {
  1418  		BitLen(int(a[i&(N-1)]))
  1419  	}
  1420  }
  1421  
  1422  func BenchmarkBitLenUint(b *testing.B) {
  1423  	const N = 1 << 16
  1424  	type t uint
  1425  	b.StopTimer()
  1426  	rng := r64()
  1427  	a := make([]t, N)
  1428  	for i := range a {
  1429  		a[i] = t(rng.Next().Int64())
  1430  	}
  1431  	b.StartTimer()
  1432  	for i := 0; i < b.N; i++ {
  1433  		BitLenUint(uint(a[i&(N-1)]))
  1434  	}
  1435  }
  1436  
  1437  func BenchmarkBitLenUintptr(b *testing.B) {
  1438  	const N = 1 << 16
  1439  	type t uintptr
  1440  	b.StopTimer()
  1441  	rng := r64()
  1442  	a := make([]t, N)
  1443  	for i := range a {
  1444  		a[i] = t(rng.Next().Int64())
  1445  	}
  1446  	b.StartTimer()
  1447  	for i := 0; i < b.N; i++ {
  1448  		BitLenUintptr(uintptr(a[i&(N-1)]))
  1449  	}
  1450  }
  1451  
  1452  func BenchmarkBitLenUint64(b *testing.B) {
  1453  	const N = 1 << 16
  1454  	type t uint64
  1455  	b.StopTimer()
  1456  	rng := r64()
  1457  	a := make([]t, N)
  1458  	for i := range a {
  1459  		a[i] = t(rng.Next().Int64())
  1460  	}
  1461  	b.StartTimer()
  1462  	for i := 0; i < b.N; i++ {
  1463  		BitLenUint64(uint64(a[i&(N-1)]))
  1464  	}
  1465  }
  1466  
  1467  func BenchmarkPopCountByte(b *testing.B) {
  1468  	const N = 1 << 16
  1469  	type t byte
  1470  	b.StopTimer()
  1471  	rng := r32()
  1472  	a := make([]t, N)
  1473  	for i := range a {
  1474  		a[i] = t(rng.Next())
  1475  	}
  1476  	b.StartTimer()
  1477  	for i := 0; i < b.N; i++ {
  1478  		PopCountByte(byte(a[i&(N-1)]))
  1479  	}
  1480  }
  1481  
  1482  func BenchmarkPopCountUint16(b *testing.B) {
  1483  	const N = 1 << 16
  1484  	type t uint16
  1485  	b.StopTimer()
  1486  	rng := r32()
  1487  	a := make([]t, N)
  1488  	for i := range a {
  1489  		a[i] = t(rng.Next())
  1490  	}
  1491  	b.StartTimer()
  1492  	for i := 0; i < b.N; i++ {
  1493  		PopCountUint16(uint16(a[i&(N-1)]))
  1494  	}
  1495  }
  1496  
  1497  func BenchmarkPopCountUint32(b *testing.B) {
  1498  	const N = 1 << 16
  1499  	type t uint32
  1500  	b.StopTimer()
  1501  	rng := r32()
  1502  	a := make([]t, N)
  1503  	for i := range a {
  1504  		a[i] = t(rng.Next())
  1505  	}
  1506  	b.StartTimer()
  1507  	for i := 0; i < b.N; i++ {
  1508  		PopCountUint32(uint32(a[i&(N-1)]))
  1509  	}
  1510  }
  1511  
  1512  func BenchmarkPopCount(b *testing.B) {
  1513  	const N = 1 << 16
  1514  	type t int
  1515  	b.StopTimer()
  1516  	rng := r64()
  1517  	a := make([]t, N)
  1518  	for i := range a {
  1519  		a[i] = t(rng.Next().Int64())
  1520  	}
  1521  	b.StartTimer()
  1522  	for i := 0; i < b.N; i++ {
  1523  		PopCount(int(a[i&(N-1)]))
  1524  	}
  1525  }
  1526  
  1527  func BenchmarkPopCountUint(b *testing.B) {
  1528  	const N = 1 << 16
  1529  	type t uint
  1530  	b.StopTimer()
  1531  	rng := r64()
  1532  	a := make([]t, N)
  1533  	for i := range a {
  1534  		a[i] = t(rng.Next().Int64())
  1535  	}
  1536  	b.StartTimer()
  1537  	for i := 0; i < b.N; i++ {
  1538  		PopCountUint(uint(a[i&(N-1)]))
  1539  	}
  1540  }
  1541  
  1542  func BenchmarkPopCountUintptr(b *testing.B) {
  1543  	const N = 1 << 16
  1544  	type t uintptr
  1545  	b.StopTimer()
  1546  	rng := r64()
  1547  	a := make([]t, N)
  1548  	for i := range a {
  1549  		a[i] = t(rng.Next().Int64())
  1550  	}
  1551  	b.StartTimer()
  1552  	for i := 0; i < b.N; i++ {
  1553  		PopCountUintptr(uintptr(a[i&(N-1)]))
  1554  	}
  1555  }
  1556  
  1557  func BenchmarkPopCountUint64(b *testing.B) {
  1558  	const N = 1 << 16
  1559  	type t uint64
  1560  	b.StopTimer()
  1561  	rng := r64()
  1562  	a := make([]t, N)
  1563  	for i := range a {
  1564  		a[i] = t(rng.Next().Int64())
  1565  	}
  1566  	b.StartTimer()
  1567  	for i := 0; i < b.N; i++ {
  1568  		PopCountUint64(uint64(a[i&(N-1)]))
  1569  	}
  1570  }
  1571  
  1572  func TestUintptrBits(t *testing.T) {
  1573  	switch g := UintptrBits(); g {
  1574  	case 32, 64:
  1575  		// ok
  1576  		t.Log(g)
  1577  	default:
  1578  		t.Fatalf("got %d, expected 32 or 64", g)
  1579  	}
  1580  }
  1581  
  1582  func BenchmarkUintptrBits(b *testing.B) {
  1583  	for i := 0; i < b.N; i++ {
  1584  		UintptrBits()
  1585  	}
  1586  }
  1587  
  1588  func TestModPowByte(t *testing.T) {
  1589  	data := []struct{ b, e, m, r byte }{
  1590  		{0, 1, 1, 0},
  1591  		{0, 2, 1, 0},
  1592  		{0, 3, 1, 0},
  1593  
  1594  		{1, 0, 1, 0},
  1595  		{1, 1, 1, 0},
  1596  		{1, 2, 1, 0},
  1597  		{1, 3, 1, 0},
  1598  
  1599  		{2, 0, 1, 0},
  1600  		{2, 1, 1, 0},
  1601  		{2, 2, 1, 0},
  1602  		{2, 3, 1, 0},
  1603  
  1604  		{2, 11, 23, 1}, // 23|M11
  1605  		{2, 11, 89, 1}, // 89|M11
  1606  		{2, 23, 47, 1}, // 47|M23
  1607  		{5, 3, 13, 8},
  1608  	}
  1609  
  1610  	for _, v := range data {
  1611  		if g, e := ModPowByte(v.b, v.e, v.m), v.r; g != e {
  1612  			t.Errorf("b %d e %d m %d: got %d, exp %d", v.b, v.e, v.m, g, e)
  1613  		}
  1614  	}
  1615  }
  1616  
  1617  func TestModPowUint16(t *testing.T) {
  1618  	data := []struct{ b, e, m, r uint16 }{
  1619  		{0, 1, 1, 0},
  1620  		{0, 2, 1, 0},
  1621  		{0, 3, 1, 0},
  1622  
  1623  		{1, 0, 1, 0},
  1624  		{1, 1, 1, 0},
  1625  		{1, 2, 1, 0},
  1626  		{1, 3, 1, 0},
  1627  
  1628  		{2, 0, 1, 0},
  1629  		{2, 1, 1, 0},
  1630  		{2, 2, 1, 0},
  1631  		{2, 3, 1, 0},
  1632  
  1633  		{2, 11, 23, 1},     // 23|M11
  1634  		{2, 11, 89, 1},     // 89|M11
  1635  		{2, 23, 47, 1},     // 47|M23
  1636  		{2, 929, 13007, 1}, // 13007|M929
  1637  		{4, 13, 497, 445},
  1638  		{5, 3, 13, 8},
  1639  	}
  1640  
  1641  	for _, v := range data {
  1642  		if g, e := ModPowUint16(v.b, v.e, v.m), v.r; g != e {
  1643  			t.Errorf("b %d e %d m %d: got %d, exp %d", v.b, v.e, v.m, g, e)
  1644  		}
  1645  	}
  1646  }
  1647  
  1648  func TestModPowUint32(t *testing.T) {
  1649  	data := []struct{ b, e, m, r uint32 }{
  1650  		{0, 1, 1, 0},
  1651  		{0, 2, 1, 0},
  1652  		{0, 3, 1, 0},
  1653  
  1654  		{1, 0, 1, 0},
  1655  		{1, 1, 1, 0},
  1656  		{1, 2, 1, 0},
  1657  		{1, 3, 1, 0},
  1658  
  1659  		{2, 0, 1, 0},
  1660  		{2, 1, 1, 0},
  1661  		{2, 2, 1, 0},
  1662  		{2, 3, 1, 0},
  1663  
  1664  		{2, 23, 47, 1},        // 47|M23
  1665  		{2, 67, 193707721, 1}, // 193707721|M67
  1666  		{2, 929, 13007, 1},    // 13007|M929
  1667  		{4, 13, 497, 445},
  1668  		{5, 3, 13, 8},
  1669  		{2, 500471, 264248689, 1},
  1670  		{2, 1000249, 112027889, 1},
  1671  		{2, 2000633, 252079759, 1},
  1672  		{2, 3000743, 222054983, 1},
  1673  		{2, 4000741, 1920355681, 1},
  1674  		{2, 5000551, 330036367, 1},
  1675  		{2, 6000479, 1020081431, 1},
  1676  		{2, 7000619, 840074281, 1},
  1677  		{2, 8000401, 624031279, 1},
  1678  		{2, 9000743, 378031207, 1},
  1679  		{2, 10000961, 380036519, 1},
  1680  		{2, 20000723, 40001447, 1},
  1681  	}
  1682  
  1683  	for _, v := range data {
  1684  		if g, e := ModPowUint32(v.b, v.e, v.m), v.r; g != e {
  1685  			t.Errorf("b %d e %d m %d: got %d, exp %d", v.b, v.e, v.m, g, e)
  1686  		}
  1687  	}
  1688  }
  1689  
  1690  func TestModPowUint64(t *testing.T) {
  1691  	data := []struct{ b, e, m, r uint64 }{
  1692  		{0, 1, 1, 0},
  1693  		{0, 2, 1, 0},
  1694  		{0, 3, 1, 0},
  1695  
  1696  		{1, 0, 1, 0},
  1697  		{1, 1, 1, 0},
  1698  		{1, 2, 1, 0},
  1699  		{1, 3, 1, 0},
  1700  
  1701  		{2, 0, 1, 0},
  1702  		{2, 1, 1, 0},
  1703  		{2, 2, 1, 0},
  1704  		{2, 3, 1, 0},
  1705  
  1706  		{2, 23, 47, 1},        // 47|M23
  1707  		{2, 67, 193707721, 1}, // 193707721|M67
  1708  		{2, 929, 13007, 1},    // 13007|M929
  1709  		{4, 13, 497, 445},
  1710  		{5, 3, 13, 8},
  1711  		{2, 500471, 264248689, 1}, // m|Me ...
  1712  		{2, 1000249, 112027889, 1},
  1713  		{2, 2000633, 252079759, 1},
  1714  		{2, 3000743, 222054983, 1},
  1715  		{2, 4000741, 1920355681, 1},
  1716  		{2, 5000551, 330036367, 1},
  1717  		{2, 6000479, 1020081431, 1},
  1718  		{2, 7000619, 840074281, 1},
  1719  		{2, 8000401, 624031279, 1},
  1720  		{2, 9000743, 378031207, 1},
  1721  		{2, 10000961, 380036519, 1},
  1722  		{2, 20000723, 40001447, 1},
  1723  		{2, 1000099, 1872347344039, 1},
  1724  
  1725  		{9223372036854775919, 9223372036854776030, 9223372036854776141, 7865333882915297658},
  1726  	}
  1727  
  1728  	for _, v := range data {
  1729  		if g, e := ModPowUint64(v.b, v.e, v.m), v.r; g != e {
  1730  			t.Errorf("b %d e %d m %d: got %d, exp %d", v.b, v.e, v.m, g, e)
  1731  		}
  1732  	}
  1733  }
  1734  
  1735  func TestModPowBigInt(t *testing.T) {
  1736  	data := []struct {
  1737  		b, e int64
  1738  		m    interface{}
  1739  		r    int64
  1740  	}{
  1741  		{0, 1, 1, 0},
  1742  		{0, 2, 1, 0},
  1743  		{0, 3, 1, 0},
  1744  
  1745  		{1, 0, 1, 0},
  1746  		{1, 1, 1, 0},
  1747  		{1, 2, 1, 0},
  1748  		{1, 3, 1, 0},
  1749  
  1750  		{2, 0, 1, 0},
  1751  		{2, 1, 1, 0},
  1752  		{2, 2, 1, 0},
  1753  		{2, 3, 1, 0},
  1754  
  1755  		{2, 23, 47, 1},        // 47|M23
  1756  		{2, 67, 193707721, 1}, // 193707721|M67
  1757  		{2, 929, 13007, 1},    // 13007|M929
  1758  		{4, 13, 497, 445},
  1759  		{5, 3, 13, 8},
  1760  		{2, 500471, 264248689, 1}, // m|Me ...
  1761  		{2, 1000249, 112027889, 1},
  1762  		{2, 2000633, 252079759, 1},
  1763  		{2, 3000743, 222054983, 1},
  1764  		{2, 4000741, 1920355681, 1},
  1765  		{2, 5000551, 330036367, 1},
  1766  		{2, 6000479, 1020081431, 1},
  1767  		{2, 7000619, 840074281, 1},
  1768  		{2, 8000401, 624031279, 1},
  1769  		{2, 9000743, 378031207, 1},
  1770  		{2, 10000961, 380036519, 1},
  1771  		{2, 20000723, 40001447, 1},
  1772  		{2, 100279, "11502865265922183403581252152383", 1},
  1773  
  1774  		{2, 7293457, "533975545077050000610542659519277030089249998649", 1},
  1775  	}
  1776  
  1777  	for _, v := range data {
  1778  		var m big.Int
  1779  		switch x := v.m.(type) {
  1780  		case int:
  1781  			m.SetInt64(int64(x))
  1782  		case string:
  1783  			m.SetString(x, 10)
  1784  		}
  1785  		b, e, r := big.NewInt(v.b), big.NewInt(v.e), big.NewInt(v.r)
  1786  		if g, e := ModPowBigInt(b, e, &m), r; g.Cmp(e) != 0 {
  1787  			t.Errorf("b %s e %s m %v: got %s, exp %s", b, e, m, g, e)
  1788  		}
  1789  	}
  1790  
  1791  	s := func(n string) *big.Int {
  1792  		i, ok := big.NewInt(0).SetString(n, 10)
  1793  		if !ok {
  1794  			t.Fatal(ok)
  1795  		}
  1796  
  1797  		return i
  1798  	}
  1799  
  1800  	if g, e := ModPowBigInt(
  1801  		s("36893488147419103343"), s("36893488147419103454"), s("36893488147419103565")), s("34853007610367449339"); g.Cmp(e) != 0 {
  1802  		t.Fatal(g, e)
  1803  	}
  1804  }
  1805  
  1806  func BenchmarkModPowByte(b *testing.B) {
  1807  	const N = 1 << 16
  1808  	b.StopTimer()
  1809  	type t struct{ b, e, m byte }
  1810  	a := make([]t, N)
  1811  	r := r32()
  1812  	for i := range a {
  1813  		a[i] = t{
  1814  			byte(r.Next() | 2),
  1815  			byte(r.Next() | 2),
  1816  			byte(r.Next() | 2),
  1817  		}
  1818  	}
  1819  	runtime.GC()
  1820  	b.StartTimer()
  1821  	for i := 0; i < b.N; i++ {
  1822  		v := a[i&(N-1)]
  1823  		ModPowByte(v.b, v.e, v.m)
  1824  	}
  1825  }
  1826  
  1827  func BenchmarkModPowUint16(b *testing.B) {
  1828  	const N = 1 << 16
  1829  	b.StopTimer()
  1830  	type t struct{ b, e, m uint16 }
  1831  	a := make([]t, N)
  1832  	r := r32()
  1833  	for i := range a {
  1834  		a[i] = t{
  1835  			uint16(r.Next() | 2),
  1836  			uint16(r.Next() | 2),
  1837  			uint16(r.Next() | 2),
  1838  		}
  1839  	}
  1840  	runtime.GC()
  1841  	b.StartTimer()
  1842  	for i := 0; i < b.N; i++ {
  1843  		v := a[i&(N-1)]
  1844  		ModPowUint16(v.b, v.e, v.m)
  1845  	}
  1846  }
  1847  
  1848  func BenchmarkModPowUint32(b *testing.B) {
  1849  	const N = 1 << 16
  1850  	b.StopTimer()
  1851  	type t struct{ b, e, m uint32 }
  1852  	a := make([]t, N)
  1853  	r := r32()
  1854  	for i := range a {
  1855  		a[i] = t{
  1856  			uint32(r.Next() | 2),
  1857  			uint32(r.Next() | 2),
  1858  			uint32(r.Next() | 2),
  1859  		}
  1860  	}
  1861  	runtime.GC()
  1862  	b.StartTimer()
  1863  	for i := 0; i < b.N; i++ {
  1864  		v := a[i&(N-1)]
  1865  		ModPowUint32(v.b, v.e, v.m)
  1866  	}
  1867  }
  1868  
  1869  func BenchmarkModPowUint64(b *testing.B) {
  1870  	const N = 1 << 16
  1871  	b.StopTimer()
  1872  	type t struct{ b, e, m uint64 }
  1873  	a := make([]t, N)
  1874  	r := r64()
  1875  	for i := range a {
  1876  		a[i] = t{
  1877  			uint64(r.Next().Int64() | 2),
  1878  			uint64(r.Next().Int64() | 2),
  1879  			uint64(r.Next().Int64() | 2),
  1880  		}
  1881  	}
  1882  	runtime.GC()
  1883  	b.StartTimer()
  1884  	for i := 0; i < b.N; i++ {
  1885  		v := a[i&(N-1)]
  1886  		ModPowUint64(v.b, v.e, v.m)
  1887  	}
  1888  }
  1889  
  1890  func BenchmarkModPowBigInt(b *testing.B) {
  1891  	const N = 1 << 16
  1892  	b.StopTimer()
  1893  	type t struct{ b, e, m *big.Int }
  1894  	a := make([]t, N)
  1895  	mx := big.NewInt(math.MaxInt64)
  1896  	mx.Mul(mx, mx)
  1897  	r, err := NewFCBig(big.NewInt(2), mx, true)
  1898  	if err != nil {
  1899  		b.Fatal(err)
  1900  	}
  1901  	for i := range a {
  1902  		a[i] = t{
  1903  			r.Next(),
  1904  			r.Next(),
  1905  			r.Next(),
  1906  		}
  1907  	}
  1908  	runtime.GC()
  1909  	b.StartTimer()
  1910  	for i := 0; i < b.N; i++ {
  1911  		v := a[i&(N-1)]
  1912  		ModPowBigInt(v.b, v.e, v.m)
  1913  	}
  1914  }
  1915  
  1916  func TestAdd128(t *testing.T) {
  1917  	const N = 1e5
  1918  	r := r64()
  1919  	var mm big.Int
  1920  	for i := 0; i < N; i++ {
  1921  		a, b := uint64(r.Next().Int64()), uint64(r.Next().Int64())
  1922  		aa, bb := big.NewInt(0).SetUint64(a), big.NewInt(0).SetUint64(b)
  1923  		mhi, mlo := AddUint128_64(a, b)
  1924  		m := big.NewInt(0).SetUint64(mhi)
  1925  		m.Lsh(m, 64)
  1926  		m.Add(m, big.NewInt(0).SetUint64(mlo))
  1927  		mm.Add(aa, bb)
  1928  		if m.Cmp(&mm) != 0 {
  1929  			t.Fatalf("%d\na %40d\nb %40d\ng %40s %032x\ne %40s %032x", i, a, b, m, m, &mm, &mm)
  1930  		}
  1931  	}
  1932  }
  1933  
  1934  func TestMul128(t *testing.T) {
  1935  	const N = 1e5
  1936  	r := r64()
  1937  	var mm big.Int
  1938  	f := func(a, b uint64) {
  1939  		aa, bb := big.NewInt(0).SetUint64(a), big.NewInt(0).SetUint64(b)
  1940  		mhi, mlo := MulUint128_64(a, b)
  1941  		m := big.NewInt(0).SetUint64(mhi)
  1942  		m.Lsh(m, 64)
  1943  		m.Add(m, big.NewInt(0).SetUint64(mlo))
  1944  		mm.Mul(aa, bb)
  1945  		if m.Cmp(&mm) != 0 {
  1946  			t.Fatalf("\na %40d\nb %40d\ng %40s %032x\ne %40s %032x", a, b, m, m, &mm, &mm)
  1947  		}
  1948  	}
  1949  	for i := 0; i < N; i++ {
  1950  		f(uint64(r.Next().Int64()), uint64(r.Next().Int64()))
  1951  	}
  1952  	for x := 0; x <= 1<<9; x++ {
  1953  		for y := 0; y <= 1<<9; y++ {
  1954  			f(math.MaxUint64-uint64(x), math.MaxUint64-uint64(y))
  1955  		}
  1956  	}
  1957  }
  1958  
  1959  func BenchmarkMul128(b *testing.B) {
  1960  	const N = 1 << 16
  1961  	b.StopTimer()
  1962  	type t struct{ a, b uint64 }
  1963  	a := make([]t, N)
  1964  	r := r64()
  1965  	for i := range a {
  1966  		a[i] = t{
  1967  			uint64(r.Next().Int64()),
  1968  			uint64(r.Next().Int64()),
  1969  		}
  1970  	}
  1971  	runtime.GC()
  1972  	b.StartTimer()
  1973  	for i := 0; i < b.N; i++ {
  1974  		v := a[i&(N-1)]
  1975  		MulUint128_64(v.a, v.b)
  1976  	}
  1977  }
  1978  
  1979  func BenchmarkMul128Big(b *testing.B) {
  1980  	const N = 1 << 16
  1981  	b.StopTimer()
  1982  	type t struct{ a, b *big.Int }
  1983  	a := make([]t, N)
  1984  	r := r64()
  1985  	for i := range a {
  1986  		a[i] = t{
  1987  			big.NewInt(r.Next().Int64()),
  1988  			big.NewInt(r.Next().Int64()),
  1989  		}
  1990  	}
  1991  	var x big.Int
  1992  	runtime.GC()
  1993  	b.StartTimer()
  1994  	for i := 0; i < b.N; i++ {
  1995  		v := a[i&(N-1)]
  1996  		x.Mul(v.a, v.b)
  1997  	}
  1998  }
  1999  
  2000  func TestIsPrimeUint64(t *testing.T) {
  2001  	f := func(lo, hi uint64, exp int) {
  2002  		got := 0
  2003  		for n := lo; n <= hi; {
  2004  			if IsPrimeUint64(n) {
  2005  				got++
  2006  			}
  2007  			n0 := n
  2008  			n++
  2009  			if n < n0 {
  2010  				break
  2011  			}
  2012  		}
  2013  		if got != exp {
  2014  			t.Fatal(lo, hi, got, exp)
  2015  		}
  2016  	}
  2017  
  2018  	// lo, hi, PrimePi(hi)-PrimePi(lo)
  2019  	f(0, 1e4, 1229)
  2020  	f(1e5, 1e5+1e4, 861)
  2021  	f(1e6, 1e6+1e4, 753)
  2022  	f(1e7, 1e7+1e4, 614)
  2023  	f(1e8, 1e8+1e4, 551)
  2024  	f(1e9, 1e9+1e4, 487)
  2025  	f(1e10, 1e10+1e4, 406)
  2026  	f(1e11, 1e11+1e4, 394)
  2027  	f(1e12, 1e12+1e4, 335)
  2028  	f(1e13, 1e13+1e4, 354)
  2029  	f(1e14, 1e14+1e4, 304)
  2030  	f(1e15, 1e15+1e4, 263)
  2031  	f(1e16, 1e16+1e4, 270)
  2032  	f(1e17, 1e17+1e4, 265)
  2033  	f(1e18, 1e18+1e4, 241)
  2034  	f(1e19, 1e19+1e4, 255)
  2035  	f(1<<64-1e4, 1<<64-1, 218)
  2036  }
  2037  
  2038  func TestProbablyPrimeUint32(t *testing.T) {
  2039  	f := func(n, firstFail uint32, primes []uint32) {
  2040  		for ; n <= firstFail; n += 2 {
  2041  			prp := true
  2042  			for _, a := range primes {
  2043  				if !ProbablyPrimeUint32(n, a) {
  2044  					prp = false
  2045  					break
  2046  				}
  2047  			}
  2048  			if prp != IsPrime(n) && n != firstFail {
  2049  				t.Fatal(n)
  2050  			}
  2051  		}
  2052  	}
  2053  	if !ProbablyPrimeUint32(5, 2) {
  2054  		t.Fatal(false)
  2055  	}
  2056  	if !ProbablyPrimeUint32(7, 2) {
  2057  		t.Fatal(false)
  2058  	}
  2059  	if ProbablyPrimeUint32(9, 2) {
  2060  		t.Fatal(true)
  2061  	}
  2062  	if !ProbablyPrimeUint32(11, 2) {
  2063  		t.Fatal(false)
  2064  	}
  2065  	// http://oeis.org/A014233
  2066  	f(5, 2047, []uint32{2})
  2067  	f(2047, 1373653, []uint32{2, 3})
  2068  	f(1373653, 25326001, []uint32{2, 3, 5})
  2069  }
  2070  
  2071  func BenchmarkProbablyPrimeUint32(b *testing.B) {
  2072  	const N = 1 << 16
  2073  	b.StopTimer()
  2074  	type t struct{ n, a uint32 }
  2075  	data := make([]t, N)
  2076  	r := r32()
  2077  	for i := range data {
  2078  		n := uint32(r.Next()) | 1
  2079  		if n <= 3 {
  2080  			n += 5
  2081  		}
  2082  		a := uint32(r.Next())
  2083  		if a <= 1 {
  2084  			a += 2
  2085  		}
  2086  		data[i] = t{n, a}
  2087  	}
  2088  	runtime.GC()
  2089  	b.StartTimer()
  2090  	for i := 0; i < b.N; i++ {
  2091  		v := data[i&(N-1)]
  2092  		ProbablyPrimeUint32(v.n, v.a)
  2093  	}
  2094  }
  2095  
  2096  func TestProbablyPrimeUint64_32(t *testing.T) {
  2097  	f := func(n, firstFail uint64, primes []uint32) {
  2098  		for ; n <= firstFail; n += 2 {
  2099  			prp := true
  2100  			for _, a := range primes {
  2101  				if !ProbablyPrimeUint64_32(n, a) {
  2102  					prp = false
  2103  					break
  2104  				}
  2105  			}
  2106  			if prp != IsPrimeUint64(n) && n != firstFail {
  2107  				t.Fatal(n)
  2108  			}
  2109  		}
  2110  	}
  2111  	if !ProbablyPrimeUint64_32(5, 2) {
  2112  		t.Fatal(false)
  2113  	}
  2114  	if !ProbablyPrimeUint64_32(7, 2) {
  2115  		t.Fatal(false)
  2116  	}
  2117  	if ProbablyPrimeUint64_32(9, 2) {
  2118  		t.Fatal(true)
  2119  	}
  2120  	if !ProbablyPrimeUint64_32(11, 2) {
  2121  		t.Fatal(false)
  2122  	}
  2123  	// http://oeis.org/A014233
  2124  	f(5, 2047, []uint32{2})
  2125  	f(2047, 1373653, []uint32{2, 3})
  2126  }
  2127  
  2128  func BenchmarkProbablyPrimeUint64_32(b *testing.B) {
  2129  	const N = 1 << 16
  2130  	b.StopTimer()
  2131  	type t struct {
  2132  		n uint64
  2133  		a uint32
  2134  	}
  2135  	data := make([]t, N)
  2136  	r := r32()
  2137  	r2 := r64()
  2138  	for i := range data {
  2139  		var n uint64
  2140  		for n <= 3 {
  2141  			n = uint64(r2.Next().Int64()) | 1
  2142  		}
  2143  		var a uint32
  2144  		for a <= 1 {
  2145  			a = uint32(r.Next())
  2146  		}
  2147  		data[i] = t{n, a}
  2148  	}
  2149  	runtime.GC()
  2150  	b.StartTimer()
  2151  	for i := 0; i < b.N; i++ {
  2152  		v := data[i&(N-1)]
  2153  		ProbablyPrimeUint64_32(v.n, v.a)
  2154  	}
  2155  }
  2156  
  2157  func TestProbablyPrimeBigInt_32(t *testing.T) {
  2158  	f := func(n0, firstFail0 uint64, primes []uint32) {
  2159  		n, firstFail := big.NewInt(0).SetUint64(n0), big.NewInt(0).SetUint64(firstFail0)
  2160  		for ; n.Cmp(firstFail) <= 0; n.Add(n, _2) {
  2161  			prp := true
  2162  			for _, a := range primes {
  2163  				if !ProbablyPrimeBigInt_32(n, a) {
  2164  					prp = false
  2165  					break
  2166  				}
  2167  			}
  2168  			if prp != IsPrimeUint64(n0) && n0 != firstFail0 {
  2169  				t.Fatal(n)
  2170  			}
  2171  			n0 += 2
  2172  		}
  2173  	}
  2174  	if !ProbablyPrimeBigInt_32(big.NewInt(5), 2) {
  2175  		t.Fatal(false)
  2176  	}
  2177  	if !ProbablyPrimeBigInt_32(big.NewInt(7), 2) {
  2178  		t.Fatal(false)
  2179  	}
  2180  	if ProbablyPrimeBigInt_32(big.NewInt(9), 2) {
  2181  		t.Fatal(true)
  2182  	}
  2183  	if !ProbablyPrimeBigInt_32(big.NewInt(11), 2) {
  2184  		t.Fatal(false)
  2185  	}
  2186  	// http://oeis.org/A014233
  2187  	f(5, 2047, []uint32{2})
  2188  	f(2047, 1373653, []uint32{2, 3})
  2189  }
  2190  
  2191  func BenchmarkProbablyPrimeBigInt_32(b *testing.B) {
  2192  	const N = 1 << 16
  2193  	b.StopTimer()
  2194  	type t struct {
  2195  		n *big.Int
  2196  		a uint32
  2197  	}
  2198  	data := make([]t, N)
  2199  	r := r32()
  2200  	r2 := r64()
  2201  	for i := range data {
  2202  		var n uint64
  2203  		for n <= 3 {
  2204  			n = uint64(r2.Next().Int64()) | 1
  2205  		}
  2206  		var a uint32
  2207  		for a <= 1 {
  2208  			a = uint32(r.Next())
  2209  		}
  2210  		data[i] = t{big.NewInt(0).SetUint64(n), a}
  2211  	}
  2212  	runtime.GC()
  2213  	b.StartTimer()
  2214  	for i := 0; i < b.N; i++ {
  2215  		v := data[i&(N-1)]
  2216  		ProbablyPrimeBigInt_32(v.n, v.a)
  2217  	}
  2218  }
  2219  
  2220  func TestProbablyPrimeBigInt(t *testing.T) {
  2221  	f := func(n0, firstFail0 uint64, primes []uint32) {
  2222  		n, firstFail := big.NewInt(0).SetUint64(n0), big.NewInt(0).SetUint64(firstFail0)
  2223  		for ; n.Cmp(firstFail) <= 0; n.Add(n, _2) {
  2224  			prp := true
  2225  			var a big.Int
  2226  			for _, a0 := range primes {
  2227  				a.SetInt64(int64(a0))
  2228  				if !ProbablyPrimeBigInt(n, &a) {
  2229  					prp = false
  2230  					break
  2231  				}
  2232  			}
  2233  			if prp != IsPrimeUint64(n0) && n0 != firstFail0 {
  2234  				t.Fatal(n)
  2235  			}
  2236  			n0 += 2
  2237  		}
  2238  	}
  2239  	if !ProbablyPrimeBigInt(big.NewInt(5), _2) {
  2240  		t.Fatal(false)
  2241  	}
  2242  	if !ProbablyPrimeBigInt(big.NewInt(7), _2) {
  2243  		t.Fatal(false)
  2244  	}
  2245  	if ProbablyPrimeBigInt(big.NewInt(9), _2) {
  2246  		t.Fatal(true)
  2247  	}
  2248  	if !ProbablyPrimeBigInt(big.NewInt(11), _2) {
  2249  		t.Fatal(false)
  2250  	}
  2251  	// http://oeis.org/A014233
  2252  	f(5, 2047, []uint32{2})
  2253  	f(2047, 1373653, []uint32{2, 3})
  2254  }
  2255  
  2256  var once2059 sync.Once
  2257  
  2258  func BenchmarkProbablyPrimeBigInt64(b *testing.B) {
  2259  	const N = 1 << 16
  2260  	b.StopTimer()
  2261  	once2059.Do(func() { b.Log("64 bit n, 64 bit a\n") })
  2262  	type t struct {
  2263  		n, a *big.Int
  2264  	}
  2265  	data := make([]t, N)
  2266  	r := r64()
  2267  	for i := range data {
  2268  		var n uint64
  2269  		for n <= 3 {
  2270  			n = uint64(r.Next().Int64()) | 1
  2271  		}
  2272  		var a uint64
  2273  		for a <= 1 {
  2274  			a = uint64(r.Next().Int64())
  2275  		}
  2276  		data[i] = t{big.NewInt(0).SetUint64(n), big.NewInt(0).SetUint64(a)}
  2277  	}
  2278  	runtime.GC()
  2279  	b.StartTimer()
  2280  	for i := 0; i < b.N; i++ {
  2281  		v := data[i&(N-1)]
  2282  		ProbablyPrimeBigInt(v.n, v.a)
  2283  	}
  2284  }
  2285  
  2286  var once2090 sync.Once
  2287  
  2288  func BenchmarkProbablyPrimeBigInt128(b *testing.B) {
  2289  	const N = 1 << 16
  2290  	b.StopTimer()
  2291  	once2090.Do(func() { b.Log("128 bit n, 128 bit a\n") })
  2292  	type t struct {
  2293  		n, a *big.Int
  2294  	}
  2295  	data := make([]t, N)
  2296  	r := r64()
  2297  	for i := range data {
  2298  		n := big.NewInt(0).SetUint64(uint64(r.Next().Int64()))
  2299  		n.Lsh(n, 64)
  2300  		n.Add(n, big.NewInt(0).SetUint64(uint64(r.Next().Int64())|1))
  2301  		a := big.NewInt(0).SetUint64(uint64(r.Next().Int64()))
  2302  		a.Lsh(a, 64)
  2303  		a.Add(a, big.NewInt(0).SetUint64(uint64(r.Next().Int64())))
  2304  		data[i] = t{n, a}
  2305  	}
  2306  	runtime.GC()
  2307  	b.StartTimer()
  2308  	for i := 0; i < b.N; i++ {
  2309  		v := data[i&(N-1)]
  2310  		ProbablyPrimeBigInt(v.n, v.a)
  2311  	}
  2312  }
  2313  
  2314  func TestQCmpUint32(t *testing.T) {
  2315  	const N = 6e4
  2316  	r := r32()
  2317  	var x, y big.Rat
  2318  	for i := 0; i < N; i++ {
  2319  		a, b, c, d := uint32(r.Next()), uint32(r.Next()), uint32(r.Next()), uint32(r.Next())
  2320  		x.SetFrac64(int64(a), int64(b))
  2321  		y.SetFrac64(int64(c), int64(d))
  2322  		if g, e := QCmpUint32(a, b, c, d), x.Cmp(&y); g != e {
  2323  			t.Fatal(a, b, c, d, g, e)
  2324  		}
  2325  	}
  2326  }
  2327  
  2328  func TestQScaleUint32(t *testing.T) {
  2329  	const N = 4e4
  2330  	r := r32()
  2331  	var x, y big.Rat
  2332  	var a uint64
  2333  	var b, c, d uint32
  2334  	for i := 0; i < N; i++ {
  2335  		for {
  2336  			b, c, d = uint32(r.Next()), uint32(r.Next()), uint32(r.Next())
  2337  			a = QScaleUint32(b, c, d)
  2338  			if a <= math.MaxInt64 {
  2339  				break
  2340  			}
  2341  		}
  2342  		x.SetFrac64(int64(a), int64(b))
  2343  		y.SetFrac64(int64(c), int64(d))
  2344  		if g := x.Cmp(&y); g < 0 {
  2345  			t.Fatal(a, b, c, d, g, "expexted 1 or 0")
  2346  		}
  2347  
  2348  		if a != 0 {
  2349  			x.SetFrac64(int64(a-1), int64(b))
  2350  			if g := x.Cmp(&y); g > 0 {
  2351  				t.Fatal(a, b, c, d, g, "expected -1 or 0")
  2352  			}
  2353  		}
  2354  	}
  2355  }
  2356  
  2357  var smalls = []uint32{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
  2358  
  2359  func isPrimorialProduct(t FactorTerms, maxp uint32) bool {
  2360  	if len(t) == 0 {
  2361  		return false
  2362  	}
  2363  
  2364  	pmax := uint32(32)
  2365  	for i, v := range t {
  2366  		if v.Prime != smalls[i] || v.Power > pmax || v.Power > maxp {
  2367  			return false
  2368  		}
  2369  		pmax = v.Power
  2370  	}
  2371  	return true
  2372  }
  2373  
  2374  func TestPrimorialProductsUint32(t *testing.T) {
  2375  	r := PrimorialProductsUint32(2*3*5*7*11*13*17*19+1, math.MaxUint32, 1)
  2376  	if len(r) != 1 {
  2377  		t.Fatal(len(r))
  2378  	}
  2379  
  2380  	if r[0] != 2*3*5*7*11*13*17*19*23 {
  2381  		t.Fatal(r[0])
  2382  	}
  2383  
  2384  	r = PrimorialProductsUint32(0, math.MaxUint32, math.MaxUint32)
  2385  	if g, e := len(r), 1679; g != e {
  2386  		t.Fatal(g, e)
  2387  	}
  2388  
  2389  	m := map[uint32]struct{}{}
  2390  	for _, v := range r {
  2391  		if _, ok := m[v]; ok {
  2392  			t.Fatal(v)
  2393  		}
  2394  
  2395  		m[v] = struct{}{}
  2396  	}
  2397  
  2398  	for lo := uint32(0); lo < 5e4; lo += 1e3 {
  2399  		hi := 1e2 * lo
  2400  		for max := uint32(0); max <= 32; max++ {
  2401  			m := map[uint32]struct{}{}
  2402  			for i, v := range PrimorialProductsUint32(lo, hi, max) {
  2403  				f := FactorInt(v)
  2404  				if v < lo || v > hi {
  2405  					t.Fatal(lo, hi, max, v)
  2406  				}
  2407  
  2408  				if _, ok := m[v]; ok {
  2409  					t.Fatal(i, lo, hi, max, v, f)
  2410  				}
  2411  
  2412  				m[v] = struct{}{}
  2413  				if !isPrimorialProduct(f, max) {
  2414  					t.Fatal(i, v)
  2415  				}
  2416  
  2417  				for _, v := range f {
  2418  					if v.Power > max {
  2419  						t.Fatal(i, v, f)
  2420  					}
  2421  				}
  2422  			}
  2423  		}
  2424  	}
  2425  }
  2426  
  2427  func BenchmarkPrimorialProductsUint32(b *testing.B) {
  2428  	for i := 0; i < b.N; i++ {
  2429  		PrimorialProductsUint32(0, math.MaxUint32, math.MaxUint32)
  2430  	}
  2431  }
  2432  
  2433  func powerizeUint32BigInt(b uint32, n *big.Int) (e uint32, p *big.Int) {
  2434  	p = big.NewInt(1)
  2435  	bb := big.NewInt(int64(b))
  2436  	for p.Cmp(n) < 0 {
  2437  		p.Mul(p, bb)
  2438  		e++
  2439  	}
  2440  	return
  2441  }
  2442  
  2443  func TestPowerizeUint32BigInt(t *testing.T) {
  2444  	var data = []struct{ b, n, e, p int }{
  2445  		{0, 10, 0, -1},
  2446  		{1, 10, 0, -1},
  2447  		{2, -1, 0, -1},
  2448  		{2, 0, 0, 1},
  2449  		{2, 1, 0, 1},
  2450  		{2, 2, 1, 2},
  2451  		{2, 3, 2, 4},
  2452  		{3, 0, 0, 1},
  2453  		{3, 1, 0, 1},
  2454  		{3, 2, 1, 3},
  2455  		{3, 3, 1, 3},
  2456  		{3, 4, 2, 9},
  2457  		{3, 8, 2, 9},
  2458  		{3, 9, 2, 9},
  2459  		{3, 10, 3, 27},
  2460  		{3, 80, 4, 81},
  2461  	}
  2462  
  2463  	var n big.Int
  2464  	for _, v := range data {
  2465  		b := v.b
  2466  		n.SetInt64(int64(v.n))
  2467  		e, p := PowerizeUint32BigInt(uint32(b), &n)
  2468  		if e != uint32(v.e) {
  2469  			t.Fatal(b, &n, e, p, v.e, v.p)
  2470  		}
  2471  
  2472  		if v.p < 0 {
  2473  			if p != nil {
  2474  				t.Fatal(b, &n, e, p, v.e, v.p)
  2475  			}
  2476  			continue
  2477  		}
  2478  
  2479  		if p.Int64() != int64(v.p) {
  2480  			t.Fatal(b, &n, e, p, v.e, v.p)
  2481  		}
  2482  	}
  2483  	const N = 1e5
  2484  	var nn big.Int
  2485  	for _, base := range []uint32{2, 3, 15, 17} {
  2486  		for n := 0; n <= N; n++ {
  2487  			nn.SetInt64(int64(n))
  2488  			ge, gp := PowerizeUint32BigInt(base, &nn)
  2489  			ee, ep := powerizeUint32BigInt(base, &nn)
  2490  			if ge != ee || gp.Cmp(ep) != 0 {
  2491  				t.Fatal(base, n, ge, gp, ee, ep)
  2492  			}
  2493  
  2494  			gp.Div(gp, big.NewInt(int64(base)))
  2495  			if gp.Sign() > 0 && gp.Cmp(&nn) >= 0 {
  2496  				t.Fatal(gp.Sign(), gp.Cmp(&nn))
  2497  			}
  2498  		}
  2499  	}
  2500  }
  2501  
  2502  func benchmarkPowerizeUint32BigInt(b *testing.B, base uint32, exp int) {
  2503  	b.StopTimer()
  2504  	var n big.Int
  2505  	n.SetBit(&n, exp, 1)
  2506  	b.StartTimer()
  2507  	for i := 0; i < b.N; i++ {
  2508  		PowerizeUint32BigInt(base, &n)
  2509  	}
  2510  }
  2511  
  2512  func BenchmarkPowerizeUint32BigInt_2_2e1e1(b *testing.B) {
  2513  	benchmarkPowerizeUint32BigInt(b, 2, 1e1)
  2514  }
  2515  
  2516  func BenchmarkPowerizeUint32BigInt_2_2e1e2(b *testing.B) {
  2517  	benchmarkPowerizeUint32BigInt(b, 2, 1e2)
  2518  }
  2519  
  2520  func BenchmarkPowerizeUint32BigInt_2_2e1e3(b *testing.B) {
  2521  	benchmarkPowerizeUint32BigInt(b, 2, 1e3)
  2522  }
  2523  
  2524  func BenchmarkPowerizeUint32BigInt_2_2e1e4(b *testing.B) {
  2525  	benchmarkPowerizeUint32BigInt(b, 2, 1e4)
  2526  }
  2527  
  2528  func BenchmarkPowerizeUint32BigInt_2_2e1e5(b *testing.B) {
  2529  	benchmarkPowerizeUint32BigInt(b, 2, 1e5)
  2530  }
  2531  
  2532  func BenchmarkPowerizeUint32BigInt_2_2e1e6(b *testing.B) {
  2533  	benchmarkPowerizeUint32BigInt(b, 2, 1e6)
  2534  }
  2535  
  2536  func BenchmarkPowerizeUint32BigInt_2_2e1e7(b *testing.B) {
  2537  	benchmarkPowerizeUint32BigInt(b, 2, 1e7)
  2538  }
  2539  
  2540  func BenchmarkPowerizeUint32BigInt_3_2e1e1(b *testing.B) {
  2541  	benchmarkPowerizeUint32BigInt(b, 3, 1e1)
  2542  }
  2543  
  2544  func BenchmarkPowerizeUint32BigInt_3_2e1e2(b *testing.B) {
  2545  	benchmarkPowerizeUint32BigInt(b, 3, 1e2)
  2546  }
  2547  
  2548  func BenchmarkPowerizeUint32BigInt_3_2e1e3(b *testing.B) {
  2549  	benchmarkPowerizeUint32BigInt(b, 3, 1e3)
  2550  }
  2551  
  2552  func BenchmarkPowerizeUint32BigInt_3_2e1e4(b *testing.B) {
  2553  	benchmarkPowerizeUint32BigInt(b, 3, 1e4)
  2554  }
  2555  
  2556  func BenchmarkPowerizeUint32BigInt_3_2e1e5(b *testing.B) {
  2557  	benchmarkPowerizeUint32BigInt(b, 3, 1e5)
  2558  }
  2559  
  2560  func BenchmarkPowerizeUint32BigInt_3_2e1e6(b *testing.B) {
  2561  	benchmarkPowerizeUint32BigInt(b, 3, 1e6)
  2562  }
  2563  
  2564  func BenchmarkPowerizeUint32BigInt_15_2e1e1(b *testing.B) {
  2565  	benchmarkPowerizeUint32BigInt(b, 15, 1e1)
  2566  }
  2567  
  2568  func BenchmarkPowerizeUint32BigInt_15_2e1e2(b *testing.B) {
  2569  	benchmarkPowerizeUint32BigInt(b, 15, 1e2)
  2570  }
  2571  
  2572  func BenchmarkPowerizeUint32BigInt_15_2e1e3(b *testing.B) {
  2573  	benchmarkPowerizeUint32BigInt(b, 15, 1e3)
  2574  }
  2575  
  2576  func BenchmarkPowerizeUint32BigInt_15_2e1e4(b *testing.B) {
  2577  	benchmarkPowerizeUint32BigInt(b, 15, 1e4)
  2578  }
  2579  
  2580  func BenchmarkPowerizeUint32BigInt_15_2e1e5(b *testing.B) {
  2581  	benchmarkPowerizeUint32BigInt(b, 15, 1e5)
  2582  }
  2583  
  2584  func BenchmarkPowerizeUint32BigInt_15_2e1e6(b *testing.B) {
  2585  	benchmarkPowerizeUint32BigInt(b, 15, 1e6)
  2586  }
  2587  
  2588  func BenchmarkPowerizeUint32BigInt_17_2e1e1(b *testing.B) {
  2589  	benchmarkPowerizeUint32BigInt(b, 17, 1e1)
  2590  }
  2591  
  2592  func BenchmarkPowerizeUint32BigInt_17_2e1e2(b *testing.B) {
  2593  	benchmarkPowerizeUint32BigInt(b, 17, 1e2)
  2594  }
  2595  
  2596  func BenchmarkPowerizeUint32BigInt_17_2e1e3(b *testing.B) {
  2597  	benchmarkPowerizeUint32BigInt(b, 17, 1e3)
  2598  }
  2599  
  2600  func BenchmarkPowerizeUint32BigInt_17_2e1e4(b *testing.B) {
  2601  	benchmarkPowerizeUint32BigInt(b, 17, 1e4)
  2602  }
  2603  
  2604  func BenchmarkPowerizeUint32BigInt_17_2e1e5(b *testing.B) {
  2605  	benchmarkPowerizeUint32BigInt(b, 17, 1e5)
  2606  }
  2607  
  2608  func BenchmarkPowerizeUint32BigInt_17_2e1e6(b *testing.B) {
  2609  	benchmarkPowerizeUint32BigInt(b, 17, 1e6)
  2610  }
  2611  
  2612  func TestPowerizeBigInt(t *testing.T) {
  2613  	var data = []struct{ b, n, e, p int }{
  2614  		{0, 10, 0, -1},
  2615  		{1, 10, 0, -1},
  2616  		{2, -1, 0, -1},
  2617  		{2, 0, 0, 1},
  2618  		{2, 1, 0, 1},
  2619  		{2, 2, 1, 2},
  2620  		{2, 3, 2, 4},
  2621  		{3, 0, 0, 1},
  2622  		{3, 1, 0, 1},
  2623  		{3, 2, 1, 3},
  2624  		{3, 3, 1, 3},
  2625  		{3, 4, 2, 9},
  2626  		{3, 8, 2, 9},
  2627  		{3, 9, 2, 9},
  2628  		{3, 10, 3, 27},
  2629  		{3, 80, 4, 81},
  2630  	}
  2631  
  2632  	var b, n big.Int
  2633  	for _, v := range data {
  2634  		b.SetInt64(int64(v.b))
  2635  		n.SetInt64(int64(v.n))
  2636  		e, p := PowerizeBigInt(&b, &n)
  2637  		if e != uint32(v.e) {
  2638  			t.Fatal(&b, &n, e, p, v.e, v.p)
  2639  		}
  2640  
  2641  		if v.p < 0 {
  2642  			if p != nil {
  2643  				t.Fatal(&b, &n, e, p, v.e, v.p)
  2644  			}
  2645  			continue
  2646  		}
  2647  
  2648  		if p.Int64() != int64(v.p) {
  2649  			t.Fatal(&b, &n, e, p, v.e, v.p)
  2650  		}
  2651  	}
  2652  	const N = 1e5
  2653  	var nn big.Int
  2654  	for _, base := range []uint32{2, 3, 15, 17} {
  2655  		b.SetInt64(int64(base))
  2656  		for n := 0; n <= N; n++ {
  2657  			nn.SetInt64(int64(n))
  2658  			ge, gp := PowerizeBigInt(&b, &nn)
  2659  			ee, ep := powerizeUint32BigInt(base, &nn)
  2660  			if ge != ee || gp.Cmp(ep) != 0 {
  2661  				t.Fatal(base, n, ge, gp, ee, ep)
  2662  			}
  2663  
  2664  			gp.Div(gp, &b)
  2665  			if gp.Sign() > 0 && gp.Cmp(&nn) >= 0 {
  2666  				t.Fatal(gp.Sign(), gp.Cmp(&nn))
  2667  			}
  2668  		}
  2669  	}
  2670  }
  2671  
  2672  func benchmarkPowerizeBigInt(b *testing.B, base uint32, exp int) {
  2673  	b.StopTimer()
  2674  	var bb, n big.Int
  2675  	n.SetBit(&n, exp, 1)
  2676  	bb.SetInt64(int64(base))
  2677  	b.StartTimer()
  2678  	for i := 0; i < b.N; i++ {
  2679  		PowerizeBigInt(&bb, &n)
  2680  	}
  2681  }
  2682  
  2683  func BenchmarkPowerizeBigInt_2_2e1e1(b *testing.B) {
  2684  	benchmarkPowerizeBigInt(b, 2, 1e1)
  2685  }
  2686  
  2687  func BenchmarkPowerizeBigInt_2_2e1e2(b *testing.B) {
  2688  	benchmarkPowerizeBigInt(b, 2, 1e2)
  2689  }
  2690  
  2691  func BenchmarkPowerizeBigInt_2_2e1e3(b *testing.B) {
  2692  	benchmarkPowerizeBigInt(b, 2, 1e3)
  2693  }
  2694  
  2695  func BenchmarkPowerizeBigInt_2_2e1e4(b *testing.B) {
  2696  	benchmarkPowerizeBigInt(b, 2, 1e4)
  2697  }
  2698  
  2699  func BenchmarkPowerizeBigInt_2_2e1e5(b *testing.B) {
  2700  	benchmarkPowerizeBigInt(b, 2, 1e5)
  2701  }
  2702  
  2703  func BenchmarkPowerizeBigInt_2_2e1e6(b *testing.B) {
  2704  	benchmarkPowerizeBigInt(b, 2, 1e6)
  2705  }
  2706  
  2707  func BenchmarkPowerizeBigInt_2_2e1e7(b *testing.B) {
  2708  	benchmarkPowerizeBigInt(b, 2, 1e7)
  2709  }
  2710  
  2711  func BenchmarkPowerizeBigInt_3_2e1e1(b *testing.B) {
  2712  	benchmarkPowerizeBigInt(b, 3, 1e1)
  2713  }
  2714  
  2715  func BenchmarkPowerizeBigInt_3_2e1e2(b *testing.B) {
  2716  	benchmarkPowerizeBigInt(b, 3, 1e2)
  2717  }
  2718  
  2719  func BenchmarkPowerizeBigInt_3_2e1e3(b *testing.B) {
  2720  	benchmarkPowerizeBigInt(b, 3, 1e3)
  2721  }
  2722  
  2723  func BenchmarkPowerizeBigInt_3_2e1e4(b *testing.B) {
  2724  	benchmarkPowerizeBigInt(b, 3, 1e4)
  2725  }
  2726  
  2727  func BenchmarkPowerizeBigInt_3_2e1e5(b *testing.B) {
  2728  	benchmarkPowerizeBigInt(b, 3, 1e5)
  2729  }
  2730  
  2731  func BenchmarkPowerizeBigInt_3_2e1e6(b *testing.B) {
  2732  	benchmarkPowerizeBigInt(b, 3, 1e6)
  2733  }
  2734  
  2735  func BenchmarkPowerizeBigInt_15_2e1e1(b *testing.B) {
  2736  	benchmarkPowerizeBigInt(b, 15, 1e1)
  2737  }
  2738  
  2739  func BenchmarkPowerizeBigInt_15_2e1e2(b *testing.B) {
  2740  	benchmarkPowerizeBigInt(b, 15, 1e2)
  2741  }
  2742  
  2743  func BenchmarkPowerizeBigInt_15_2e1e3(b *testing.B) {
  2744  	benchmarkPowerizeBigInt(b, 15, 1e3)
  2745  }
  2746  
  2747  func BenchmarkPowerizeBigInt_15_2e1e4(b *testing.B) {
  2748  	benchmarkPowerizeBigInt(b, 15, 1e4)
  2749  }
  2750  
  2751  func BenchmarkPowerizeBigInt_15_2e1e5(b *testing.B) {
  2752  	benchmarkPowerizeBigInt(b, 15, 1e5)
  2753  }
  2754  
  2755  func BenchmarkPowerizeBigInt_15_2e1e6(b *testing.B) {
  2756  	benchmarkPowerizeBigInt(b, 15, 1e6)
  2757  }
  2758  
  2759  func BenchmarkPowerizeBigInt_17_2e1e1(b *testing.B) {
  2760  	benchmarkPowerizeBigInt(b, 17, 1e1)
  2761  }
  2762  
  2763  func BenchmarkPowerizeBigInt_17_2e1e2(b *testing.B) {
  2764  	benchmarkPowerizeBigInt(b, 17, 1e2)
  2765  }
  2766  
  2767  func BenchmarkPowerizeBigInt_17_2e1e3(b *testing.B) {
  2768  	benchmarkPowerizeBigInt(b, 17, 1e3)
  2769  }
  2770  
  2771  func BenchmarkPowerizeBigInt_17_2e1e4(b *testing.B) {
  2772  	benchmarkPowerizeBigInt(b, 17, 1e4)
  2773  }
  2774  
  2775  func BenchmarkPowerizeBigInt_17_2e1e5(b *testing.B) {
  2776  	benchmarkPowerizeBigInt(b, 17, 1e5)
  2777  }
  2778  
  2779  func BenchmarkPowerizeBigInt_17_2e1e6(b *testing.B) {
  2780  	benchmarkPowerizeBigInt(b, 17, 1e6)
  2781  }
  2782  
  2783  func TestEnvelope(t *testing.T) {
  2784  	const prec = 1e-3
  2785  	type check struct {
  2786  		approx Approximation
  2787  		x, y   float64
  2788  	}
  2789  	data := []struct {
  2790  		points []float64
  2791  		checks []check
  2792  	}{
  2793  		{
  2794  			[]float64{0, 1},
  2795  			[]check{
  2796  				{Linear, 0, 0},
  2797  				{Linear, 0.25, 0.25},
  2798  				{Linear, 0.5, 0.5},
  2799  				{Linear, 0.75, 0.75},
  2800  				{Linear, 0.9999, 1},
  2801  			},
  2802  		},
  2803  		{
  2804  			[]float64{-1, 0},
  2805  			[]check{
  2806  				{Linear, 0, -1},
  2807  				{Linear, 0.25, -0.75},
  2808  				{Linear, 0.5, -0.5},
  2809  				{Linear, 0.75, -0.25},
  2810  				{Linear, 0.9999, 0},
  2811  			},
  2812  		},
  2813  		{
  2814  			[]float64{-1, 1},
  2815  			[]check{
  2816  				{Linear, 0, -1},
  2817  				{Linear, 0.25, -0.5},
  2818  				{Linear, 0.5, 0},
  2819  				{Linear, 0.75, 0.5},
  2820  				{Linear, 0.9999, 1},
  2821  			},
  2822  		},
  2823  		{
  2824  			[]float64{-1, 1, -2},
  2825  			[]check{
  2826  				{Linear, 0, -1},
  2827  				{Linear, 0.25, 0},
  2828  				{Linear, 0.5, 1},
  2829  				{Linear, 0.75, -0.5},
  2830  				{Linear, 0.9, -1.4},
  2831  				{Linear, 0.9999, -2},
  2832  			},
  2833  		},
  2834  		{
  2835  			[]float64{-1, 1},
  2836  			[]check{
  2837  				{Sinusoidal, 0, -1},
  2838  				{Sinusoidal, 0.25, -math.Sqrt2 / 2},
  2839  				{Sinusoidal, 0.5, 0},
  2840  				{Sinusoidal, 0.75, math.Sqrt2 / 2},
  2841  				{Sinusoidal, 0.9999, 1},
  2842  			},
  2843  		},
  2844  		{
  2845  			[]float64{-1, 1, -2},
  2846  			[]check{
  2847  				{Sinusoidal, 0, -1},
  2848  				{Sinusoidal, 1. / 8, -math.Sqrt2 / 2},
  2849  				{Sinusoidal, 2. / 8, 0},
  2850  				{Sinusoidal, 3. / 8, math.Sqrt2 / 2},
  2851  				{Sinusoidal, 4. / 8, 1},
  2852  				{Sinusoidal, 5. / 8, (3*math.Sqrt2 - 2) / 4},
  2853  				{Sinusoidal, 6. / 8, -0.5},
  2854  				{Sinusoidal, 7. / 8, (-3*math.Sqrt2 - 2) / 4},
  2855  				{Sinusoidal, 0.9999, -2},
  2856  			},
  2857  		},
  2858  	}
  2859  	for i, suite := range data {
  2860  		for j, test := range suite.checks {
  2861  			e, g := test.y, Envelope(test.x, suite.points, test.approx)
  2862  			d := math.Abs(e - g)
  2863  			if d > prec {
  2864  				t.Errorf(
  2865  					"i %d, j %d, x %v, e %v, g %v, d %v, prec %v",
  2866  					i, j, test.x, e, g, d, prec,
  2867  				)
  2868  			}
  2869  		}
  2870  	}
  2871  }
  2872  
  2873  func TestMaxInt(t *testing.T) {
  2874  	n := int64(MaxInt)
  2875  	if n != math.MaxInt32 && n != math.MaxInt64 {
  2876  		t.Error(n)
  2877  	}
  2878  
  2879  	t.Logf("64 bit ints: %t, MaxInt: %d", n == math.MaxInt64, n)
  2880  }
  2881  
  2882  func TestMinInt(t *testing.T) {
  2883  	n := int64(MinInt)
  2884  	if n != math.MinInt32 && n != math.MinInt64 {
  2885  		t.Error(n)
  2886  	}
  2887  
  2888  	t.Logf("64 bit ints: %t. MinInt: %d", n == math.MinInt64, n)
  2889  }
  2890  
  2891  func TestMaxUint(t *testing.T) {
  2892  	n := uint64(MaxUint)
  2893  	if n != math.MaxUint32 && n != math.MaxUint64 {
  2894  		t.Error(n)
  2895  	}
  2896  
  2897  	t.Logf("64 bit uints: %t. MaxUint: %d", n == math.MaxUint64, n)
  2898  }
  2899  
  2900  func TestMax(t *testing.T) {
  2901  	tests := []struct{ a, b, e int }{
  2902  		{MinInt, MinIntM1, MaxInt},
  2903  		{MinIntM1, MinInt, MaxInt},
  2904  		{MinIntM1, MinIntM1, MaxInt},
  2905  
  2906  		{MinInt, MinInt, MinInt},
  2907  		{MinInt + 1, MinInt, MinInt + 1},
  2908  		{MinInt, MinInt + 1, MinInt + 1},
  2909  
  2910  		{-1, -1, -1},
  2911  		{-1, 0, 0},
  2912  		{-1, 1, 1},
  2913  
  2914  		{0, -1, 0},
  2915  		{0, 0, 0},
  2916  		{0, 1, 1},
  2917  
  2918  		{1, -1, 1},
  2919  		{1, 0, 1},
  2920  		{1, 1, 1},
  2921  
  2922  		{MaxInt, MaxInt, MaxInt},
  2923  		{MaxInt - 1, MaxInt, MaxInt},
  2924  		{MaxInt, MaxInt - 1, MaxInt},
  2925  
  2926  		{MaxIntP1, MaxInt, MaxInt},
  2927  		{MaxInt, MaxIntP1, MaxInt},
  2928  		{MaxIntP1, MaxIntP1, MinInt},
  2929  	}
  2930  
  2931  	for _, test := range tests {
  2932  		if g, e := Max(test.a, test.b), test.e; g != e {
  2933  			t.Fatal(test.a, test.b, g, e)
  2934  		}
  2935  	}
  2936  }
  2937  
  2938  func TestMin(t *testing.T) {
  2939  	tests := []struct{ a, b, e int }{
  2940  		{MinIntM1, MinInt, MinInt},
  2941  		{MinInt, MinIntM1, MinInt},
  2942  		{MinIntM1, MinIntM1, MaxInt},
  2943  
  2944  		{MinInt, MinInt, MinInt},
  2945  		{MinInt + 1, MinInt, MinInt},
  2946  		{MinInt, MinInt + 1, MinInt},
  2947  
  2948  		{-1, -1, -1},
  2949  		{-1, 0, -1},
  2950  		{-1, 1, -1},
  2951  
  2952  		{0, -1, -1},
  2953  		{0, 0, 0},
  2954  		{0, 1, 0},
  2955  
  2956  		{1, -1, -1},
  2957  		{1, 0, 0},
  2958  		{1, 1, 1},
  2959  
  2960  		{MaxInt, MaxInt, MaxInt},
  2961  		{MaxInt - 1, MaxInt, MaxInt - 1},
  2962  		{MaxInt, MaxInt - 1, MaxInt - 1},
  2963  
  2964  		{MaxIntP1, MaxInt, MinInt},
  2965  		{MaxInt, MaxIntP1, MinInt},
  2966  		{MaxIntP1, MaxIntP1, MinInt},
  2967  	}
  2968  
  2969  	for _, test := range tests {
  2970  		if g, e := Min(test.a, test.b), test.e; g != e {
  2971  			t.Fatal(test.a, test.b, g, e)
  2972  		}
  2973  	}
  2974  }
  2975  
  2976  func TestMaxPtr(t *testing.T) {
  2977  	tests := []struct{ a, b, e *int }{
  2978  		{intPtr(MinInt), intPtr(MinIntM1), intPtr(MaxInt)},
  2979  		{intPtr(MinIntM1), intPtr(MinInt), intPtr(MaxInt)},
  2980  		{intPtr(MinIntM1), intPtr(MinIntM1), intPtr(MaxInt)},
  2981  		{nil, intPtr(MinIntM1), intPtr(MaxInt)},
  2982  
  2983  		{intPtr(MinInt), intPtr(MinInt), intPtr(MinInt)},
  2984  		{intPtr(MinInt + 1), intPtr(MinInt), intPtr(MinInt + 1)},
  2985  		{intPtr(MinInt), intPtr(MinInt + 1), intPtr(MinInt + 1)},
  2986  		{nil, intPtr(MinInt + 1), intPtr(MinInt + 1)},
  2987  
  2988  		{intPtr(-1), intPtr(-1), intPtr(-1)},
  2989  		{intPtr(-1), intPtr(0), intPtr(0)},
  2990  		{intPtr(-1), intPtr(1), intPtr(1)},
  2991  		{intPtr(-1), nil, intPtr(-1)},
  2992  
  2993  		{intPtr(0), intPtr(-1), intPtr(0)},
  2994  		{intPtr(0), intPtr(0), intPtr(0)},
  2995  		{intPtr(0), intPtr(1), intPtr(1)},
  2996  		{intPtr(0), nil, intPtr(0)},
  2997  
  2998  		{intPtr(1), intPtr(-1), intPtr(1)},
  2999  		{intPtr(1), intPtr(0), intPtr(1)},
  3000  		{intPtr(1), intPtr(1), intPtr(1)},
  3001  		{nil, nil, nil},
  3002  
  3003  		{intPtr(MaxInt), intPtr(MaxInt), intPtr(MaxInt)},
  3004  		{intPtr(MaxInt - 1), intPtr(MaxInt), intPtr(MaxInt)},
  3005  		{intPtr(MaxInt), intPtr(MaxInt - 1), intPtr(MaxInt)},
  3006  		{intPtr(MaxInt), nil, intPtr(MaxInt)},
  3007  
  3008  		{intPtr(MaxIntP1), intPtr(MaxInt), intPtr(MaxInt)},
  3009  		{intPtr(MaxInt), intPtr(MaxIntP1), intPtr(MaxInt)},
  3010  		{intPtr(MaxIntP1), intPtr(MaxIntP1), intPtr(MinInt)},
  3011  		{nil, intPtr(MaxIntP1), intPtr(MinInt)},
  3012  	}
  3013  
  3014  	for c, test := range tests {
  3015  		g, e := MaxPtr(test.a, test.b), test.e
  3016  		if e != nil {
  3017  			if *g != *e {
  3018  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3019  			}
  3020  		} else {
  3021  			if e != g {
  3022  				t.Fatal(*test.a, *test.b, *g, e, c)
  3023  			}
  3024  		}
  3025  	}
  3026  }
  3027  
  3028  func TestMinPtr(t *testing.T) {
  3029  	tests := []struct{ a, b, e *int }{
  3030  		{intPtr(MinIntM1), intPtr(MinInt), intPtr(MinInt)},
  3031  		{intPtr(MinInt), intPtr(MinIntM1), intPtr(MinInt)},
  3032  		{intPtr(MinIntM1), intPtr(MinIntM1), intPtr(MaxInt)},
  3033  		{nil, intPtr(MinIntM1), intPtr(MinIntM1)},
  3034  
  3035  		{intPtr(MinInt), intPtr(MinInt), intPtr(MinInt)},
  3036  		{intPtr(MinInt + 1), intPtr(MinInt), intPtr(MinInt)},
  3037  		{intPtr(MinInt), intPtr(MinInt + 1), intPtr(MinInt)},
  3038  		{nil, intPtr(MinInt + 1), intPtr(MinInt + 1)},
  3039  
  3040  		{intPtr(-1), intPtr(-1), intPtr(-1)},
  3041  		{intPtr(-1), intPtr(0), intPtr(-1)},
  3042  		{intPtr(-1), intPtr(1), intPtr(-1)},
  3043  		{intPtr(-1), nil, intPtr(-1)},
  3044  
  3045  		{intPtr(0), intPtr(-1), intPtr(-1)},
  3046  		{intPtr(0), intPtr(0), intPtr(0)},
  3047  		{intPtr(0), intPtr(1), intPtr(0)},
  3048  		{intPtr(0), nil, intPtr(0)},
  3049  
  3050  		{intPtr(1), intPtr(-1), intPtr(-1)},
  3051  		{intPtr(1), intPtr(0), intPtr(0)},
  3052  		{intPtr(1), intPtr(1), intPtr(1)},
  3053  		{nil, nil, nil},
  3054  
  3055  		{intPtr(MaxInt), intPtr(MaxInt), intPtr(MaxInt)},
  3056  		{intPtr(MaxInt - 1), intPtr(MaxInt), intPtr(MaxInt - 1)},
  3057  		{intPtr(MaxInt), intPtr(MaxInt - 1), intPtr(MaxInt - 1)},
  3058  		{intPtr(MaxInt), nil, intPtr(MaxInt)},
  3059  
  3060  		{intPtr(MaxIntP1), intPtr(MaxInt), intPtr(MinInt)},
  3061  		{intPtr(MaxInt), intPtr(MaxIntP1), intPtr(MinInt)},
  3062  		{intPtr(MaxIntP1), intPtr(MaxIntP1), intPtr(MinInt)},
  3063  		{nil, intPtr(MaxIntP1), intPtr(MinInt)},
  3064  	}
  3065  
  3066  	for c, test := range tests {
  3067  		g, e := MinPtr(test.a, test.b), test.e
  3068  		if e != nil {
  3069  			if *g != *e {
  3070  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3071  			}
  3072  		} else {
  3073  			if e != g {
  3074  				t.Fatal(*test.a, *test.b, *g, e, c)
  3075  			}
  3076  		}
  3077  	}
  3078  }
  3079  
  3080  func TestMaxVal(t *testing.T) {
  3081  	tests := []struct{ a, b, c, e int }{
  3082  		{MinInt, MinInt, MinIntM1, MaxInt},
  3083  		{MinIntM1, MinIntM1, MinInt, MaxInt},
  3084  		{MinIntM1, MinIntM1, MinIntM1, MaxInt},
  3085  
  3086  		{MinInt, MinInt, MinInt, MinInt},
  3087  		{MinInt + 1, MinInt + 1, MinInt, MinInt + 1},
  3088  		{MinInt, MinInt, MinInt + 1, MinInt + 1},
  3089  
  3090  		{-1, -1, -1, -1},
  3091  		{-1, -1, 0, 0},
  3092  		{-1, -1, 1, 1},
  3093  
  3094  		{0, 0, -1, 0},
  3095  		{0, 0, 0, 0},
  3096  		{0, 0, 1, 1},
  3097  
  3098  		{1, 1, -1, 1},
  3099  		{1, 1, 0, 1},
  3100  		{1, 1, 1, 1},
  3101  
  3102  		{MaxInt, MaxInt, MaxInt, MaxInt},
  3103  		{MaxInt - 1, MaxInt - 1, MaxInt, MaxInt},
  3104  		{MaxInt, MaxInt, MaxInt - 1, MaxInt},
  3105  
  3106  		{MaxIntP1, MaxIntP1, MaxInt, MaxInt},
  3107  		{MaxInt, MaxInt, MaxIntP1, MaxInt},
  3108  		{MaxIntP1, MaxIntP1, MaxIntP1, MinInt},
  3109  	}
  3110  
  3111  	for i, test := range tests {
  3112  		if g, e := MaxVal(test.a, test.b, test.c), test.e; g != e {
  3113  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3114  		}
  3115  		if g, e := MaxVal(test.a), test.a; g != e {
  3116  			t.Fatal(i, test.a, g, e)
  3117  		}
  3118  	}
  3119  }
  3120  
  3121  func TestMinVal(t *testing.T) {
  3122  	tests := []struct{ a, b, c, e int }{
  3123  		{MinIntM1, MinIntM1, MinInt, MinInt},
  3124  		{MinInt, MinInt, MinIntM1, MinInt},
  3125  		{MinIntM1, MinIntM1, MinIntM1, MaxInt},
  3126  
  3127  		{MinInt, MinInt, MinInt, MinInt},
  3128  		{MinInt + 1, MinInt + 1, MinInt, MinInt},
  3129  		{MinInt, MinInt, MinInt + 1, MinInt},
  3130  
  3131  		{-1, -1, -1, -1},
  3132  		{-1, -1, 0, -1},
  3133  		{-1, -1, 1, -1},
  3134  
  3135  		{0, 0, -1, -1},
  3136  		{0, 0, 0, 0},
  3137  		{0, 0, 1, 0},
  3138  
  3139  		{1, 1, -1, -1},
  3140  		{1, 1, 0, 0},
  3141  		{1, 1, 1, 1},
  3142  
  3143  		{MaxInt, MaxInt, MaxInt, MaxInt},
  3144  		{MaxInt - 1, MaxInt - 1, MaxInt, MaxInt - 1},
  3145  		{MaxInt, MaxInt, MaxInt - 1, MaxInt - 1},
  3146  
  3147  		{MaxIntP1, MaxIntP1, MaxInt, MinInt},
  3148  		{MaxInt, MaxInt, MaxIntP1, MinInt},
  3149  		{MaxIntP1, MaxIntP1, MaxIntP1, MinInt},
  3150  	}
  3151  
  3152  	for i, test := range tests {
  3153  		if g, e := MinVal(test.a, test.b, test.c), test.e; g != e {
  3154  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3155  		}
  3156  		if g, e := MinVal(test.a), test.a; g != e {
  3157  			t.Fatal(i, test.a, g, e)
  3158  		}
  3159  	}
  3160  }
  3161  
  3162  func TestClamp(t *testing.T) {
  3163  	tests := []struct{ v, lo, hi, e int }{
  3164  		{0, 0, 0, 0},
  3165  		{5, 10, 20, 10},
  3166  		{10, 10, 20, 10},
  3167  		{15, 10, 20, 15},
  3168  		{20, 10, 20, 20},
  3169  		{25, 10, 20, 20},
  3170  	}
  3171  
  3172  	for _, test := range tests {
  3173  		if g, e := Clamp(test.v, test.lo, test.hi), test.e; g != e {
  3174  			t.Fatal(test.v, test.lo, test.hi, g, e)
  3175  		}
  3176  	}
  3177  }
  3178  
  3179  func TestUMax(t *testing.T) {
  3180  	tests := []struct{ a, b, e uint }{
  3181  		{0, 0, 0},
  3182  		{0, 1, 1},
  3183  		{1, 0, 1},
  3184  
  3185  		{10, 10, 10},
  3186  		{10, 11, 11},
  3187  		{11, 10, 11},
  3188  		{11, 11, 11},
  3189  
  3190  		{MaxUint, MaxUint, MaxUint},
  3191  		{MaxUint, MaxUint - 1, MaxUint},
  3192  		{MaxUint - 1, MaxUint, MaxUint},
  3193  		{MaxUint - 1, MaxUint - 1, MaxUint - 1},
  3194  
  3195  		{MaxUint, MaxUintP1, MaxUint},
  3196  		{MaxUintP1, MaxUint, MaxUint},
  3197  		{MaxUintP1, MaxUintP1, 0},
  3198  	}
  3199  
  3200  	for _, test := range tests {
  3201  		if g, e := UMax(test.a, test.b), test.e; g != e {
  3202  			t.Fatal(test.a, test.b, g, e)
  3203  		}
  3204  	}
  3205  }
  3206  
  3207  func TestUMin(t *testing.T) {
  3208  	tests := []struct{ a, b, e uint }{
  3209  		{0, 0, 0},
  3210  		{0, 1, 0},
  3211  		{1, 0, 0},
  3212  
  3213  		{10, 10, 10},
  3214  		{10, 11, 10},
  3215  		{11, 10, 10},
  3216  		{11, 11, 11},
  3217  
  3218  		{MaxUint, MaxUint, MaxUint},
  3219  		{MaxUint, MaxUint - 1, MaxUint - 1},
  3220  		{MaxUint - 1, MaxUint, MaxUint - 1},
  3221  		{MaxUint - 1, MaxUint - 1, MaxUint - 1},
  3222  
  3223  		{MaxUint, MaxUintP1, 0},
  3224  		{MaxUintP1, MaxUint, 0},
  3225  		{MaxUintP1, MaxUintP1, 0},
  3226  	}
  3227  
  3228  	for _, test := range tests {
  3229  		if g, e := UMin(test.a, test.b), test.e; g != e {
  3230  			t.Fatal(test.a, test.b, g, e)
  3231  		}
  3232  	}
  3233  }
  3234  
  3235  func TestUMaxPtr(t *testing.T) {
  3236  	tests := []struct{ a, b, e *uint }{
  3237  		{uIntPtr(0), uIntPtr(0), uIntPtr(0)},
  3238  		{uIntPtr(0), uIntPtr(1), uIntPtr(1)},
  3239  		{uIntPtr(1), uIntPtr(0), uIntPtr(1)},
  3240  		{nil, uIntPtr(1), uIntPtr(1)},
  3241  		{nil, uIntPtr(0), uIntPtr(0)},
  3242  
  3243  		{uIntPtr(10), uIntPtr(10), uIntPtr(10)},
  3244  		{uIntPtr(10), uIntPtr(11), uIntPtr(11)},
  3245  		{uIntPtr(11), uIntPtr(10), uIntPtr(11)},
  3246  		{uIntPtr(11), uIntPtr(11), uIntPtr(11)},
  3247  		{nil, nil, nil},
  3248  
  3249  		{uIntPtr(MaxUint), uIntPtr(MaxUint), uIntPtr(MaxUint)},
  3250  		{uIntPtr(MaxUint), uIntPtr(MaxUint - 1), uIntPtr(MaxUint)},
  3251  		{uIntPtr(MaxUint - 1), nil, uIntPtr(MaxUint - 1)},
  3252  		{nil, uIntPtr(MaxUint - 1), uIntPtr(MaxUint - 1)},
  3253  		{uIntPtr(MaxUint - 1), nil, uIntPtr(MaxUint - 1)},
  3254  
  3255  		{uIntPtr(MaxUint), uIntPtr(MaxUintP1), uIntPtr(MaxUint)},
  3256  		{uIntPtr(MaxUintP1), uIntPtr(MaxUint), uIntPtr(MaxUint)},
  3257  		{uIntPtr(MaxUintP1), uIntPtr(MaxUintP1), uIntPtr(0)},
  3258  		{uIntPtr(MaxUintP1), nil, uIntPtr(0)},
  3259  	}
  3260  
  3261  	for c, test := range tests {
  3262  		g, e := UMaxPtr(test.a, test.b), test.e
  3263  		if e != nil {
  3264  			if *g != *e {
  3265  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3266  			}
  3267  		} else {
  3268  			if e != g {
  3269  				t.Fatal(*test.a, *test.b, *g, e, c)
  3270  			}
  3271  		}
  3272  	}
  3273  }
  3274  
  3275  func TestUMinPtr(t *testing.T) {
  3276  	tests := []struct{ a, b, e *uint }{
  3277  		{uIntPtr(0), uIntPtr(0), uIntPtr(0)},
  3278  		{uIntPtr(0), uIntPtr(1), uIntPtr(0)},
  3279  		{uIntPtr(1), uIntPtr(0), uIntPtr(0)},
  3280  		{nil, uIntPtr(1), uIntPtr(1)},
  3281  		{nil, uIntPtr(0), uIntPtr(0)},
  3282  
  3283  		{uIntPtr(10), uIntPtr(10), uIntPtr(10)},
  3284  		{uIntPtr(10), uIntPtr(11), uIntPtr(10)},
  3285  		{uIntPtr(11), uIntPtr(10), uIntPtr(10)},
  3286  		{uIntPtr(11), uIntPtr(11), uIntPtr(11)},
  3287  		{nil, nil, nil},
  3288  
  3289  		{uIntPtr(MaxUint), uIntPtr(MaxUint), uIntPtr(MaxUint)},
  3290  		{uIntPtr(MaxUint), uIntPtr(MaxUint - 1), uIntPtr(MaxUint - 1)},
  3291  		{uIntPtr(MaxUint - 1), nil, uIntPtr(MaxUint - 1)},
  3292  		{nil, uIntPtr(MaxUint - 1), uIntPtr(MaxUint - 1)},
  3293  		{uIntPtr(MaxUint - 1), nil, uIntPtr(MaxUint - 1)},
  3294  
  3295  		{uIntPtr(MaxUint), uIntPtr(MaxUintP1), uIntPtr(0)},
  3296  		{uIntPtr(MaxUintP1), uIntPtr(MaxUint), uIntPtr(0)},
  3297  		{uIntPtr(MaxUintP1), uIntPtr(MaxUintP1), uIntPtr(0)},
  3298  		{uIntPtr(MaxUintP1), nil, uIntPtr(0)},
  3299  	}
  3300  
  3301  	for c, test := range tests {
  3302  		g, e := UMinPtr(test.a, test.b), test.e
  3303  		if e != nil {
  3304  			if *g != *e {
  3305  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3306  			}
  3307  		} else {
  3308  			if e != g {
  3309  				t.Fatal(*test.a, *test.b, *g, e, c)
  3310  			}
  3311  		}
  3312  	}
  3313  }
  3314  
  3315  func TestUMaxVal(t *testing.T) {
  3316  	tests := []struct{ a, b, c, e uint }{
  3317  		{0, 0, 0, 0},
  3318  		{0, 0, 1, 1},
  3319  		{1, 1, 0, 1},
  3320  
  3321  		{10, 10, 10, 10},
  3322  		{10, 10, 11, 11},
  3323  		{11, 11, 10, 11},
  3324  		{11, 11, 11, 11},
  3325  
  3326  		{MaxUint, MaxUint, MaxUint, MaxUint},
  3327  		{MaxUint, MaxUint, MaxUint - 1, MaxUint},
  3328  		{MaxUint - 1, MaxUint - 1, MaxUint, MaxUint},
  3329  		{MaxUint - 1, MaxUint - 1, MaxUint - 1, MaxUint - 1},
  3330  
  3331  		{MaxUint, MaxUint, MaxUintP1, MaxUint},
  3332  		{MaxUintP1, MaxUintP1, MaxUint, MaxUint},
  3333  		{MaxUintP1, MaxUintP1, MaxUintP1, 0},
  3334  	}
  3335  
  3336  	for i, test := range tests {
  3337  		if g, e := UMaxVal(test.a, test.b, test.c), test.e; g != e {
  3338  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3339  		}
  3340  		if g, e := UMaxVal(test.a), test.a; g != e {
  3341  			t.Fatal(i, test.a, g, e)
  3342  		}
  3343  	}
  3344  }
  3345  
  3346  func TestUMinVal(t *testing.T) {
  3347  	tests := []struct{ a, b, c, e uint }{
  3348  		{0, 0, 0, 0},
  3349  		{0, 0, 1, 0},
  3350  		{1, 1, 0, 0},
  3351  
  3352  		{10, 10, 10, 10},
  3353  		{10, 10, 11, 10},
  3354  		{11, 11, 10, 10},
  3355  		{11, 11, 11, 11},
  3356  
  3357  		{MaxUint, MaxUint, MaxUint, MaxUint},
  3358  		{MaxUint, MaxUint, MaxUint - 1, MaxUint - 1},
  3359  		{MaxUint - 1, MaxUint - 1, MaxUint, MaxUint - 1},
  3360  		{MaxUint - 1, MaxUint - 1, MaxUint - 1, MaxUint - 1},
  3361  
  3362  		{MaxUint, MaxUint, MaxUintP1, 0},
  3363  		{MaxUintP1, MaxUintP1, MaxUint, 0},
  3364  		{MaxUintP1, MaxUintP1, MaxUintP1, 0},
  3365  	}
  3366  
  3367  	for i, test := range tests {
  3368  		if g, e := UMinVal(test.a, test.b, test.c), test.e; g != e {
  3369  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3370  		}
  3371  		if g, e := UMinVal(test.a), test.a; g != e {
  3372  			t.Fatal(i, test.a, g, e)
  3373  		}
  3374  	}
  3375  }
  3376  
  3377  func TestUClamp(t *testing.T) {
  3378  	tests := []struct{ v, lo, hi, e uint }{
  3379  		{0, 0, 0, 0},
  3380  		{5, 10, 20, 10},
  3381  		{10, 10, 20, 10},
  3382  		{15, 10, 20, 15},
  3383  		{20, 10, 20, 20},
  3384  		{25, 10, 20, 20},
  3385  	}
  3386  
  3387  	for _, test := range tests {
  3388  		if g, e := UClamp(test.v, test.lo, test.hi), test.e; g != e {
  3389  			t.Fatal(test.v, test.lo, test.hi, g, e)
  3390  		}
  3391  	}
  3392  }
  3393  
  3394  func TestMaxByte(t *testing.T) {
  3395  	tests := []struct{ a, b, e byte }{
  3396  		{0, 0, 0},
  3397  		{0, 1, 1},
  3398  		{1, 0, 1},
  3399  
  3400  		{10, 10, 10},
  3401  		{10, 11, 11},
  3402  		{11, 10, 11},
  3403  		{11, 11, 11},
  3404  
  3405  		{math.MaxUint8, math.MaxUint8, math.MaxUint8},
  3406  		{math.MaxUint8, math.MaxUint8 - 1, math.MaxUint8},
  3407  		{math.MaxUint8 - 1, math.MaxUint8, math.MaxUint8},
  3408  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3409  	}
  3410  
  3411  	for _, test := range tests {
  3412  		if g, e := MaxByte(test.a, test.b), test.e; g != e {
  3413  			t.Fatal(test.a, test.b, g, e)
  3414  		}
  3415  	}
  3416  }
  3417  
  3418  func TestMinByte(t *testing.T) {
  3419  	tests := []struct{ a, b, e byte }{
  3420  		{0, 0, 0},
  3421  		{0, 1, 0},
  3422  		{1, 0, 0},
  3423  
  3424  		{10, 10, 10},
  3425  		{10, 11, 10},
  3426  		{11, 10, 10},
  3427  		{11, 11, 11},
  3428  
  3429  		{math.MaxUint8, math.MaxUint8, math.MaxUint8},
  3430  		{math.MaxUint8, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3431  		{math.MaxUint8 - 1, math.MaxUint8, math.MaxUint8 - 1},
  3432  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3433  	}
  3434  
  3435  	for _, test := range tests {
  3436  		if g, e := MinByte(test.a, test.b), test.e; g != e {
  3437  			t.Fatal(test.a, test.b, g, e)
  3438  		}
  3439  	}
  3440  }
  3441  
  3442  func TestMaxBytePtr(t *testing.T) {
  3443  	tests := []struct{ a, b, e *byte }{
  3444  		{bytePtr(0), bytePtr(0), bytePtr(0)},
  3445  		{bytePtr(0), bytePtr(1), bytePtr(1)},
  3446  		{bytePtr(1), bytePtr(0), bytePtr(1)},
  3447  		{nil, bytePtr(0), bytePtr(0)},
  3448  		{bytePtr(1), nil, bytePtr(1)},
  3449  
  3450  		{bytePtr(10), bytePtr(10), bytePtr(10)},
  3451  		{bytePtr(10), bytePtr(11), bytePtr(11)},
  3452  		{bytePtr(11), bytePtr(10), bytePtr(11)},
  3453  		{bytePtr(11), bytePtr(11), bytePtr(11)},
  3454  		{nil, nil, nil},
  3455  
  3456  		{bytePtr(math.MaxUint8), bytePtr(math.MaxUint8), bytePtr(math.MaxUint8)},
  3457  		{bytePtr(math.MaxUint8), bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8)},
  3458  		{bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8), bytePtr(math.MaxUint8)},
  3459  		{bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1)},
  3460  		{nil, bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1)},
  3461  		{bytePtr(math.MaxUint8), nil, bytePtr(math.MaxUint8)},
  3462  	}
  3463  
  3464  	for c, test := range tests {
  3465  		g, e := MaxBytePtr(test.a, test.b), test.e
  3466  		if e != nil {
  3467  			if *g != *e {
  3468  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3469  			}
  3470  		} else {
  3471  			if e != g {
  3472  				t.Fatal(*test.a, *test.b, *g, e, c)
  3473  			}
  3474  		}
  3475  	}
  3476  }
  3477  
  3478  func TestMinBytePtr(t *testing.T) {
  3479  	tests := []struct{ a, b, e *byte }{
  3480  		{bytePtr(0), bytePtr(0), bytePtr(0)},
  3481  		{bytePtr(0), bytePtr(1), bytePtr(0)},
  3482  		{bytePtr(1), bytePtr(0), bytePtr(0)},
  3483  		{nil, bytePtr(0), bytePtr(0)},
  3484  		{bytePtr(1), nil, bytePtr(1)},
  3485  
  3486  		{bytePtr(10), bytePtr(10), bytePtr(10)},
  3487  		{bytePtr(10), bytePtr(11), bytePtr(10)},
  3488  		{bytePtr(11), bytePtr(10), bytePtr(10)},
  3489  		{bytePtr(11), bytePtr(11), bytePtr(11)},
  3490  		{nil, nil, nil},
  3491  
  3492  		{bytePtr(math.MaxUint8), bytePtr(math.MaxUint8), bytePtr(math.MaxUint8)},
  3493  		{bytePtr(math.MaxUint8), bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1)},
  3494  		{bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8), bytePtr(math.MaxUint8 - 1)},
  3495  		{bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1)},
  3496  		{nil, bytePtr(math.MaxUint8 - 1), bytePtr(math.MaxUint8 - 1)},
  3497  		{bytePtr(math.MaxUint8), nil, bytePtr(math.MaxUint8)},
  3498  	}
  3499  
  3500  	for c, test := range tests {
  3501  		g, e := MinBytePtr(test.a, test.b), test.e
  3502  		if e != nil {
  3503  			if *g != *e {
  3504  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3505  			}
  3506  		} else {
  3507  			if e != g {
  3508  				t.Fatal(*test.a, *test.b, *g, e, c)
  3509  			}
  3510  		}
  3511  	}
  3512  }
  3513  
  3514  func TestMaxByteVal(t *testing.T) {
  3515  	tests := []struct{ a, b, c, e byte }{
  3516  		{0, 0, 0, 0},
  3517  		{0, 0, 1, 1},
  3518  		{1, 1, 0, 1},
  3519  
  3520  		{10, 10, 10, 10},
  3521  		{10, 10, 11, 11},
  3522  		{11, 11, 10, 11},
  3523  		{11, 11, 11, 11},
  3524  
  3525  		{math.MaxUint8, math.MaxUint8, math.MaxUint8, math.MaxUint8},
  3526  		{math.MaxUint8, math.MaxUint8, math.MaxUint8 - 1, math.MaxUint8},
  3527  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8, math.MaxUint8},
  3528  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3529  	}
  3530  
  3531  	for i, test := range tests {
  3532  		if g, e := MaxByteVal(test.a, test.b, test.c), test.e; g != e {
  3533  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3534  		}
  3535  		if g, e := MaxByteVal(test.a), test.a; g != e {
  3536  			t.Fatal(i, test.a, g, e)
  3537  		}
  3538  	}
  3539  }
  3540  
  3541  func TestMinByteVal(t *testing.T) {
  3542  	tests := []struct{ a, b, c, e byte }{
  3543  		{0, 0, 0, 0},
  3544  		{0, 0, 1, 0},
  3545  		{1, 1, 0, 0},
  3546  
  3547  		{10, 10, 10, 10},
  3548  		{10, 10, 11, 10},
  3549  		{11, 11, 10, 10},
  3550  		{11, 11, 11, 11},
  3551  
  3552  		{math.MaxUint8, math.MaxUint8, math.MaxUint8, math.MaxUint8},
  3553  		{math.MaxUint8, math.MaxUint8, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3554  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8, math.MaxUint8 - 1},
  3555  		{math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1, math.MaxUint8 - 1},
  3556  	}
  3557  
  3558  	for i, test := range tests {
  3559  		if g, e := MinByteVal(test.a, test.b, test.c), test.e; g != e {
  3560  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3561  		}
  3562  		if g, e := MinByteVal(test.a), test.a; g != e {
  3563  			t.Fatal(i, test.a, g, e)
  3564  		}
  3565  	}
  3566  }
  3567  
  3568  func TestClampByte(t *testing.T) {
  3569  	tests := []struct{ v, lo, hi, e byte }{
  3570  		{0, 0, 0, 0},
  3571  		{5, 10, 20, 10},
  3572  		{10, 10, 20, 10},
  3573  		{15, 10, 20, 15},
  3574  		{20, 10, 20, 20},
  3575  		{25, 10, 20, 20},
  3576  	}
  3577  
  3578  	for _, test := range tests {
  3579  		if g, e := ClampByte(test.v, test.lo, test.hi), test.e; g != e {
  3580  			t.Fatal(test.v, test.lo, test.hi, g, e)
  3581  		}
  3582  	}
  3583  }
  3584  
  3585  func TestMaxUint16(t *testing.T) {
  3586  	tests := []struct{ a, b, e uint16 }{
  3587  		{0, 0, 0},
  3588  		{0, 1, 1},
  3589  		{1, 0, 1},
  3590  
  3591  		{10, 10, 10},
  3592  		{10, 11, 11},
  3593  		{11, 10, 11},
  3594  		{11, 11, 11},
  3595  
  3596  		{math.MaxUint16, math.MaxUint16, math.MaxUint16},
  3597  		{math.MaxUint16, math.MaxUint16 - 1, math.MaxUint16},
  3598  		{math.MaxUint16 - 1, math.MaxUint16, math.MaxUint16},
  3599  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3600  	}
  3601  
  3602  	for _, test := range tests {
  3603  		if g, e := MaxUint16(test.a, test.b), test.e; g != e {
  3604  			t.Fatal(test.a, test.b, g, e)
  3605  		}
  3606  	}
  3607  }
  3608  
  3609  func TestMinUint16(t *testing.T) {
  3610  	tests := []struct{ a, b, e uint16 }{
  3611  		{0, 0, 0},
  3612  		{0, 1, 0},
  3613  		{1, 0, 0},
  3614  
  3615  		{10, 10, 10},
  3616  		{10, 11, 10},
  3617  		{11, 10, 10},
  3618  		{11, 11, 11},
  3619  
  3620  		{math.MaxUint16, math.MaxUint16, math.MaxUint16},
  3621  		{math.MaxUint16, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3622  		{math.MaxUint16 - 1, math.MaxUint16, math.MaxUint16 - 1},
  3623  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3624  	}
  3625  
  3626  	for _, test := range tests {
  3627  		if g, e := MinUint16(test.a, test.b), test.e; g != e {
  3628  			t.Fatal(test.a, test.b, g, e)
  3629  		}
  3630  	}
  3631  }
  3632  
  3633  func TestMaxUint16Ptr(t *testing.T) {
  3634  	tests := []struct{ a, b, e *uint16 }{
  3635  		{uInt16Ptr(0), uInt16Ptr(0), uInt16Ptr(0)},
  3636  		{uInt16Ptr(0), uInt16Ptr(1), uInt16Ptr(1)},
  3637  		{uInt16Ptr(1), uInt16Ptr(0), uInt16Ptr(1)},
  3638  		{nil, uInt16Ptr(0), uInt16Ptr(0)},
  3639  		{uInt16Ptr(1), nil, uInt16Ptr(1)},
  3640  
  3641  		{uInt16Ptr(10), uInt16Ptr(10), uInt16Ptr(10)},
  3642  		{uInt16Ptr(10), uInt16Ptr(11), uInt16Ptr(11)},
  3643  		{uInt16Ptr(11), uInt16Ptr(10), uInt16Ptr(11)},
  3644  		{uInt16Ptr(11), uInt16Ptr(11), uInt16Ptr(11)},
  3645  		{nil, nil, nil},
  3646  
  3647  		{uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16)},
  3648  		{uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16)},
  3649  		{uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16)},
  3650  		{uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1)},
  3651  		{nil, uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1)},
  3652  		{uInt16Ptr(math.MaxUint16), nil, uInt16Ptr(math.MaxUint16)},
  3653  	}
  3654  
  3655  	for c, test := range tests {
  3656  		g, e := MaxUint16Ptr(test.a, test.b), test.e
  3657  		if e != nil {
  3658  			if *g != *e {
  3659  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3660  			}
  3661  		} else {
  3662  			if e != g {
  3663  				t.Fatal(*test.a, *test.b, *g, e, c)
  3664  			}
  3665  		}
  3666  	}
  3667  }
  3668  
  3669  func TestMinUint16Ptr(t *testing.T) {
  3670  	tests := []struct{ a, b, e *uint16 }{
  3671  		{uInt16Ptr(0), uInt16Ptr(0), uInt16Ptr(0)},
  3672  		{uInt16Ptr(0), uInt16Ptr(1), uInt16Ptr(0)},
  3673  		{uInt16Ptr(1), uInt16Ptr(0), uInt16Ptr(0)},
  3674  		{nil, uInt16Ptr(0), uInt16Ptr(0)},
  3675  		{uInt16Ptr(1), nil, uInt16Ptr(1)},
  3676  
  3677  		{uInt16Ptr(10), uInt16Ptr(10), uInt16Ptr(10)},
  3678  		{uInt16Ptr(10), uInt16Ptr(11), uInt16Ptr(10)},
  3679  		{uInt16Ptr(11), uInt16Ptr(10), uInt16Ptr(10)},
  3680  		{uInt16Ptr(11), uInt16Ptr(11), uInt16Ptr(11)},
  3681  		{nil, nil, nil},
  3682  
  3683  		{uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16)},
  3684  		{uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1)},
  3685  		{uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16), uInt16Ptr(math.MaxUint16 - 1)},
  3686  		{uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1)},
  3687  		{nil, uInt16Ptr(math.MaxUint16 - 1), uInt16Ptr(math.MaxUint16 - 1)},
  3688  		{uInt16Ptr(math.MaxUint16), nil, uInt16Ptr(math.MaxUint16)},
  3689  	}
  3690  
  3691  	for c, test := range tests {
  3692  		g, e := MinUint16Ptr(test.a, test.b), test.e
  3693  		if e != nil {
  3694  			if *g != *e {
  3695  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3696  			}
  3697  		} else {
  3698  			if e != g {
  3699  				t.Fatal(*test.a, *test.b, *g, e, c)
  3700  			}
  3701  		}
  3702  	}
  3703  }
  3704  
  3705  func TestMaxUint16Val(t *testing.T) {
  3706  	tests := []struct{ a, b, c, e uint16 }{
  3707  		{0, 0, 0, 0},
  3708  		{0, 0, 1, 1},
  3709  		{1, 1, 0, 1},
  3710  
  3711  		{10, 10, 10, 10},
  3712  		{10, 10, 11, 11},
  3713  		{11, 11, 10, 11},
  3714  		{11, 11, 11, 11},
  3715  
  3716  		{math.MaxUint16, math.MaxUint16, math.MaxUint16, math.MaxUint16},
  3717  		{math.MaxUint16, math.MaxUint16, math.MaxUint16 - 1, math.MaxUint16},
  3718  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16, math.MaxUint16},
  3719  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3720  	}
  3721  
  3722  	for i, test := range tests {
  3723  		if g, e := MaxUint16Val(test.a, test.b, test.c), test.e; g != e {
  3724  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3725  		}
  3726  		if g, e := MaxUint16Val(test.a), test.a; g != e {
  3727  			t.Fatal(i, test.a, g, e)
  3728  		}
  3729  	}
  3730  }
  3731  
  3732  func TestMinUint16Val(t *testing.T) {
  3733  	tests := []struct{ a, b, c, e uint16 }{
  3734  		{0, 0, 0, 0},
  3735  		{0, 0, 1, 0},
  3736  		{1, 1, 0, 0},
  3737  
  3738  		{10, 10, 10, 10},
  3739  		{10, 10, 11, 10},
  3740  		{11, 11, 10, 10},
  3741  		{11, 11, 11, 11},
  3742  
  3743  		{math.MaxUint16, math.MaxUint16, math.MaxUint16, math.MaxUint16},
  3744  		{math.MaxUint16, math.MaxUint16, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3745  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16, math.MaxUint16 - 1},
  3746  		{math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1, math.MaxUint16 - 1},
  3747  	}
  3748  
  3749  	for i, test := range tests {
  3750  		if g, e := MinUint16Val(test.a, test.b, test.c), test.e; g != e {
  3751  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3752  		}
  3753  		if g, e := MinUint16Val(test.a), test.a; g != e {
  3754  			t.Fatal(i, test.a, g, e)
  3755  		}
  3756  	}
  3757  }
  3758  
  3759  func TestClampUint16(t *testing.T) {
  3760  	tests := []struct{ v, lo, hi, e uint16 }{
  3761  		{0, 0, 0, 0},
  3762  		{5, 10, 20, 10},
  3763  		{10, 10, 20, 10},
  3764  		{15, 10, 20, 15},
  3765  		{20, 10, 20, 20},
  3766  		{25, 10, 20, 20},
  3767  	}
  3768  
  3769  	for _, test := range tests {
  3770  		if g, e := ClampUint16(test.v, test.lo, test.hi), test.e; g != e {
  3771  			t.Fatal(test.v, test.lo, test.hi, g, e)
  3772  		}
  3773  	}
  3774  }
  3775  
  3776  func TestMaxUint32(t *testing.T) {
  3777  	tests := []struct{ a, b, e uint32 }{
  3778  		{0, 0, 0},
  3779  		{0, 1, 1},
  3780  		{1, 0, 1},
  3781  
  3782  		{10, 10, 10},
  3783  		{10, 11, 11},
  3784  		{11, 10, 11},
  3785  		{11, 11, 11},
  3786  
  3787  		{math.MaxUint32, math.MaxUint32, math.MaxUint32},
  3788  		{math.MaxUint32, math.MaxUint32 - 1, math.MaxUint32},
  3789  		{math.MaxUint32 - 1, math.MaxUint32, math.MaxUint32},
  3790  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3791  	}
  3792  
  3793  	for _, test := range tests {
  3794  		if g, e := MaxUint32(test.a, test.b), test.e; g != e {
  3795  			t.Fatal(test.a, test.b, g, e)
  3796  		}
  3797  	}
  3798  }
  3799  
  3800  func TestMinUint32(t *testing.T) {
  3801  	tests := []struct{ a, b, e uint32 }{
  3802  		{0, 0, 0},
  3803  		{0, 1, 0},
  3804  		{1, 0, 0},
  3805  
  3806  		{10, 10, 10},
  3807  		{10, 11, 10},
  3808  		{11, 10, 10},
  3809  		{11, 11, 11},
  3810  
  3811  		{math.MaxUint32, math.MaxUint32, math.MaxUint32},
  3812  		{math.MaxUint32, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3813  		{math.MaxUint32 - 1, math.MaxUint32, math.MaxUint32 - 1},
  3814  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3815  	}
  3816  
  3817  	for _, test := range tests {
  3818  		if g, e := MinUint32(test.a, test.b), test.e; g != e {
  3819  			t.Fatal(test.a, test.b, g, e)
  3820  		}
  3821  	}
  3822  }
  3823  
  3824  func TestMaxUint32Ptr(t *testing.T) {
  3825  	tests := []struct{ a, b, e *uint32 }{
  3826  		{uInt32Ptr(0), uInt32Ptr(0), uInt32Ptr(0)},
  3827  		{uInt32Ptr(0), uInt32Ptr(1), uInt32Ptr(1)},
  3828  		{uInt32Ptr(1), uInt32Ptr(0), uInt32Ptr(1)},
  3829  		{nil, uInt32Ptr(0), uInt32Ptr(0)},
  3830  		{uInt32Ptr(1), nil, uInt32Ptr(1)},
  3831  
  3832  		{uInt32Ptr(10), uInt32Ptr(10), uInt32Ptr(10)},
  3833  		{uInt32Ptr(10), uInt32Ptr(11), uInt32Ptr(11)},
  3834  		{uInt32Ptr(11), uInt32Ptr(10), uInt32Ptr(11)},
  3835  		{uInt32Ptr(11), uInt32Ptr(11), uInt32Ptr(11)},
  3836  		{nil, nil, nil},
  3837  
  3838  		{uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32)},
  3839  		{uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32)},
  3840  		{uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32)},
  3841  		{uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1)},
  3842  		{nil, uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1)},
  3843  		{uInt32Ptr(math.MaxInt32), nil, uInt32Ptr(math.MaxInt32)},
  3844  	}
  3845  
  3846  	for c, test := range tests {
  3847  		g, e := MaxUint32Ptr(test.a, test.b), test.e
  3848  		if e != nil {
  3849  			if *g != *e {
  3850  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3851  			}
  3852  		} else {
  3853  			if e != g {
  3854  				t.Fatal(*test.a, *test.b, *g, e, c)
  3855  			}
  3856  		}
  3857  	}
  3858  }
  3859  
  3860  func TestMinUint32Ptr(t *testing.T) {
  3861  	tests := []struct{ a, b, e *uint32 }{
  3862  		{uInt32Ptr(0), uInt32Ptr(0), uInt32Ptr(0)},
  3863  		{uInt32Ptr(0), uInt32Ptr(1), uInt32Ptr(0)},
  3864  		{uInt32Ptr(1), uInt32Ptr(0), uInt32Ptr(0)},
  3865  		{nil, uInt32Ptr(0), uInt32Ptr(0)},
  3866  		{uInt32Ptr(1), nil, uInt32Ptr(1)},
  3867  
  3868  		{uInt32Ptr(10), uInt32Ptr(10), uInt32Ptr(10)},
  3869  		{uInt32Ptr(10), uInt32Ptr(11), uInt32Ptr(10)},
  3870  		{uInt32Ptr(11), uInt32Ptr(10), uInt32Ptr(10)},
  3871  		{uInt32Ptr(11), uInt32Ptr(11), uInt32Ptr(11)},
  3872  		{nil, nil, nil},
  3873  
  3874  		{uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32)},
  3875  		{uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1)},
  3876  		{uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32), uInt32Ptr(math.MaxInt32 - 1)},
  3877  		{uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1)},
  3878  		{nil, uInt32Ptr(math.MaxInt32 - 1), uInt32Ptr(math.MaxInt32 - 1)},
  3879  		{uInt32Ptr(math.MaxInt32), nil, uInt32Ptr(math.MaxInt32)},
  3880  	}
  3881  
  3882  	for c, test := range tests {
  3883  		g, e := MinUint32Ptr(test.a, test.b), test.e
  3884  		if e != nil {
  3885  			if *g != *e {
  3886  				t.Fatal(*test.a, *test.b, *g, *e, c)
  3887  			}
  3888  		} else {
  3889  			if e != g {
  3890  				t.Fatal(*test.a, *test.b, *g, e, c)
  3891  			}
  3892  		}
  3893  	}
  3894  }
  3895  
  3896  func TestMaxUint32Val(t *testing.T) {
  3897  	tests := []struct{ a, b, c, e uint32 }{
  3898  		{0, 0, 0, 0},
  3899  		{0, 0, 1, 1},
  3900  		{1, 1, 0, 1},
  3901  
  3902  		{10, 10, 10, 10},
  3903  		{10, 10, 11, 11},
  3904  		{11, 11, 10, 11},
  3905  		{11, 11, 11, 11},
  3906  
  3907  		{math.MaxUint32, math.MaxUint32, math.MaxUint32, math.MaxUint32},
  3908  		{math.MaxUint32, math.MaxUint32, math.MaxUint32 - 1, math.MaxUint32},
  3909  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32, math.MaxUint32},
  3910  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3911  	}
  3912  
  3913  	for i, test := range tests {
  3914  		if g, e := MaxUint32Val(test.a, test.b, test.c), test.e; g != e {
  3915  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3916  		}
  3917  		if g, e := MaxUint32Val(test.a), test.a; g != e {
  3918  			t.Fatal(i, test.a, g, e)
  3919  		}
  3920  	}
  3921  }
  3922  
  3923  func TestMinUint32Val(t *testing.T) {
  3924  	tests := []struct{ a, b, c, e uint32 }{
  3925  		{0, 0, 0, 0},
  3926  		{0, 0, 1, 0},
  3927  		{1, 1, 0, 0},
  3928  
  3929  		{10, 10, 10, 10},
  3930  		{10, 10, 11, 10},
  3931  		{11, 11, 10, 10},
  3932  		{11, 11, 11, 11},
  3933  
  3934  		{math.MaxUint32, math.MaxUint32, math.MaxUint32, math.MaxUint32},
  3935  		{math.MaxUint32, math.MaxUint32, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3936  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32, math.MaxUint32 - 1},
  3937  		{math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1, math.MaxUint32 - 1},
  3938  	}
  3939  
  3940  	for i, test := range tests {
  3941  		if g, e := MinUint32Val(test.a, test.b, test.c), test.e; g != e {
  3942  			t.Fatal(i, test.a, test.b, test.c, g, e)
  3943  		}
  3944  		if g, e := MinUint32Val(test.a), test.a; g != e {
  3945  			t.Fatal(i, test.a, g, e)
  3946  		}
  3947  	}
  3948  }
  3949  
  3950  func TestClampUint32(t *testing.T) {
  3951  	tests := []struct{ v, lo, hi, e uint32 }{
  3952  		{0, 0, 0, 0},
  3953  		{5, 10, 20, 10},
  3954  		{10, 10, 20, 10},
  3955  		{15, 10, 20, 15},
  3956  		{20, 10, 20, 20},
  3957  		{25, 10, 20, 20},
  3958  	}
  3959  
  3960  	for _, test := range tests {
  3961  		if g, e := ClampUint32(test.v, test.lo, test.hi), test.e; g != e {
  3962  			t.Fatal(test.v, test.lo, test.hi, g, e)
  3963  		}
  3964  	}
  3965  }
  3966  
  3967  func TestMaxUint64(t *testing.T) {
  3968  	tests := []struct{ a, b, e uint64 }{
  3969  		{0, 0, 0},
  3970  		{0, 1, 1},
  3971  		{1, 0, 1},
  3972  
  3973  		{10, 10, 10},
  3974  		{10, 11, 11},
  3975  		{11, 10, 11},
  3976  		{11, 11, 11},
  3977  
  3978  		{math.MaxUint64, math.MaxUint64, math.MaxUint64},
  3979  		{math.MaxUint64, math.MaxUint64 - 1, math.MaxUint64},
  3980  		{math.MaxUint64 - 1, math.MaxUint64, math.MaxUint64},
  3981  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1},
  3982  	}
  3983  
  3984  	for _, test := range tests {
  3985  		if g, e := MaxUint64(test.a, test.b), test.e; g != e {
  3986  			t.Fatal(test.a, test.b, g, e)
  3987  		}
  3988  	}
  3989  }
  3990  
  3991  func TestMinUint64(t *testing.T) {
  3992  	tests := []struct{ a, b, e uint64 }{
  3993  		{0, 0, 0},
  3994  		{0, 1, 0},
  3995  		{1, 0, 0},
  3996  
  3997  		{10, 10, 10},
  3998  		{10, 11, 10},
  3999  		{11, 10, 10},
  4000  		{11, 11, 11},
  4001  
  4002  		{math.MaxUint64, math.MaxUint64, math.MaxUint64},
  4003  		{math.MaxUint64, math.MaxUint64 - 1, math.MaxUint64 - 1},
  4004  		{math.MaxUint64 - 1, math.MaxUint64, math.MaxUint64 - 1},
  4005  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1},
  4006  	}
  4007  
  4008  	for _, test := range tests {
  4009  		if g, e := MinUint64(test.a, test.b), test.e; g != e {
  4010  			t.Fatal(test.a, test.b, g, e)
  4011  		}
  4012  	}
  4013  }
  4014  
  4015  func TestMaxUint64Ptr(t *testing.T) {
  4016  	tests := []struct{ a, b, e *uint64 }{
  4017  		{uInt64Ptr(0), uInt64Ptr(0), uInt64Ptr(0)},
  4018  		{uInt64Ptr(0), uInt64Ptr(1), uInt64Ptr(1)},
  4019  		{uInt64Ptr(1), uInt64Ptr(0), uInt64Ptr(1)},
  4020  		{nil, uInt64Ptr(0), uInt64Ptr(0)},
  4021  		{uInt64Ptr(1), nil, uInt64Ptr(1)},
  4022  
  4023  		{uInt64Ptr(10), uInt64Ptr(10), uInt64Ptr(10)},
  4024  		{uInt64Ptr(10), uInt64Ptr(11), uInt64Ptr(11)},
  4025  		{uInt64Ptr(11), uInt64Ptr(10), uInt64Ptr(11)},
  4026  		{uInt64Ptr(11), uInt64Ptr(11), uInt64Ptr(11)},
  4027  		{nil, nil, nil},
  4028  
  4029  		{uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64)},
  4030  		{uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64)},
  4031  		{uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64)},
  4032  		{uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1)},
  4033  		{nil, uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1)},
  4034  		{uInt64Ptr(math.MaxUint64), nil, uInt64Ptr(math.MaxUint64)},
  4035  	}
  4036  
  4037  	for c, test := range tests {
  4038  		g, e := MaxUint64Ptr(test.a, test.b), test.e
  4039  		if e != nil {
  4040  			if *g != *e {
  4041  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4042  			}
  4043  		} else {
  4044  			if e != g {
  4045  				t.Fatal(*test.a, *test.b, *g, e, c)
  4046  			}
  4047  		}
  4048  	}
  4049  }
  4050  
  4051  func TestMinUint64Ptr(t *testing.T) {
  4052  	tests := []struct{ a, b, e *uint64 }{
  4053  		{uInt64Ptr(0), uInt64Ptr(0), uInt64Ptr(0)},
  4054  		{uInt64Ptr(0), uInt64Ptr(1), uInt64Ptr(0)},
  4055  		{uInt64Ptr(1), uInt64Ptr(0), uInt64Ptr(0)},
  4056  		{nil, uInt64Ptr(0), uInt64Ptr(0)},
  4057  		{uInt64Ptr(1), nil, uInt64Ptr(1)},
  4058  
  4059  		{uInt64Ptr(10), uInt64Ptr(10), uInt64Ptr(10)},
  4060  		{uInt64Ptr(10), uInt64Ptr(11), uInt64Ptr(10)},
  4061  		{uInt64Ptr(11), uInt64Ptr(10), uInt64Ptr(10)},
  4062  		{uInt64Ptr(11), uInt64Ptr(11), uInt64Ptr(11)},
  4063  		{nil, nil, nil},
  4064  
  4065  		{uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64)},
  4066  		{uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1)},
  4067  		{uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64), uInt64Ptr(math.MaxUint64 - 1)},
  4068  		{uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1)},
  4069  		{nil, uInt64Ptr(math.MaxUint64 - 1), uInt64Ptr(math.MaxUint64 - 1)},
  4070  		{uInt64Ptr(math.MaxUint64), nil, uInt64Ptr(math.MaxUint64)},
  4071  	}
  4072  
  4073  	for c, test := range tests {
  4074  		g, e := MinUint64Ptr(test.a, test.b), test.e
  4075  		if e != nil {
  4076  			if *g != *e {
  4077  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4078  			}
  4079  		} else {
  4080  			if e != g {
  4081  				t.Fatal(*test.a, *test.b, *g, e, c)
  4082  			}
  4083  		}
  4084  	}
  4085  }
  4086  
  4087  func TestMaxUint64Val(t *testing.T) {
  4088  	tests := []struct{ a, b, c, e uint64 }{
  4089  		{0, 0, 0, 0},
  4090  		{0, 0, 1, 1},
  4091  		{1, 1, 0, 1},
  4092  
  4093  		{10, 10, 10, 10},
  4094  		{10, 10, 11, 11},
  4095  		{11, 11, 10, 11},
  4096  		{11, 11, 11, 11},
  4097  
  4098  		{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64},
  4099  		{math.MaxUint64, math.MaxUint64, math.MaxUint64 - 1, math.MaxUint64},
  4100  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64, math.MaxUint64},
  4101  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1},
  4102  	}
  4103  
  4104  	for i, test := range tests {
  4105  		if g, e := MaxUint64Val(test.a, test.b, test.c), test.e; g != e {
  4106  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4107  		}
  4108  		if g, e := MaxUint64Val(test.a), test.a; g != e {
  4109  			t.Fatal(i, test.a, g, e)
  4110  		}
  4111  	}
  4112  }
  4113  
  4114  func TestMinUint64Val(t *testing.T) {
  4115  	tests := []struct{ a, b, c, e uint64 }{
  4116  		{0, 0, 0, 0},
  4117  		{0, 0, 1, 0},
  4118  		{1, 1, 0, 0},
  4119  
  4120  		{10, 10, 10, 10},
  4121  		{10, 10, 11, 10},
  4122  		{11, 11, 10, 10},
  4123  		{11, 11, 11, 11},
  4124  
  4125  		{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64},
  4126  		{math.MaxUint64, math.MaxUint64, math.MaxUint64 - 1, math.MaxUint64 - 1},
  4127  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64, math.MaxUint64 - 1},
  4128  		{math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1, math.MaxUint64 - 1},
  4129  	}
  4130  
  4131  	for i, test := range tests {
  4132  		if g, e := MinUint64Val(test.a, test.b, test.c), test.e; g != e {
  4133  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4134  		}
  4135  		if g, e := MinUint64Val(test.a), test.a; g != e {
  4136  			t.Fatal(i, test.a, g, e)
  4137  		}
  4138  	}
  4139  }
  4140  
  4141  func TestClampUint64(t *testing.T) {
  4142  	tests := []struct{ v, lo, hi, e uint64 }{
  4143  		{0, 0, 0, 0},
  4144  		{5, 10, 20, 10},
  4145  		{10, 10, 20, 10},
  4146  		{15, 10, 20, 15},
  4147  		{20, 10, 20, 20},
  4148  		{25, 10, 20, 20},
  4149  	}
  4150  
  4151  	for _, test := range tests {
  4152  		if g, e := ClampUint64(test.v, test.lo, test.hi), test.e; g != e {
  4153  			t.Fatal(test.v, test.lo, test.hi, g, e)
  4154  		}
  4155  	}
  4156  }
  4157  
  4158  func TestMaxInt8(t *testing.T) {
  4159  	tests := []struct{ a, b, e int8 }{
  4160  		{math.MinInt8, math.MinInt8, math.MinInt8},
  4161  		{math.MinInt8 + 1, math.MinInt8, math.MinInt8 + 1},
  4162  		{math.MinInt8, math.MinInt8 + 1, math.MinInt8 + 1},
  4163  
  4164  		{-1, -1, -1},
  4165  		{-1, 0, 0},
  4166  		{-1, 1, 1},
  4167  
  4168  		{0, -1, 0},
  4169  		{0, 0, 0},
  4170  		{0, 1, 1},
  4171  
  4172  		{1, -1, 1},
  4173  		{1, 0, 1},
  4174  		{1, 1, 1},
  4175  
  4176  		{math.MaxInt8, math.MaxInt8, math.MaxInt8},
  4177  		{math.MaxInt8 - 1, math.MaxInt8, math.MaxInt8},
  4178  		{math.MaxInt8, math.MaxInt8 - 1, math.MaxInt8},
  4179  	}
  4180  
  4181  	for _, test := range tests {
  4182  		if g, e := MaxInt8(test.a, test.b), test.e; g != e {
  4183  			t.Fatal(test.a, test.b, g, e)
  4184  		}
  4185  	}
  4186  }
  4187  
  4188  func TestMinInt8(t *testing.T) {
  4189  	tests := []struct{ a, b, e int8 }{
  4190  		{math.MinInt8, math.MinInt8, math.MinInt8},
  4191  		{math.MinInt8 + 1, math.MinInt8, math.MinInt8},
  4192  		{math.MinInt8, math.MinInt8 + 1, math.MinInt8},
  4193  
  4194  		{-1, -1, -1},
  4195  		{-1, 0, -1},
  4196  		{-1, 1, -1},
  4197  
  4198  		{0, -1, -1},
  4199  		{0, 0, 0},
  4200  		{0, 1, 0},
  4201  
  4202  		{1, -1, -1},
  4203  		{1, 0, 0},
  4204  		{1, 1, 1},
  4205  
  4206  		{math.MaxInt8, math.MaxInt8, math.MaxInt8},
  4207  		{math.MaxInt8 - 1, math.MaxInt8, math.MaxInt8 - 1},
  4208  		{math.MaxInt8, math.MaxInt8 - 1, math.MaxInt8 - 1},
  4209  	}
  4210  
  4211  	for _, test := range tests {
  4212  		if g, e := MinInt8(test.a, test.b), test.e; g != e {
  4213  			t.Fatal(test.a, test.b, g, e)
  4214  		}
  4215  	}
  4216  }
  4217  
  4218  func TestMaxInt8Ptr(t *testing.T) {
  4219  	tests := []struct{ a, b, e *int8 }{
  4220  		{int8Ptr(0), int8Ptr(0), int8Ptr(0)},
  4221  		{int8Ptr(0), int8Ptr(1), int8Ptr(1)},
  4222  		{int8Ptr(1), int8Ptr(0), int8Ptr(1)},
  4223  		{nil, int8Ptr(0), int8Ptr(0)},
  4224  		{int8Ptr(1), nil, int8Ptr(1)},
  4225  
  4226  		{int8Ptr(10), int8Ptr(10), int8Ptr(10)},
  4227  		{int8Ptr(10), int8Ptr(11), int8Ptr(11)},
  4228  		{int8Ptr(11), int8Ptr(10), int8Ptr(11)},
  4229  		{int8Ptr(11), int8Ptr(11), int8Ptr(11)},
  4230  		{nil, nil, nil},
  4231  
  4232  		{int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8)},
  4233  		{int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8)},
  4234  		{int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8)},
  4235  		{int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1)},
  4236  		{nil, int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1)},
  4237  		{int8Ptr(math.MaxInt8), nil, int8Ptr(math.MaxInt8)},
  4238  	}
  4239  
  4240  	for c, test := range tests {
  4241  		g, e := MaxInt8Ptr(test.a, test.b), test.e
  4242  		if e != nil {
  4243  			if *g != *e {
  4244  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4245  			}
  4246  		} else {
  4247  			if e != g {
  4248  				t.Fatal(*test.a, *test.b, *g, e, c)
  4249  			}
  4250  		}
  4251  	}
  4252  }
  4253  
  4254  func TestMinInt8Ptr(t *testing.T) {
  4255  	tests := []struct{ a, b, e *int8 }{
  4256  		{int8Ptr(0), int8Ptr(0), int8Ptr(0)},
  4257  		{int8Ptr(0), int8Ptr(1), int8Ptr(0)},
  4258  		{int8Ptr(1), int8Ptr(0), int8Ptr(0)},
  4259  		{nil, int8Ptr(0), int8Ptr(0)},
  4260  		{int8Ptr(1), nil, int8Ptr(1)},
  4261  
  4262  		{int8Ptr(10), int8Ptr(10), int8Ptr(10)},
  4263  		{int8Ptr(10), int8Ptr(11), int8Ptr(10)},
  4264  		{int8Ptr(11), int8Ptr(10), int8Ptr(10)},
  4265  		{int8Ptr(11), int8Ptr(11), int8Ptr(11)},
  4266  		{nil, nil, nil},
  4267  
  4268  		{int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8)},
  4269  		{int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1)},
  4270  		{int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8), int8Ptr(math.MaxInt8 - 1)},
  4271  		{int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1)},
  4272  		{nil, int8Ptr(math.MaxInt8 - 1), int8Ptr(math.MaxInt8 - 1)},
  4273  		{int8Ptr(math.MaxInt8), nil, int8Ptr(math.MaxInt8)},
  4274  	}
  4275  
  4276  	for c, test := range tests {
  4277  		g, e := MinInt8Ptr(test.a, test.b), test.e
  4278  		if e != nil {
  4279  			if *g != *e {
  4280  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4281  			}
  4282  		} else {
  4283  			if e != g {
  4284  				t.Fatal(*test.a, *test.b, *g, e, c)
  4285  			}
  4286  		}
  4287  	}
  4288  }
  4289  
  4290  func TestMaxInt8Val(t *testing.T) {
  4291  	tests := []struct{ a, b, c, e int8 }{
  4292  		{math.MinInt8, math.MinInt8, math.MinInt8, math.MinInt8},
  4293  		{math.MinInt8, math.MinInt8 + 1, math.MinInt8, math.MinInt8 + 1},
  4294  		{math.MinInt8, math.MinInt8, math.MinInt8 + 1, math.MinInt8 + 1},
  4295  
  4296  		{-1, -1, -1, -1},
  4297  		{-1, -1, 0, 0},
  4298  		{-1, -1, 1, 1},
  4299  
  4300  		{0, 0, -1, 0},
  4301  		{0, 0, 0, 0},
  4302  		{0, 0, 1, 1},
  4303  
  4304  		{1, 1, -1, 1},
  4305  		{1, 1, 0, 1},
  4306  		{1, 1, 1, 1},
  4307  
  4308  		{math.MaxInt8, math.MaxInt8, math.MaxInt8, math.MaxInt8},
  4309  		{math.MaxInt8 - 1, math.MaxInt8 - 1, math.MaxInt8, math.MaxInt8},
  4310  		{math.MaxInt8, math.MaxInt8, math.MaxInt8 - 1, math.MaxInt8},
  4311  	}
  4312  
  4313  	for i, test := range tests {
  4314  		if g, e := MaxInt8Val(test.a, test.b, test.c), test.e; g != e {
  4315  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4316  		}
  4317  		if g, e := MaxInt8Val(test.a), test.a; g != e {
  4318  			t.Fatal(i, test.a, g, e)
  4319  		}
  4320  	}
  4321  }
  4322  
  4323  func TestMinInt8Val(t *testing.T) {
  4324  	tests := []struct{ a, b, c, e int8 }{
  4325  		{math.MinInt8, math.MinInt8, math.MinInt8, math.MinInt8},
  4326  		{math.MinInt8, math.MinInt8 + 1, math.MinInt8, math.MinInt8},
  4327  		{math.MinInt8, math.MinInt8, math.MinInt8 + 1, math.MinInt8},
  4328  
  4329  		{-1, -1, -1, -1},
  4330  		{-1, -1, 0, -1},
  4331  		{-1, -1, 1, -1},
  4332  
  4333  		{0, 0, -1, -1},
  4334  		{0, 0, 0, 0},
  4335  		{0, 0, 1, 0},
  4336  
  4337  		{1, 1, -1, -1},
  4338  		{1, 1, 0, 0},
  4339  		{1, 1, 1, 1},
  4340  
  4341  		{math.MaxInt8, math.MaxInt8, math.MaxInt8, math.MaxInt8},
  4342  		{math.MaxInt8 - 1, math.MaxInt8 - 1, math.MaxInt8, math.MaxInt8 - 1},
  4343  		{math.MaxInt8, math.MaxInt8, math.MaxInt8 - 1, math.MaxInt8 - 1},
  4344  	}
  4345  
  4346  	for i, test := range tests {
  4347  		if g, e := MinInt8Val(test.a, test.b, test.c), test.e; g != e {
  4348  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4349  		}
  4350  		if g, e := MinInt8Val(test.a), test.a; g != e {
  4351  			t.Fatal(i, test.a, g, e)
  4352  		}
  4353  	}
  4354  }
  4355  
  4356  func TestClampInt8(t *testing.T) {
  4357  	tests := []struct{ v, lo, hi, e int8 }{
  4358  		{0, 0, 0, 0},
  4359  		{5, 10, 20, 10},
  4360  		{10, 10, 20, 10},
  4361  		{15, 10, 20, 15},
  4362  		{20, 10, 20, 20},
  4363  		{25, 10, 20, 20},
  4364  	}
  4365  
  4366  	for _, test := range tests {
  4367  		if g, e := ClampInt8(test.v, test.lo, test.hi), test.e; g != e {
  4368  			t.Fatal(test.v, test.lo, test.hi, g, e)
  4369  		}
  4370  	}
  4371  }
  4372  
  4373  func TestMaxInt16(t *testing.T) {
  4374  	tests := []struct{ a, b, e int16 }{
  4375  		{math.MinInt16, math.MinInt16, math.MinInt16},
  4376  		{math.MinInt16 + 1, math.MinInt16, math.MinInt16 + 1},
  4377  		{math.MinInt16, math.MinInt16 + 1, math.MinInt16 + 1},
  4378  
  4379  		{-1, -1, -1},
  4380  		{-1, 0, 0},
  4381  		{-1, 1, 1},
  4382  
  4383  		{0, -1, 0},
  4384  		{0, 0, 0},
  4385  		{0, 1, 1},
  4386  
  4387  		{1, -1, 1},
  4388  		{1, 0, 1},
  4389  		{1, 1, 1},
  4390  
  4391  		{math.MaxInt16, math.MaxInt16, math.MaxInt16},
  4392  		{math.MaxInt16 - 1, math.MaxInt16, math.MaxInt16},
  4393  		{math.MaxInt16, math.MaxInt16 - 1, math.MaxInt16},
  4394  	}
  4395  
  4396  	for _, test := range tests {
  4397  		if g, e := MaxInt16(test.a, test.b), test.e; g != e {
  4398  			t.Fatal(test.a, test.b, g, e)
  4399  		}
  4400  	}
  4401  }
  4402  
  4403  func TestMinInt16(t *testing.T) {
  4404  	tests := []struct{ a, b, e int16 }{
  4405  		{math.MinInt16, math.MinInt16, math.MinInt16},
  4406  		{math.MinInt16 + 1, math.MinInt16, math.MinInt16},
  4407  		{math.MinInt16, math.MinInt16 + 1, math.MinInt16},
  4408  
  4409  		{-1, -1, -1},
  4410  		{-1, 0, -1},
  4411  		{-1, 1, -1},
  4412  
  4413  		{0, -1, -1},
  4414  		{0, 0, 0},
  4415  		{0, 1, 0},
  4416  
  4417  		{1, -1, -1},
  4418  		{1, 0, 0},
  4419  		{1, 1, 1},
  4420  
  4421  		{math.MaxInt16, math.MaxInt16, math.MaxInt16},
  4422  		{math.MaxInt16 - 1, math.MaxInt16, math.MaxInt16 - 1},
  4423  		{math.MaxInt16, math.MaxInt16 - 1, math.MaxInt16 - 1},
  4424  	}
  4425  
  4426  	for _, test := range tests {
  4427  		if g, e := MinInt16(test.a, test.b), test.e; g != e {
  4428  			t.Fatal(test.a, test.b, g, e)
  4429  		}
  4430  	}
  4431  }
  4432  
  4433  func TestMaxInt16Ptr(t *testing.T) {
  4434  	tests := []struct{ a, b, e *int16 }{
  4435  		{int16Ptr(0), int16Ptr(0), int16Ptr(0)},
  4436  		{int16Ptr(0), int16Ptr(1), int16Ptr(1)},
  4437  		{int16Ptr(1), int16Ptr(0), int16Ptr(1)},
  4438  		{nil, int16Ptr(0), int16Ptr(0)},
  4439  		{int16Ptr(1), nil, int16Ptr(1)},
  4440  
  4441  		{int16Ptr(10), int16Ptr(10), int16Ptr(10)},
  4442  		{int16Ptr(10), int16Ptr(11), int16Ptr(11)},
  4443  		{int16Ptr(11), int16Ptr(10), int16Ptr(11)},
  4444  		{int16Ptr(11), int16Ptr(11), int16Ptr(11)},
  4445  		{nil, nil, nil},
  4446  
  4447  		{int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8)},
  4448  		{int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8)},
  4449  		{int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8)},
  4450  		{int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1)},
  4451  		{nil, int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1)},
  4452  		{int16Ptr(math.MaxInt8), nil, int16Ptr(math.MaxInt8)},
  4453  	}
  4454  
  4455  	for c, test := range tests {
  4456  		g, e := MaxInt16Ptr(test.a, test.b), test.e
  4457  		if e != nil {
  4458  			if *g != *e {
  4459  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4460  			}
  4461  		} else {
  4462  			if e != g {
  4463  				t.Fatal(*test.a, *test.b, *g, e, c)
  4464  			}
  4465  		}
  4466  	}
  4467  }
  4468  
  4469  func TestMinInt16Ptr(t *testing.T) {
  4470  	tests := []struct{ a, b, e *int16 }{
  4471  		{int16Ptr(0), int16Ptr(0), int16Ptr(0)},
  4472  		{int16Ptr(0), int16Ptr(1), int16Ptr(0)},
  4473  		{int16Ptr(1), int16Ptr(0), int16Ptr(0)},
  4474  		{nil, int16Ptr(0), int16Ptr(0)},
  4475  		{int16Ptr(1), nil, int16Ptr(1)},
  4476  
  4477  		{int16Ptr(10), int16Ptr(10), int16Ptr(10)},
  4478  		{int16Ptr(10), int16Ptr(11), int16Ptr(10)},
  4479  		{int16Ptr(11), int16Ptr(10), int16Ptr(10)},
  4480  		{int16Ptr(11), int16Ptr(11), int16Ptr(11)},
  4481  		{nil, nil, nil},
  4482  
  4483  		{int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8)},
  4484  		{int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1)},
  4485  		{int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8), int16Ptr(math.MaxInt8 - 1)},
  4486  		{int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1)},
  4487  		{nil, int16Ptr(math.MaxInt8 - 1), int16Ptr(math.MaxInt8 - 1)},
  4488  		{int16Ptr(math.MaxInt8), nil, int16Ptr(math.MaxInt8)},
  4489  	}
  4490  
  4491  	for c, test := range tests {
  4492  		g, e := MinInt16Ptr(test.a, test.b), test.e
  4493  		if e != nil {
  4494  			if *g != *e {
  4495  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4496  			}
  4497  		} else {
  4498  			if e != g {
  4499  				t.Fatal(*test.a, *test.b, *g, e, c)
  4500  			}
  4501  		}
  4502  	}
  4503  }
  4504  
  4505  func TestMaxInt16Val(t *testing.T) {
  4506  	tests := []struct{ a, b, c, e int16 }{
  4507  		{math.MinInt16, math.MinInt16, math.MinInt16, math.MinInt16},
  4508  		{math.MinInt16, math.MinInt16 + 1, math.MinInt16, math.MinInt16 + 1},
  4509  		{math.MinInt16, math.MinInt16, math.MinInt16 + 1, math.MinInt16 + 1},
  4510  
  4511  		{-1, -1, -1, -1},
  4512  		{-1, -1, 0, 0},
  4513  		{-1, -1, 1, 1},
  4514  
  4515  		{0, 0, -1, 0},
  4516  		{0, 0, 0, 0},
  4517  		{0, 0, 1, 1},
  4518  
  4519  		{1, 1, -1, 1},
  4520  		{1, 1, 0, 1},
  4521  		{1, 1, 1, 1},
  4522  
  4523  		{math.MaxInt16, math.MaxInt16, math.MaxInt16, math.MaxInt16},
  4524  		{math.MaxInt16 - 1, math.MaxInt16 - 1, math.MaxInt16, math.MaxInt16},
  4525  		{math.MaxInt16, math.MaxInt16, math.MaxInt16 - 1, math.MaxInt16},
  4526  	}
  4527  
  4528  	for i, test := range tests {
  4529  		if g, e := MaxInt16Val(test.a, test.b, test.c), test.e; g != e {
  4530  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4531  		}
  4532  		if g, e := MaxInt16Val(test.a), test.a; g != e {
  4533  			t.Fatal(i, test.a, g, e)
  4534  		}
  4535  	}
  4536  }
  4537  
  4538  func TestMinInt16Val(t *testing.T) {
  4539  	tests := []struct{ a, b, c, e int16 }{
  4540  		{math.MinInt16, math.MinInt16, math.MinInt16, math.MinInt16},
  4541  		{math.MinInt16, math.MinInt16 + 1, math.MinInt16, math.MinInt16},
  4542  		{math.MinInt16, math.MinInt16, math.MinInt16 + 1, math.MinInt16},
  4543  
  4544  		{-1, -1, -1, -1},
  4545  		{-1, -1, 0, -1},
  4546  		{-1, -1, 1, -1},
  4547  
  4548  		{0, 0, -1, -1},
  4549  		{0, 0, 0, 0},
  4550  		{0, 0, 1, 0},
  4551  
  4552  		{1, 1, -1, -1},
  4553  		{1, 1, 0, 0},
  4554  		{1, 1, 1, 1},
  4555  
  4556  		{math.MaxInt16, math.MaxInt16, math.MaxInt16, math.MaxInt16},
  4557  		{math.MaxInt16 - 1, math.MaxInt16 - 1, math.MaxInt16, math.MaxInt16 - 1},
  4558  		{math.MaxInt16, math.MaxInt16, math.MaxInt16 - 1, math.MaxInt16 - 1},
  4559  	}
  4560  
  4561  	for i, test := range tests {
  4562  		if g, e := MinInt16Val(test.a, test.b, test.c), test.e; g != e {
  4563  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4564  		}
  4565  		if g, e := MinInt16Val(test.a), test.a; g != e {
  4566  			t.Fatal(i, test.a, g, e)
  4567  		}
  4568  	}
  4569  }
  4570  
  4571  func TestClampInt16(t *testing.T) {
  4572  	tests := []struct{ v, lo, hi, e int16 }{
  4573  		{0, 0, 0, 0},
  4574  		{5, 10, 20, 10},
  4575  		{10, 10, 20, 10},
  4576  		{15, 10, 20, 15},
  4577  		{20, 10, 20, 20},
  4578  		{25, 10, 20, 20},
  4579  	}
  4580  
  4581  	for _, test := range tests {
  4582  		if g, e := ClampInt16(test.v, test.lo, test.hi), test.e; g != e {
  4583  			t.Fatal(test.v, test.lo, test.hi, g, e)
  4584  		}
  4585  	}
  4586  }
  4587  
  4588  func TestMaxInt32(t *testing.T) {
  4589  	tests := []struct{ a, b, e int32 }{
  4590  		{math.MinInt32, math.MinInt32, math.MinInt32},
  4591  		{math.MinInt32 + 1, math.MinInt32, math.MinInt32 + 1},
  4592  		{math.MinInt32, math.MinInt32 + 1, math.MinInt32 + 1},
  4593  
  4594  		{-1, -1, -1},
  4595  		{-1, 0, 0},
  4596  		{-1, 1, 1},
  4597  
  4598  		{0, -1, 0},
  4599  		{0, 0, 0},
  4600  		{0, 1, 1},
  4601  
  4602  		{1, -1, 1},
  4603  		{1, 0, 1},
  4604  		{1, 1, 1},
  4605  
  4606  		{math.MaxInt32, math.MaxInt32, math.MaxInt32},
  4607  		{math.MaxInt32 - 1, math.MaxInt32, math.MaxInt32},
  4608  		{math.MaxInt32, math.MaxInt32 - 1, math.MaxInt32},
  4609  	}
  4610  
  4611  	for _, test := range tests {
  4612  		if g, e := MaxInt32(test.a, test.b), test.e; g != e {
  4613  			t.Fatal(test.a, test.b, g, e)
  4614  		}
  4615  	}
  4616  }
  4617  
  4618  func TestMinInt32(t *testing.T) {
  4619  	tests := []struct{ a, b, e int32 }{
  4620  		{math.MinInt32, math.MinInt32, math.MinInt32},
  4621  		{math.MinInt32 + 1, math.MinInt32, math.MinInt32},
  4622  		{math.MinInt32, math.MinInt32 + 1, math.MinInt32},
  4623  
  4624  		{-1, -1, -1},
  4625  		{-1, 0, -1},
  4626  		{-1, 1, -1},
  4627  
  4628  		{0, -1, -1},
  4629  		{0, 0, 0},
  4630  		{0, 1, 0},
  4631  
  4632  		{1, -1, -1},
  4633  		{1, 0, 0},
  4634  		{1, 1, 1},
  4635  
  4636  		{math.MaxInt32, math.MaxInt32, math.MaxInt32},
  4637  		{math.MaxInt32 - 1, math.MaxInt32, math.MaxInt32 - 1},
  4638  		{math.MaxInt32, math.MaxInt32 - 1, math.MaxInt32 - 1},
  4639  	}
  4640  
  4641  	for _, test := range tests {
  4642  		if g, e := MinInt32(test.a, test.b), test.e; g != e {
  4643  			t.Fatal(test.a, test.b, g, e)
  4644  		}
  4645  	}
  4646  }
  4647  
  4648  func TestMaxInt32Ptr(t *testing.T) {
  4649  	tests := []struct{ a, b, e *int32 }{
  4650  		{int32Ptr(0), int32Ptr(0), int32Ptr(0)},
  4651  		{int32Ptr(0), int32Ptr(1), int32Ptr(1)},
  4652  		{int32Ptr(1), int32Ptr(0), int32Ptr(1)},
  4653  		{nil, int32Ptr(0), int32Ptr(0)},
  4654  		{int32Ptr(1), nil, int32Ptr(1)},
  4655  
  4656  		{int32Ptr(10), int32Ptr(10), int32Ptr(10)},
  4657  		{int32Ptr(10), int32Ptr(11), int32Ptr(11)},
  4658  		{int32Ptr(11), int32Ptr(10), int32Ptr(11)},
  4659  		{int32Ptr(11), int32Ptr(11), int32Ptr(11)},
  4660  		{nil, nil, nil},
  4661  
  4662  		{int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32)},
  4663  		{int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32)},
  4664  		{int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32)},
  4665  		{int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1)},
  4666  		{nil, int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1)},
  4667  		{int32Ptr(math.MaxInt32), nil, int32Ptr(math.MaxInt32)},
  4668  	}
  4669  
  4670  	for c, test := range tests {
  4671  		g, e := MaxInt32Ptr(test.a, test.b), test.e
  4672  		if e != nil {
  4673  			if *g != *e {
  4674  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4675  			}
  4676  		} else {
  4677  			if e != g {
  4678  				t.Fatal(*test.a, *test.b, *g, e, c)
  4679  			}
  4680  		}
  4681  	}
  4682  }
  4683  
  4684  func TestMinInt32Ptr(t *testing.T) {
  4685  	tests := []struct{ a, b, e *int32 }{
  4686  		{int32Ptr(0), int32Ptr(0), int32Ptr(0)},
  4687  		{int32Ptr(0), int32Ptr(1), int32Ptr(0)},
  4688  		{int32Ptr(1), int32Ptr(0), int32Ptr(0)},
  4689  		{nil, int32Ptr(0), int32Ptr(0)},
  4690  		{int32Ptr(1), nil, int32Ptr(1)},
  4691  
  4692  		{int32Ptr(10), int32Ptr(10), int32Ptr(10)},
  4693  		{int32Ptr(10), int32Ptr(11), int32Ptr(10)},
  4694  		{int32Ptr(11), int32Ptr(10), int32Ptr(10)},
  4695  		{int32Ptr(11), int32Ptr(11), int32Ptr(11)},
  4696  		{nil, nil, nil},
  4697  
  4698  		{int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32)},
  4699  		{int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1)},
  4700  		{int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32), int32Ptr(math.MaxInt32 - 1)},
  4701  		{int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1)},
  4702  		{nil, int32Ptr(math.MaxInt32 - 1), int32Ptr(math.MaxInt32 - 1)},
  4703  		{int32Ptr(math.MaxInt32), nil, int32Ptr(math.MaxInt32)},
  4704  	}
  4705  
  4706  	for c, test := range tests {
  4707  		g, e := MinInt32Ptr(test.a, test.b), test.e
  4708  		if e != nil {
  4709  			if *g != *e {
  4710  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4711  			}
  4712  		} else {
  4713  			if e != g {
  4714  				t.Fatal(*test.a, *test.b, *g, e, c)
  4715  			}
  4716  		}
  4717  	}
  4718  }
  4719  
  4720  func TestMaxInt32Val(t *testing.T) {
  4721  	tests := []struct{ a, b, c, e int32 }{
  4722  		{math.MinInt32, math.MinInt32, math.MinInt32, math.MinInt32},
  4723  		{math.MinInt32, math.MinInt32 + 1, math.MinInt32, math.MinInt32 + 1},
  4724  		{math.MinInt32, math.MinInt32, math.MinInt32 + 1, math.MinInt32 + 1},
  4725  
  4726  		{-1, -1, -1, -1},
  4727  		{-1, -1, 0, 0},
  4728  		{-1, -1, 1, 1},
  4729  
  4730  		{0, 0, -1, 0},
  4731  		{0, 0, 0, 0},
  4732  		{0, 0, 1, 1},
  4733  
  4734  		{1, 1, -1, 1},
  4735  		{1, 1, 0, 1},
  4736  		{1, 1, 1, 1},
  4737  
  4738  		{math.MaxInt32, math.MaxInt32, math.MaxInt32, math.MaxInt32},
  4739  		{math.MaxInt32 - 1, math.MaxInt32 - 1, math.MaxInt32, math.MaxInt32},
  4740  		{math.MaxInt32, math.MaxInt32, math.MaxInt32 - 1, math.MaxInt32},
  4741  	}
  4742  
  4743  	for i, test := range tests {
  4744  		if g, e := MaxInt32Val(test.a, test.b, test.c), test.e; g != e {
  4745  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4746  		}
  4747  		if g, e := MaxInt32Val(test.a), test.a; g != e {
  4748  			t.Fatal(i, test.a, g, e)
  4749  		}
  4750  	}
  4751  }
  4752  
  4753  func TestMinInt32Val(t *testing.T) {
  4754  	tests := []struct{ a, b, c, e int32 }{
  4755  		{math.MinInt32, math.MinInt32, math.MinInt32, math.MinInt32},
  4756  		{math.MinInt32, math.MinInt32 + 1, math.MinInt32, math.MinInt32},
  4757  		{math.MinInt32, math.MinInt32, math.MinInt32 + 1, math.MinInt32},
  4758  
  4759  		{-1, -1, -1, -1},
  4760  		{-1, -1, 0, -1},
  4761  		{-1, -1, 1, -1},
  4762  
  4763  		{0, 0, -1, -1},
  4764  		{0, 0, 0, 0},
  4765  		{0, 0, 1, 0},
  4766  
  4767  		{1, 1, -1, -1},
  4768  		{1, 1, 0, 0},
  4769  		{1, 1, 1, 1},
  4770  
  4771  		{math.MaxInt32, math.MaxInt32, math.MaxInt32, math.MaxInt32},
  4772  		{math.MaxInt32 - 1, math.MaxInt32 - 1, math.MaxInt32, math.MaxInt32 - 1},
  4773  		{math.MaxInt32, math.MaxInt32, math.MaxInt32 - 1, math.MaxInt32 - 1},
  4774  	}
  4775  
  4776  	for i, test := range tests {
  4777  		if g, e := MinInt32Val(test.a, test.b, test.c), test.e; g != e {
  4778  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4779  		}
  4780  		if g, e := MinInt32Val(test.a), test.a; g != e {
  4781  			t.Fatal(i, test.a, g, e)
  4782  		}
  4783  	}
  4784  }
  4785  
  4786  func TestClampInt32(t *testing.T) {
  4787  	tests := []struct{ v, lo, hi, e int32 }{
  4788  		{0, 0, 0, 0},
  4789  		{5, 10, 20, 10},
  4790  		{10, 10, 20, 10},
  4791  		{15, 10, 20, 15},
  4792  		{20, 10, 20, 20},
  4793  		{25, 10, 20, 20},
  4794  	}
  4795  
  4796  	for _, test := range tests {
  4797  		if g, e := ClampInt32(test.v, test.lo, test.hi), test.e; g != e {
  4798  			t.Fatal(test.v, test.lo, test.hi, g, e)
  4799  		}
  4800  	}
  4801  }
  4802  
  4803  func TestMaxInt64(t *testing.T) {
  4804  	tests := []struct{ a, b, e int64 }{
  4805  		{math.MinInt64, math.MinInt64, math.MinInt64},
  4806  		{math.MinInt64 + 1, math.MinInt64, math.MinInt64 + 1},
  4807  		{math.MinInt64, math.MinInt64 + 1, math.MinInt64 + 1},
  4808  
  4809  		{-1, -1, -1},
  4810  		{-1, 0, 0},
  4811  		{-1, 1, 1},
  4812  
  4813  		{0, -1, 0},
  4814  		{0, 0, 0},
  4815  		{0, 1, 1},
  4816  
  4817  		{1, -1, 1},
  4818  		{1, 0, 1},
  4819  		{1, 1, 1},
  4820  
  4821  		{math.MaxInt64, math.MaxInt64, math.MaxInt64},
  4822  		{math.MaxInt64 - 1, math.MaxInt64, math.MaxInt64},
  4823  		{math.MaxInt64, math.MaxInt64 - 1, math.MaxInt64},
  4824  	}
  4825  
  4826  	for _, test := range tests {
  4827  		if g, e := MaxInt64(test.a, test.b), test.e; g != e {
  4828  			t.Fatal(test.a, test.b, g, e)
  4829  		}
  4830  	}
  4831  }
  4832  
  4833  func TestMinInt64(t *testing.T) {
  4834  	tests := []struct{ a, b, e int64 }{
  4835  		{math.MinInt64, math.MinInt64, math.MinInt64},
  4836  		{math.MinInt64 + 1, math.MinInt64, math.MinInt64},
  4837  		{math.MinInt64, math.MinInt64 + 1, math.MinInt64},
  4838  
  4839  		{-1, -1, -1},
  4840  		{-1, 0, -1},
  4841  		{-1, 1, -1},
  4842  
  4843  		{0, -1, -1},
  4844  		{0, 0, 0},
  4845  		{0, 1, 0},
  4846  
  4847  		{1, -1, -1},
  4848  		{1, 0, 0},
  4849  		{1, 1, 1},
  4850  
  4851  		{math.MaxInt64, math.MaxInt64, math.MaxInt64},
  4852  		{math.MaxInt64 - 1, math.MaxInt64, math.MaxInt64 - 1},
  4853  		{math.MaxInt64, math.MaxInt64 - 1, math.MaxInt64 - 1},
  4854  	}
  4855  
  4856  	for _, test := range tests {
  4857  		if g, e := MinInt64(test.a, test.b), test.e; g != e {
  4858  			t.Fatal(test.a, test.b, g, e)
  4859  		}
  4860  	}
  4861  }
  4862  
  4863  func TestMaxInt64Ptr(t *testing.T) {
  4864  	tests := []struct{ a, b, e *int64 }{
  4865  		{int64Ptr(0), int64Ptr(0), int64Ptr(0)},
  4866  		{int64Ptr(0), int64Ptr(1), int64Ptr(1)},
  4867  		{int64Ptr(1), int64Ptr(0), int64Ptr(1)},
  4868  		{nil, int64Ptr(0), int64Ptr(0)},
  4869  		{int64Ptr(1), nil, int64Ptr(1)},
  4870  
  4871  		{int64Ptr(10), int64Ptr(10), int64Ptr(10)},
  4872  		{int64Ptr(10), int64Ptr(11), int64Ptr(11)},
  4873  		{int64Ptr(11), int64Ptr(10), int64Ptr(11)},
  4874  		{int64Ptr(11), int64Ptr(11), int64Ptr(11)},
  4875  		{nil, nil, nil},
  4876  
  4877  		{int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64)},
  4878  		{int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64)},
  4879  		{int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64)},
  4880  		{int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1)},
  4881  		{nil, int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1)},
  4882  		{int64Ptr(math.MaxInt64), nil, int64Ptr(math.MaxInt64)},
  4883  	}
  4884  
  4885  	for c, test := range tests {
  4886  		g, e := MaxInt64Ptr(test.a, test.b), test.e
  4887  		if e != nil {
  4888  			if *g != *e {
  4889  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4890  			}
  4891  		} else {
  4892  			if e != g {
  4893  				t.Fatal(*test.a, *test.b, *g, e, c)
  4894  			}
  4895  		}
  4896  	}
  4897  }
  4898  
  4899  func TestMinInt64Ptr(t *testing.T) {
  4900  	tests := []struct{ a, b, e *int64 }{
  4901  		{int64Ptr(0), int64Ptr(0), int64Ptr(0)},
  4902  		{int64Ptr(0), int64Ptr(1), int64Ptr(0)},
  4903  		{int64Ptr(1), int64Ptr(0), int64Ptr(0)},
  4904  		{nil, int64Ptr(0), int64Ptr(0)},
  4905  		{int64Ptr(1), nil, int64Ptr(1)},
  4906  
  4907  		{int64Ptr(10), int64Ptr(10), int64Ptr(10)},
  4908  		{int64Ptr(10), int64Ptr(11), int64Ptr(10)},
  4909  		{int64Ptr(11), int64Ptr(10), int64Ptr(10)},
  4910  		{int64Ptr(11), int64Ptr(11), int64Ptr(11)},
  4911  		{nil, nil, nil},
  4912  
  4913  		{int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64)},
  4914  		{int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1)},
  4915  		{int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64), int64Ptr(math.MaxInt64 - 1)},
  4916  		{int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1)},
  4917  		{nil, int64Ptr(math.MaxInt64 - 1), int64Ptr(math.MaxInt64 - 1)},
  4918  		{int64Ptr(math.MaxInt64), nil, int64Ptr(math.MaxInt64)},
  4919  	}
  4920  
  4921  	for c, test := range tests {
  4922  		g, e := MinInt64Ptr(test.a, test.b), test.e
  4923  		if e != nil {
  4924  			if *g != *e {
  4925  				t.Fatal(*test.a, *test.b, *g, *e, c)
  4926  			}
  4927  		} else {
  4928  			if e != g {
  4929  				t.Fatal(*test.a, *test.b, *g, e, c)
  4930  			}
  4931  		}
  4932  	}
  4933  }
  4934  
  4935  func TestMaxInt64Val(t *testing.T) {
  4936  	tests := []struct{ a, b, c, e int64 }{
  4937  		{math.MinInt64, math.MinInt64, math.MinInt64, math.MinInt64},
  4938  		{math.MinInt64, math.MinInt64 + 1, math.MinInt64, math.MinInt64 + 1},
  4939  		{math.MinInt64, math.MinInt64, math.MinInt64 + 1, math.MinInt64 + 1},
  4940  
  4941  		{-1, -1, -1, -1},
  4942  		{-1, -1, 0, 0},
  4943  		{-1, -1, 1, 1},
  4944  
  4945  		{0, 0, -1, 0},
  4946  		{0, 0, 0, 0},
  4947  		{0, 0, 1, 1},
  4948  
  4949  		{1, 1, -1, 1},
  4950  		{1, 1, 0, 1},
  4951  		{1, 1, 1, 1},
  4952  
  4953  		{math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64},
  4954  		{math.MaxInt64 - 1, math.MaxInt64 - 1, math.MaxInt64, math.MaxInt64},
  4955  		{math.MaxInt64, math.MaxInt64, math.MaxInt64 - 1, math.MaxInt64},
  4956  	}
  4957  
  4958  	for i, test := range tests {
  4959  		if g, e := MaxInt64Val(test.a, test.b, test.c), test.e; g != e {
  4960  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4961  		}
  4962  		if g, e := MaxInt64Val(test.a), test.a; g != e {
  4963  			t.Fatal(i, test.a, g, e)
  4964  		}
  4965  	}
  4966  }
  4967  
  4968  func TestMinInt64Val(t *testing.T) {
  4969  	tests := []struct{ a, b, c, e int64 }{
  4970  		{math.MinInt64, math.MinInt64, math.MinInt64, math.MinInt64},
  4971  		{math.MinInt64, math.MinInt64 + 1, math.MinInt64, math.MinInt64},
  4972  		{math.MinInt64, math.MinInt64, math.MinInt64 + 1, math.MinInt64},
  4973  
  4974  		{-1, -1, -1, -1},
  4975  		{-1, -1, 0, -1},
  4976  		{-1, -1, 1, -1},
  4977  
  4978  		{0, 0, -1, -1},
  4979  		{0, 0, 0, 0},
  4980  		{0, 0, 1, 0},
  4981  
  4982  		{1, 1, -1, -1},
  4983  		{1, 1, 0, 0},
  4984  		{1, 1, 1, 1},
  4985  
  4986  		{math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64},
  4987  		{math.MaxInt64 - 1, math.MaxInt64 - 1, math.MaxInt64, math.MaxInt64 - 1},
  4988  		{math.MaxInt64, math.MaxInt64, math.MaxInt64 - 1, math.MaxInt64 - 1},
  4989  	}
  4990  
  4991  	for i, test := range tests {
  4992  		if g, e := MinInt64Val(test.a, test.b, test.c), test.e; g != e {
  4993  			t.Fatal(i, test.a, test.b, test.c, g, e)
  4994  		}
  4995  		if g, e := MinInt64Val(test.a), test.a; g != e {
  4996  			t.Fatal(i, test.a, g, e)
  4997  		}
  4998  	}
  4999  }
  5000  
  5001  func TestClampInt64(t *testing.T) {
  5002  	tests := []struct{ v, lo, hi, e int64 }{
  5003  		{0, 0, 0, 0},
  5004  		{5, 10, 20, 10},
  5005  		{10, 10, 20, 10},
  5006  		{15, 10, 20, 15},
  5007  		{20, 10, 20, 20},
  5008  		{25, 10, 20, 20},
  5009  	}
  5010  
  5011  	for _, test := range tests {
  5012  		if g, e := ClampInt64(test.v, test.lo, test.hi), test.e; g != e {
  5013  			t.Fatal(test.v, test.lo, test.hi, g, e)
  5014  		}
  5015  	}
  5016  }
  5017  
  5018  func TestPopCountBigInt(t *testing.T) {
  5019  	const N = 1e4
  5020  	rng := rand.New(rand.NewSource(42))
  5021  	lim := big.NewInt(0)
  5022  	lim.SetBit(lim, 1e3, 1)
  5023  	z := big.NewInt(0)
  5024  	m1 := big.NewInt(-1)
  5025  	for i := 0; i < N; i++ {
  5026  		z.Rand(rng, lim)
  5027  		g := PopCountBigInt(z)
  5028  		e := 0
  5029  		for bit := 0; bit < z.BitLen(); bit++ {
  5030  			if z.Bit(bit) != 0 {
  5031  				e++
  5032  			}
  5033  		}
  5034  		if g != e {
  5035  			t.Fatal(g, e)
  5036  		}
  5037  
  5038  		z.Mul(z, m1)
  5039  		if g := PopCountBigInt(z); g != e {
  5040  			t.Fatal(g, e)
  5041  		}
  5042  	}
  5043  }
  5044  
  5045  func benchmarkPopCountBigInt(b *testing.B, bits int) {
  5046  	lim := big.NewInt(0)
  5047  	lim.SetBit(lim, bits, 1)
  5048  	z := big.NewInt(0)
  5049  	z.Rand(rand.New(rand.NewSource(42)), lim)
  5050  	b.ResetTimer()
  5051  	for i := 0; i < b.N; i++ {
  5052  		PopCountBigInt(z)
  5053  	}
  5054  }
  5055  
  5056  func BenchmarkPopCountBigInt1e1(b *testing.B) {
  5057  	benchmarkPopCountBigInt(b, 1e1)
  5058  }
  5059  
  5060  func BenchmarkPopCountBigInt1e2(b *testing.B) {
  5061  	benchmarkPopCountBigInt(b, 1e2)
  5062  }
  5063  
  5064  func BenchmarkPopCountBigInt1e3(b *testing.B) {
  5065  	benchmarkPopCountBigInt(b, 1e3)
  5066  }
  5067  
  5068  func BenchmarkPopCountBigIbnt1e4(b *testing.B) {
  5069  	benchmarkPopCountBigInt(b, 1e4)
  5070  }
  5071  
  5072  func BenchmarkPopCountBigInt1e5(b *testing.B) {
  5073  	benchmarkPopCountBigInt(b, 1e5)
  5074  }
  5075  
  5076  func BenchmarkPopCountBigInt1e6(b *testing.B) {
  5077  	benchmarkPopCountBigInt(b, 1e6)
  5078  }
  5079  
  5080  func TestToBase(t *testing.T) {
  5081  	x := ToBase(big.NewInt(0), 42)
  5082  	e := []int{0}
  5083  	if g, e := len(x), len(e); g != e {
  5084  		t.Fatal(g, e)
  5085  	}
  5086  
  5087  	for i, g := range x {
  5088  		if e := e[i]; g != e {
  5089  			t.Fatal(i, g, e)
  5090  		}
  5091  	}
  5092  
  5093  	x = ToBase(big.NewInt(2047), 22)
  5094  	e = []int{1, 5, 4}
  5095  	if g, e := len(x), len(e); g != e {
  5096  		t.Fatal(g, e)
  5097  	}
  5098  
  5099  	for i, g := range x {
  5100  		if e := e[i]; g != e {
  5101  			t.Fatal(i, g, e)
  5102  		}
  5103  	}
  5104  
  5105  	x = ToBase(big.NewInt(-2047), 22)
  5106  	e = []int{-1, -5, -4}
  5107  	if g, e := len(x), len(e); g != e {
  5108  		t.Fatal(g, e)
  5109  	}
  5110  
  5111  	for i, g := range x {
  5112  		if e := e[i]; g != e {
  5113  			t.Fatal(i, g, e)
  5114  		}
  5115  	}
  5116  }
  5117  
  5118  func TestBug(t *testing.T) {
  5119  	if BitLenUint(MaxUint) != 64 {
  5120  		t.Logf("Bug reproducible only on 64 bit architecture")
  5121  		return
  5122  	}
  5123  
  5124  	_, err := NewFC32(MinInt, MaxInt, true)
  5125  	if err == nil {
  5126  		t.Fatal("Expected non nil err")
  5127  	}
  5128  }
  5129  
  5130  func poly(a ...int) string {
  5131  	var b bytes.Buffer
  5132  	for i, v := range a {
  5133  		p := len(a) - i - 1
  5134  		if v == 0 && p != 0 {
  5135  			continue
  5136  		}
  5137  
  5138  		if v == 0 && p == 0 && b.Len() != 0 {
  5139  			continue
  5140  		}
  5141  
  5142  		if av := abs(v); av == 1 && p != 0 {
  5143  			if b.Len() != 0 {
  5144  				if v == 1 {
  5145  					b.WriteByte('+')
  5146  				} else {
  5147  					b.WriteByte('-')
  5148  				}
  5149  			} else if v == -1 {
  5150  				b.WriteByte('-')
  5151  			}
  5152  		} else {
  5153  			switch {
  5154  			case b.Len() == 0:
  5155  				fmt.Fprintf(&b, "%d", v)
  5156  			default:
  5157  				fmt.Fprintf(&b, "%+d", v)
  5158  			}
  5159  		}
  5160  
  5161  		if p == 0 {
  5162  			continue
  5163  		}
  5164  
  5165  		if p == 1 {
  5166  			fmt.Fprintf(&b, "x")
  5167  			continue
  5168  		}
  5169  
  5170  		fmt.Fprintf(&b, "x^%d", p)
  5171  	}
  5172  	return b.String()
  5173  }
  5174  
  5175  func polyBig(a ...*big.Int) string {
  5176  	var b bytes.Buffer
  5177  	for i, v := range a {
  5178  		p := len(a) - i - 1
  5179  		if v.Sign() == 0 && p != 0 {
  5180  			continue
  5181  		}
  5182  
  5183  		if v.Sign() == 0 && p == 0 && b.Len() != 0 {
  5184  			continue
  5185  		}
  5186  
  5187  		if av := bigAbs(v); av.Cmp(_1) == 0 && p != 0 {
  5188  			if b.Len() != 0 {
  5189  				if v.Cmp(_1) == 0 {
  5190  					b.WriteByte('+')
  5191  				} else {
  5192  					b.WriteByte('-')
  5193  				}
  5194  			} else if v.Cmp(_m1) == 0 {
  5195  				b.WriteByte('-')
  5196  			}
  5197  		} else {
  5198  			switch {
  5199  			case b.Len() == 0:
  5200  				fmt.Fprintf(&b, "%d", v)
  5201  			default:
  5202  				fmt.Fprintf(&b, "%+d", v)
  5203  			}
  5204  		}
  5205  
  5206  		if p == 0 {
  5207  			continue
  5208  		}
  5209  
  5210  		if p == 1 {
  5211  			fmt.Fprintf(&b, "x")
  5212  			continue
  5213  		}
  5214  
  5215  		fmt.Fprintf(&b, "x^%d", p)
  5216  	}
  5217  	return b.String()
  5218  }
  5219  
  5220  func polyK(k int) string {
  5221  	switch {
  5222  	case k == -1:
  5223  		return "-"
  5224  	case k == 1:
  5225  		return ""
  5226  	default:
  5227  		return fmt.Sprint(k)
  5228  	}
  5229  }
  5230  
  5231  func polyKBig(k *big.Int) string {
  5232  	switch {
  5233  	case k.Cmp(_m1) == 0:
  5234  		return "-"
  5235  	case k.Cmp(_1) == 0:
  5236  		return ""
  5237  	default:
  5238  		return fmt.Sprint(k)
  5239  	}
  5240  }
  5241  
  5242  func TestQuadPolyDiscriminantBig(t *testing.T) {
  5243  	for i, test := range []struct {
  5244  		a, b, c, ds, d int
  5245  	}{
  5246  		{-1, -5, 6, 49, 7},
  5247  		{-1, 5, 6, 49, 7},
  5248  		{1, -5, -6, 49, 7},
  5249  		{1, 5, -6, 49, 7},
  5250  		{1, 5, 6, 1, 1},
  5251  		{2, 3, 5, -31, -1},
  5252  		{2, 7, 3, 25, 5},
  5253  		{3, 8, 5, 4, 2},
  5254  		{3, 9, 5, 21, -1},
  5255  		{4, 5, 1, 9, 3},
  5256  		{5, 3, 2, -31, -1},
  5257  	} {
  5258  		ds, d := QuadPolyDiscriminantBig(big.NewInt(int64(test.a)), big.NewInt(int64(test.b)), big.NewInt(int64(test.c)))
  5259  		if g, e := ds, big.NewInt(int64(test.ds)); g.Cmp(e) != 0 {
  5260  			t.Fatal(i, g, e)
  5261  		}
  5262  
  5263  		switch {
  5264  		case test.d < 0:
  5265  			if d != nil {
  5266  				t.Fatal(i, d, nil)
  5267  			}
  5268  		default:
  5269  			if g, e := d, big.NewInt(int64(test.d)); g.Cmp(e) != 0 {
  5270  				t.Fatal(i, g, e)
  5271  			}
  5272  		}
  5273  	}
  5274  }
  5275  
  5276  func testQuadPolyFactorsBig(t *testing.T, p1, q1, p2, q2, k *big.Int, cases int) {
  5277  	a := bigMul(k, bigMul(p1, p2))
  5278  	b := bigMul(k, bigAdd(bigMul(p1, q2), bigMul(q1, p2)))
  5279  	c := bigMul(k, bigMul(q1, q2))
  5280  	con, f := QuadPolyFactorsBig(a, b, c)
  5281  
  5282  	switch {
  5283  	case a.Sign() == 0:
  5284  		if g, e := len(f), 1; g != e {
  5285  			t.Fatalf(
  5286  				"%d: %s(%s)(%s) = %s -> got %v factors, expected %v",
  5287  				cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5288  				g, e,
  5289  			)
  5290  		}
  5291  
  5292  		a2 := big.NewInt(0)
  5293  		b2 := bigMul(con, f[0].P)
  5294  		c2 := bigMul(con, f[0].Q)
  5295  		if a.Cmp(a2) != 0 || b.Cmp(b2) != 0 || c.Cmp(c2) != 0 {
  5296  			t.Fatalf(
  5297  				"%d: %s(%s)(%s) = %s -> %s(%s) = %s",
  5298  				cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5299  				polyKBig(con), polyBig(f[0].P, f[0].Q), polyBig(a2, b2, c2),
  5300  			)
  5301  		}
  5302  
  5303  		t.Logf(
  5304  			"%d: %s(%s)(%s) = %s -> %s(%s) = %s",
  5305  			cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5306  			polyKBig(con), polyBig(f[0].P, f[0].Q), polyBig(a2, b2, c2),
  5307  		)
  5308  	default:
  5309  		if g, e := len(f), 2; g != e {
  5310  			t.Fatalf(
  5311  				"%d: %s(%s)(%s) = %s -> got %v factors, expected %v",
  5312  				cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5313  				g, e,
  5314  			)
  5315  		}
  5316  
  5317  		a2 := bigMul(con, bigMul(f[0].P, f[1].P))
  5318  		b2 := bigMul(con, bigAdd(bigMul(f[0].P, f[1].Q), bigMul(f[0].Q, f[1].P)))
  5319  		c2 := bigMul(con, bigMul(f[0].Q, f[1].Q))
  5320  		if a.Cmp(a2) != 0 || b.Cmp(b2) != 0 || c.Cmp(c2) != 0 {
  5321  			t.Fatalf(
  5322  				"%d: %s(%s)(%s) = %s -> %s(%s)(%s) = %s",
  5323  				cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5324  				polyKBig(con), polyBig(f[0].P, f[0].Q), polyBig(f[1].P, f[1].Q), polyBig(a2, b2, c2),
  5325  			)
  5326  		}
  5327  
  5328  		t.Logf(
  5329  			"%d: %s(%s)(%s) = %s -> %s(%s)(%s) = %s",
  5330  			cases, polyKBig(k), polyBig(p1, q1), polyBig(p2, q2), polyBig(a, b, c),
  5331  			polyKBig(con), polyBig(f[0].P, f[0].Q), polyBig(f[1].P, f[1].Q), polyBig(a2, b2, c2),
  5332  		)
  5333  	}
  5334  }
  5335  
  5336  func TestQuadPolyFactorsBig(t *testing.T) {
  5337  	cases := 0
  5338  
  5339  	const N = 1e4
  5340  	rng := rand.New(rand.NewSource(42))
  5341  	for i := 0; i < N; i++ {
  5342  		p1 := big.NewInt(rng.Int63())
  5343  		q1 := big.NewInt(rng.Int63())
  5344  		p2 := big.NewInt(rng.Int63())
  5345  		q2 := big.NewInt(rng.Int63())
  5346  		k := big.NewInt(rng.Int63())
  5347  		testQuadPolyFactorsBig(t, p1, q1, p2, q2, k, cases)
  5348  		cases++
  5349  	}
  5350  
  5351  	cons := []int{-1, 1}
  5352  	const lim = 7
  5353  	for p1 := -lim; p1 <= lim; p1++ {
  5354  		for q1 := -lim; q1 <= lim; q1++ {
  5355  			for p2 := -lim; p2 <= lim; p2++ {
  5356  				for q2 := -lim; q2 <= lim; q2++ {
  5357  					for _, k := range cons {
  5358  						testQuadPolyFactorsBig(
  5359  							t,
  5360  							big.NewInt(int64(p1)),
  5361  							big.NewInt(int64(q1)),
  5362  							big.NewInt(int64(p2)),
  5363  							big.NewInt(int64(q2)),
  5364  							big.NewInt(int64(k)),
  5365  							cases,
  5366  						)
  5367  						cases++
  5368  					}
  5369  				}
  5370  			}
  5371  		}
  5372  	}
  5373  }
  5374  
  5375  func TestQuadPolyDiscriminant(t *testing.T) {
  5376  	for i, test := range []struct {
  5377  		a, b, c, ds, d int
  5378  	}{
  5379  		{-1, -5, 6, 49, 7},
  5380  		{-1, 5, 6, 49, 7},
  5381  		{1, -5, -6, 49, 7},
  5382  		{1, 5, -6, 49, 7},
  5383  		{1, 5, 6, 1, 1},
  5384  		{2, 3, 5, -31, -1},
  5385  		{2, 7, 3, 25, 5},
  5386  		{3, 8, 5, 4, 2},
  5387  		{3, 9, 5, 21, -1},
  5388  		{4, 5, 1, 9, 3},
  5389  		{5, 3, 2, -31, -1},
  5390  	} {
  5391  		ds, d, err := QuadPolyDiscriminant(test.a, test.b, test.c)
  5392  		if err != nil {
  5393  			t.Fatal(i, err)
  5394  		}
  5395  
  5396  		if g, e := ds, test.ds; g != e {
  5397  			t.Fatal(i, g, e)
  5398  		}
  5399  
  5400  		if g, e := d, test.d; g != e {
  5401  			t.Fatal(i, g, e)
  5402  		}
  5403  	}
  5404  }
  5405  
  5406  func testQuadPolyFactors(t *testing.T, p1, q1, p2, q2, k, cases int) {
  5407  	a := k * p1 * p2
  5408  	b := k * (p1*q2 + q1*p2)
  5409  	c := k * (q1 * q2)
  5410  	con, f, err := QuadPolyFactors(a, b, c)
  5411  	if err != nil {
  5412  		t.Fatalf(
  5413  			"%d: %s(%s)(%s) = %s -> %v",
  5414  			cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5415  			err,
  5416  		)
  5417  	}
  5418  
  5419  	switch {
  5420  	case a == 0:
  5421  		if g, e := len(f), 1; g != e {
  5422  			t.Fatalf(
  5423  				"%d: %s(%s)(%s) = %s -> got %v factors, expected %v",
  5424  				cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5425  				g, e,
  5426  			)
  5427  		}
  5428  
  5429  		a2 := 0
  5430  		b2 := con * f[0].P
  5431  		c2 := con * f[0].Q
  5432  		if a != a2 || b != b2 || c != c2 {
  5433  			t.Fatalf(
  5434  				"%d: %s(%s)(%s) = %s -> %s(%s) = %s",
  5435  				cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5436  				polyK(con), poly(f[0].P, f[0].Q), poly(a2, b2, c2),
  5437  			)
  5438  		}
  5439  
  5440  		t.Logf(
  5441  			"%d: %s(%s)(%s) = %s -> %s(%s) = %s",
  5442  			cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5443  			polyK(con), poly(f[0].P, f[0].Q), poly(a2, b2, c2),
  5444  		)
  5445  	default:
  5446  		if g, e := len(f), 2; g != e {
  5447  			t.Fatalf(
  5448  				"%d: %s(%s)(%s) = %s -> got %v factors, expected %v",
  5449  				cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5450  				g, e,
  5451  			)
  5452  		}
  5453  
  5454  		a2 := con * f[0].P * f[1].P
  5455  		b2 := con * (f[0].P*f[1].Q + f[0].Q*f[1].P)
  5456  		c2 := con * f[0].Q * f[1].Q
  5457  		if a != a2 || b != b2 || c != c2 {
  5458  			t.Fatalf(
  5459  				"%d: %s(%s)(%s) = %s -> %s(%s)(%s) = %s",
  5460  				cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5461  				polyK(con), poly(f[0].P, f[0].Q), poly(f[1].P, f[1].Q), poly(a2, b2, c2),
  5462  			)
  5463  		}
  5464  
  5465  		t.Logf(
  5466  			"%d: %s(%s)(%s) = %s -> %s(%s)(%s) = %s",
  5467  			cases, polyK(k), poly(p1, q1), poly(p2, q2), poly(a, b, c),
  5468  			polyK(con), poly(f[0].P, f[0].Q), poly(f[1].P, f[1].Q), poly(a2, b2, c2),
  5469  		)
  5470  	}
  5471  }
  5472  
  5473  func TestQuadPolyFactors(t *testing.T) {
  5474  	cases := 0
  5475  
  5476  	const N = 1e4
  5477  	mask := 1<<14 - 1
  5478  	if IntBits < 64 {
  5479  		mask = 1<<7 - 1
  5480  	}
  5481  	rng := rand.New(rand.NewSource(42))
  5482  	for i := 0; i < N; i++ {
  5483  		p1 := int(rng.Int63()) & mask
  5484  		q1 := int(rng.Int63()) & mask
  5485  		p2 := int(rng.Int63()) & mask
  5486  		q2 := int(rng.Int63()) & mask
  5487  		k := int(rng.Int63()) & mask
  5488  		testQuadPolyFactors(t, p1, q1, p2, q2, k, cases)
  5489  		cases++
  5490  	}
  5491  
  5492  	cons := []int{-1, 1}
  5493  	const lim = 7
  5494  	for p1 := -lim; p1 <= lim; p1++ {
  5495  		for q1 := -lim; q1 <= lim; q1++ {
  5496  			for p2 := -lim; p2 <= lim; p2++ {
  5497  				for q2 := -lim; q2 <= lim; q2++ {
  5498  					for _, k := range cons {
  5499  						testQuadPolyFactors(t, p1, q1, p2, q2, k, cases)
  5500  						cases++
  5501  					}
  5502  				}
  5503  			}
  5504  		}
  5505  	}
  5506  }
  5507  
  5508  // https://github.com/cznic/sqlite/issues/12#issuecomment-310155204
  5509  func TestFCPRNG(t *testing.T) {
  5510  	const N = 131072
  5511  	rng, err := NewFC32(1, N, true)
  5512  	if err != nil {
  5513  		t.Fatal(err)
  5514  	}
  5515  
  5516  	var mods, exp [3]int
  5517  	m := make(map[int]byte, N)
  5518  	for i := 1; i <= N; i++ {
  5519  		n := rng.Next()
  5520  		if _, ok := m[n]; ok {
  5521  			t.Fatal(i, n)
  5522  		}
  5523  
  5524  		m[n] = 1
  5525  		mods[n%len(mods)]++
  5526  	}
  5527  	if g, e := len(m), N; g != e {
  5528  		t.Fatal(g, e)
  5529  	}
  5530  
  5531  	for i := 1; i <= N; i++ {
  5532  		n := rng.Next()
  5533  		if m[n] != 1 {
  5534  			t.Fatal(i, n)
  5535  		}
  5536  
  5537  		m[n] = 0
  5538  	}
  5539  
  5540  	for i := 1; i <= N; i++ {
  5541  		exp[i%len(mods)]++
  5542  	}
  5543  
  5544  	for i, g := range mods {
  5545  		if e := exp[i]; g != e {
  5546  			t.Fatal(g, e)
  5547  		}
  5548  	}
  5549  
  5550  	t.Log(mods)
  5551  }