libept
apt.test.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <ept/test.h>
20 #include <ept/apt/apt.h>
21 #include <set>
22 #include <algorithm>
23 
24 using namespace std;
25 using namespace ept;
26 using namespace ept::apt;
27 
30 
31  // Check that iterations iterates among some packages
32  Test iterators()
33  {
34  Apt::iterator i = apt.begin();
35  assert(i != apt.end());
36 
37  size_t count = 0;
38  for (; i != apt.end(); ++i)
39  ++count;
40 
41  assert(count > 100);
42  }
43 
44  // Check that iteration gives some well-known packages
45  Test aptExists()
46  {
47  set<string> packages;
48 
49  std::copy(apt.begin(), apt.end(), inserter(packages, packages.begin()));
50 
51  assert(packages.find("libsp1") != packages.end());
52  // TODO this exposes a bug somewhere... sp definitely is among
53  // the packages
54  // assert(packages.find("sp") != packages.end());
55  assert(packages.find("") == packages.end());
56  }
57 
58  // Check that timestamp gives some meaningful timestamp
59  Test timestamp()
60  {
61  time_t ts = apt.timestamp();
62  assert(ts > 1000000);
63  }
64 
65  // Check the package validator
66  Test validity()
67  {
68  assert(apt.isValid("apt"));
69  assert(!apt.isValid("this-package-does-not-really-exists"));
70  }
71 
72  // Check the version instantiators
73  Test versions()
74  {
75  std::string pkg("apt");
76  Version ver = apt.candidateVersion(pkg);
77  assert(ver.isValid());
78 
79  ver = apt.installedVersion(pkg);
80  assert(ver.isValid());
81 
82  ver = apt.anyVersion(pkg);
83  assert(ver.isValid());
84 
85  std::string pkg1("this-package-does-not-really-exists");
86  ver = apt.candidateVersion(pkg1);
87  assert(!ver.isValid());
88 
89  ver = apt.installedVersion(pkg1);
90  assert(!ver.isValid());
91 
92  ver = apt.anyVersion(pkg1);
93  assert(!ver.isValid());
94  }
95 
96  // Check the version validator
97  Test versionValidity()
98  {
99  Version ver = apt.candidateVersion("apt");
100  assert(apt.validate(ver) == ver);
101 
102  ver = Version("this-package-does-not-really-exists", "0.1");
103  assert(!apt.validate(ver).isValid());
104 
105  ver = Version("apt", "0.31415");
106  assert(!apt.validate(ver).isValid());
107  }
108 
109  // Check the raw record accessor
110  Test rawRecord()
111  {
112  string pkg("sp");
113  Version ver = apt.candidateVersion(pkg);
114  assert(ver.isValid());
115  assert(apt.validate(ver) == ver);
116 
117  string record = apt.rawRecord(ver);
118  assert(record.find("Package: sp") != string::npos);
119  assert(record.find("Section: text") != string::npos);
120 
121  record = apt.rawRecord(Version("sp", "0.31415"));
122  assert_eq(record, string());
123 
124  assert_eq(apt.rawRecord(pkg), apt.rawRecord(apt.anyVersion(pkg)));
125  }
126 
127  // Check the package state accessor
128  Test state()
129  {
130  PackageState s = apt.state("kdenetwork");
131  assert(s.isValid());
132  assert(s.isInstalled());
133 
134  s = apt.state("this-package-does-not-really-exists");
135  assert(!s.isValid());
136  }
137 
138  // Check the record iterator (accessing with *)
139  Test recordIteration()
140  {
141  size_t count = 0;
142  for (Apt::record_iterator i = apt.recordBegin();
143  i != apt.recordEnd(); ++i)
144  {
145  assert((*i).size() > 8);
146  assert_eq((*i).substr(0, 8), "Package:");
147  ++count;
148  }
149  assert(count > 200);
150  }
151 
152  // Check the record iterator (accessing with ->)
153  Test recordIteration2()
154  {
155  size_t count = 0;
156  for (Apt::record_iterator i = apt.recordBegin();
157  i != apt.recordEnd(); ++i)
158  {
159  assert(i->size() > 8);
160  assert_eq(i->substr(0, 8), "Package:");
161  ++count;
162  }
163  assert(count > 200);
164  }
165 
166  // Check that the iterators can be used with the algorithms
167  Test stlIteration()
168  {
169  vector<string> out;
170  std::copy(apt.begin(), apt.end(), back_inserter(out));
171  }
172 
173  // Check that the iterators can be used with the algorithms
174  Test stlRecordIteration()
175  {
176  vector<string> out;
177  std::copy(apt.recordBegin(), apt.recordEnd(), back_inserter(out));
178  }
179 
180  // Check that checkUpdates will keep a working Apt object
181  Test checkUpdates()
182  {
183  assert(apt.isValid("apt"));
184  apt.checkCacheUpdates();
185  assert(apt.isValid("apt"));
186  apt.invalidateTimestamp();
187  apt.checkCacheUpdates();
188  assert(apt.isValid("apt"));
189  }
190 
191 };
192 
193 // vim:set ts=4 sw=4: