Debian Codegraph @

sortedset/sortedset_test.go

raw
1 package sortedset
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "math/rand"
7 "sort"
8 "testing"
9 )
10
11 func assertArraysEqual(t *testing.T, expected, actual []byte, size int) {
12 t.Helper()
13
14 if !bytes.Equal(expected, actual) {
15 t.Logf("\nexpected (%d):\n%s\nfound (%d):\n%s",
16 len(expected), hex.Dump(expected),
17 len(actual), hex.Dump(actual))
18 t.Fatal("arrays are not equal")
19 }
20 }
21
22 func randomSortedArray(prng *rand.Rand, size int, count int, repeatChance float64) (array []byte, uniques []byte) {
23 if count == 0 {
24 return nil, nil
25 }
26
27 // Generate `count` random chunks of `size` bytes and then sort them.
28 pool := make([]byte, size*count)
29 prng.Read(pool)
30 sortArray(pool, size)
31
32 // Sanity checks — the items must be unique and sorted.
33 for i := size; i < len(pool); i += size {
34 switch bytes.Compare(pool[i-size:i], pool[i:i+size]) {
35 case 0:
36 panic("duplicate item in pool")
37 case 1:
38 panic("not sorted correctly")
39 }
40 }
41
42 array = make([]byte, 0, size*count)
43
44 // Build an array from the pool of unique items, using the configurable
45 // chance of repeat. A repeatChance of 0 will yield an array where every
46 // item is unique, while a repeatChance of 1 will yield an array where
47 // every item is a duplicate of the first item.
48 uniq := size
49 for i := 0; i < count; i++ {
50 array = append(array, pool[uniq-size:uniq]...)
51 if prng.Float64() >= repeatChance && i != count-1 {
52 uniq += size
53 }
54 }
55
56 // Return a second array with just the unique items.
57 uniques = pool[:uniq]
58 return
59 }
60
61 func randomSortedSet(prng *rand.Rand, size int, count int) []byte {
62 _, set := randomSortedArray(prng, size, count, 0.0)
63 return set
64 }
65
66 func randomSortedSetPair(prng *rand.Rand, size int, count int, overlapChance float64) ([]byte, []byte) {
67 setA := randomSortedSet(prng, size, count)
68 setB := randomSortedSet(prng, size, count)
69
70 // Sanity check: there must be no duplicates.
71 if len(combineArrays(setA, setB, size)) != count*size*2 {
72 panic("sorted sets overlap")
73 }
74
75 // Build a new set by taking items from both setA and setB depending
76 // on the value of overlapChance.
77 split := int(float64(count)*overlapChance) * size
78 overlap := combineArrays(setA[:split], setB[:len(setB)-split], size)
79
80 return setA, overlap
81 }
82
83 func combineArrays(a, b []byte, size int) []byte {
84 return sortArray(append(append([]byte{}, a...), b...), size)
85 }
86
87 func sortArray(b []byte, size int) []byte {
88 sort.Sort(&chunks{b: b, size: size})
89 return b
90 }
91
92 type chunks struct {
93 b []byte
94 size int
95 tmp []byte
96 }
97
98 func (s *chunks) Len() int {
99 return len(s.b) / s.size
100 }
101
102 func (s *chunks) Less(i, j int) bool {
103 return bytes.Compare(s.slice(i), s.slice(j)) < 0
104 }
105
106 func (s *chunks) Swap(i, j int) {
107 tmp := make([]byte, s.size)
108 copy(tmp, s.slice(j))
109 copy(s.slice(j), s.slice(i))
110 copy(s.slice(i), tmp)
111 }
112
113 func (s *chunks) slice(i int) []byte {
114 return s.b[i*s.size : (i+1)*s.size]
115 }