Blame view

RIOT/tests/unittests/tests-timex/tests-timex.c 2.54 KB
a752c7ab   elopes   add first test an...
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  /*
   * Copyright (C) 2014 Philipp Rosenkranz, Daniel Jentsch
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  #include "tests-timex.h"
  
  #include "timex.h"
  
  static void test_timex_set(void)
  {
      timex_t time;
      time = timex_set(1, 0);
      TEST_ASSERT_EQUAL_INT(1, time.seconds);
      TEST_ASSERT_EQUAL_INT(0, time.microseconds);
  }
  
  static void test_timex_add(void)
  {
      timex_t time;
      time = timex_add(timex_set(100, 100), timex_set(40, 10));
      TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(140, 110)));
      time = timex_add(timex_set(100, 700000), timex_set(40, 800000));
      TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(141, 500000)));
  }
  
  static void test_timex_sub(void)
  {
      timex_t time;
      time = timex_sub(timex_set(100, 100), timex_set(40, 10));
      TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(60, 90)));
      time = timex_sub(timex_set(100, 100), timex_set(40, 200));
      TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(59, 999900)));
  }
  
  static void test_timex_from_uint64(void)
  {
      timex_t time;
      time = timex_from_uint64(1001000);
      TEST_ASSERT(time.seconds == 1);
      TEST_ASSERT(time.microseconds == 1000);
  }
  
  static void test_timex_to_str(void)
  {
      timex_t t = { 0, 0 };
      char t_str[TIMEX_MAX_STR_LEN];
  
      TEST_ASSERT_EQUAL_STRING("0.000000 s", timex_to_str(t, t_str));
      t.seconds = 1;
      TEST_ASSERT_EQUAL_STRING("1.000000 s", timex_to_str(t, t_str));
      t.seconds = 9;
      t.microseconds = 2640288149;
      TEST_ASSERT_EQUAL_STRING("2649.288149 s", timex_to_str(t, t_str));
      t.seconds = 1689940699;
      t.microseconds = 4361;
      TEST_ASSERT_EQUAL_STRING("1689940699.004361 s", timex_to_str(t, t_str));
      t.seconds = 100;
      t.microseconds = 101010;
      TEST_ASSERT_EQUAL_STRING("100.101010 s", timex_to_str(t, t_str));
      t.seconds = UINT32_MAX;
      t.microseconds = 999999;        /* should be .999999 */
      TEST_ASSERT_EQUAL_STRING("4294967295.999999 s", timex_to_str(t, t_str));
  }
  
  Test *tests_timex_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_timex_set),
          new_TestFixture(test_timex_add),
          new_TestFixture(test_timex_sub),
          new_TestFixture(test_timex_from_uint64),
          new_TestFixture(test_timex_to_str),
      };
  
      EMB_UNIT_TESTCALLER(timex_tests, NULL, NULL, fixtures);
  
      return (Test *)&timex_tests;
  }
  
  void tests_timex(void)
  {
      TESTS_RUN(tests_timex_tests());
  }