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
|
// Miscellaneous tests.
//
// Copyright (C) 2005, 2007, 2010 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., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301, USA.
// Local includes:
#include <generic/util/util.h>
// System includes:
#include <cppunit/extensions/HelperMacros.h>
#include <sys/time.h>
using aptitude::util::subtract_timevals;
class MiscTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(MiscTest);
CPPUNIT_TEST(testStripWS);
CPPUNIT_TEST(testOrderlessLt);
CPPUNIT_TEST(testSubtractTimevalGreaterInBothComponents);
CPPUNIT_TEST(testSubtractTimevalGreaterInSecondsLessInMilliseconds);
CPPUNIT_TEST(testSubtractTimevalEqual);
CPPUNIT_TEST(testSubtractTimevalLessInSecondsGreaterInMilliseconds);
CPPUNIT_TEST(testSubtractTimevalLessInBothComponents);
CPPUNIT_TEST_SUITE_END();
private:
void assertStripWS(const std::string &s,
const std::string &expected)
{
std::string s2 = s;
stripws(s2);
CPPUNIT_ASSERT_EQUAL(expected, s2);
}
void testStripWS()
{
assertStripWS(" abc", "abc");
assertStripWS("abc ", "abc");
assertStripWS(" abc ", "abc");
assertStripWS(" ", "");
// double-check that there are no weird corner cases involving
// singly letters.
assertStripWS(" a", "a");
assertStripWS("a ", "a");
assertStripWS(" a ", "a");
}
void testOrderlessLt()
{
orderless_lt<int> cmp;
std::pair<int, int> a(1, 2);
std::pair<int, int> b(2, 1);
std::pair<int, int> c(4, 1);
std::pair<int, int> d(1, 4);
std::pair<int, int> e(4, 6);
CPPUNIT_ASSERT(!cmp(a, a));
CPPUNIT_ASSERT(!cmp(b, b));
CPPUNIT_ASSERT(!cmp(c, c));
CPPUNIT_ASSERT(!cmp(d, d));
CPPUNIT_ASSERT(!cmp(e, e));
CPPUNIT_ASSERT(!cmp(a, b));
CPPUNIT_ASSERT( cmp(b, c));
CPPUNIT_ASSERT(!cmp(c, d));
CPPUNIT_ASSERT( cmp(d, e));
CPPUNIT_ASSERT(!cmp(e, a));
CPPUNIT_ASSERT( cmp(a, c));
CPPUNIT_ASSERT( cmp(b, d));
CPPUNIT_ASSERT( cmp(c, e));
CPPUNIT_ASSERT(!cmp(d, a));
CPPUNIT_ASSERT(!cmp(e, b));
CPPUNIT_ASSERT( cmp(a, d));
CPPUNIT_ASSERT( cmp(b, e));
CPPUNIT_ASSERT(!cmp(c, a));
CPPUNIT_ASSERT(!cmp(d, b));
CPPUNIT_ASSERT(!cmp(e, c));
CPPUNIT_ASSERT( cmp(a, e));
CPPUNIT_ASSERT(!cmp(b, a));
CPPUNIT_ASSERT(!cmp(c, b));
CPPUNIT_ASSERT(!cmp(d, c));
CPPUNIT_ASSERT(!cmp(e, d));
}
void testSubtractTimevalGreaterInBothComponents()
{
struct timeval a, b;
a.tv_sec = 10;
a.tv_usec = 1000;
b.tv_sec = 5;
b.tv_usec = 50;
struct timeval c = subtract_timevals(a, b);
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(5), c.tv_sec);
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(c.tv_usec)>(950), c.tv_usec);
}
void testSubtractTimevalGreaterInSecondsLessInMilliseconds()
{
struct timeval a, b;
a.tv_sec = 10;
a.tv_usec = 75;
b.tv_sec = 5;
b.tv_usec = 100;
struct timeval c = subtract_timevals(a, b);
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(4), c.tv_sec);
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(c.tv_usec)>(999975), c.tv_usec);
}
void testSubtractTimevalEqual()
{
struct timeval a, b;
a.tv_sec = 50;
a.tv_usec = 230;
b.tv_sec = 50;
b.tv_usec = 230;
struct timeval c = subtract_timevals(a, b);
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(0), c.tv_sec);
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(c.tv_usec)>(0), c.tv_usec);
}
void testSubtractTimevalLessInSecondsGreaterInMilliseconds()
{
struct timeval a, b;
a.tv_sec = 70;
a.tv_usec = 500;
b.tv_sec = 80;
b.tv_usec = 10;
struct timeval c = subtract_timevals(a, b);
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(-10), c.tv_sec);
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(c.tv_usec)>(490), c.tv_usec);
}
void testSubtractTimevalLessInBothComponents()
{
struct timeval a, b;
a.tv_sec = 100;
a.tv_usec = 130;
b.tv_sec = 123;
b.tv_usec = 131;
struct timeval c = subtract_timevals(a, b);
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(-24), c.tv_sec);
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(c.tv_usec)>(999999), c.tv_usec);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTest);
|