package otherutils

import (
	"fmt"
	"testing"
)

func TestRandChoiceByWeighted(t *testing.T) {
	items := [][]int64{
		{1, 1000},
		{2, 1000},
		{3, 500},
		{4, 10},
	}

	frequencies := make(map[int64]int64)
	for i := 0; i < 10000; i++ {
		selected := RandChoiceByWeighted(items)
		//fmt.Println(selected)
		frequencies[selected]++
	}

	fmt.Println("Frequencies of selection after 10000 trials:")
	for k, v := range frequencies {
		fmt.Printf("%d: %d\n", k, v)
	}
}

func TestRandOneNum(t *testing.T) {
	for i := 0; i < 10; i++ {
		got := RandOneNum(0, 5)
		fmt.Println(got)
	}

}

func TestWeightedTable_RandOneItem(t *testing.T) {
	items := [][]int64{
		{1, 1000},
		{2, 1000},
		{3, 500},
		{4, 0},
		{5, 0},
	}

	weightedTable := SetWeightedTable(items, func(item []int64) int64 {
		return item[1]
	})
	res := make(map[int64]int64)
	for i := 0; i < 100000; i++ {
		item, _ := weightedTable.RandOneItem(false)
		if _, ok := res[item[0]]; ok {
			res[item[0]]++
		} else {
			res[item[0]] = 1
		}
	}
	fmt.Println(res)
	var num int64
	for _, n := range res {
		num += n
	}
	fmt.Println(num)
}
