weight_test.go 611 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package rand
  2. import (
  3. "testing"
  4. )
  5. func TestSliceWeight(t *testing.T) {
  6. list := []int32{1, 2, 3}
  7. weight := NewSliceWeight[int32](list, func(v int32) int32 {
  8. return v
  9. })
  10. for i := 0; i < 10; i++ {
  11. t.Log(weight.SelectOne())
  12. }
  13. }
  14. func TestMapWeight(t *testing.T) {
  15. type Item struct {
  16. Key int32
  17. Value int32
  18. }
  19. maps := map[int32]*Item{
  20. 1: {
  21. Key: 1,
  22. Value: 2,
  23. },
  24. 2: {
  25. Key: 2,
  26. Value: 3,
  27. },
  28. 3: {
  29. Key: 3,
  30. Value: 4,
  31. },
  32. }
  33. weight := NewMapWeight[int32](maps, func(v *Item) int32 {
  34. return v.Value
  35. })
  36. for i := 0; i < 10; i++ {
  37. t.Log(weight.SelectOne())
  38. }
  39. }