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 "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.
21 ChunksToRangeString(chunks
, &range_string
);
22 EXPECT_EQ(range_string
, std::string("1-4,7"));
27 // Test all chunk numbers in one range.
36 ChunksToRangeString(chunks
, &range_string
);
37 EXPECT_EQ(range_string
, std::string("3-10"));
42 // Test no chunk numbers in contiguous ranges.
51 ChunksToRangeString(chunks
, &range_string
);
52 EXPECT_EQ(range_string
, std::string("3,5,7,9,11,13,15,17"));
57 // Test a single chunk number.
59 ChunksToRangeString(chunks
, &range_string
);
60 EXPECT_EQ(range_string
, std::string("17"));
75 ChunksToRangeString(chunks
, &range_string
);
76 EXPECT_EQ(range_string
, std::string("1-3,7"));
78 // Test unsorted chunks.
84 ChunksToRangeString(chunks
, &range_string
);
85 EXPECT_EQ(range_string
, std::string("1-4,7"));
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);
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));
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);
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);
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
));