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.
5 // Test program to convert lists of integers into ranges, and vice versa.
7 #include "chrome/browser/safe_browsing/chunk_range.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // Test various configurations of chunk numbers.
12 TEST(SafeBrowsingChunkRangeTest
, TestChunksToRangeString
) {
13 std::vector
<int> chunks
;
14 std::string range_string
;
16 // Test one chunk range and one single value.
22 ChunksToRangeString(chunks
, &range_string
);
23 EXPECT_EQ(range_string
, std::string("1-4,7"));
28 // Test all chunk numbers in one range.
37 ChunksToRangeString(chunks
, &range_string
);
38 EXPECT_EQ(range_string
, std::string("3-10"));
43 // Test no chunk numbers in contiguous ranges.
52 ChunksToRangeString(chunks
, &range_string
);
53 EXPECT_EQ(range_string
, std::string("3,5,7,9,11,13,15,17"));
58 // Test a single chunk number.
60 ChunksToRangeString(chunks
, &range_string
);
61 EXPECT_EQ(range_string
, std::string("17"));
76 ChunksToRangeString(chunks
, &range_string
);
77 EXPECT_EQ(range_string
, std::string("1-3,7"));
79 // Test unsorted chunks.
85 ChunksToRangeString(chunks
, &range_string
);
86 EXPECT_EQ(range_string
, std::string("1-4,7"));
92 TEST(SafeBrowsingChunkRangeTest
, TestStringToRanges
) {
93 std::vector
<ChunkRange
> ranges
;
95 std::string input
= "1-100,398,415,1138-2001,2019";
96 EXPECT_TRUE(StringToRanges(input
, &ranges
));
97 EXPECT_EQ(ranges
.size(), static_cast<size_t>(5));
98 EXPECT_EQ(ranges
[0].start(), 1);
99 EXPECT_EQ(ranges
[0].stop(), 100);
100 EXPECT_EQ(ranges
[1].start(), 398);
101 EXPECT_EQ(ranges
[1].stop(), 398);
102 EXPECT_EQ(ranges
[3].start(), 1138);
103 EXPECT_EQ(ranges
[3].stop(), 2001);
107 input
= "1,2,3,4,5,6,7";
108 EXPECT_TRUE(StringToRanges(input
, &ranges
));
109 EXPECT_EQ(ranges
.size(), static_cast<size_t>(7));
114 EXPECT_TRUE(StringToRanges(input
, &ranges
));
115 EXPECT_EQ(ranges
.size(), static_cast<size_t>(1));
116 EXPECT_EQ(ranges
[0].start(), 300);
117 EXPECT_EQ(ranges
[0].stop(), 3001);
122 EXPECT_TRUE(StringToRanges(input
, &ranges
));
123 EXPECT_EQ(ranges
.size(), static_cast<size_t>(1));
124 EXPECT_EQ(ranges
[0].start(), 17);
125 EXPECT_EQ(ranges
[0].stop(), 17);
130 EXPECT_FALSE(StringToRanges(input
, &ranges
));
134 TEST(SafeBrowsingChunkRangeTest
, TestRangesToChunks
) {
135 std::vector
<ChunkRange
> ranges
;
136 ranges
.push_back(ChunkRange(1, 4));
137 ranges
.push_back(ChunkRange(17));
139 std::vector
<int> chunks
;
140 RangesToChunks(ranges
, &chunks
);
142 EXPECT_EQ(chunks
.size(), static_cast<size_t>(5));
143 EXPECT_EQ(chunks
[0], 1);
144 EXPECT_EQ(chunks
[1], 2);
145 EXPECT_EQ(chunks
[2], 3);
146 EXPECT_EQ(chunks
[3], 4);
147 EXPECT_EQ(chunks
[4], 17);
151 TEST(SafeBrowsingChunkRangeTest
, TestSearchChunkRanges
) {
152 std::string
range_str("1-10,15-17,21-410,555,991-1000");
153 std::vector
<ChunkRange
> ranges
;
154 StringToRanges(range_str
, &ranges
);
156 EXPECT_TRUE(IsChunkInRange(7, ranges
));
157 EXPECT_TRUE(IsChunkInRange(300, ranges
));
158 EXPECT_TRUE(IsChunkInRange(555, ranges
));
159 EXPECT_TRUE(IsChunkInRange(1, ranges
));
160 EXPECT_TRUE(IsChunkInRange(1000, ranges
));
162 EXPECT_FALSE(IsChunkInRange(11, ranges
));
163 EXPECT_FALSE(IsChunkInRange(990, ranges
));
164 EXPECT_FALSE(IsChunkInRange(2000, ranges
));