summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2011-08-29 09:55:22 -0700
committerJosh Blum <josh@joshknows.com>2011-08-29 09:55:22 -0700
commit465431b1a3674710e6accfc8c82375713a8a5efc (patch)
treec262d2e64e5476b4041f1fe1f397efb6fbd9ede0
parent997fff3bb81513408e618aa411a8973f80d32521 (diff)
downloaduhd-465431b1a3674710e6accfc8c82375713a8a5efc.tar.gz
uhd-465431b1a3674710e6accfc8c82375713a8a5efc.tar.bz2
uhd-465431b1a3674710e6accfc8c82375713a8a5efc.zip
uhd: fix for dealing with negative wrapping in time_spec
-rw-r--r--host/lib/types/time_spec.cpp35
-rw-r--r--host/tests/time_spec_test.cpp14
2 files changed, 29 insertions, 20 deletions
diff --git a/host/lib/types/time_spec.cpp b/host/lib/types/time_spec.cpp
index a785332c2..0224fb6c2 100644
--- a/host/lib/types/time_spec.cpp
+++ b/host/lib/types/time_spec.cpp
@@ -83,25 +83,26 @@ time_spec_t time_spec_t::get_system_time(void){
/***********************************************************************
* Time spec constructors
**********************************************************************/
-time_spec_t::time_spec_t(double secs):
- _full_secs(0),
- _frac_secs(secs)
-{
- /* NOP */
+#define time_spec_init(full, frac) { \
+ _full_secs = full + time_t(frac); \
+ _frac_secs = frac - time_t(frac); \
+ if (_frac_secs < 0) {\
+ _full_secs -= 1; \
+ _frac_secs += 1; \
+ } \
}
-time_spec_t::time_spec_t(time_t full_secs, double frac_secs):
- _full_secs(full_secs),
- _frac_secs(frac_secs)
-{
- /* NOP */
+time_spec_t::time_spec_t(double secs){
+ time_spec_init(0, secs);
}
-time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate):
- _full_secs(full_secs),
- _frac_secs(tick_count/tick_rate)
-{
- /* NOP */
+time_spec_t::time_spec_t(time_t full_secs, double frac_secs){
+ time_spec_init(full_secs, frac_secs);
+}
+
+time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){
+ const double frac_secs = tick_count/tick_rate;
+ time_spec_init(full_secs, frac_secs);
}
/***********************************************************************
@@ -116,11 +117,11 @@ double time_spec_t::get_real_secs(void) const{
}
time_t time_spec_t::get_full_secs(void) const{
- return this->_full_secs + time_t(this->_frac_secs);
+ return this->_full_secs;
}
double time_spec_t::get_frac_secs(void) const{
- return this->_frac_secs - time_t(this->_frac_secs);
+ return this->_frac_secs;
}
/***********************************************************************
diff --git a/host/tests/time_spec_test.cpp b/host/tests/time_spec_test.cpp
index e57bbced1..97e0dec21 100644
--- a/host/tests/time_spec_test.cpp
+++ b/host/tests/time_spec_test.cpp
@@ -56,9 +56,9 @@ BOOST_AUTO_TEST_CASE(test_time_spec_parts){
BOOST_CHECK_CLOSE(uhd::time_spec_t(1.1).get_frac_secs(), 0.1, 0.001);
BOOST_CHECK_EQUAL(uhd::time_spec_t(1.1).get_tick_count(100), 10);
- BOOST_CHECK_EQUAL(uhd::time_spec_t(-1.1).get_full_secs(), -1);
- BOOST_CHECK_CLOSE(uhd::time_spec_t(-1.1).get_frac_secs(), -0.1, 0.001);
- BOOST_CHECK_EQUAL(uhd::time_spec_t(-1.1).get_tick_count(100), -10);
+ BOOST_CHECK_EQUAL(uhd::time_spec_t(-1.1).get_full_secs(), -2);
+ BOOST_CHECK_CLOSE(uhd::time_spec_t(-1.1).get_frac_secs(), 0.9, 0.001);
+ BOOST_CHECK_EQUAL(uhd::time_spec_t(-1.1).get_tick_count(100), 90);
}
BOOST_AUTO_TEST_CASE(test_time_spec_get_system_time){
@@ -78,3 +78,11 @@ BOOST_AUTO_TEST_CASE(test_time_spec_get_system_time){
BOOST_CHECK(diff.get_real_secs() > 0); //assert positive
BOOST_CHECK(diff.get_real_secs() < 1.0); //assert under 1s
}
+
+BOOST_AUTO_TEST_CASE(test_time_spec_neg_values){
+ uhd::time_spec_t ts1(0.3);
+ uhd::time_spec_t ts2(1, -0.9);
+ std::cout << "ts1 " << ts1.get_real_secs() << std::endl;
+ std::cout << "ts2 " << ts2.get_real_secs() << std::endl;
+ BOOST_CHECK(ts1 > ts2);
+}