| 1 |
package random |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
// To execute tests: |
| 8 |
// |
| 9 |
// go test -v github.com/antithesishq/antithesis-sdk-go/random |
| 10 |
|
| 11 |
func TestRandomChoice(t *testing.T) { |
| 12 |
type Thing struct { |
| 13 |
chosenCount int |
| 14 |
} |
| 15 |
|
| 16 |
choices := []any{ |
| 17 |
&Thing{}, |
| 18 |
&Thing{}, |
| 19 |
&Thing{}, |
| 20 |
&Thing{}, |
| 21 |
&Thing{}, |
| 22 |
} |
| 23 |
|
| 24 |
const N = 100 |
| 25 |
for i := 0; i < N; i++ { |
| 26 |
chosen := RandomChoice(choices) |
| 27 |
chosen.(*Thing).chosenCount += 1 |
| 28 |
} |
| 29 |
|
| 30 |
for i, anything := range choices { |
| 31 |
thing := anything.(*Thing) |
| 32 |
t.Logf("Thing %d/%d was chosen %d times", i+1, len(choices), thing.chosenCount) |
| 33 |
if thing.chosenCount == 0 { |
| 34 |
t.Fatalf("Some element was never chosen in %d random choices!", N) |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestChoiceGenericCompatibility(t *testing.T) { |
| 40 |
type Thing struct { |
| 41 |
chosenCount int |
| 42 |
} |
| 43 |
|
| 44 |
choices := []any{ |
| 45 |
&Thing{}, |
| 46 |
&Thing{}, |
| 47 |
&Thing{}, |
| 48 |
&Thing{}, |
| 49 |
&Thing{}, |
| 50 |
} |
| 51 |
|
| 52 |
const N = 100 |
| 53 |
for i := 0; i < N; i++ { |
| 54 |
chosen := RandomChoice(choices) |
| 55 |
chosen.(*Thing).chosenCount += 1 |
| 56 |
} |
| 57 |
|
| 58 |
for i, anything := range choices { |
| 59 |
thing := anything.(*Thing) |
| 60 |
t.Logf("Thing %d/%d was chosen %d times", i+1, len(choices), thing.chosenCount) |
| 61 |
if thing.chosenCount == 0 { |
| 62 |
t.Fatalf("Some element was never chosen in %d random choices!", N) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestChoiceGenericMixedArrayCompatibility(t *testing.T) { |
| 68 |
type This struct { |
| 69 |
thisCount int |
| 70 |
} |
| 71 |
|
| 72 |
type That struct { |
| 73 |
thatCount int |
| 74 |
} |
| 75 |
|
| 76 |
choices := []any{ |
| 77 |
&This{}, |
| 78 |
&This{}, |
| 79 |
&That{}, |
| 80 |
&That{}, |
| 81 |
&That{}, |
| 82 |
} |
| 83 |
|
| 84 |
const N = 100 |
| 85 |
for i := 0; i < N; i++ { |
| 86 |
chosen := RandomChoice(choices) |
| 87 |
if t1, ok := chosen.(*This); ok { |
| 88 |
t1.thisCount += 1 |
| 89 |
} |
| 90 |
if t2, ok := chosen.(*That); ok { |
| 91 |
t2.thatCount += 1 |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
for i, anything := range choices { |
| 96 |
if t1, ok := anything.(*This); ok { |
| 97 |
t.Logf("This at index %d of %d was chosen %d times", i+1, len(choices), t1.thisCount) |
| 98 |
if t1.thisCount == 0 { |
| 99 |
t.Fatalf("'This' element was never chosen in %d random choices!", N) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if t2, ok := anything.(*That); ok { |
| 104 |
t.Logf("'That' at index %d of %d was chosen %d times", i+1, len(choices), t2.thatCount) |
| 105 |
if t2.thatCount == 0 { |
| 106 |
t.Fatalf("'That' element was never chosen in %d random choices!", N) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestRandomChoiceGenericMixedPrimitives(t *testing.T) { |
| 113 |
|
| 114 |
choices := []any{ |
| 115 |
"Hello", |
| 116 |
12.4, |
| 117 |
"How", |
| 118 |
true, |
| 119 |
"You", |
| 120 |
10025, |
| 121 |
} |
| 122 |
|
| 123 |
counts := make(map[any]int) |
| 124 |
const N = 100 |
| 125 |
for i := 0; i < N; i++ { |
| 126 |
chosen := RandomChoice(choices) |
| 127 |
counts[chosen] += 1 |
| 128 |
} |
| 129 |
|
| 130 |
for i, s := range choices { |
| 131 |
count, present := counts[s] |
| 132 |
t.Logf("Item %v %d/%d was chosen %d times", s, i+1, len(choices), count) |
| 133 |
if !present { |
| 134 |
t.Fatalf("Some element was never chosen in %d random choices!", N) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func TestRandomChoiceGeneric(t *testing.T) { |
| 140 |
|
| 141 |
choices := []string{ |
| 142 |
"Hello", |
| 143 |
"World", |
| 144 |
"How", |
| 145 |
"Are", |
| 146 |
"You", |
| 147 |
"?", |
| 148 |
} |
| 149 |
|
| 150 |
counts := make(map[string]int) |
| 151 |
const N = 100 |
| 152 |
for i := 0; i < N; i++ { |
| 153 |
chosen := RandomChoice(choices) |
| 154 |
counts[chosen] += 1 |
| 155 |
} |
| 156 |
|
| 157 |
for i, s := range choices { |
| 158 |
count, present := counts[s] |
| 159 |
t.Logf("Item %d/%d was chosen %d times", i+1, len(choices), count) |
| 160 |
if !present { |
| 161 |
t.Fatalf("Some element was never chosen in %d random choices!", N) |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestEmptyFloatChoiceGeneric(t *testing.T) { |
| 167 |
var choices []float64 |
| 168 |
|
| 169 |
got := RandomChoice(choices) |
| 170 |
want := float64(0.0) |
| 171 |
if got != want { |
| 172 |
t.Fatalf("Unexpected choice received - got %v want %f", got, want) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestEmptyStringChoiceGeneric(t *testing.T) { |
| 177 |
var choices []string |
| 178 |
|
| 179 |
got := RandomChoice(choices) |
| 180 |
want := "" |
| 181 |
if got != want { |
| 182 |
t.Fatalf("Unexpected choice received - got %v want %s", got, want) |
| 183 |
} |
| 184 |
} |