feat: 기본 unit test 등록
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user