1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/task_scheduler/sequence_sort_key.h"
6
7 #include "base/task_scheduler/task_traits.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12 namespace internal {
13
TEST(TaskSchedulerSequenceSortKeyTest,OperatorLessThan)14 TEST(TaskSchedulerSequenceSortKeyTest, OperatorLessThan) {
15 SequenceSortKey key_a(TaskPriority::USER_BLOCKING,
16 TimeTicks::FromInternalValue(1000));
17 SequenceSortKey key_b(TaskPriority::USER_BLOCKING,
18 TimeTicks::FromInternalValue(2000));
19 SequenceSortKey key_c(TaskPriority::USER_VISIBLE,
20 TimeTicks::FromInternalValue(1000));
21 SequenceSortKey key_d(TaskPriority::USER_VISIBLE,
22 TimeTicks::FromInternalValue(2000));
23 SequenceSortKey key_e(TaskPriority::BACKGROUND,
24 TimeTicks::FromInternalValue(1000));
25 SequenceSortKey key_f(TaskPriority::BACKGROUND,
26 TimeTicks::FromInternalValue(2000));
27
28 EXPECT_FALSE(key_a < key_a);
29 EXPECT_LT(key_b, key_a);
30 EXPECT_LT(key_c, key_a);
31 EXPECT_LT(key_d, key_a);
32 EXPECT_LT(key_e, key_a);
33 EXPECT_LT(key_f, key_a);
34
35 EXPECT_FALSE(key_a < key_b);
36 EXPECT_FALSE(key_b < key_b);
37 EXPECT_LT(key_c, key_b);
38 EXPECT_LT(key_d, key_b);
39 EXPECT_LT(key_e, key_b);
40 EXPECT_LT(key_f, key_b);
41
42 EXPECT_FALSE(key_a < key_c);
43 EXPECT_FALSE(key_b < key_c);
44 EXPECT_FALSE(key_c < key_c);
45 EXPECT_LT(key_d, key_c);
46 EXPECT_LT(key_e, key_c);
47 EXPECT_LT(key_f, key_c);
48
49 EXPECT_FALSE(key_a < key_d);
50 EXPECT_FALSE(key_b < key_d);
51 EXPECT_FALSE(key_c < key_d);
52 EXPECT_FALSE(key_d < key_d);
53 EXPECT_LT(key_e, key_d);
54 EXPECT_LT(key_f, key_d);
55
56 EXPECT_FALSE(key_a < key_e);
57 EXPECT_FALSE(key_b < key_e);
58 EXPECT_FALSE(key_c < key_e);
59 EXPECT_FALSE(key_d < key_e);
60 EXPECT_FALSE(key_e < key_e);
61 EXPECT_LT(key_f, key_e);
62
63 EXPECT_FALSE(key_a < key_f);
64 EXPECT_FALSE(key_b < key_f);
65 EXPECT_FALSE(key_c < key_f);
66 EXPECT_FALSE(key_d < key_f);
67 EXPECT_FALSE(key_e < key_f);
68 EXPECT_FALSE(key_f < key_f);
69 }
70
TEST(TaskSchedulerSequenceSortKeyTest,OperatorGreaterThan)71 TEST(TaskSchedulerSequenceSortKeyTest, OperatorGreaterThan) {
72 SequenceSortKey key_a(TaskPriority::USER_BLOCKING,
73 TimeTicks::FromInternalValue(1000));
74 SequenceSortKey key_b(TaskPriority::USER_BLOCKING,
75 TimeTicks::FromInternalValue(2000));
76 SequenceSortKey key_c(TaskPriority::USER_VISIBLE,
77 TimeTicks::FromInternalValue(1000));
78 SequenceSortKey key_d(TaskPriority::USER_VISIBLE,
79 TimeTicks::FromInternalValue(2000));
80 SequenceSortKey key_e(TaskPriority::BACKGROUND,
81 TimeTicks::FromInternalValue(1000));
82 SequenceSortKey key_f(TaskPriority::BACKGROUND,
83 TimeTicks::FromInternalValue(2000));
84
85 EXPECT_FALSE(key_a > key_a);
86 EXPECT_FALSE(key_b > key_a);
87 EXPECT_FALSE(key_c > key_a);
88 EXPECT_FALSE(key_d > key_a);
89 EXPECT_FALSE(key_e > key_a);
90 EXPECT_FALSE(key_f > key_a);
91
92 EXPECT_GT(key_a, key_b);
93 EXPECT_FALSE(key_b > key_b);
94 EXPECT_FALSE(key_c > key_b);
95 EXPECT_FALSE(key_d > key_b);
96 EXPECT_FALSE(key_e > key_b);
97 EXPECT_FALSE(key_f > key_b);
98
99 EXPECT_GT(key_a, key_c);
100 EXPECT_GT(key_b, key_c);
101 EXPECT_FALSE(key_c > key_c);
102 EXPECT_FALSE(key_d > key_c);
103 EXPECT_FALSE(key_e > key_c);
104 EXPECT_FALSE(key_f > key_c);
105
106 EXPECT_GT(key_a, key_d);
107 EXPECT_GT(key_b, key_d);
108 EXPECT_GT(key_c, key_d);
109 EXPECT_FALSE(key_d > key_d);
110 EXPECT_FALSE(key_e > key_d);
111 EXPECT_FALSE(key_f > key_d);
112
113 EXPECT_GT(key_a, key_e);
114 EXPECT_GT(key_b, key_e);
115 EXPECT_GT(key_c, key_e);
116 EXPECT_GT(key_d, key_e);
117 EXPECT_FALSE(key_e > key_e);
118 EXPECT_FALSE(key_f > key_e);
119
120 EXPECT_GT(key_a, key_f);
121 EXPECT_GT(key_b, key_f);
122 EXPECT_GT(key_c, key_f);
123 EXPECT_GT(key_d, key_f);
124 EXPECT_GT(key_e, key_f);
125 EXPECT_FALSE(key_f > key_f);
126 }
127
TEST(TaskSchedulerSequenceSortKeyTest,OperatorEqual)128 TEST(TaskSchedulerSequenceSortKeyTest, OperatorEqual) {
129 SequenceSortKey key_a(TaskPriority::USER_BLOCKING,
130 TimeTicks::FromInternalValue(1000));
131 SequenceSortKey key_b(TaskPriority::USER_BLOCKING,
132 TimeTicks::FromInternalValue(2000));
133 SequenceSortKey key_c(TaskPriority::USER_VISIBLE,
134 TimeTicks::FromInternalValue(1000));
135 SequenceSortKey key_d(TaskPriority::USER_VISIBLE,
136 TimeTicks::FromInternalValue(2000));
137 SequenceSortKey key_e(TaskPriority::BACKGROUND,
138 TimeTicks::FromInternalValue(1000));
139 SequenceSortKey key_f(TaskPriority::BACKGROUND,
140 TimeTicks::FromInternalValue(2000));
141
142 EXPECT_EQ(key_a, key_a);
143 EXPECT_FALSE(key_b == key_a);
144 EXPECT_FALSE(key_c == key_a);
145 EXPECT_FALSE(key_d == key_a);
146 EXPECT_FALSE(key_e == key_a);
147 EXPECT_FALSE(key_f == key_a);
148
149 EXPECT_FALSE(key_a == key_b);
150 EXPECT_EQ(key_b, key_b);
151 EXPECT_FALSE(key_c == key_b);
152 EXPECT_FALSE(key_d == key_b);
153 EXPECT_FALSE(key_e == key_b);
154 EXPECT_FALSE(key_f == key_b);
155
156 EXPECT_FALSE(key_a == key_c);
157 EXPECT_FALSE(key_b == key_c);
158 EXPECT_EQ(key_c, key_c);
159 EXPECT_FALSE(key_d == key_c);
160 EXPECT_FALSE(key_e == key_c);
161 EXPECT_FALSE(key_f == key_c);
162
163 EXPECT_FALSE(key_a == key_d);
164 EXPECT_FALSE(key_b == key_d);
165 EXPECT_FALSE(key_c == key_d);
166 EXPECT_EQ(key_d, key_d);
167 EXPECT_FALSE(key_e == key_d);
168 EXPECT_FALSE(key_f == key_d);
169
170 EXPECT_FALSE(key_a == key_e);
171 EXPECT_FALSE(key_b == key_e);
172 EXPECT_FALSE(key_c == key_e);
173 EXPECT_FALSE(key_d == key_e);
174 EXPECT_EQ(key_e, key_e);
175 EXPECT_FALSE(key_f == key_e);
176
177 EXPECT_FALSE(key_a == key_f);
178 EXPECT_FALSE(key_b == key_f);
179 EXPECT_FALSE(key_c == key_f);
180 EXPECT_FALSE(key_d == key_f);
181 EXPECT_FALSE(key_e == key_f);
182 EXPECT_EQ(key_f, key_f);
183 }
184
TEST(TaskSchedulerSequenceSortKeyTest,OperatorNotEqual)185 TEST(TaskSchedulerSequenceSortKeyTest, OperatorNotEqual) {
186 SequenceSortKey key_a(TaskPriority::USER_BLOCKING,
187 TimeTicks::FromInternalValue(1000));
188 SequenceSortKey key_b(TaskPriority::USER_BLOCKING,
189 TimeTicks::FromInternalValue(2000));
190 SequenceSortKey key_c(TaskPriority::USER_VISIBLE,
191 TimeTicks::FromInternalValue(1000));
192 SequenceSortKey key_d(TaskPriority::USER_VISIBLE,
193 TimeTicks::FromInternalValue(2000));
194 SequenceSortKey key_e(TaskPriority::BACKGROUND,
195 TimeTicks::FromInternalValue(1000));
196 SequenceSortKey key_f(TaskPriority::BACKGROUND,
197 TimeTicks::FromInternalValue(2000));
198
199 EXPECT_FALSE(key_a != key_a);
200 EXPECT_NE(key_b, key_a);
201 EXPECT_NE(key_c, key_a);
202 EXPECT_NE(key_d, key_a);
203 EXPECT_NE(key_e, key_a);
204 EXPECT_NE(key_f, key_a);
205
206 EXPECT_NE(key_a, key_b);
207 EXPECT_FALSE(key_b != key_b);
208 EXPECT_NE(key_c, key_b);
209 EXPECT_NE(key_d, key_b);
210 EXPECT_NE(key_e, key_b);
211 EXPECT_NE(key_f, key_b);
212
213 EXPECT_NE(key_a, key_c);
214 EXPECT_NE(key_b, key_c);
215 EXPECT_FALSE(key_c != key_c);
216 EXPECT_NE(key_d, key_c);
217 EXPECT_NE(key_e, key_c);
218 EXPECT_NE(key_f, key_c);
219
220 EXPECT_NE(key_a, key_d);
221 EXPECT_NE(key_b, key_d);
222 EXPECT_NE(key_c, key_d);
223 EXPECT_FALSE(key_d != key_d);
224 EXPECT_NE(key_e, key_d);
225 EXPECT_NE(key_f, key_d);
226
227 EXPECT_NE(key_a, key_e);
228 EXPECT_NE(key_b, key_e);
229 EXPECT_NE(key_c, key_e);
230 EXPECT_NE(key_d, key_e);
231 EXPECT_FALSE(key_e != key_e);
232 EXPECT_NE(key_f, key_e);
233
234 EXPECT_NE(key_a, key_f);
235 EXPECT_NE(key_b, key_f);
236 EXPECT_NE(key_c, key_f);
237 EXPECT_NE(key_d, key_f);
238 EXPECT_NE(key_e, key_f);
239 EXPECT_FALSE(key_f != key_f);
240 }
241
242 } // namespace internal
243 } // namespace base
244