Blame view

RIOT/tests/driver_ds1307/main.c 3.92 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  /*
   * Copyright (C) 2017 Freie Universitรคt Berlin
   *
   * 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.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test application for the DS1307 RTC driver
   *
   * @author      Martine Lenders <m.lenders@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "ds1307.h"
  #include "ds1307_params.h"
  #include "embUnit.h"
  #include "embUnit/embUnit.h"
  #include "timex.h"
  #include "xtimer.h"
  
  #define TEST_STRING "This is a test"
  
  static ds1307_t dev;
  
  static struct tm init = {  /* Wed Sep 22 15:10:42 2010 is the author date of
                              * RIOT's initial commit ;-) */
      .tm_sec = 42,
      .tm_min = 10,
      .tm_hour = 15,
      .tm_wday = 3,
      .tm_mday = 22,
      .tm_mon = 8,
      .tm_year = 110
  };
  
  static int _tm_cmp(struct tm *a, struct tm *b)
  {
      if (a->tm_year == b->tm_year) {
          if (a->tm_mon == b->tm_mon) {
              if (a->tm_mday == b->tm_mday) {
                  /* ignoring week day */
                  if (a->tm_hour == b->tm_hour) {
                      if (a->tm_min == b->tm_min) {
                          return a->tm_sec - b->tm_sec;
                      }
                      return a->tm_min - b->tm_min;
                  }
                  return a->tm_hour - b->tm_hour;
              }
              return a->tm_mday - b->tm_mday;
          }
          return a->tm_mon - b->tm_mon;
      }
      return a->tm_year - b->tm_year;
  }
  
  static void set_up(void)
  {
      ds1307_set_time(&dev, &init);
  }
  
  static void test_nvram(void)
  {
      struct tm time;
      uint8_t buf[DS1307_NVRAM_MAX_SIZE] = { 0 };
  
      ds1307_halt(&dev);
      TEST_ASSERT_NOT_NULL(dev.nvram.read);
      TEST_ASSERT_NOT_NULL(dev.nvram.write);
      TEST_ASSERT(dev.nvram.read(&dev.nvram, buf, 0, DS1307_NVRAM_MAX_SIZE + 1) < 0);
      TEST_ASSERT(dev.nvram.write(&dev.nvram, buf, 0, DS1307_NVRAM_MAX_SIZE + 1) < 0);
      TEST_ASSERT(dev.nvram.read(&dev.nvram, buf, 1, DS1307_NVRAM_MAX_SIZE) < 0);
      TEST_ASSERT(dev.nvram.write(&dev.nvram, buf, 1, DS1307_NVRAM_MAX_SIZE) < 0);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING),
                            dev.nvram.write(&dev.nvram, (uint8_t *)TEST_STRING, 0,
                                            sizeof(TEST_STRING)));
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING),
                            dev.nvram.read(&dev.nvram, buf, 0,
                                            sizeof(TEST_STRING)));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING, (char *)buf);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING) - 1,
                            dev.nvram.read(&dev.nvram, buf, 5,
                                            sizeof(TEST_STRING) - 1));
      TEST_ASSERT_EQUAL_STRING("is a test", (char *)buf);
      ds1307_get_time(&dev, &time);
      TEST_ASSERT_EQUAL_INT(0, _tm_cmp(&init, &time));
  }
  
  static void test_get_time(void)
  {
      for (int i = 0; i < 5; i++) {
          struct tm time;
  
          xtimer_sleep(1);
          ds1307_get_time(&dev, &time);
          TEST_ASSERT(_tm_cmp(&init, &time) <= 0);
      }
  }
  
  static void test_halt(void)
  {
      ds1307_halt(&dev);
      for (int i = 0; i < 3; i++) {
          struct tm time;
  
          xtimer_sleep(1);
          ds1307_get_time(&dev, &time);
          TEST_ASSERT_EQUAL_INT(0, _tm_cmp(&init, &time));
      }
  }
  
  static Test *tests_ds1307(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_nvram),
          new_TestFixture(test_get_time),
          /* set tested in set_up */
          new_TestFixture(test_halt),
      };
  
      EMB_UNIT_TESTCALLER(tests, set_up, NULL, fixtures);
      return (Test *)&tests;
  }
  
  int main(void)
  {
      int res;
  
      puts("DS1307 RTC test\n");
  
      /* initialize the device */
      res = ds1307_init(&dev, (&ds1307_params[0]));
      if (res != 0) {
          puts("error: unable to initialize RTC [I2C initialization error]");
          return 1;
      }
  
      TESTS_START();
      TESTS_RUN(tests_ds1307());
      TESTS_END();
  
      return 0;
  }