-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
128 lines (114 loc) · 3.3 KB
/
test.ts
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
import {
assert,
assertArrayIncludes,
assertEquals,
assertExists,
assertThrows,
} from "https://deno.land/std@0.157.0/testing/asserts.ts";
import { GachaMachine, roll } from "./mod.ts";
const testData = [
{ result: "SSR cool character", chance: 1 },
{ result: "Kinda rare character", chance: 3 },
{ result: "Mob character", chance: 5 },
{ result: "Mob character", chance: 5 },
{ result: "Mob character", chance: 5 },
];
Deno.test({
name: "Is GachaMachine defined?",
fn() {
const machine = new GachaMachine(testData);
assertExists(machine);
},
});
Deno.test({
name:
"Is the initialization time less than 200 + (number of elements / 3) microseconds?",
fn() {
const t1 = performance.now();
new GachaMachine(testData);
const t2 = performance.now();
assert((t2 - t1) * 100 < 200 + (testData.length / 3));
},
});
Deno.test({
name: `Roll 3 unique items from a collection of 5 items`,
fn() {
const machine = new GachaMachine(testData);
const res = machine.get(3, true);
assertExists(res);
},
});
Deno.test({
name: `Roll 5 unique items from a collection of 5 items`,
fn() {
const machine = new GachaMachine(testData);
const res = machine.get(5, true);
assertArrayIncludes(res, [
"SSR cool character",
"Kinda rare character",
"Mob character",
"Mob character",
"Mob character",
]);
},
});
Deno.test({
name: `Roll 7 unique items from a collection of 5 items (throw error)`,
fn() {
const machine = new GachaMachine(testData);
assertThrows(() => machine.get(7, true));
},
});
Deno.test({
name:
`Roll 7 non-unique items from a collection of 5 items (don't throw error)`,
fn() {
const machine = new GachaMachine(testData);
const res = machine.get(7);
assertEquals(res.length, 7);
},
});
Deno.test({
name:
`Machine can be updated by just setting machine.items after initialization.`,
fn() {
const machine = new GachaMachine(testData);
const machine2 = new GachaMachine(testData.slice(0, 3));
machine.items = testData.slice(0, 3);
assertEquals(machine.items, machine2.items, "Items are not equal.");
// assertEquals(machine.tiers, machine2.tiers, "Tiers are not equal.");
// assertEquals(machine.totalChance, machine2.totalChance, "totalChance are not equal.");
// assertEquals(machine.maxTier, machine2.maxTier, "maxTier are not equal.");
// assertEquals(machine.pool, machine2.pool, "pool are not equal.");
},
});
Deno.test({
name: `Roll 1 items from a collection of 5 items using the roll() method`,
fn() {
const res = roll(testData);
console.log(`Rolled: `, res);
assertExists(res);
},
});
Deno.test({
name:
`Roll 1 items from a collection of 5 items using the roll() method (overload)`,
fn() {
const res = roll(
testData.map((x) => x.result),
testData.map((x) => x.chance),
);
console.log(`Rolled: `, res);
assertExists(res);
},
});
/*
Deno.test({
name: `${sortedTestData[0].chance} in ${testData.reduce((acc, val) => acc + val.chance, 0)} rolls return a ${sortedTestData[0].result}?`,
fn() {
const machine = new GachaMachine(testData);
const res = machine.get(testData.reduce((acc, val) => acc + val.chance, 0));
assertArrayIncludes(res, sortedTestData[0].result)
},
});
*/