subset_test.go 924 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package resolver
  2. import (
  3. "strconv"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/mathx"
  7. )
  8. func TestSubset(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. set int
  12. sub int
  13. }{
  14. {
  15. name: "more vals to subset",
  16. set: 100,
  17. sub: 36,
  18. },
  19. {
  20. name: "less vals to subset",
  21. set: 100,
  22. sub: 200,
  23. },
  24. }
  25. for _, test := range tests {
  26. test := test
  27. t.Run(test.name, func(t *testing.T) {
  28. var vals []string
  29. for i := 0; i < test.set; i++ {
  30. vals = append(vals, strconv.Itoa(i))
  31. }
  32. m := make(map[interface{}]int)
  33. for i := 0; i < 1000; i++ {
  34. set := subset(append([]string(nil), vals...), test.sub)
  35. if test.sub < test.set {
  36. assert.Equal(t, test.sub, len(set))
  37. } else {
  38. assert.Equal(t, test.set, len(set))
  39. }
  40. for _, val := range set {
  41. m[val]++
  42. }
  43. }
  44. assert.True(t, mathx.CalcEntropy(m) > 0.95)
  45. })
  46. }
  47. }