• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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/thread_pool/task_source_sort_key.h"
6 
7 #include <iterator>
8 
9 #include "base/task/task_traits.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 namespace internal {
15 
16 namespace {
17 
18 // Keys are manually ordered from the least important to the most important.
19 const TaskSourceSortKey kTestKeys[] = {
20     {TaskPriority::BEST_EFFORT, TimeTicks() + Seconds(2000)},
21     {TaskPriority::BEST_EFFORT, TimeTicks() + Seconds(1000)},
22     {TaskPriority::USER_VISIBLE, TimeTicks() + Seconds(2000), 1},
23     {TaskPriority::USER_VISIBLE, TimeTicks() + Seconds(1000), 1},
24     {TaskPriority::USER_VISIBLE, TimeTicks() + Seconds(2000)},
25     {TaskPriority::USER_VISIBLE, TimeTicks() + Seconds(1000)},
26     {TaskPriority::USER_BLOCKING, TimeTicks() + Seconds(2000)},
27     {TaskPriority::USER_BLOCKING, TimeTicks() + Seconds(1000)},
28 };
29 
30 }  // namespace
31 
TEST(TaskSourceSortKeyTest,OperatorLessThan)32 TEST(TaskSourceSortKeyTest, OperatorLessThan) {
33   for (size_t i = 0; i < std::size(kTestKeys); i++) {
34     // All the entries before the index of the current key are smaller.
35     for (size_t j = 0; j < i; j++)
36       EXPECT_LT(kTestKeys[j], kTestKeys[i]);
37 
38     // All the other entries (including itself) are not smaller than the current
39     // key.
40     for (size_t j = i; j < std::size(kTestKeys); j++)
41       EXPECT_FALSE(kTestKeys[j] < kTestKeys[i]);
42   }
43 }
44 
TEST(TaskSourceSortKeyTest,OperatorEqual)45 TEST(TaskSourceSortKeyTest, OperatorEqual) {
46   // Compare each test key to every other key. They will be equal only when
47   // their index is the same.
48   for (size_t i = 0; i < std::size(kTestKeys); i++) {
49     for (size_t j = 0; j < std::size(kTestKeys); j++) {
50       if (i == j)
51         EXPECT_EQ(kTestKeys[i], kTestKeys[j]);
52       else
53         EXPECT_FALSE(kTestKeys[i] == kTestKeys[j]);
54     }
55   }
56 }
57 
TEST(TaskSourceSortKeyTest,OperatorNotEqual)58 TEST(TaskSourceSortKeyTest, OperatorNotEqual) {
59   // Compare each test key to every other key. They will not be equal only when
60   // their index is different.
61   for (size_t i = 0; i < std::size(kTestKeys); i++) {
62     for (size_t j = 0; j < std::size(kTestKeys); j++) {
63       if (i != j)
64         EXPECT_NE(kTestKeys[i], kTestKeys[j]);
65       else
66         EXPECT_FALSE(kTestKeys[i] != kTestKeys[j]);
67     }
68   }
69 }
70 
71 }  // namespace internal
72 }  // namespace base
73