Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / chunk_range_unittest.cc
blob67686b96a21b3a31832a0f8162c5afe8a519c3ba
1 // Copyright (c) 2006-2008 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 // Test program to convert lists of integers into ranges, and vice versa.
7 #include "chunk_range.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 // Test various configurations of chunk numbers.
11 TEST(SafeBrowsingChunkRangeTest, TestChunksToRangeString) {
12 std::vector<int> chunks;
13 std::string range_string;
15 // Test one chunk range and one single value.
16 chunks.push_back(1);
17 chunks.push_back(2);
18 chunks.push_back(3);
19 chunks.push_back(4);
20 chunks.push_back(7);
21 ChunksToRangeString(chunks, &range_string);
22 EXPECT_EQ(range_string, std::string("1-4,7"));
24 chunks.clear();
25 range_string.clear();
27 // Test all chunk numbers in one range.
28 chunks.push_back(3);
29 chunks.push_back(4);
30 chunks.push_back(5);
31 chunks.push_back(6);
32 chunks.push_back(7);
33 chunks.push_back(8);
34 chunks.push_back(9);
35 chunks.push_back(10);
36 ChunksToRangeString(chunks, &range_string);
37 EXPECT_EQ(range_string, std::string("3-10"));
39 chunks.clear();
40 range_string.clear();
42 // Test no chunk numbers in contiguous ranges.
43 chunks.push_back(3);
44 chunks.push_back(5);
45 chunks.push_back(7);
46 chunks.push_back(9);
47 chunks.push_back(11);
48 chunks.push_back(13);
49 chunks.push_back(15);
50 chunks.push_back(17);
51 ChunksToRangeString(chunks, &range_string);
52 EXPECT_EQ(range_string, std::string("3,5,7,9,11,13,15,17"));
54 chunks.clear();
55 range_string.clear();
57 // Test a single chunk number.
58 chunks.push_back(17);
59 ChunksToRangeString(chunks, &range_string);
60 EXPECT_EQ(range_string, std::string("17"));
62 chunks.clear();
63 range_string.clear();
65 // Test duplicates.
66 chunks.push_back(1);
67 chunks.push_back(2);
68 chunks.push_back(2);
69 chunks.push_back(2);
70 chunks.push_back(3);
71 chunks.push_back(7);
72 chunks.push_back(7);
73 chunks.push_back(7);
74 chunks.push_back(7);
75 ChunksToRangeString(chunks, &range_string);
76 EXPECT_EQ(range_string, std::string("1-3,7"));
78 // Test unsorted chunks.
79 chunks.push_back(4);
80 chunks.push_back(1);
81 chunks.push_back(7);
82 chunks.push_back(3);
83 chunks.push_back(2);
84 ChunksToRangeString(chunks, &range_string);
85 EXPECT_EQ(range_string, std::string("1-4,7"));
87 chunks.clear();
88 range_string.clear();
91 TEST(SafeBrowsingChunkRangeTest, TestStringToRanges) {
92 std::vector<ChunkRange> ranges;
94 std::string input = "1-100,398,415,1138-2001,2019";
95 EXPECT_TRUE(StringToRanges(input, &ranges));
96 EXPECT_EQ(ranges.size(), static_cast<size_t>(5));
97 EXPECT_EQ(ranges[0].start(), 1);
98 EXPECT_EQ(ranges[0].stop(), 100);
99 EXPECT_EQ(ranges[1].start(), 398);
100 EXPECT_EQ(ranges[1].stop(), 398);
101 EXPECT_EQ(ranges[3].start(), 1138);
102 EXPECT_EQ(ranges[3].stop(), 2001);
104 ranges.clear();
106 input = "1,2,3,4,5,6,7";
107 EXPECT_TRUE(StringToRanges(input, &ranges));
108 EXPECT_EQ(ranges.size(), static_cast<size_t>(7));
110 ranges.clear();
112 input = "300-3001";
113 EXPECT_TRUE(StringToRanges(input, &ranges));
114 EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
115 EXPECT_EQ(ranges[0].start(), 300);
116 EXPECT_EQ(ranges[0].stop(), 3001);
118 ranges.clear();
120 input = "17";
121 EXPECT_TRUE(StringToRanges(input, &ranges));
122 EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
123 EXPECT_EQ(ranges[0].start(), 17);
124 EXPECT_EQ(ranges[0].stop(), 17);
126 ranges.clear();
128 input = "x-y";
129 EXPECT_FALSE(StringToRanges(input, &ranges));
133 TEST(SafeBrowsingChunkRangeTest, TestRangesToChunks) {
134 std::vector<ChunkRange> ranges;
135 ranges.push_back(ChunkRange(1, 4));
136 ranges.push_back(ChunkRange(17));
138 std::vector<int> chunks;
139 RangesToChunks(ranges, &chunks);
141 EXPECT_EQ(chunks.size(), static_cast<size_t>(5));
142 EXPECT_EQ(chunks[0], 1);
143 EXPECT_EQ(chunks[1], 2);
144 EXPECT_EQ(chunks[2], 3);
145 EXPECT_EQ(chunks[3], 4);
146 EXPECT_EQ(chunks[4], 17);
150 TEST(SafeBrowsingChunkRangeTest, TestSearchChunkRanges) {
151 std::string range_str("1-10,15-17,21-410,555,991-1000");
152 std::vector<ChunkRange> ranges;
153 StringToRanges(range_str, &ranges);
155 EXPECT_TRUE(IsChunkInRange(7, ranges));
156 EXPECT_TRUE(IsChunkInRange(300, ranges));
157 EXPECT_TRUE(IsChunkInRange(555, ranges));
158 EXPECT_TRUE(IsChunkInRange(1, ranges));
159 EXPECT_TRUE(IsChunkInRange(1000, ranges));
161 EXPECT_FALSE(IsChunkInRange(11, ranges));
162 EXPECT_FALSE(IsChunkInRange(990, ranges));
163 EXPECT_FALSE(IsChunkInRange(2000, ranges));