feat: 기본 unit test 등록

This commit is contained in:
2022-03-12 20:31:13 +09:00
parent 7ef9e67f5d
commit d0576806e2
3 changed files with 230 additions and 0 deletions
+12
View File
@@ -37,6 +37,9 @@ export class RandUtil {
public shuffle<T>(srcArray: T[]): T[] {
const cnt = srcArray.length;
if(cnt === 0){
return [];
}
if (cnt > this.rng.getMaxInt()) {
throw 'Invalid random int range';
}
@@ -54,6 +57,9 @@ export class RandUtil {
public choice<T>(items: T[] | Record<string | number, T> | Set<T>): T {
if (items instanceof Array) {
if(items.length === 0){
throw new Error('Empty items');
}
const idx = this.rng.nextInt(items.length - 1);
return items[idx];
}
@@ -66,6 +72,9 @@ export class RandUtil {
}
public choiceUsingWeight(items: Record<string | number, number>): string | number {
if(Object.keys(items).length === 0){
throw new Error('Empty items');
}
let sum = 0;
for (const value of Object.values(items)) {
if (value <= 0) {
@@ -94,6 +103,9 @@ export class RandUtil {
}
public choiceUsingWeightPair<T>(items: [T, number][]): T {
if(items.length === 0){
throw new Error('Empty items');
}
let sum = 0;
for (const [, value] of items) {
if (value <= 0) {