summaryrefslogtreecommitdiff
path: root/tests/test_choice.cc
blob: 128e10bb2bac4a35630f4e078a01d6285036b649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// test_choice.cc
//
//   Copyright (C) 2009 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

#include <generic/problemresolver/choice.h>
#include <generic/problemresolver/dummy_universe.h>

#include <generic/util/compare3.h>

#include <cppunit/extensions/HelperMacros.h>

#include <iostream>

namespace
{
  const char *dummy_universe_1 = "\
UNIVERSE [			  \
  PACKAGE a < v1 v2 v3 > v1	  \
  PACKAGE b < v1 v2 v3 > v1	  \
  PACKAGE c < v1 v2 v3 > v1	  \
				  \
  DEP a v1 -> < b v2 >		  \
  DEP b v2 -> < c v2 >		  \
				  \
  DEP a v2 -> < >		  \
  DEP a v3 -> < >		  \
]";
}

class Choice_Test : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(Choice_Test);

  CPPUNIT_TEST(testContains);
  CPPUNIT_TEST(testCompare);

  CPPUNIT_TEST_SUITE_END();

  static dummy_universe_ref parseUniverse(const std::string &s)
  {
    std::istringstream in(s);

    return parse_universe(in);
  }

  typedef dummy_universe_ref::package package;
  typedef dummy_universe_ref::version version;
  typedef dummy_universe_ref::dep dep;
  typedef generic_choice<dummy_universe_ref> choice;

  // We don't care about id in these tests, so these are convenience
  // routines that use a dummy value.
  static choice make_install_version(const version &v)
  {
    return choice::make_install_version(v, -1);
  }

  static choice make_install_version(const version &v, const dep &d)
  {
    return choice::make_install_version(v, d, -1);
  }

  static choice make_install_version_from_dep_source(const version &v, const dep &d)
  {
    return choice::make_install_version_from_dep_source(v, d, -1);
  }

  static choice make_break_soft_dep(const dep &d)
  {
    return choice::make_break_soft_dep(d, -1);
  }

public:
  void testCompare()
  {
    dummy_universe_ref u(parseUniverse(dummy_universe_1));

    package a(u.find_package("a"));
    package b(u.find_package("b"));
    package c(u.find_package("c"));

    version av1(a.version_from_name("v1"));
    version av2(a.version_from_name("v2"));
    version av3(a.version_from_name("v3"));

    version bv1(b.version_from_name("v1"));
    version bv2(b.version_from_name("v2"));
    version bv3(b.version_from_name("v3"));

    version cv1(c.version_from_name("v1"));
    version cv2(c.version_from_name("v2"));
    version cv3(c.version_from_name("v3"));

    dep av1d1(*av1.deps_begin());
    dep bv2d1(*bv2.deps_begin());
    dep av2d1(*av2.deps_begin());
    dep av3d1(*av3.deps_begin());

    std::vector<choice> choices;
    choices.push_back(make_install_version(av1));
    choices.push_back(make_install_version_from_dep_source(av1, av2d1));
    choices.push_back(make_install_version(av1, av2d1));
    choices.push_back(make_install_version_from_dep_source(av1, av3d1));
    choices.push_back(make_install_version(av2));
    choices.push_back(make_install_version(bv1));
    choices.push_back(make_break_soft_dep(av1d1));
    choices.push_back(make_break_soft_dep(av2d1));
    choices.push_back(make_install_version_from_dep_source(bv1, bv2d1));
    choices.push_back(make_install_version(cv2));

    using aptitude::util::compare3;
    for(std::vector<choice>::const_iterator itA = choices.begin(); itA != choices.end(); ++itA)
      {
	// Check reflexivity.
	CPPUNIT_ASSERT_EQUAL(0, compare3(*itA, *itA));
	CPPUNIT_ASSERT_EQUAL(*itA, *itA);

	for(std::vector<choice>::const_iterator itB = choices.begin(); itB != choices.end(); ++itB)
	  {
	    int cmpAB = compare3(*itA, *itB);
	    int cmpBA = compare3(*itB, *itA);

	    if(itA != itB)
	      {
		CPPUNIT_ASSERT(!(*itA == *itB));
		CPPUNIT_ASSERT(cmpAB != 0);
		CPPUNIT_ASSERT(cmpBA != 0);
	      }

	    // Check antisymmetry.
	    if(cmpAB < 0)
	      CPPUNIT_ASSERT(cmpBA > 0);
	    else if(cmpAB > 0)
	      CPPUNIT_ASSERT(cmpBA < 0);
	    else
	      CPPUNIT_ASSERT_EQUAL(0, cmpBA);

	    for(std::vector<choice>::const_iterator itC = choices.begin(); itC != choices.end(); ++itC)
	      {
		// Check transitivity.
		int cmpBC = compare3(*itB, *itC);
		int cmpAC = compare3(*itA, *itC);

		if(cmpAB == 0 && cmpBC == 0)
		  CPPUNIT_ASSERT_EQUAL(0, cmpAC);
		else if(cmpAB <= 0 && cmpBC <= 0)
		  CPPUNIT_ASSERT(cmpAC < 0);
		else if(cmpAB >= 0 && cmpBC >= 0)
		  CPPUNIT_ASSERT(cmpAC > 0);
	      }
	  }
      }
  }

  void testContains()
  {
    dummy_universe_ref u(parseUniverse(dummy_universe_1));

    package a(u.find_package("a"));
    package b(u.find_package("b"));
    package c(u.find_package("c"));

    version av1(a.version_from_name("v1"));
    version av2(a.version_from_name("v2"));
    version av3(a.version_from_name("v3"));

    version bv1(b.version_from_name("v1"));
    version bv2(b.version_from_name("v2"));
    version bv3(b.version_from_name("v3"));

    version cv1(c.version_from_name("v1"));
    version cv2(c.version_from_name("v2"));
    version cv3(c.version_from_name("v3"));

    dep av1d1(*av1.deps_begin());
    dep bv2d1(*bv2.deps_begin());
    dep av2d1(*av2.deps_begin());
    dep av3d1(*av3.deps_begin());

    const choice cav1 = make_install_version(av1);
    const choice cav1av2d1 = make_install_version_from_dep_source(av1, av2d1);
    const choice cav1av2d1_unscoped = make_install_version(av1, av2d1);
    const choice cav1av3d1 = make_install_version_from_dep_source(av1, av3d1);
    const choice cav2 = make_install_version(av2);
    const choice cbv1 = make_install_version(bv1);
    const choice cav1d1 = make_break_soft_dep(av1d1);
    const choice cav2d1 = make_break_soft_dep(av2d1);

    // Check that all the containment relations here are correct.
    CPPUNIT_ASSERT(cav1.contains(cav1));
    CPPUNIT_ASSERT(cav1.contains(cav1av2d1));
    CPPUNIT_ASSERT(cav1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(cav1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav1.contains(cav2));
    CPPUNIT_ASSERT(!cav1.contains(cbv1));
    CPPUNIT_ASSERT(!cav1.contains(cav1d1));
    CPPUNIT_ASSERT(!cav1.contains(cav2d1));

    CPPUNIT_ASSERT(!cav1av2d1.contains(cav1));
    CPPUNIT_ASSERT(cav1av2d1.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cav2));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cbv1));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cav1d1));
    CPPUNIT_ASSERT(!cav1av2d1.contains(cav2d1));

    CPPUNIT_ASSERT(cav1av2d1_unscoped.contains(cav1));
    CPPUNIT_ASSERT(cav1av2d1_unscoped.contains(cav1av2d1));
    CPPUNIT_ASSERT(cav1av2d1_unscoped.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(cav1av2d1_unscoped.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav1av2d1_unscoped.contains(cav2));
    CPPUNIT_ASSERT(!cav1av2d1_unscoped.contains(cbv1));
    CPPUNIT_ASSERT(!cav1av2d1_unscoped.contains(cav1d1));
    CPPUNIT_ASSERT(!cav1av2d1_unscoped.contains(cav2d1));

    CPPUNIT_ASSERT(!cav1av3d1.contains(cav1));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(cav1av3d1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cav2));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cbv1));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cav1d1));
    CPPUNIT_ASSERT(!cav1av3d1.contains(cav2d1));

    // All the rest should only contain themselves.

    CPPUNIT_ASSERT(!cav2.contains(cav1));
    CPPUNIT_ASSERT(!cav2.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cav2.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(!cav2.contains(cav1av3d1));
    CPPUNIT_ASSERT(cav2.contains(cav2));
    CPPUNIT_ASSERT(!cav2.contains(cbv1));
    CPPUNIT_ASSERT(!cav2.contains(cav1d1));
    CPPUNIT_ASSERT(!cav2.contains(cav2d1));

    CPPUNIT_ASSERT(!cbv1.contains(cav1));
    CPPUNIT_ASSERT(!cbv1.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cbv1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(!cbv1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cbv1.contains(cav2));
    CPPUNIT_ASSERT(cbv1.contains(cbv1));
    CPPUNIT_ASSERT(!cbv1.contains(cav1d1));
    CPPUNIT_ASSERT(!cbv1.contains(cav2d1));

    CPPUNIT_ASSERT(!cav1d1.contains(cav1));
    CPPUNIT_ASSERT(!cav1d1.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cav1d1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(!cav1d1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav1d1.contains(cav2));
    CPPUNIT_ASSERT(!cav1d1.contains(cbv1));
    CPPUNIT_ASSERT(cav1d1.contains(cav1d1));
    CPPUNIT_ASSERT(!cav1d1.contains(cav2d1));

    CPPUNIT_ASSERT(!cav2d1.contains(cav1));
    CPPUNIT_ASSERT(!cav2d1.contains(cav1av2d1));
    CPPUNIT_ASSERT(!cav2d1.contains(cav1av2d1_unscoped));
    CPPUNIT_ASSERT(!cav2d1.contains(cav1av3d1));
    CPPUNIT_ASSERT(!cav2d1.contains(cav2));
    CPPUNIT_ASSERT(!cav2d1.contains(cbv1));
    CPPUNIT_ASSERT(!cav2d1.contains(cav1d1));
    CPPUNIT_ASSERT(cav2d1.contains(cav2d1));
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(Choice_Test);