summaryrefslogtreecommitdiff
path: root/toxcore/mono_time_test.cc
blob: 20a6826e07eb18640da6847be274e8b79708c898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "mono_time.h"

#include <gtest/gtest.h>

namespace {

TEST(MonoTime, UnixTimeIncreasesOverTime) {
  Mono_Time *mono_time = mono_time_new();

  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(mono_time);
}

TEST(MonoTime, IsTimeout) {
  Mono_Time *mono_time = mono_time_new();

  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(mono_time);
}

uint64_t test_current_time_callback(Mono_Time *mono_time, void *user_data) {
  return *static_cast<uint64_t *>(user_data);
}

TEST(MonoTime, CustomTime) {
  Mono_Time *mono_time = mono_time_new();

  uint64_t test_time = current_time_monotonic(mono_time) + 42137;

  mono_time_set_current_time_callback(mono_time, test_current_time_callback, &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(mono_time);
}

}  // namespace