changeset 1541:71207822a7e0 zoom

Provide fromMicroseconds (and test it)
author Chris Cannam
date Thu, 27 Sep 2018 15:53:34 +0100
parents a53bf95f0bea
children 6415ac3becb6
files base/RealTime.h base/RealTimeSV.cpp base/test/TestOurRealTime.h
diffstat 3 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/base/RealTime.h	Thu Sep 27 15:19:39 2018 +0100
+++ b/base/RealTime.h	Thu Sep 27 15:53:34 2018 +0100
@@ -58,6 +58,7 @@
 
     static RealTime fromSeconds(double sec);
     static RealTime fromMilliseconds(int msec);
+    static RealTime fromMicroseconds(int usec);
     static RealTime fromTimeval(const struct timeval &);
     static RealTime fromXsdDuration(std::string xsdd);
 
--- a/base/RealTimeSV.cpp	Thu Sep 27 15:19:39 2018 +0100
+++ b/base/RealTimeSV.cpp	Thu Sep 27 15:53:34 2018 +0100
@@ -67,6 +67,12 @@
 }
 
 RealTime
+RealTime::fromMicroseconds(int usec)
+{
+    return RealTime(usec / 1000000, (usec % 1000000) * 1000);
+}
+
+RealTime
 RealTime::fromTimeval(const struct timeval &tv)
 {
     return RealTime(int(tv.tv_sec), int(tv.tv_usec * 1000));
--- a/base/test/TestOurRealTime.h	Thu Sep 27 15:19:39 2018 +0100
+++ b/base/test/TestOurRealTime.h	Thu Sep 27 15:53:34 2018 +0100
@@ -138,6 +138,20 @@
         QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0));
         QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2));
     }
+
+    void fromMicroseconds()
+    {
+        QCOMPARE(RealTime::fromMicroseconds(0), RealTime(0, 0));
+        QCOMPARE(RealTime::fromMicroseconds(500000), RealTime(0, ONE_BILLION/2));
+        QCOMPARE(RealTime::fromMicroseconds(1000000), RealTime(1, 0));
+        QCOMPARE(RealTime::fromMicroseconds(1500000), RealTime(1, ONE_BILLION/2));
+
+        QCOMPARE(RealTime::fromMicroseconds(-0), RealTime(0, 0));
+        QCOMPARE(RealTime::fromMicroseconds(-500000), RealTime(0, -ONE_BILLION/2));
+        QCOMPARE(RealTime::fromMicroseconds(-1000000), RealTime(-1, 0));
+        QCOMPARE(RealTime::fromMicroseconds(-1500000), RealTime(-1, -ONE_BILLION/2));
+        QCOMPARE(RealTime::fromMicroseconds(13500000), RealTime(13, ONE_BILLION/2));
+    }
     
     void fromTimeval()
     {