OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 <algorithm> | |
6 | |
7 #include "base/time/time.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "tools/gn/uniquify.h" | |
10 | |
11 namespace { | |
12 | |
13 template<typename T> | |
14 bool RunTest(const std::vector<T>& input, const std::vector<T>& expected) { | |
15 std::vector<T> result = input; | |
16 UniquifyBruteForce(&result); | |
17 if (result.size() != expected.size()) | |
18 return false; | |
19 if (!std::equal(result.begin(), result.end(), expected.begin())) | |
20 return false; | |
21 | |
22 result = input; | |
23 UniquifyHash(&result); | |
24 if (result.size() != expected.size()) | |
25 return false; | |
26 if (!std::equal(result.begin(), result.end(), expected.begin())) | |
27 return false; | |
28 | |
29 return true; | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 TEST(Uniquify, Empty) { | |
35 std::vector<int> empty; | |
36 EXPECT_TRUE(RunTest(empty, empty)); | |
37 } | |
38 | |
39 TEST(Uniquify, OneElt) { | |
40 std::vector<void*> one; | |
41 one.push_back(&one); | |
42 EXPECT_TRUE(RunTest(one, one)); | |
43 } | |
44 | |
45 TEST(Uniquify, AllSame) { | |
46 std::vector<void*> input; | |
47 for (int i = 0; i < 100; i++) | |
48 input.push_back(&input); | |
49 std::vector<void*> expected; | |
50 expected.push_back(&input); | |
51 EXPECT_TRUE(RunTest(input, expected)); | |
52 } | |
53 | |
54 TEST(Uniquify, AllUnique) { | |
55 std::vector<int> vals; | |
56 for (int i = 0; i < 100; i++) | |
57 vals.push_back(i); | |
58 EXPECT_TRUE(RunTest(vals, vals)); | |
59 } | |
60 | |
61 TEST(Uniquify, Various) { | |
62 std::vector<int> input; | |
63 input.push_back(0); | |
64 input.push_back(1); | |
65 input.push_back(2); | |
66 input.push_back(1); | |
67 input.push_back(1); | |
68 input.push_back(0); | |
69 input.push_back(7); | |
70 input.push_back(0); | |
71 | |
72 std::vector<int> output; | |
73 output.push_back(0); | |
74 output.push_back(1); | |
75 output.push_back(2); | |
76 output.push_back(7); | |
77 EXPECT_TRUE(RunTest(input, output)); | |
78 } | |
79 | |
80 /* Performance test for measuring relative performance of brute-force and | |
viettrungluu
2013/10/08 23:39:41
I wish it were easier to add perf tests (and monit
| |
81 hashing for strings over containers of different lengths. This was used | |
82 to pick the array size below which brute-force is faster. | |
83 TEST(Uniquify, Perf) { | |
84 // Length of the strings we're using. | |
85 const size_t kStrLen = 32; | |
86 | |
87 // This loop tries input vectors of different lengths. | |
88 for (size_t vec_len = 1; vec_len < 256; vec_len += 2) { | |
89 std::vector<std::string> strs; | |
90 for (size_t cur_str_i = 0; cur_str_i < vec_len; cur_str_i++) { | |
91 // Make a unique string. | |
92 std::string str; | |
93 str.push_back(static_cast<char>(cur_str_i % 255)); | |
94 str.push_back(static_cast<char>(cur_str_i / 255)); | |
95 for (size_t str_i = 1; str_i < kStrLen; str_i++) | |
96 str.push_back('a'); | |
97 strs.push_back(str); | |
98 } | |
99 | |
100 const size_t kIters = 1000; // Number of times to run algorithm. | |
101 base::TimeTicks brute_begin = base::TimeTicks::Now(); | |
102 for (size_t i = 0; i < kIters; i++) | |
103 UniquifyBruteForce(&strs); | |
104 base::TimeTicks mid = base::TimeTicks::Now(); | |
105 for (size_t i = 0; i < kIters; i++) | |
106 UniquifyHash(&strs); | |
107 base::TimeTicks end = base::TimeTicks::Now(); | |
108 | |
109 printf("%d: brute = %f, hash = %f\n", | |
110 vec_len, | |
111 (mid - brute_begin).InMillisecondsF(), | |
112 (end - mid).InMillisecondsF()); | |
113 } | |
114 } | |
115 */ | |
OLD | NEW |