-
Notifications
You must be signed in to change notification settings - Fork 1
/
dices_test.go
305 lines (277 loc) · 6.85 KB
/
dices_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Package rpg provides tools to develop rpg games
package rpg
import (
"fmt"
"math"
"testing"
)
/*func TestNewDice(sides int) *Dice {
return &Dice{sides}
}
func Test(d *Dice) Roll() int {
return rand.Intn(d.sides)
}
*/
const ACCEPTABLE_ERROR = 0.1
// Test that D2() returns a random integer between 1 and 2
func TestD2(t *testing.T) {
var v int
v = D2()
if v < 1 || v > 2 {
t.Error("Expected value between 1 and 2", v)
}
}
// Test that D4() returns a random integer between 1 and 4
func TestD4(t *testing.T) {
var v int
v = D4()
if v < 1 || v > 4 {
t.Error("Expected value between 1 and 4", v)
}
}
// Test that D6() returns a random integer between 1 and 6
func TestD6(t *testing.T) {
var v int
v = D6()
if v < 1 || v > 6 {
t.Error("Expected value between 1 and 6", v)
}
}
// Test that D8() returns a random integer between 1 and 8
func TestD8(t *testing.T) {
var v int
v = D8()
if v < 1 || v > 8 {
t.Error("Expected value between 1 and 8", v)
}
}
// Test that D10() returns a random integer between 1 and 10
func TestD10(t *testing.T) {
var v int
v = D10()
if v < 1 || v > 10 {
t.Error("Expected value between 1 and 10", v)
}
}
// Test that D12() returns a random integer between 1 and 12
func TestD12(t *testing.T) {
var v int
v = D12()
if v < 1 || v > 12 {
t.Error("Expected value between 1 and 12", v)
}
}
// Test that D20() returns a random integer between 1 and 20
func TestD20(t *testing.T) {
var v int
v = D20()
if v < 1 || v > 20 {
t.Error("Expected value between 1 and 20", v)
}
}
// Test that D30() returns a random integer between 1 and 30
func TestD30(t *testing.T) {
var v int
v = D30()
if v < 1 || v > 30 {
t.Error("Expected value between 1 and 30", v)
}
}
// Test that D100() returns a random integer between 1 and 100
func TestD100(t *testing.T) {
var v int
v = D100()
if v < 1 || v > 100 {
t.Error("Expected value between 1 and 100", v)
}
}
// Test that D200() returns a random integer between 1 and 200
func TestD200(t *testing.T) {
var v int
v = D200()
if v < 1 || v > 200 {
t.Error("Expected value between 1 and 200", v)
}
}
// Test that D1000() returns a random integer between 1 and 1000
func TestD1000(t *testing.T) {
var v int
v = D1000()
if v < 1 || v > 1000 {
t.Error("Expected value between 1 and 1000", v)
}
}
// Test that average of 1000000 D6() is arround 3.5
func Test1000000D2(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D2()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D2: ", average)
if math.Abs(average-1.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 1.4 and 1.6", average)
}
}
// Test that average of 1000000 D4() is arround 3.5
func Test1000000D4(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D4()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D4: ", average)
if math.Abs(average-2.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 2.4 and 2.6", average)
}
}
// Test that average of 1000000 D6() is arround 3.5
func Test1000000D6(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D6()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D6: ", average)
if math.Abs(average-3.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 3.4 and 3.6", average)
}
}
// Test that average of 1000000 D8() is arround 3.5
func Test1000000D8(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D8()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D8: ", average)
if math.Abs(average-4.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 4.4 and 4.6", average)
}
}
// Test that average of 1000000 D10() is arround 3.5
func Test1000000D10(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D10()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D10: ", average)
if math.Abs(average-5.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 5.4 and 5.6", average)
}
}
// Test that average of 1000000 D12() is arround 3.5
func Test1000000D12(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D12()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D12: ", average)
if math.Abs(average-6.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 6.4 and 6.6", average)
}
}
// Test that average of 1000000 D20() is arround 3.5
func Test1000000D20(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D20()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D20: ", average)
if math.Abs(average-10.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 10.4 and 10.6", average)
}
}
// Test that average of 1000000 D30() is arround 3.5
func Test1000000D30(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D30()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D30: ", average)
if math.Abs(average-15.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 15.4 and 15.6", average)
}
}
// Test that average of 1000000 D100() is arround 3.5
func Test1000000D100(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D100()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D100: ", average)
if math.Abs(average-50.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 50.4 and 50.6", average)
}
}
// Test that average of 1000000 D200() is arround 3.5
func Test1000000D200(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 1000000; i++ {
sum += D200()
}
var average float64
average = float64(sum) / 1000000.0
fmt.Println("Average D200: ", average)
if math.Abs(average-100.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 100.4 and 100.6", average)
}
}
// Test that average of 10000000 D1000() is arround 3.5
func Test10000000D1000(t *testing.T) {
var sum int
sum = 0
for i := 0; i < 10000000; i++ {
sum += D1000()
}
var average float64
average = float64(sum) / 10000000.0
fmt.Println("Average D1000: ", average)
if math.Abs(average-500.5) > ACCEPTABLE_ERROR {
t.Error("Expected value between 500.4 and 500.6", average)
}
}
// Test a dice with random sideso
func TestRandomDice(t *testing.T) {
// Create a random dice
var sides int
sides = D100()
var expectedAverage float64
expectedAverage = float64(sides+1) / 2.0
d := NewDice(sides)
// Test it
var sum int
for i := 0; i < 10000000; i++ {
sum += d.Roll()
}
var average float64
average = float64(sum) / 10000000.0
fmt.Printf("Average D%d: %g\n", sides, average)
if math.Abs(average-expectedAverage) > ACCEPTABLE_ERROR {
t.Error("Expected value for D", sides, " was around ", expectedAverage, " with an ", ACCEPTABLE_ERROR, " error")
}
}