You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestCombinations(t *testing.T) {
  10. input := map[string][]interface{}{
  11. "bool": {false, true},
  12. "int": {1, 2, 3},
  13. "string": {"foo", "bar"},
  14. }
  15. c := combinations(input)
  16. assert.Equal(t, []map[string]interface{}{
  17. {"bool": false, "int": 1, "string": "foo"},
  18. {"bool": false, "int": 1, "string": "bar"},
  19. {"bool": false, "int": 2, "string": "foo"},
  20. {"bool": false, "int": 2, "string": "bar"},
  21. {"bool": false, "int": 3, "string": "foo"},
  22. {"bool": false, "int": 3, "string": "bar"},
  23. {"bool": true, "int": 1, "string": "foo"},
  24. {"bool": true, "int": 1, "string": "bar"},
  25. {"bool": true, "int": 2, "string": "foo"},
  26. {"bool": true, "int": 2, "string": "bar"},
  27. {"bool": true, "int": 3, "string": "foo"},
  28. {"bool": true, "int": 3, "string": "bar"},
  29. }, c)
  30. }
  31. func TestUniformSetChoice(t *testing.T) {
  32. set := uniformSetChoice([]string{"a", "b", "c"})
  33. r := rand.New(rand.NewSource(2384))
  34. for i := 0; i < 100; i++ {
  35. t.Run(fmt.Sprintf("Iteration%03d", i), func(t *testing.T) {
  36. set = append(set, t.Name())
  37. t.Run("ChooseAtLeastSubset", func(t *testing.T) {
  38. require.True(t, len(set.ChooseAtLeast(r, 1)) >= 1)
  39. require.True(t, len(set.ChooseAtLeast(r, 2)) >= 2)
  40. require.True(t, len(set.ChooseAtLeast(r, len(set)/2)) >= len(set)/2)
  41. })
  42. t.Run("ChooseAtLeastEqualOrGreaterToLength", func(t *testing.T) {
  43. require.Len(t, set.ChooseAtLeast(r, len(set)), len(set))
  44. require.Len(t, set.ChooseAtLeast(r, len(set)+1), len(set))
  45. require.Len(t, set.ChooseAtLeast(r, len(set)*10), len(set))
  46. })
  47. t.Run("ChooseSingle", func(t *testing.T) {
  48. require.True(t, len(set.Choose(r)) >= 1)
  49. })
  50. })
  51. }
  52. }