-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from retraigo/main
I messed up
- Loading branch information
Showing
4 changed files
with
159 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"tasks": { | ||
"bench": "deno bench --unstable benches/100k_rolls.ts", | ||
"test": "deno run --allow-hrtime test.ts" | ||
"test": "deno test --allow-hrtime test.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,100 @@ | ||
import { | ||
assert, | ||
assertArrayIncludes, | ||
assertEquals, | ||
assertExists, | ||
assertThrows, | ||
} from "https://deno.land/std@0.157.0/testing/asserts.ts"; | ||
import { GachaMachine } from "./mod.ts"; | ||
import { assertAlmostEquals } from "https://deno.land/std@0.145.0/testing/asserts.ts"; | ||
|
||
import { Duration } from "https://deno.land/x/durationjs@v4.0.0/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 }, | ||
]; | ||
|
||
const sortedTestData = testData.sort((a, b) => a.chance - b.chance); | ||
/* | ||
import pokemon from "./testdata/pokemon.json" assert { type: "json" }; | ||
function countDupes(arr: string[]): Record<string, number> { | ||
const items: Record<string, number> = {}; | ||
arr.forEach((v) => { | ||
if (items[v]) items[v]++; | ||
else items[v] = 1; | ||
}); | ||
return items; | ||
} | ||
|
||
const timeStart = performance.now(); | ||
|
||
const items = pokemon.slice(0, 151).map((x) => ({ | ||
let testData = pokemon.slice().map((x) => ({ | ||
result: x.id, | ||
id: x.id, | ||
tier: x.tier === "legendary" ? 3 : x.tier === "mythic" ? 2 : 1, | ||
chance: x.tier === "legendary" ? 10 : x.tier === "mythic" ? 1 : 25, | ||
weight: x.tier === "legendary" ? 10 : x.tier === "mythic" ? 1 : 25, | ||
chance: x.tier === "legendary" ? 11 : x.tier === "mythic" ? 1 : 25, | ||
})); | ||
testData = testData.concat(testData); | ||
const timeConfig = performance.now(); | ||
|
||
const machine = new GachaMachine(items); | ||
|
||
const timeInit = performance.now(); | ||
|
||
const res = machine.get(1000000); | ||
|
||
const timeRoll = performance.now(); | ||
|
||
const dupes = countDupes(res.map((x) => String(x))); | ||
testData = testData.concat(testData); | ||
*/ | ||
Deno.test({ | ||
name: "Is GachaMachine defined?", | ||
fn() { | ||
const machine = new GachaMachine(testData); | ||
assertExists(machine); | ||
}, | ||
}); | ||
|
||
const timeReduce = performance.now(); | ||
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)); | ||
}, | ||
}); | ||
|
||
console.log( | ||
Object.entries(dupes).map((x) => { | ||
const pk = pokemon.find((y) => y.id === Number(x[0])); | ||
return { name: pk?.name, count: x[1], tier: pk?.tier }; | ||
}).sort((a, b) => a.count - b.count).reverse().map((x) => | ||
`Name: ${x.name}\nRate: ${x.count / 10000}%\nTier: ${x.tier}` | ||
).join("\n\n"), | ||
); | ||
Deno.test({ | ||
name: `Roll 3 unique items from a collection of 5 items`, | ||
fn() { | ||
const machine = new GachaMachine(testData); | ||
const res = machine.getUnique(3); | ||
assertExists(res); | ||
}, | ||
}); | ||
|
||
// TESTS | ||
Deno.test({ | ||
name: `Roll 5 unique items from a collection of 5 items`, | ||
fn() { | ||
const machine = new GachaMachine(testData); | ||
const res = machine.getUnique(5); | ||
assertArrayIncludes(res, [ | ||
"SSR cool character", | ||
"Kinda rare character", | ||
"Mob character", | ||
"Mob character", | ||
"Mob character", | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test("Range Check", () => { | ||
Object.entries(dupes).map((x) => { | ||
const pk = pokemon.find((y) => y.id === Number(x[0])); | ||
return { name: pk?.name, count: x[1], tier: pk?.tier }; | ||
}).sort((a, b) => a.count - b.count).reverse().forEach((x) => { | ||
if (x.tier === "normal") assertAlmostEquals(x.count / 10000, 0.67, 1e-1); | ||
else if (x.tier === "legendary") { | ||
assertAlmostEquals(x.count / 10000, 0.26, 4e-2); | ||
} else if (x.tier === "mythic") { | ||
assertAlmostEquals(x.count / 10000, 0.03, 4e-2); | ||
} | ||
}); | ||
Deno.test({ | ||
name: `Roll 7 unique items from a collection of 5 items (throw error)`, | ||
fn() { | ||
const machine = new GachaMachine(testData); | ||
assertThrows(() => machine.getUnique(7)); | ||
}, | ||
}); | ||
|
||
Deno.test("Time Check", () => { | ||
assertAlmostEquals((timeRoll - timeInit) / 1000, 1.56, 1e-1) | ||
}) | ||
// TESTS END | ||
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) | ||
}, | ||
}); | ||
|
||
console.log("-----REPORT-----"); | ||
console.log( | ||
"Time taken to configure:", | ||
Duration.between(timeConfig, timeStart).toShortString( | ||
["s", "ms", "us", "ns"], | ||
), | ||
); | ||
console.log( | ||
"Time taken to setup machine:", | ||
Duration.between(timeConfig, timeInit).toShortString( | ||
["s", "ms", "us", "ns"], | ||
), | ||
); | ||
console.log( | ||
"Time taken to roll 1000000 items:", | ||
Duration.between(timeInit, timeRoll).toShortString(["s", "ms", "us", "ns"]), | ||
); | ||
console.log( | ||
"Time taken to reduce data into result:", | ||
Duration.between(timeReduce, timeRoll).toShortString( | ||
["s", "ms", "us", "ns"], | ||
), | ||
); | ||
// console.log(items.map(x => `GachaChoice {${JSON.stringify(x)}}`).join(",\n")) | ||
/* | ||
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) | ||
}, | ||
}); | ||
*/ |