aboutsummaryrefslogtreecommitdiff
path: root/test/timer.cc
blob: 3b5b5ac9b97d8b9decd27185cfb7f79cd949f993 (plain) (blame)
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
#include <timer.h>
#include <unistd.h>
#include <cassert>
#include <cstdio>

int main() {
  timer::timer t;

  const auto kShowTime = [&t]() {
    std::fprintf(stderr, "usec=%f msec=%f sec=%f\n", t.as_usec(), t.as_msec(),
                 t.as_sec());
  };

  {
    puts("Sleep 100ms");
    const timer::scoped_timer kS{t};
    usleep(100 * 1000);
  }
  assert(100000 < t.as_usec() && t.as_usec() < 101000);
  assert(100 < t.as_msec() && t.as_msec() < 101);
  assert(0.100 < t.as_sec() && t.as_sec() < 0.101);
  kShowTime();

  {
    puts("Sleep 500ms");
    const timer::scoped_timer kS{t};
    usleep(500 * 1000);
  }
  assert(600000 < t.as_usec() && t.as_usec() < 601000);
  assert(600 < t.as_msec() && t.as_msec() < 601);
  assert(0.600 < t.as_sec() && t.as_sec() < 0.601);
  kShowTime();

  t.reset();
  {
    puts("Sleep 200ms");
    timer::scoped_timer s{t};
    usleep(200 * 1000);
  }
  assert(200000 < t.as_usec() && t.as_usec() < 201000);
  assert(200 < t.as_msec() && t.as_msec() < 201);
  assert(0.200 < t.as_sec() && t.as_sec() < 0.201);
  kShowTime();

  return 0;
}