1 /*
2 * test_sid_list.cc: Part of GNU CSSC.
3 *
4 *
5 * Copyright (C) 2010, 2011, 2014, 2019 Free Software Foundation, Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Unit tests for sid_list.h.
21 *
22 */
23 #include "sid_list.h"
24 #include "sl-merge.h"
25
26 #include <gtest/gtest.h>
27
28
TEST(SidListTest,NullConstructor)29 TEST(SidListTest, NullConstructor)
30 {
31 sid_list x;
32 ASSERT_TRUE(x.valid());
33 ASSERT_TRUE(x.empty());
34 }
35
TEST(SidListTest,OtherConstructors)36 TEST(SidListTest, OtherConstructors)
37 {
38 sid_list a;
39 ASSERT_TRUE(a.valid());
40 ASSERT_TRUE(a.empty());
41 sid_list b(a);
42 ASSERT_TRUE(b.empty());
43 ASSERT_TRUE(b.valid());
44 sid_list c = a;
45 ASSERT_TRUE(c.empty());
46 ASSERT_TRUE(c.valid());
47 }
48
TEST(SidListTest,Invalidate)49 TEST(SidListTest, Invalidate)
50 {
51 sid_list x;
52 ASSERT_TRUE(x.valid());
53 x.invalidate();
54 ASSERT_FALSE(x.valid());
55 }
56
TEST(SidListTest,OpenRange)57 TEST(SidListTest, OpenRange)
58 {
59 /* SourceForge bug number #438857:
60 * ranges like "1.1.1.2," cause an assertion
61 * failure while SCCS just ignores the empty list item.
62 */
63 const sid_list x("1.1.1.2,");
64 ASSERT_TRUE(x.valid());
65
66 const sid s("1.1.1.2");
67 ASSERT_TRUE(x.member(s));
68 }
69
TEST(SidListTest,SingleItem)70 TEST(SidListTest, SingleItem)
71 {
72 const sid_list x("1.7");
73 ASSERT_TRUE(x.valid());
74 ASSERT_FALSE(x.empty());
75 }
76
TEST(SidListTest,CommaSeparatedItems)77 TEST(SidListTest, CommaSeparatedItems)
78 {
79 const sid_list x("1.1.1.2,2.2.2.3");
80 ASSERT_TRUE(x.valid());
81
82 const sid s("1.1.1.2");
83 const sid t("2.2.2.3");
84 ASSERT_TRUE(x.member(s));
85 ASSERT_TRUE(x.member(t));
86 }
87
TEST(SidListTest,OneRangeTrunk)88 TEST(SidListTest, OneRangeTrunk)
89 {
90 const sid_list x("1.1-1.8");
91 ASSERT_TRUE(x.valid());
92
93 ASSERT_TRUE(x.member(sid("1.2")));
94 ASSERT_TRUE(x.member(sid("1.3")));
95 ASSERT_TRUE(x.member(sid("1.8")));
96 ASSERT_FALSE(x.member(sid("1.9")));
97 ASSERT_FALSE(x.member(sid("1.2.1.1")));
98 }
99
TEST(SidListTest,OneRangeBranch)100 TEST(SidListTest, OneRangeBranch)
101 {
102 const sid_list x("1.2.1.1-1.2.1.4");
103 ASSERT_TRUE(x.valid());
104
105 ASSERT_FALSE(x.member(sid("1.2")));
106 ASSERT_TRUE(x.member(sid("1.2.1.1")));
107 ASSERT_TRUE(x.member(sid("1.2.1.2")));
108 ASSERT_TRUE(x.member(sid("1.2.1.3")));
109 ASSERT_TRUE(x.member(sid("1.2.1.4")));
110 ASSERT_FALSE(x.member(sid("1.2.1.5")));
111 ASSERT_FALSE(x.member(sid("1.2")));
112 }
113
TEST(SidListTest,BackwardRange)114 TEST(SidListTest, BackwardRange)
115 {
116 const sid_list x("1.2.1.4-1.2.1.1");
117 ASSERT_FALSE(x.valid());
118 }
119
TEST(SidListTest,Overlap)120 TEST(SidListTest, Overlap)
121 {
122 const sid_list x("1.2.1.1-1.2.1.9,1.2.1.7-1.2.1.14");
123 ASSERT_TRUE(x.valid());
124 ASSERT_TRUE(x.member(sid("1.2.1.1")));
125 ASSERT_TRUE(x.member(sid("1.2.1.7")));
126 ASSERT_TRUE(x.member(sid("1.2.1.9")));
127 ASSERT_TRUE(x.member(sid("1.2.1.10")));
128 ASSERT_TRUE(x.member(sid("1.2.1.14")));
129 ASSERT_FALSE(x.member(sid("1.2.1.15")));
130 }
131
TEST(SidListTest,OverlapOtherOrder)132 TEST(SidListTest, OverlapOtherOrder)
133 {
134 const sid_list x("1.2.1.7-1.2.1.14,1.2.1.1-1.2.1.9");
135 ASSERT_TRUE(x.valid());
136 ASSERT_TRUE(x.member(sid("1.2.1.1")));
137 ASSERT_TRUE(x.member(sid("1.2.1.7")));
138 ASSERT_TRUE(x.member(sid("1.2.1.9")));
139 ASSERT_TRUE(x.member(sid("1.2.1.10")));
140 ASSERT_TRUE(x.member(sid("1.2.1.14")));
141 ASSERT_FALSE(x.member(sid("1.2.1.15")));
142 }
143
TEST(SidListTest,Island)144 TEST(SidListTest, Island)
145 {
146 const sid_list x("1.2.1.7-1.2.1.14,1.2.1.9");
147 ASSERT_TRUE(x.valid());
148 ASSERT_TRUE(x.member(sid("1.2.1.7")));
149 ASSERT_TRUE(x.member(sid("1.2.1.9")));
150 ASSERT_TRUE(x.member(sid("1.2.1.14")));
151 ASSERT_FALSE(x.member(sid("1.2.1.15")));
152 }
153
TEST(SidListTest,Outlier)154 TEST(SidListTest, Outlier)
155 {
156 const sid_list x("1.2.1.7-1.2.1.14,1.2.1.19");
157 ASSERT_TRUE(x.valid());
158 ASSERT_FALSE(x.empty());
159 ASSERT_TRUE(x.member(sid("1.2.1.7")));
160 ASSERT_TRUE(x.member(sid("1.2.1.9")));
161 ASSERT_TRUE(x.member(sid("1.2.1.14")));
162 ASSERT_FALSE(x.member(sid("1.2.1.15")));
163 ASSERT_TRUE(x.member(sid("1.2.1.19")));
164 }
165
TEST(SidListTest,Assignment)166 TEST(SidListTest, Assignment)
167 {
168 const sid_list y("1.2.1.7-1.2.1.14,1.2.1.19");
169 const sid_list x(y);
170 ASSERT_TRUE(x.valid());
171 ASSERT_TRUE(x.member(sid("1.2.1.7")));
172 ASSERT_TRUE(x.member(sid("1.2.1.9")));
173 ASSERT_TRUE(x.member(sid("1.2.1.14")));
174 ASSERT_FALSE(x.member(sid("1.2.1.15")));
175 ASSERT_TRUE(x.member(sid("1.2.1.19")));
176 }
177
TEST(SidListTest,Merge)178 TEST(SidListTest, Merge)
179 {
180 sid_list x("1.2.1.7-1.2.1.14");
181 const sid_list y("1.2.1.10-1.2.1.20");
182 const sid_list z("1.8-1.20");
183
184 x.merge(y);
185 x.merge(z);
186
187 // Verify that the pre-merge conditions still hold.
188 ASSERT_TRUE(x.member(sid("1.2.1.7")));
189 ASSERT_TRUE(y.member(sid("1.2.1.10")));
190 ASSERT_TRUE(z.member(sid("1.10")));
191
192 // Now verify that the y and z were merged into x.
193 ASSERT_TRUE(x.member(sid("1.2.1.7")));
194 ASSERT_TRUE(x.member(sid("1.2.1.10")));
195 ASSERT_TRUE(x.member(sid("1.10")));
196
197 // Verify that we don't just answer all questions with yes.
198 ASSERT_FALSE(x.member(sid("1.2.1.51")));
199 }
200
TEST(SidListTest,Remove)201 TEST(SidListTest, Remove)
202 {
203 sid_list x("1.2.1.7-1.2.1.14");
204 ASSERT_TRUE(x.member(sid("1.2.1.10")));
205 x.remove("1.2.1.10");
206 ASSERT_FALSE(x.member(sid("1.2.1.10")));
207 }
208