2022-08-06 02:24:05 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2022-08-06 02:24:05 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/struct/timespec.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2022-09-19 23:13:37 +00:00
|
|
|
#include "libc/limits.h"
|
2022-08-11 19:26:30 +00:00
|
|
|
#include "libc/stdio/rand.h"
|
2022-08-06 02:24:05 +00:00
|
|
|
#include "libc/testlib/testlib.h"
|
2022-10-08 04:29:40 +00:00
|
|
|
#include "third_party/nsync/time.h"
|
2022-08-06 02:24:05 +00:00
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_sub, test) {
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){-1},
|
|
|
|
timespec_sub((struct timespec){1}, (struct timespec){2})));
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){1},
|
|
|
|
timespec_sub((struct timespec){2}, (struct timespec){1})));
|
|
|
|
EXPECT_TRUE(!timespec_cmp(
|
2022-08-06 02:24:05 +00:00
|
|
|
(struct timespec){1, 1},
|
2022-11-06 02:49:41 +00:00
|
|
|
timespec_sub((struct timespec){2, 2}, (struct timespec){1, 1})));
|
|
|
|
EXPECT_TRUE(!timespec_cmp(
|
2022-08-06 02:24:05 +00:00
|
|
|
(struct timespec){0, 999999999},
|
2022-11-06 02:49:41 +00:00
|
|
|
timespec_sub((struct timespec){2, 1}, (struct timespec){1, 2})));
|
2022-08-06 02:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_frommillis, test) {
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){0, 1000000}, timespec_frommillis(1)));
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){0, 2000000}, timespec_frommillis(2)));
|
|
|
|
EXPECT_TRUE(!timespec_cmp((struct timespec){1}, timespec_frommillis(1000)));
|
2022-08-06 02:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_frommicros, test) {
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){0, 1000}, timespec_frommicros(1)));
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){0, 2000}, timespec_frommicros(2)));
|
2022-08-06 02:24:05 +00:00
|
|
|
EXPECT_TRUE(
|
2022-11-06 02:49:41 +00:00
|
|
|
!timespec_cmp((struct timespec){1}, timespec_frommicros(1000000)));
|
|
|
|
EXPECT_TRUE(!timespec_cmp((struct timespec){2, 123000},
|
|
|
|
timespec_frommicros(2000123)));
|
2022-08-11 07:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_tomillis, test) {
|
|
|
|
EXPECT_EQ(0, timespec_tomillis((struct timespec){0, 0}));
|
|
|
|
EXPECT_EQ(1, timespec_tomillis((struct timespec){0, 1}));
|
|
|
|
EXPECT_EQ(1, timespec_tomillis((struct timespec){0, 999999}));
|
|
|
|
EXPECT_EQ(1, timespec_tomillis((struct timespec){0, 1000000}));
|
|
|
|
EXPECT_EQ(1000, timespec_tomillis((struct timespec){0, 999999999}));
|
|
|
|
EXPECT_EQ(2123, timespec_tomillis((struct timespec){2, 123000000}));
|
|
|
|
EXPECT_EQ(INT64_MAX, timespec_tomillis((struct timespec){INT64_MAX, 0}));
|
|
|
|
EXPECT_EQ(INT64_MIN, timespec_tomillis((struct timespec){INT64_MIN, 0}));
|
|
|
|
EXPECT_EQ(INT64_MAX, timespec_tomillis(
|
2022-10-08 04:29:40 +00:00
|
|
|
(struct timespec){0x7fffffffffffffff, 999999999}));
|
2022-08-11 07:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_tomicros, test) {
|
|
|
|
EXPECT_EQ(0, timespec_tomicros((struct timespec){0, 0}));
|
|
|
|
EXPECT_EQ(1, timespec_tomicros((struct timespec){0, 1}));
|
|
|
|
EXPECT_EQ(2000123, timespec_tomicros((struct timespec){2, 123000}));
|
|
|
|
EXPECT_EQ(INT64_MAX, timespec_tomicros((struct timespec){INT64_MAX, 0}));
|
|
|
|
EXPECT_EQ(INT64_MIN, timespec_tomicros((struct timespec){INT64_MIN, 0}));
|
2022-08-11 07:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_tonanos, test) {
|
|
|
|
EXPECT_EQ(2000123000, timespec_tonanos((struct timespec){2, 123000}));
|
|
|
|
EXPECT_EQ(INT64_MAX, timespec_tonanos((struct timespec){INT64_MAX, 0}));
|
|
|
|
EXPECT_EQ(INT64_MIN, timespec_tonanos((struct timespec){INT64_MIN, 0}));
|
2022-08-06 02:24:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:00:29 +00:00
|
|
|
TEST(timeval_toseconds, test) {
|
|
|
|
ASSERT_EQ(0, timeval_toseconds((struct timeval){0, 0}));
|
|
|
|
ASSERT_EQ(1, timeval_toseconds((struct timeval){0, 1}));
|
|
|
|
ASSERT_EQ(1, timeval_toseconds((struct timeval){0, 2}));
|
|
|
|
ASSERT_EQ(1, timeval_toseconds((struct timeval){1, 0}));
|
|
|
|
ASSERT_EQ(2, timeval_toseconds((struct timeval){1, 1}));
|
|
|
|
ASSERT_EQ(INT64_MAX, timeval_toseconds(timeval_max));
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:24:05 +00:00
|
|
|
static long mod(long x, long y) {
|
|
|
|
if (y == -1) return 0;
|
|
|
|
return x - y * (x / y - (x % y && (x ^ y) < 0));
|
|
|
|
}
|
|
|
|
|
2022-11-06 02:49:41 +00:00
|
|
|
TEST(timespec_sub, math) {
|
2022-08-06 02:24:05 +00:00
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
struct timespec x = {mod(lemur64(), 10), mod(lemur64(), 10)};
|
|
|
|
struct timespec y = {mod(lemur64(), 10), mod(lemur64(), 10)};
|
2022-11-06 02:49:41 +00:00
|
|
|
EXPECT_TRUE(!timespec_cmp(x, timespec_add(timespec_sub(x, y), y)));
|
2022-08-06 02:24:05 +00:00
|
|
|
}
|
|
|
|
}
|