Blame view

RIOT/tests/unittests/tests-saul_reg/tests-saul_reg.c 4.75 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  /*
   * Copyright (C) 2016 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.
   */
  
  /**
   * @addtogroup  unittests
   * @{
   *
   * @file
   * @brief       Implementations of unit tests for the SAUL registry
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @}
   */
  
  #include <errno.h>
  #include <stdint.h>
  
  #include "embUnit/embUnit.h"
  
  #include "saul_reg.h"
  #include "tests-saul_reg.h"
  
  static const saul_driver_t s0_dri = { NULL, NULL, SAUL_ACT_SERVO };
  static const saul_driver_t s1_dri = { NULL, NULL, SAUL_SENSE_TEMP };
  static const saul_driver_t s2_dri = { NULL, NULL, SAUL_SENSE_LIGHT };
  static const saul_driver_t s3_dri = { NULL, NULL, SAUL_ACT_LED_RGB };
  
  static saul_reg_t s0 = { NULL, NULL, "S0", &s0_dri };
  static saul_reg_t s1 = { NULL, NULL, "S1", &s1_dri };
  static saul_reg_t s2 = { NULL, NULL, "S2", &s2_dri };
  static saul_reg_t s3 = { NULL, NULL, "S3", &s3_dri };
  
  
  static int count(void)
  {
      int i = 0;
      saul_reg_t *cur = saul_reg;
  
      while (cur) {
          ++i;
          cur = cur->next;
      }
  
      return i;
  }
  
  static saul_reg_t *last(void)
  {
      saul_reg_t *cur = saul_reg;
  
      if (cur == NULL) {
          return NULL;
      }
  
      while (cur->next != NULL) {
          cur = cur->next;
      }
      return cur;
  }
  
  static void test_reg_initial_size(void)
  {
      TEST_ASSERT_NULL(saul_reg);
      TEST_ASSERT_EQUAL_INT(count(), 0);
  }
  
  static void test_reg_add(void)
  {
      int res;
  
      res = saul_reg_add(&s0);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(count(), 1);
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S0", last()->name);
  
      res = saul_reg_add(&s1);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(count(), 2);
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S1", last()->name);
  
      res = saul_reg_add(&s2);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(count(), 3);
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S2", last()->name);
  
      res = saul_reg_add(&s3);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(count(), 4);
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S3", last()->name);
  
      res = saul_reg_add(NULL);
      TEST_ASSERT_EQUAL_INT(-ENODEV, res);
      TEST_ASSERT_EQUAL_INT(count(), 4);
  }
  
  static void test_reg_find_nth(void)
  {
      saul_reg_t *dev = saul_reg_find_nth(0);
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_STRING("S0", dev->name);
  
      dev = saul_reg_find_nth(2);
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_STRING("S2", dev->name);
  
      dev = saul_reg_find_nth(17);
      TEST_ASSERT_NULL(dev);
  }
  
  static void test_reg_find_type(void)
  {
      saul_reg_t *dev = saul_reg_find_type(SAUL_SENSE_TEMP);
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_STRING("S1", dev->name);
  
      dev = saul_reg_find_type(SAUL_ACT_LED_RGB);
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_STRING("S3", dev->name);
  
      dev = saul_reg_find_type(SAUL_ACT_MOTOR);
      TEST_ASSERT_NULL(dev);
  }
  
  static void test_reg_find_name(void)
  {
      saul_reg_t *dev = saul_reg_find_name("S1");
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_INT(SAUL_SENSE_TEMP, dev->driver->type);
  
      dev = saul_reg_find_name("S0");
      TEST_ASSERT_NOT_NULL(dev);
      TEST_ASSERT_EQUAL_INT(SAUL_ACT_SERVO, dev->driver->type);
  
      dev = saul_reg_find_name("TUUT");
      TEST_ASSERT_NULL(dev);
  }
  
  static void test_reg_rm(void)
  {
      int res;
  
      TEST_ASSERT_EQUAL_INT(4, count());
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S3", last()->name);
  
      res = saul_reg_rm(&s3);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(3, count());
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S2", last()->name);
  
      res = saul_reg_rm(&s1);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(2, count());
      TEST_ASSERT_EQUAL_STRING("S0", saul_reg->name);
      TEST_ASSERT_EQUAL_STRING("S2", last()->name);
  
      res = saul_reg_rm(&s1);
      TEST_ASSERT_EQUAL_INT(-ENODEV, res);
  
      res = saul_reg_rm(NULL);
      TEST_ASSERT_EQUAL_INT(-ENODEV, res);
  
      TEST_ASSERT_EQUAL_INT(2, count());
  }
  
  Test *tests_saul_reg_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_reg_initial_size),
          new_TestFixture(test_reg_add),
          new_TestFixture(test_reg_find_nth),
          new_TestFixture(test_reg_find_type),
          new_TestFixture(test_reg_find_name),
          new_TestFixture(test_reg_rm)
      };
  
      EMB_UNIT_TESTCALLER(pkt_tests, NULL, NULL, fixtures);
  
      return (Test *)&pkt_tests;
  }
  
  void tests_saul_reg(void)
  {
      TESTS_RUN(tests_saul_reg_tests());
  }