sliceutil_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package sliceutil
  2. import (
  3. "maps"
  4. "slices"
  5. "testing"
  6. )
  7. func TestSliceMap(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. input []int
  11. expected []int
  12. }{
  13. {
  14. name: "empty slice",
  15. input: []int{},
  16. expected: []int{},
  17. },
  18. {
  19. name: "single element",
  20. input: []int{1},
  21. expected: []int{2},
  22. },
  23. {
  24. name: "multiple elements",
  25. input: []int{1, 2, 3},
  26. expected: []int{2, 4, 6},
  27. },
  28. }
  29. for _, test := range tests {
  30. t.Run(test.name, func(t *testing.T) {
  31. result := Map(test.input, func(i int) int { return i * 2 })
  32. for i, v := range result {
  33. if v != test.expected[i] {
  34. t.Errorf("expected %v, got %v", test.expected[i], v)
  35. }
  36. }
  37. })
  38. }
  39. }
  40. type seqTestCase[T comparable] struct {
  41. name string
  42. input []T
  43. }
  44. func runSeqTest[T comparable](test seqTestCase[T]) func(*testing.T) {
  45. return func(t *testing.T) {
  46. result := AsSeq(test.input)
  47. i := 0
  48. for v := range result {
  49. if v != test.input[i] {
  50. t.Errorf("expected %v, got %v", test.input[i], v)
  51. }
  52. i++
  53. }
  54. }
  55. }
  56. func runSeqTests[T comparable](t *testing.T, testCases []seqTestCase[T]) {
  57. t.Helper()
  58. for _, test := range testCases {
  59. t.Run(test.name, runSeqTest(test))
  60. }
  61. }
  62. func TestToSeq(t *testing.T) {
  63. intTests := []seqTestCase[int]{
  64. {
  65. name: "int empty slice",
  66. input: []int{},
  67. },
  68. {
  69. name: "int single element",
  70. input: []int{1},
  71. },
  72. {
  73. name: "int multiple elements",
  74. input: []int{1, 2, 3},
  75. },
  76. }
  77. floatTests := []seqTestCase[float64]{
  78. {
  79. name: "float64 empty slice",
  80. input: []float64{},
  81. },
  82. {
  83. name: "float64 single element",
  84. input: []float64{1.54},
  85. },
  86. {
  87. name: "float64 multiple elements",
  88. input: []float64{52.32, 23.12, 54.123},
  89. },
  90. }
  91. stringTests := []seqTestCase[string]{
  92. {
  93. name: "string empty slice",
  94. input: []string{},
  95. },
  96. {
  97. name: "single single element",
  98. input: []string{"foo"},
  99. },
  100. {
  101. name: "string multiple elements",
  102. input: []string{"foo", "bar", "baz"},
  103. },
  104. }
  105. runSeqTests(t, intTests)
  106. runSeqTests(t, floatTests)
  107. runSeqTests(t, stringTests)
  108. }
  109. func TestSeqToSlice(t *testing.T) {
  110. keys := []string{
  111. "a", "b", "c", "d", "e", "f", "g",
  112. }
  113. m := make(map[string]string, len(keys))
  114. for _, k := range keys {
  115. m[k] = "value-" + k
  116. }
  117. seqKeys := maps.Keys(m)
  118. seqValues := maps.Values(m)
  119. // These do *NOT* align on indexes!
  120. keySlice := SeqToSlice(seqKeys)
  121. valueSlice := SeqToSlice(seqValues)
  122. for _, k := range keySlice {
  123. if !slices.Contains(keys, k) {
  124. t.Errorf("expected %v to be in %v", k, keys)
  125. }
  126. }
  127. for _, v := range valueSlice {
  128. if !mapContainsValue(m, v) {
  129. t.Errorf("expected %v to be in %v", v, m)
  130. }
  131. }
  132. }
  133. func mapContainsValue(m map[string]string, value string) bool {
  134. for _, v := range m {
  135. if v == value {
  136. return true
  137. }
  138. }
  139. return false
  140. }