m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/__tests__/dice.test.js
blob: 1274b3de8f3b76105d38aa928d6cf2e2cbd0bb16 (plain)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const {
  pool,
  constant,
  d,
  add,
  subtract,
  negative,
  explode
} = require('../src/dice.js')

const defaultNumberRolls = 500
const defaultError = 0.2

const isWithinError = (value, expected, error) => {
  if (expected < 0) {
    value *= -1
    expected *= -1
  }

  const margin = expected * error
  return value >= expected - margin && value <= expected + margin
}

expect.extend({
  toBeOnAverage(received, expected, error = defaultError) {
    const average = received.reduce(plus) / received.length
    const pass = isWithinError(average, expected, error)

    return {
      pass,
      message: () => (
        `expected average to${pass ? ' not ' : ' '}be around ${expected} (error: ${error * 100}%), got ${average}`
      )
    }
  },
  toBeBetween(received, low, high) {
    const pass = received.reduce((allOk, value) => {
      return allOk && value >= low && value <= high
    }, true)

    return {
      pass,
      message: () => (
        `expected all to${pass ? ' not ' : ' '}be between ${low} and ${high}`
      )
    }
  },
  toAllBe(received, expected) {
    const pass = received.reduce((allOk, value) => {
      return allOk && value === expected
    }, true)

    return {
      pass,
      message: () => (
        `expected all to${pass ? ' not ' : ' '}be equal to ${expected}`
      )
    }
  },
  toHaveVariance(received, expected, error = defaultError) {
    const average = received.reduce(plus) / received.length
    const variance = received.map((value) => (Math.pow(value - average, 2)))
      .reduce(plus) / (received.length - 1)
    const pass = isWithinError(variance, expected, error)

    return {
      pass,
      message: () => (
        `expected variance to${pass ? ' not ' : ' '}be around ${expected} (error: ${error * 100}%), got ${variance}`
      )
    }
  }
})

const plus = (a, b) => a + b
const times = (a, b) => a * b

const rollForTest = (die, numberRolls) => {
  let pools = []
  let rolls = []

  for (let i = 0; i < numberRolls; i++) {
    let rolled = pool(die)
    pools.push(rolled)
    rolls.push(rolled.reduce(plus, 0))
  }

  return { pools, rolls }
}

const testDie = (die, testSpecs, numberRolls = defaultNumberRolls) => {
  let { pools, rolls } = rollForTest(die, numberRolls)

  if ('diceCount' in testSpecs) {
    it(`rolls ${testSpecs.diceCount} dice`, () => {
      expect(pools.map((pool) => (pool.length))).toAllBe(testSpecs.diceCount)
    })
  }

  if ('variableDiceCount' in testSpecs) {
    const {min, max} = testSpecs.variableDiceCount
    it(`rolls between ${min} and ${max} dice`, () => {
      expect(pools.map((pool) => (pool.length))).toBeBetween(min, max)
    })
  }

  if ('average' in testSpecs) {
    let { average, error } = testSpecs.average

    it(`has an expected value of ${average}`, () => {
      expect(rolls).toBeOnAverage(average, error)
    })
  }

  if ('variance' in testSpecs) {
    let { variance, error } = testSpecs.variance

    it(`has a variance of ${variance}`, () => {
      expect(rolls).toHaveVariance(variance, error)
    })
  }

  if ('bounds' in testSpecs) {
    let { low, high, expectLow, expectHigh } = testSpecs.bounds

    it(`rolls between ${low} and ${high}`, () => {
      expect(rolls).toBeBetween(low, high)
    })

    if (expectLow) {
      it('attains its minimum', () => {
        expect(rolls).toContain(low)
      })
    }

    if (expectHigh) {
      it('attains its maximum', () => {
        expect(rolls).toContain(high)
      })
    }
  }
}

const basicDieTestSpecs = (number, sides, negative = false) => {
  const multiplier = negative ? -1 : 1
  const low = negative ? -(number * sides) : number
  const high = negative ? -number : number * sides
  const expectExtrema = number * sides < 50
  return {
    diceCount: number,
    average: {
      average: (number * (sides + 1)) / 2 * multiplier
    },
    variance: {
      variance: (number * (sides * sides - 1)) / 12
    },
    bounds: {
      low: low,
      high: high,
      expectLow: expectExtrema,
      expectHigh: expectExtrema
    }
  }
}

const combinedDiceTestSpecs = (dieSpecs) => {
  const individualTestSpecs =
    dieSpecs.map(spec => (
      basicDieTestSpecs(spec.number, spec.sides, spec.negative)
    ))

  const combineSpecField = (fieldGetter) => {
    return individualTestSpecs.reduce((total, spec) => {
      return total + fieldGetter(spec)
    }, 0)
  }

  const expectExtrema =
    dieSpecs.map(spec => (Math.pow(spec.sides, spec.number)))
      .reduce(times) < 50
  return {
    diceCount: combineSpecField(spec => (spec.diceCount)),
    average: {
      average: combineSpecField(spec => (spec.average.average))
    },
    variance: {
      variance: combineSpecField(spec => (spec.variance.variance))
    },
    bounds: {
      low: combineSpecField(spec => (spec.bounds.low)),
      high: combineSpecField(spec => (spec.bounds.high)),
      expectLow: expectExtrema,
      expectHigh: expectExtrema
    }
  }
}

const describeBasicDie = (number, sides, numberRolls = defaultNumberRolls,
  neg = false) => {
  describe(`${number}d${sides}`, () => {
    let die = d(constant(number), constant(sides))
    if (neg) {
      die = negative(die)
    }
    testDie(die, basicDieTestSpecs(number, sides, neg), numberRolls)
  })
}

const getDieString = diceSpecs => (
  diceSpecs.reduce((string, spec, index) => (
    string + `${spec.number}d${spec.sides}` +
    (index < diceSpecs.length - 1 ?
      (diceSpecs[index + 1].negative ? ' - ' : ' + ')
      : '')
  ), '')
)

const combineDiceSpecs = diceSpecs => (
  diceSpecs.slice(1).reduce((die, spec) => {
    const combinator = spec.negative ? subtract : add
    return combinator(die, d(constant(spec.number), constant(spec.sides)))
  }, d(constant(diceSpecs[0].number), constant(diceSpecs[0].sides)))
)

const describeCompoundDice = (diceSpecs, numberRolls = defaultNumberRolls) => {
  const dieString = getDieString(diceSpecs)
  const die = combineDiceSpecs(diceSpecs)
  describe(dieString, () => testDie(die, combinedDiceTestSpecs(diceSpecs),
    numberRolls))
}

describe('constant', () => {
  describe('1', () => {
    const die = constant(1)
    testDie(die, {
      diceCount: 1,
      average: { average: 1 },
      variance: { variance: 0 },
      bounds: {
        low: 1,
        high: 1,
        expectLow: true,
      }
    }, 10)
  })

  describe('10', () => {
    const die = constant(10)
    testDie(die, {
      diceCount: 1,
      average: { average: 10 },
      variance: { variance: 0 },
      bounds: {
        low: 10,
        high: 10,
        expectLow: true,
      }
    }, 10)
  })

  describe('-5', () => {
    const die = constant(-5)
    testDie(die, {
      diceCount: 1,
      average: { average: -5 },
      variance: { variance: 0 },
      bounds: {
        low: -5,
        high: -5,
        expectLow: true,
      }
    }, 10)
  })
})

describe('basic dice', () => {
  describeBasicDie(1, 6)
  describeBasicDie(0, 6)
  describeBasicDie(2, 8, 500)
  describeBasicDie(20, 1)
})

describe('add', () => {
  describeCompoundDice([ { number: 1, sides: 6 }, { number: 1, sides: 4 } ])
  describeCompoundDice([
    { number: 3, sides: 6 },
    { number: 2, sides: 8 },
    { number: 1, sides: 1 }
  ])
})

describe('negative', () => {
  describeBasicDie(1, 6, defaultNumberRolls, true)
  describeBasicDie(0, 6, defaultNumberRolls, true)
  describeBasicDie(2, 8, defaultNumberRolls, true)
})

describe('subtract', () => {
  describeCompoundDice([
    { number: 1, sides: 6 },
    { number: 1, sides: 4, negative: true }
  ])
  describe('3d6 - 2d8 - 1d1', () => {
    const diceSpecs = [
      { number: 3, sides: 6 },
      { number: 2, sides: 8, negative: true },
      { number: 1, sides: 1, negative: true }
    ]
    const die = combineDiceSpecs(diceSpecs)
    const testSpecs = combinedDiceTestSpecs(diceSpecs)
    testSpecs.average.error = 1
    describe('3d6 - 2d8 - 1d1', () => testDie(die, testSpecs, 700))
  })
})

describe('compound dice', () => {
  describe('(1d4)d(1d6)', () => {
    const die = d(d(constant(1), constant(4)), d(constant(1), constant(6)))
    const testSpec = {
      variableDiceCount: {
        min: 1,
        max: 4,
      },
      average: {
        average: 5.625
      },
      bounds: {
        low: 1,
        high: 24,
        expectLow: true
      }
    }

    testDie(die, testSpec)
  })

  describe('(2d1)d(2d1)', () => {
    const die = d(d(constant(2), constant(1)), d(constant(2), constant(1)))

    testDie(die, basicDieTestSpecs(2, 2))
  })
})

describe('exploding dice', () => {
  describe('6E1d6', () => {
    const die = explode(constant(6), d(constant(1), constant(6)))
    testDie(die, {
      diceCount: 1,
      average: {
        average: 4.2
      },
      variance: {
        variance: 10.64
      },
      bounds: {
        low: 1,
        expectLow: true,
        high: Infinity
      }
    })
  })

  describe('6E2d6', () => {
    const die = explode(constant(6), d(constant(2), constant(6)))
    testDie(die, {
      diceCount: 2,
      average: {
        average: 2 * 4.2
      },
      variance: {
        variance: 2 * 10.64
      },
      bounds: {
        low: 2,
        expectLow: true,
        high: Infinity
      }
    })
  })
})