소스 검색

2008-09-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Added a constuctor that receives struct timeval.
	Added operator<.
	* src/TimeA2.cc
	* src/TimeA2.h
	* test/TimeTest.cc
Tatsuhiro Tsujikawa 17 년 전
부모
커밋
5fb94a3af0
4개의 변경된 파일42개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 0
      ChangeLog
  2. 10 1
      src/TimeA2.cc
  3. 4 1
      src/TimeA2.h
  4. 20 0
      test/TimeTest.cc

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2008-09-07  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Added a constuctor that receives struct timeval.
+	Added operator<.
+	* src/TimeA2.cc
+	* src/TimeA2.h
+	* test/TimeTest.cc
+
 2008-09-07  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Parse `expires' of Set-Cookie using Time::parseHTTPDate()

+ 10 - 1
src/TimeA2.cc

@@ -52,6 +52,10 @@ Time::Time(time_t sec) {
   setTimeInSec(sec);
 }
 
+Time::Time(const struct timeval& tv) {
+  this->tv = tv;
+}
+
 Time::~Time() {}
 
 Time& Time::operator=(const Time& time)
@@ -62,6 +66,11 @@ Time& Time::operator=(const Time& time)
   return *this;
 }
 
+bool Time::operator<(const Time& time) const
+{
+  return Util::difftv(time.tv, tv) > 0;
+}
+
 void Time::reset() {
   gettimeofday(&tv, 0);
 }
@@ -191,5 +200,5 @@ Time Time::parseHTTPDate(const std::string& datetime)
   }
   return Time(-1);
 }
-	
+
 } // namespace aria2

+ 4 - 1
src/TimeA2.h

@@ -53,10 +53,13 @@ public:
   Time();
   Time(const Time& time);
   Time(time_t sec);
+  Time(const struct timeval& tv);
+
+  ~Time();
 
   Time& operator=(const Time& time);
 
-  ~Time();
+  bool operator<(const Time& time) const;
 
   // Makes this object's time value up to date.
   void reset();

+ 20 - 0
test/TimeTest.cc

@@ -13,6 +13,7 @@ class TimeTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testParseRFC850);
   CPPUNIT_TEST(testParseRFC850Ext);
   CPPUNIT_TEST(testParseHTTPDate);
+  CPPUNIT_TEST(testOperatorLess);
   CPPUNIT_TEST_SUITE_END();
 public:
   void setUp() {}
@@ -23,6 +24,7 @@ public:
   void testParseRFC850();
   void testParseRFC850Ext();
   void testParseHTTPDate();
+  void testOperatorLess();
 };
 
 
@@ -62,4 +64,22 @@ void TimeTest::testParseHTTPDate()
 		       ("Sat, 2008-09-06 15:26:33 GMT").getTime());
 }
 
+void TimeTest::testOperatorLess()
+{
+  CPPUNIT_ASSERT(Time(1) < Time(2));
+  CPPUNIT_ASSERT(!(Time(1) < Time(1)));
+  CPPUNIT_ASSERT(!(Time(2) < Time(1)));
+
+  struct timeval tv1;
+  tv1.tv_sec = 0;
+  tv1.tv_usec = 1;
+  struct timeval tv2;
+  tv2.tv_sec = 1;
+  tv2.tv_usec = 0;
+  CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
+
+  tv2.tv_sec = 0;
+  CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
+}
+
 } // namespace aria2