-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency_method_test.go
More file actions
142 lines (137 loc) · 5.21 KB
/
frequency_method_test.go
File metadata and controls
142 lines (137 loc) · 5.21 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package priority_channels
import (
"fmt"
"reflect"
"testing"
"github.com/dimag-jfrog/priority-channels/strategies/frequency_strategies"
)
func TestGetFrequencyStrategy(t *testing.T) {
probabilisticMode := ProbabilisticMode
strictOrderMode := StrictOrderMode
probabilisticByCaseDuplication := ProbabilisticByCaseDuplication
probabilisticByMultipleRandCalls := ProbabilisticByMultipleRandCalls
strictOrderAcrossCycles := StrictOrderAcrossCycles
strictOrderFully := StrictOrderFully
unsupportedMode := FrequencyMode(1000)
unsupportedMethod := FrequencyMethod(1000)
var testCases = []struct {
name string
level priorityChannelLevel
mode *FrequencyMode
method *FrequencyMethod
numChannels int
expectedStrategy PrioritizationStrategy[int]
expectedError error
}{
{
name: "NewLevel - Unsupported Mode - Expected Error",
level: levelNew,
mode: &unsupportedMode,
numChannels: 10,
expectedError: ErrInvalidFrequencyMode,
},
{
name: "NewLevel - Unsupported Method - Expected Error",
level: levelNew,
method: &unsupportedMethod,
numChannels: 10,
expectedError: ErrInvalidFrequencyMethod,
},
{
name: "NewLevel - Default strategy is by Case Duplication",
level: levelNew,
numChannels: 10,
expectedStrategy: frequency_strategies.NewProbabilisticByCaseDuplication(),
},
{
name: "NewLevel - If By Probability method is given it is used",
level: levelNew,
numChannels: 10,
method: &probabilisticByMultipleRandCalls,
expectedStrategy: frequency_strategies.NewByProbabilityFromFreqRatios(),
},
{
name: "NewLevel - If number of channels is greater than recommended - strategy is By Probability",
level: levelNew,
numChannels: maxRecommendedChannelsForCaseDuplication + 1,
expectedStrategy: frequency_strategies.NewByProbabilityFromFreqRatios(),
},
{
name: "NewLevel - Strategy provided but number of channels is greater than recommended - still use provided Strategy",
level: levelNew,
numChannels: maxRecommendedChannelsForCaseDuplication + 1,
method: &probabilisticByCaseDuplication,
expectedStrategy: frequency_strategies.NewProbabilisticByCaseDuplication(),
},
{
name: "NewLevel - Strategy is Case Duplication and number of channels is greater than supported - Expected Error",
level: levelNew,
method: &probabilisticByCaseDuplication,
numChannels: maxSupportedChannelsForCaseDuplication + 1,
expectedError: fmt.Errorf("too many channels %d for frequency method %s (max %d)",
maxSupportedChannelsForCaseDuplication+1, frequencyMethodNames[ProbabilisticByCaseDuplication], maxSupportedChannelsForCaseDuplication),
},
{
name: "NewLevel - Default for strict mode is Strict Order Across Cycles",
level: levelNew,
mode: &strictOrderMode,
numChannels: 10,
expectedStrategy: frequency_strategies.NewWithStrictOrderAcrossCycles(),
},
{
name: "NewLevel - if Strict Order Fully method is provided it is used",
level: levelNew,
method: &strictOrderFully,
numChannels: 10,
expectedStrategy: frequency_strategies.NewWithStrictOrderFully(),
},
{
name: "CombineLevel - Default strategy is By Probability",
level: levelCombine,
numChannels: 10,
expectedStrategy: frequency_strategies.NewByProbabilityFromFreqRatios(),
},
{
name: "CombineLevel - Strategy for probabilistic mode is By Probability",
level: levelCombine,
mode: &probabilisticMode,
numChannels: 10,
expectedStrategy: frequency_strategies.NewByProbabilityFromFreqRatios(),
},
{
name: "CombineLevel - Case Duplication method is not supported",
level: levelCombine,
method: &probabilisticByCaseDuplication,
numChannels: 10,
expectedError: &UnsupportedFrequencyMethodForCombineError{FrequencyMethod: ProbabilisticByCaseDuplication},
},
{
name: "CombineLevel - Strategy for strict mode is Strict Order Fully",
level: levelCombine,
mode: &strictOrderMode,
numChannels: 10,
expectedStrategy: frequency_strategies.NewWithStrictOrderFully(),
},
{
name: "CombineLevel - Strict Order Across Cycles method is not supported",
level: levelCombine,
method: &strictOrderAcrossCycles,
numChannels: 10,
expectedError: &UnsupportedFrequencyMethodForCombineError{FrequencyMethod: StrictOrderAcrossCycles},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := getFrequencyStrategy(tc.level, tc.mode, tc.method, tc.numChannels)
if tc.expectedError != nil {
if err == nil || err.Error() != tc.expectedError.Error() {
t.Errorf("Expected error %v, got %v", tc.expectedError, err)
}
return
}
if reflect.TypeOf(res) != reflect.TypeOf(tc.expectedStrategy) {
t.Errorf("Expected strategy type %v, got %v", reflect.TypeOf(tc.expectedStrategy), reflect.TypeOf(res))
}
})
}
}