toxcore/toxcore/mono_time_test.cc
iphydf a549807df7
refactor: Add mem module to allow tests to override allocators.
This will allow us to do more interesting things with memory allocation
within toxcore, and allow fuzzers to explore various allocation failure
paths.
2023-08-30 22:55:05 +00:00

71 lines
1.7 KiB
C++

#include "mono_time.h"
#include <gtest/gtest.h>
namespace {
TEST(MonoTime, UnixTimeIncreasesOverTime)
{
const Memory *mem = system_memory();
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ASSERT_NE(mono_time, nullptr);
mono_time_update(mono_time);
uint64_t const start = mono_time_get(mono_time);
while (start == mono_time_get(mono_time)) {
mono_time_update(mono_time);
}
uint64_t const end = mono_time_get(mono_time);
EXPECT_GT(end, start);
mono_time_free(mem, mono_time);
}
TEST(MonoTime, IsTimeout)
{
const Memory *mem = system_memory();
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ASSERT_NE(mono_time, nullptr);
uint64_t const start = mono_time_get(mono_time);
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 1));
while (start == mono_time_get(mono_time)) {
mono_time_update(mono_time);
}
EXPECT_TRUE(mono_time_is_timeout(mono_time, start, 1));
mono_time_free(mem, mono_time);
}
TEST(MonoTime, CustomTime)
{
const Memory *mem = system_memory();
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ASSERT_NE(mono_time, nullptr);
uint64_t test_time = current_time_monotonic(mono_time) + 42137;
mono_time_set_current_time_callback(
mono_time, [](void *user_data) { return *static_cast<uint64_t *>(user_data); }, &test_time);
mono_time_update(mono_time);
EXPECT_EQ(current_time_monotonic(mono_time), test_time);
uint64_t const start = mono_time_get(mono_time);
test_time += 7000;
mono_time_update(mono_time);
EXPECT_EQ(mono_time_get(mono_time) - start, 7);
EXPECT_EQ(current_time_monotonic(mono_time), test_time);
mono_time_free(mem, mono_time);
}
} // namespace