summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Blum2013-06-11 17:55:31 -0700
committerJosh Blum2013-06-11 17:55:31 -0700
commitc73eb9bf1954135372b1cc8e7b21d7cc0cdfd489 (patch)
treec68ba6daf09021df58c6ae11f18bb3dc36e88eae
parent8ff889a8160bdf61739066c74c469ca2bcad093e (diff)
downloadsandhi-c73eb9bf1954135372b1cc8e7b21d7cc0cdfd489.tar.gz
sandhi-c73eb9bf1954135372b1cc8e7b21d7cc0cdfd489.tar.bz2
sandhi-c73eb9bf1954135372b1cc8e7b21d7cc0cdfd489.zip
gras: reverted chrono changes w/ 32-bit math fix
The seconds * ticks per second math was 32 bit, because UL follows the system width, we needed long long. * for loop for heartier unit testing * use get_tps rather than duplicate const number
-rw-r--r--include/gras/detail/chrono.hpp11
-rw-r--r--tests/chrono_time_test.cpp25
2 files changed, 17 insertions, 19 deletions
diff --git a/include/gras/detail/chrono.hpp b/include/gras/detail/chrono.hpp
index 9fe317a..6225e55 100644
--- a/include/gras/detail/chrono.hpp
+++ b/include/gras/detail/chrono.hpp
@@ -29,7 +29,6 @@ namespace gras
#else
#include <ctime>
-#include <sys/time.h>
namespace gras
{
@@ -37,14 +36,8 @@ namespace gras
GRAS_FORCE_INLINE time_ticks_t time_now(void)
{
struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- return ts.tv_sec*1000000000UL + ts.tv_nsec;
-
- struct timeval tv;
- if (gettimeofday(&tv, NULL) == 0)
- return tv.tv_sec*1000000000UL + tv.tv_usec*1000UL;
-
- return 0;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec*time_tps() + ts.tv_nsec;
}
GRAS_FORCE_INLINE time_ticks_t time_tps(void)
diff --git a/tests/chrono_time_test.cpp b/tests/chrono_time_test.cpp
index 80c80ab..d7f76ad 100644
--- a/tests/chrono_time_test.cpp
+++ b/tests/chrono_time_test.cpp
@@ -8,16 +8,21 @@
BOOST_AUTO_TEST_CASE(test_chrono_time)
{
- gras::time_ticks_t t0 = gras::time_now();
- boost::this_thread::sleep(boost::posix_time::seconds(1));
- gras::time_ticks_t t1 = gras::time_now();
+ for (size_t trial_i = 0; trial_i < 100; trial_i++)
+ {
+ const double sleep_secs = 0.01 + trial_i*0.0001;
+ gras::time_ticks_t t0 = gras::time_now();
+ boost::this_thread::sleep(boost::posix_time::milliseconds(long(sleep_secs*1000)));
+ gras::time_ticks_t t1 = gras::time_now();
- std::cout << "t0 " << t0 << std::endl;
- std::cout << "t1 " << t1 << std::endl;
- BOOST_CHECK(t1 > t0);
+ std::cout << "t0 " << t0 << std::endl;
+ std::cout << "t1 " << t1 << std::endl;
+ BOOST_CHECK(t1 > t0);
- const double delta_time = double(t1-t0)/gras::time_tps();
- std::cout << "delta_time " << delta_time << std::endl;
- //this check allows +/- 0.5 seconds -- so it should always pass
- BOOST_CHECK(delta_time < 1.5 and delta_time > 0.5);
+ const double delta_time = double(t1-t0)/gras::time_tps();
+ std::cout << "delta_time " << delta_time << std::endl;
+ const double low_margin = sleep_secs - sleep_secs/2;
+ const double high_margin = sleep_secs + sleep_secs/2;
+ BOOST_CHECK(delta_time < high_margin and delta_time > low_margin);
+ }
}