-
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.
fix: Fixed probability in WeightPicker
- Loading branch information
Showing
9 changed files
with
209 additions
and
13 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 |
---|---|---|
|
@@ -69,5 +69,5 @@ | |
"test:watch": "jest --watch" | ||
}, | ||
"types": "dist/index.d.ts", | ||
"version": "2.1.1" | ||
"version": "2.1.2" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Picker } from "../Picker"; | ||
|
||
it("probability should be the same for all", () => { | ||
const picker = new Picker([1, 2, 3, 4]); | ||
const picks = picker.pick(10000); | ||
const numberOf = picks.reduce((acc, x) => { | ||
acc[x] = (acc[x] || 0) + 1; | ||
|
||
return acc; | ||
}, {} as Record<number, number>); | ||
const entries: [string, number][] = Object.entries(numberOf); | ||
|
||
for (const [_key, value] of entries) | ||
expect(value).toBeGreaterThan(0); | ||
|
||
for (const [_key, value] of entries) | ||
expect(entries[0][1] / value).toBeCloseTo(1, 0.5); | ||
} ); |
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Picker } from "../Picker"; | ||
import { throwDart } from "../ThrowDart"; | ||
|
||
let picker: Picker<number>; | ||
|
||
beforeEach(() => { | ||
picker = new Picker([1, 2]); | ||
|
||
picker.put(1, 1); | ||
picker.put(2, 2); | ||
} ); | ||
|
||
it("throwDart", () => { | ||
const dartToObj: Record<number, number | undefined> = {}; | ||
|
||
for (let i = -1; i < 4; i++) { | ||
dartToObj[i] = throwDart( { | ||
data: picker.data, | ||
dart: i, | ||
getWeight: picker.getWeight.bind(picker), | ||
} ); | ||
} | ||
|
||
expect(dartToObj[-1]).toBeUndefined(); | ||
expect(dartToObj[0]).toBe(1); | ||
expect(dartToObj[1]).toBe(2); | ||
expect(dartToObj[2]).toBe(2); | ||
expect(dartToObj[3]).toBeUndefined(); | ||
} ); | ||
|
||
it("probability should be 2:1", () => { | ||
const picks = picker.pick(10000); | ||
const numberOf = picks.reduce((acc, x) => { | ||
acc[x] = (acc[x] || 0) + 1; | ||
|
||
return acc; | ||
}, {} as Record<number, number>); | ||
|
||
for (const [_key, value] of Object.entries(numberOf)) | ||
expect(value).toBeGreaterThan(0); | ||
|
||
expect(numberOf[2] / numberOf[1]).toBeCloseTo(2, 0.5); | ||
} ); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { pickerNumbers1, resetData } from "./fixtures"; | ||
beforeEach(() => { | ||
resetData(); | ||
} ); | ||
|
||
it("should get weight of sum of individual weights", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
expect(picker.weight).toBe(21); | ||
} ); | ||
|
||
it("should update total weight without removed elements", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
// eslint-disable-next-line max-len | ||
// Se llama a picker.weight en todos los tests para que internamente se precalcule y se utilice #precalcWeight | ||
expect(picker.weight).toBe(21); | ||
|
||
picker.remove(3); | ||
picker.remove(6); | ||
|
||
expect(picker.weight).toBe(21 - 3 - 6); | ||
} ); | ||
|
||
it("should update total weight with updated individual weight", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
expect(picker.weight).toBe(21); | ||
|
||
picker.put(3, 1); | ||
picker.put(5, 1); | ||
|
||
expect(picker.weight).toBe(21 - 3 + 1 - 5 + 1); | ||
} ); | ||
|
||
it("should update total weight with added individual weight", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
expect(picker.weight).toBe(21); | ||
|
||
picker.put(7, 7); | ||
|
||
expect(picker.weight).toBe(21 + 7); | ||
} ); | ||
|
||
it("should update total weight with weight fixers", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
expect(picker.weight).toBe(21); | ||
|
||
picker.fixWeights((w) => w + 1); | ||
|
||
expect(picker.weight).toBe(21 + 6); | ||
} ); | ||
|
||
it("should only consider non removed items by filters", () => { | ||
const picker = pickerNumbers1(); | ||
|
||
expect(picker.weight).toBe(21); | ||
|
||
picker.filter((r) => r % 2 === 0); | ||
|
||
expect(picker.weight).toBe(2 + 4 + 6); | ||
} ); |