blob: eba6fe2ef91bce55d09111b4057bf6b3791e891e [file] [log] [blame]
Scott Baker2b0ad652019-08-21 14:57:07 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Zack Williamse940c7a2019-08-21 14:25:39 -070017package order
18
19import (
20 "testing"
21)
22
23type SortTestStruct struct {
24 Id int
25 One string
26 Two string
27 Three uint
28 Four int
29}
30
31var testSetOne = []SortTestStruct{
32 {
33 Id: 0,
34 One: "a",
35 Two: "x",
36 Three: 10,
37 Four: 1,
38 },
39 {
40 Id: 1,
41 One: "a",
42 Two: "c",
43 Three: 1,
44 Four: 10,
45 },
46 {
47 Id: 2,
48 One: "a",
49 Two: "b",
50 Three: 2,
51 Four: 1000,
52 },
53 {
54 Id: 3,
55 One: "a",
56 Two: "a",
57 Three: 3,
58 Four: 100,
59 },
60 {
61 Id: 4,
62 One: "b",
63 Two: "a",
64 Three: 3,
65 Four: 0,
66 },
67}
68
69var testSetTwo = []SortTestStruct{
70 {
71 Id: 0,
72 One: "a",
73 Two: "x",
74 Three: 10,
75 Four: 10,
76 },
77 {
78 Id: 1,
79 One: "a",
80 Two: "y",
81 Three: 1,
82 Four: 1,
83 },
84}
85
86func Verify(v []SortTestStruct, order []int) bool {
87 for i, item := range v {
88 if item.Id != order[i] {
89 return false
90 }
91 }
92 return true
93}
94
95func TestSort(t *testing.T) {
96 s, err := Parse("+One,-Two")
97 if err != nil {
98 t.Errorf("Unable to parse sort specification")
99 }
100 o, err := s.Process(testSetOne)
101 if err != nil {
102 t.Errorf("Sort failed: %s", err.Error())
103 }
104
105 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
106 t.Errorf("incorrect sort")
107 }
108}
109
110func TestSortASC(t *testing.T) {
111 s, err := Parse("+One,Two")
112 if err != nil {
113 t.Errorf("Unable to parse sort specification")
114 }
115 o, err := s.Process(testSetTwo)
116 if err != nil {
117 t.Errorf("Sort failed: %s", err.Error())
118 }
119
120 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
121 t.Errorf("incorrect sort")
122 }
123}
124
125func TestSortUintASC(t *testing.T) {
126 s, err := Parse("Three,One")
127 if err != nil {
128 t.Errorf("Unable to parse sort specification")
129 }
130 o, err := s.Process(testSetOne)
131 if err != nil {
132 t.Errorf("Sort failed: %s", err.Error())
133 }
134
135 if !Verify(o.([]SortTestStruct), []int{1, 2, 3, 4, 0}) {
136 t.Errorf("incorrect sort")
137 }
138}
139
140func TestSortUintDSC(t *testing.T) {
141 s, err := Parse("-Three,One")
142 if err != nil {
143 t.Errorf("Unable to parse sort specification")
144 }
145 o, err := s.Process(testSetOne)
146 if err != nil {
147 t.Errorf("Sort failed: %s", err.Error())
148 }
149
150 if !Verify(o.([]SortTestStruct), []int{0, 3, 4, 2, 1}) {
151 t.Errorf("incorrect sort")
152 }
153}
154
155func TestSortUintDSC2(t *testing.T) {
156 s, err := Parse("-Three,One")
157 if err != nil {
158 t.Errorf("Unable to parse sort specification")
159 }
160 o, err := s.Process(testSetTwo)
161 if err != nil {
162 t.Errorf("Sort failed: %s", err.Error())
163 }
164
165 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
166 t.Errorf("incorrect sort")
167 }
168}
169
170func TestSortIntASC(t *testing.T) {
171 s, err := Parse("Four,One")
172 if err != nil {
173 t.Errorf("Unable to parse sort specification")
174 }
175 o, err := s.Process(testSetOne)
176 if err != nil {
177 t.Errorf("Sort failed: %s", err.Error())
178 }
179 if !Verify(o.([]SortTestStruct), []int{4, 0, 1, 3, 2}) {
180 t.Errorf("incorrect sort")
181 }
182}
183
184func TestSortIntDSC(t *testing.T) {
185 s, err := Parse("-Four,One")
186 if err != nil {
187 t.Errorf("Unable to parse sort specification")
188 }
189 o, err := s.Process(testSetOne)
190 if err != nil {
191 t.Errorf("Sort failed: %s", err.Error())
192 }
193 if !Verify(o.([]SortTestStruct), []int{2, 3, 1, 0, 4}) {
194 t.Errorf("incorrect sort")
195 }
196}
197
198func TestSortIntDSC2(t *testing.T) {
199 s, err := Parse("-Four,One")
200 if err != nil {
201 t.Errorf("Unable to parse sort specification")
202 }
203 o, err := s.Process(testSetTwo)
204 if err != nil {
205 t.Errorf("Sort failed: %s", err.Error())
206 }
207 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
208 t.Errorf("incorrect sort")
209 }
210}
211
212func TestOperString(t *testing.T) {
213 if ASC.String() != "ASC" {
214 t.Errorf("ASC to string failed")
215 }
216 if DSC.String() != "DSC" {
217 t.Errorf("DSC to string failed")
218 }
219 var o Operation = 5 // Invalid
220 if o.String() != "ASC" {
221 t.Errorf("to string default failed")
222 }
223}
224
225func TestSortSingle(t *testing.T) {
226 s, err := Parse("-Four,One")
227 if err != nil {
228 t.Errorf("Unable to parse sort specification")
229 }
230 o, err := s.Process(testSetOne[0])
231 if err != nil {
232 t.Errorf("Sort failed: %s", err.Error())
233 }
234
235 if o == nil {
236 t.Errorf("expected value, got nil")
237 }
238
239 r, ok := o.(SortTestStruct)
240 if !ok {
241 t.Errorf("Unexpected result type")
242 }
243
244 if r.Id != testSetOne[0].Id {
245 t.Errorf("results don't match input")
246 }
247}