summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Burrows <dburrows@debian.org>2010-08-06 09:16:48 -0700
committerDaniel Burrows <dburrows@debian.org>2010-08-06 18:30:03 -0700
commitc1eb79a295d98c91c9432411fe621a496a5fa166 (patch)
tree5287e709c65deb82864f903b446eb9e103e830fe
parent671252e8731f316952687fe6c5c2b026de4073af (diff)
downloadaptitude-c1eb79a295d98c91c9432411fe621a496a5fa166.tar.gz
Write a routine to subtract timevals.
-rw-r--r--src/generic/util/util.cc18
-rw-r--r--src/generic/util/util.h5
-rw-r--r--tests/test_misc.cc96
3 files changed, 117 insertions, 2 deletions
diff --git a/src/generic/util/util.cc b/src/generic/util/util.cc
index 8debc414..065d5331 100644
--- a/src/generic/util/util.cc
+++ b/src/generic/util/util.cc
@@ -38,6 +38,8 @@
#include <cwidget/generic/util/eassert.h>
+#include <sys/time.h>
+
using namespace std;
std::string backslash_escape_nonalnum(const std::string &s)
@@ -338,6 +340,22 @@ namespace aptitude
{
namespace util
{
+ timeval subtract_timevals(const timeval &a, const timeval &b)
+ {
+ timeval rval;
+
+ rval.tv_sec = a.tv_sec - b.tv_sec;
+ if(a.tv_usec < b.tv_usec)
+ {
+ --rval.tv_sec;
+ rval.tv_usec = (a.tv_usec - b.tv_usec) + 1000000;
+ }
+ else
+ rval.tv_usec = a.tv_usec - b.tv_usec;
+
+ return rval;
+ }
+
bool recursive_remdir(const std::string &dirname)
{
struct stat stbuf;
diff --git a/src/generic/util/util.h b/src/generic/util/util.h
index b12e5de7..965e3f10 100644
--- a/src/generic/util/util.h
+++ b/src/generic/util/util.h
@@ -29,6 +29,7 @@
/** \file util.h
*/
+struct timeval;
struct tm;
/** \brief Backslash-escape anything in the given string that is not a
@@ -94,6 +95,10 @@ namespace aptitude
{
namespace util
{
+ /** \brief Return the difference between two timevals as a timeval. */
+ struct timeval subtract_timevals(const struct timeval &a,
+ const struct timeval &b);
+
/** Remove the given file/directory and all its children. Behaves
* like rm -fr.
*
diff --git a/tests/test_misc.cc b/tests/test_misc.cc
index a86ad244..ace85183 100644
--- a/tests/test_misc.cc
+++ b/tests/test_misc.cc
@@ -1,6 +1,6 @@
// Miscellaneous tests.
//
-// Copyright (C) 2005, 2007 Daniel Burrows
+// 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
@@ -17,9 +17,15 @@
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
+// Local includes:
+#include <generic/util/util.h>
+
+// System includes:
#include <cppunit/extensions/HelperMacros.h>
-#include <generic/util/util.h>
+#include <sys/time.h>
+
+using aptitude::util::subtract_timevals;
class MiscTest : public CppUnit::TestFixture
{
@@ -27,6 +33,11 @@ class MiscTest : public CppUnit::TestFixture
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:
@@ -94,6 +105,87 @@ private:
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<time_t>(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<time_t>(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<time_t>(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<time_t>(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<time_t>(999999), c.tv_usec);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTest);