summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlexandros Frantzis <alexandros.frantzis@collabora.com>2017-11-16 18:20:51 +0200
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>2017-11-27 11:18:25 +0200
commit6c2752a863becf41a43c1b0af99e1b713698e0b1 (patch)
tree329a19a85c0923c86de5ffd9484b0f2173d8314b /tests
parente2a5f9e02d22f2271f6f0d6be10ba3d1b320dca2 (diff)
downloadweston-6c2752a863becf41a43c1b0af99e1b713698e0b1.tar.gz
weston-6c2752a863becf41a43c1b0af99e1b713698e0b1.tar.bz2
weston-6c2752a863becf41a43c1b0af99e1b713698e0b1.zip
shared: Add helpers to convert between various time units and timespec
Add helper functions to make it easy and less error-prone to convert between values in various time units (nsec, usec, msec) and struct timespec. These helpers are going to be used in the upcoming commits to transition the Weston codebase to struct timespec. Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com> Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Diffstat (limited to 'tests')
-rw-r--r--tests/timespec-test.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/timespec-test.c b/tests/timespec-test.c
index 4e83605d..f127bcee 100644
--- a/tests/timespec-test.c
+++ b/tests/timespec-test.c
@@ -60,6 +60,15 @@ ZUC_TEST(timespec_test, timespec_to_nsec)
ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
}
+ZUC_TEST(timespec_test, timespec_to_usec)
+{
+ struct timespec a;
+
+ a.tv_sec = 4;
+ a.tv_nsec = 4000;
+ ZUC_ASSERT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
+}
+
ZUC_TEST(timespec_test, timespec_to_msec)
{
struct timespec a;
@@ -165,6 +174,69 @@ ZUC_TEST(timespec_test, timespec_sub_to_msec)
ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
}
+ZUC_TEST(timespec_test, timespec_from_nsec)
+{
+ struct timespec a;
+
+ timespec_from_nsec(&a, 0);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_nsec(&a, NSEC_PER_SEC - 1);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
+
+ timespec_from_nsec(&a, NSEC_PER_SEC);
+ ZUC_ASSERT_EQ(1, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_nsec(&a, (5L * NSEC_PER_SEC) + 1);
+ ZUC_ASSERT_EQ(5, a.tv_sec);
+ ZUC_ASSERT_EQ(1, a.tv_nsec);
+}
+
+ZUC_TEST(timespec_test, timespec_from_usec)
+{
+ struct timespec a;
+
+ timespec_from_usec(&a, 0);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_usec(&a, 999999);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(999999 * 1000, a.tv_nsec);
+
+ timespec_from_usec(&a, 1000000);
+ ZUC_ASSERT_EQ(1, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_usec(&a, 5000001);
+ ZUC_ASSERT_EQ(5, a.tv_sec);
+ ZUC_ASSERT_EQ(1000, a.tv_nsec);
+}
+
+ZUC_TEST(timespec_test, timespec_from_msec)
+{
+ struct timespec a;
+
+ timespec_from_msec(&a, 0);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_msec(&a, 999);
+ ZUC_ASSERT_EQ(0, a.tv_sec);
+ ZUC_ASSERT_EQ(999 * 1000000, a.tv_nsec);
+
+ timespec_from_msec(&a, 1000);
+ ZUC_ASSERT_EQ(1, a.tv_sec);
+ ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+ timespec_from_msec(&a, 5001);
+ ZUC_ASSERT_EQ(5, a.tv_sec);
+ ZUC_ASSERT_EQ(1000000, a.tv_nsec);
+}
+
ZUC_TEST(timespec_test, timespec_is_zero)
{
struct timespec zero = { 0 };