v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/mathutil/reduction_test.go (about)

     1  package mathutil
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  )
     7  
     8  func Test_BoundUint32(t *testing.T) {
     9  	for N := 1; N < 100; N++ {
    10  		for x := 0; x < 1000000; x++ {
    11  			if got := BoundUint32(rand.Uint32(), uint32(N)); got >= uint32(N) {
    12  				t.Errorf("reduce() >= N: got=%d, N=%d", got, N)
    13  			}
    14  		}
    15  	}
    16  }
    17  
    18  func Test_BoundUint16(t *testing.T) {
    19  	for N := 1; N < 100; N++ {
    20  		for x := 0; x < 1000000; x++ {
    21  			if got := BoundUint16(uint16(rand.Uint32()), uint16(N)); got >= uint16(N) {
    22  				t.Errorf("reduce() >= N: got=%d, N=%d", got, N)
    23  			}
    24  		}
    25  	}
    26  }
    27  
    28  func BenchmarkBoundUint16(b *testing.B) {
    29  	for i := 0; i < b.N; i++ {
    30  		for N := 1; N < 1024; N++ {
    31  			for x := 0; x < N; x++ {
    32  				v := BoundUint16(uint16(x), uint16(N))
    33  				_ = v
    34  			}
    35  		}
    36  	}
    37  }
    38  
    39  func BenchmarkBoundUint32(b *testing.B) {
    40  	for i := 0; i < b.N; i++ {
    41  		for N := 1; N < 1024; N++ {
    42  			for x := 0; x < N; x++ {
    43  				v := BoundUint32(uint32(x), uint32(N))
    44  				_ = v
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func boundUint32Mod(x uint32, N uint32) uint32 {
    51  	return x % N
    52  }
    53  func BenchmarkBoundUint32Mod(b *testing.B) {
    54  	for i := 0; i < b.N; i++ {
    55  		for N := 1; N < 1024; N++ {
    56  			for x := 0; x < N; x++ {
    57  				v := boundUint32Mod(uint32(x), uint32(N))
    58  				_ = v
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  func boundUint16Mod(x uint16, N uint16) uint16 {
    65  	return x % N
    66  }
    67  func BenchmarkBoundUint16Mod(b *testing.B) {
    68  	for i := 0; i < b.N; i++ {
    69  		for N := 1; N < 1024; N++ {
    70  			for x := 0; x < N; x++ {
    71  				v := boundUint16Mod(uint16(x), uint16(N))
    72  				_ = v
    73  			}
    74  		}
    75  	}
    76  }