-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_test.go
More file actions
121 lines (99 loc) · 2.51 KB
/
Copy pathsample_test.go
File metadata and controls
121 lines (99 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package enumerators_test
import (
"fmt"
"slices"
"sync"
"testing"
"github.com/fgrzl/enumerators"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShouldSampleElements(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3, 4, 5})
// Act
sampled := enumerators.Sample(input, 3)
result, err := enumerators.ToSlice(sampled)
// Assert
assert.NoError(t, err)
assert.Len(t, result, 3)
for _, value := range result {
assert.Contains(t, []int{1, 2, 3, 4, 5}, value)
}
}
func TestShouldSampleAllWhenNExceedsLength(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3})
// Act
sampled := enumerators.Sample(input, 5)
result, err := enumerators.ToSlice(sampled)
// Assert
assert.NoError(t, err)
assert.Len(t, result, 3)
assert.ElementsMatch(t, []int{1, 2, 3}, result)
}
func TestShouldReturnEmptySampleWhenNIsZero(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3})
// Act
result, err := enumerators.ToSlice(enumerators.Sample(input, 0))
// Assert
assert.NoError(t, err)
assert.Empty(t, result)
}
func TestShouldReturnEmptySampleWhenNIsNegative(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3})
// Act
result, err := enumerators.ToSlice(enumerators.Sample(input, -1))
// Assert
assert.NoError(t, err)
assert.Empty(t, result)
}
func TestShouldReturnEmptySampleForEmptyInput(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{})
// Act
result, err := enumerators.ToSlice(enumerators.Sample(input, 3))
// Assert
assert.NoError(t, err)
assert.Empty(t, result)
}
func TestShouldSampleConcurrently(t *testing.T) {
// Arrange
input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
const goroutineCount = 12
const iterationsPerGoroutine = 20
var wg sync.WaitGroup
errCh := make(chan error, goroutineCount)
// Act
for idx := 0; idx < goroutineCount; idx++ {
wg.Add(1)
go func() {
defer wg.Done()
for iteration := 0; iteration < iterationsPerGoroutine; iteration++ {
result, err := enumerators.ToSlice(enumerators.Sample(enumerators.Slice(input), 4))
if err != nil {
errCh <- err
return
}
if len(result) != 4 {
errCh <- fmt.Errorf("unexpected sample length: %d", len(result))
return
}
for _, value := range result {
if !slices.Contains(input, value) {
errCh <- fmt.Errorf("unexpected sampled value: %d", value)
return
}
}
}
}()
}
wg.Wait()
close(errCh)
// Assert
for err := range errCh {
require.NoError(t, err)
}
}