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>
This commit is contained in:
Alexandros Frantzis
2017-11-16 18:20:51 +02:00
committed by Pekka Paalanen
parent e2a5f9e02d
commit 6c2752a863
2 changed files with 119 additions and 0 deletions
+72
View File
@@ -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 };