1package rand
2
3import (
4	. "github.com/onsi/ginkgo"
5	. "github.com/onsi/gomega"
6)
7
8var _ = Describe("Nearest Power of 2 uint64", func() {
9	Context("With different uint64 inputs", func() {
10		It("should return uint64 that are of 2^x", func() {
11			Expect(nextNearestPow2uint64(2)).To(Equal(uint64(2)))
12			Expect(nextNearestPow2uint64(3)).To(Equal(uint64(4)))
13			Expect(nextNearestPow2uint64(5)).To(Equal(uint64(8)))
14			Expect(nextNearestPow2uint64(10)).To(Equal(uint64(16)))
15		})
16	})
17})
18
19var _ = Describe("Fast modulus", func() {
20	Context("With different integers in the power of 2", func() {
21		It("should return the correct modulus", func() {
22			scenarios := []struct {
23				number   int
24				divisor  int
25				expected int
26			}{
27				{
28					number:   13,
29					divisor:  4,
30					expected: 1,
31				},
32				{
33					number:   13,
34					divisor:  8,
35					expected: 5,
36				},
37				{
38					number:   16,
39					divisor:  8,
40					expected: 0,
41				},
42			}
43			for _, sc := range scenarios {
44				ans := sc.number & (sc.divisor - 1)
45				Expect(ans).To(Equal(sc.expected), "should be equal")
46			}
47		})
48	})
49})
50
51var _ = Describe("Pool", func() {
52	Context("With any input size", func() {
53		It("should return a pool of random number generators of length in the power 2", func() {
54			pool := NewPool(1, 3)
55			Expect(len(pool.sources)).To(Equal(4))
56		})
57	})
58})
59