libept  0.5.25
apt/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
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(apt.validate(ver) == ver);
115 
116  string record = apt.rawRecord(ver);
117  assert(record.find("Package: sp") != string::npos);
118  assert(record.find("Section: text") != string::npos);
119 
120  record = apt.rawRecord(Version("sp", "0.31415"));
121  assert_eq(record, string());
122 
123  assert_eq(apt.rawRecord(pkg), apt.rawRecord(apt.anyVersion(pkg)));
124  }
125 
126  // Check the package state accessor
127  Test state()
128  {
129  PackageState s = apt.state("kdenetwork");
130  assert(s.isValid());
131  assert(s.isInstalled());
132 
133  s = apt.state("this-package-does-not-really-exists");
134  assert(!s.isValid());
135  }
136 
137  // Check the record iterator (accessing with *)
139  {
140  size_t count = 0;
141  for (Apt::record_iterator i = apt.recordBegin();
142  i != apt.recordEnd(); ++i)
143  {
144  assert((*i).size() > 8);
145  assert_eq((*i).substr(0, 8), "Package:");
146  ++count;
147  }
148  assert(count > 200);
149  }
150 
151  // Check the record iterator (accessing with ->)
153  {
154  size_t count = 0;
155  for (Apt::record_iterator i = apt.recordBegin();
156  i != apt.recordEnd(); ++i)
157  {
158  assert(i->size() > 8);
159  assert_eq(i->substr(0, 8), "Package:");
160  ++count;
161  }
162  assert(count > 200);
163  }
164 
165  // Check that the iterators can be used with the algorithms
167  {
168  vector<string> out;
169  std::copy(apt.begin(), apt.end(), back_inserter(out));
170  }
171 
172  // Check that the iterators can be used with the algorithms
174  {
175  vector<string> out;
176  std::copy(apt.recordBegin(), apt.recordEnd(), back_inserter(out));
177  }
178 
179  // Check that checkUpdates will keep a working Apt object
181  {
182  assert(apt.isValid("apt"));
183  apt.checkCacheUpdates();
184  assert(apt.isValid("apt"));
185  apt.invalidateTimestamp();
186  apt.checkCacheUpdates();
187  assert(apt.isValid("apt"));
188  }
189 
190 };
191 
192 // vim:set ts=4 sw=4:
Definition: test.h:10
Definition: apt/apt.test.h:28
Test state()
Definition: apt/apt.test.h:127
Test timestamp()
Definition: apt/apt.test.h:59
Test validity()
Definition: apt/apt.test.h:66
bool isValid() const
Return true if this package contains a valid value.
Definition: apt/version.h:77
Test versionValidity()
Definition: apt/apt.test.h:97
Test iterators()
Definition: apt/apt.test.h:32
High-level access to the Apt cache, as a data provider for the ept framework.
Definition: apt/apt.h:60
Test recordIteration()
Definition: apt/apt.test.h:138
Definition: apt/apt.h:67
High-level front-end to libapt-pkg, as a data provider for the ept framework.
Test stlIteration()
Definition: apt/apt.test.h:166
Apt apt
Definition: apt/apt.test.h:29
Test checkUpdates()
Definition: apt/apt.test.h:180
Test rawRecord()
Definition: apt/apt.test.h:110
Definition: core/apt.h:388
Test stlRecordIteration()
Definition: apt/apt.test.h:173
Definition: apt/apt.h:95
Test aptExists()
Definition: apt/apt.test.h:45
Test versions()
Definition: apt/apt.test.h:73
Lightweight Version class that represent a package with a version, with very cheap value copy operati...
Definition: apt/version.h:40
Test recordIteration2()
Definition: apt/apt.test.h:152