Blame view

RIOT/tests/unittests/tests-fmt/tests-fmt.c 7.49 KB
fb11e647   vrobic   reseau statique a...
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  /*
   * Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
   *
   * 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.
   */
  
  /**
   * @{
   *
   * @file
   */
  #include <errno.h>
  #include <stdint.h>
  
  #include "embUnit/embUnit.h"
  
  #include "fmt.h"
  #include "tests-fmt.h"
  
  static void test_fmt_byte_hex(void)
  {
      char out[3] = "--";
  
      TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 0));
      TEST_ASSERT_EQUAL_STRING("00", (char *) out);
  
      TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 128));
      TEST_ASSERT_EQUAL_STRING("80", (char *) out);
  
      TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 255));
      TEST_ASSERT_EQUAL_STRING("FF", (char *) out);
  }
  
  static void test_fmt_bytes_hex_reverse(void)
  {
      char out[10];
      uint8_t val[4] = { 9, 8, 7, 6 };
      uint8_t bytes = 0;
  
      bytes = fmt_bytes_hex_reverse(out, val, 1);
      out[bytes] = '\0';
      TEST_ASSERT_EQUAL_INT(2, bytes);
      TEST_ASSERT_EQUAL_STRING("09", (char *) out);
  
      bytes = fmt_bytes_hex_reverse(out, val, 2);
      out[bytes] = '\0';
      TEST_ASSERT_EQUAL_INT(4, bytes);
      TEST_ASSERT_EQUAL_STRING("0809", (char *) out);
  
      bytes = fmt_bytes_hex_reverse(out, val, 3);
      out[bytes] = '\0';
      TEST_ASSERT_EQUAL_INT(6, bytes);
      TEST_ASSERT_EQUAL_STRING("070809", (char *) out);
  
      bytes = fmt_bytes_hex_reverse(out, val, 4);
      out[bytes] = '\0';
      TEST_ASSERT_EQUAL_INT(8, bytes);
      TEST_ASSERT_EQUAL_STRING("06070809", (char *) out);
  }
  
  static void test_fmt_u32_hex(void)
  {
      char out[9] = "--------";
      uint32_t val = 3735928559;
  
      TEST_ASSERT_EQUAL_INT(8, fmt_u32_hex(out, val));
      TEST_ASSERT_EQUAL_STRING("DEADBEEF", (char *) out);
  }
  
  static void test_fmt_u64_hex(void)
  {
      char out[17] = "----------------";
      uint64_t val = 1002843385516306400;
  
      TEST_ASSERT_EQUAL_INT(16, fmt_u64_hex(out, val));
      TEST_ASSERT_EQUAL_STRING("0DEAD0BEEF0CAFE0", (char *) out);
  }
  
  static void test_fmt_u32_dec(void)
  {
      char out[9] = "--------";
      uint32_t val = 12345678;
      uint8_t chars = 0;
  
      chars = fmt_u32_dec(out, val);
      TEST_ASSERT_EQUAL_INT(8, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("12345678", (char *) out);
  }
  
  static void test_fmt_u16_dec(void)
  {
      char out[5] = "----";
      uint16_t val = 6556;
      uint8_t chars = 0;
  
      chars = fmt_u16_dec(out, val);
      TEST_ASSERT_EQUAL_INT(4, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("6556", (char *) out);
  }
  
  static void test_fmt_s32_dec(void)
  {
      char out[8] = "--------";
      int32_t val = 9876;
      uint8_t chars = 0;
  
      chars = fmt_s32_dec(out, val);
      TEST_ASSERT_EQUAL_INT(4, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("9876", (char *) out);
  
      val = -9876;
      chars = fmt_s32_dec(out, val);
      TEST_ASSERT_EQUAL_INT(5, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("-9876", (char *) out);
  }
  
  static void test_fmt_u64_dec_a(void)
  {
      char out[21] = "------------------";
      uint64_t val = 0;
      uint8_t chars = 0;
  
      chars = fmt_u64_dec(out, val);
      TEST_ASSERT_EQUAL_INT(1, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("0", (char *) out);
  }
  
  static void test_fmt_u64_dec_b(void)
  {
      char out[21] = "--------------------";
      uint64_t val = 18446744073709551615LLU;
      uint8_t chars = 0;
  
      chars = fmt_u64_dec(out, val);
      TEST_ASSERT_EQUAL_INT(20, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("18446744073709551615", (char *) out);
  }
  
  static void test_fmt_u64_dec_c(void)
  {
      char out[21] = "--------------------";
      uint64_t val = 1234567890123456789LLU;
      uint8_t chars = 0;
  
      chars = fmt_u64_dec(out, val);
      TEST_ASSERT_EQUAL_INT(19, chars);
      out[chars] = '\0';
      TEST_ASSERT_EQUAL_STRING("1234567890123456789", (char *) out);
  }
  
  static void test_rmt_s16_dec(void)
  {
      char out[7] = "-------";
      int16_t val;
      size_t len;
  
      val = 0;
      len = fmt_s16_dec(out, val);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(1, len);
      TEST_ASSERT_EQUAL_STRING("0", (char *)out);
  
      val = -32000;
      len = fmt_s16_dec(out, val);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(6, len);
      TEST_ASSERT_EQUAL_STRING("-32000", (char *)out);
  
      val = 12345;
      len = fmt_s16_dec(out, val);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(5, len);
      TEST_ASSERT_EQUAL_STRING("12345", (char *)out);
  }
  
  static void test_rmt_s16_dfp(void)
  {
      char out[8] = "--------";
      int16_t val;
      unsigned fpp;
      size_t len;
  
      val = 0;
      fpp = 3;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(5, len);
      TEST_ASSERT_EQUAL_STRING("0.000", (char *)out);
  
      val = 12345;
      fpp = 4;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(6, len);
      TEST_ASSERT_EQUAL_STRING("1.2345", (char *)out);
  
      val = 12030;
      fpp = 3;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(6, len);
      TEST_ASSERT_EQUAL_STRING("12.030", (char *)out);
  
      val = -3548;
      fpp = 2;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(6, len);
      TEST_ASSERT_EQUAL_STRING("-35.48", (char *)out);
  
      val = -23;
      fpp = 4;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(7, len);
      TEST_ASSERT_EQUAL_STRING("-0.0023", (char *)out);
  
      val = 50;
      fpp = 3;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(5, len);
      TEST_ASSERT_EQUAL_STRING("0.050", (char *)out);
  
      val = -12345;
      fpp = 0;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(6, len);
      TEST_ASSERT_EQUAL_STRING("-12345", (char *)out);
  
      val = 31987;
      fpp = 5;
      len = fmt_s16_dfp(out, val, fpp);
      out[len] = '\0';
      TEST_ASSERT_EQUAL_INT(0, len);
      TEST_ASSERT_EQUAL_STRING("", (char *)out);
  }
  
  static void test_fmt_strlen(void)
  {
      const char *empty_str = "";
      const char *short_str = "short";
      const char *long_str = "this is a long string";
  
      TEST_ASSERT_EQUAL_INT(0, fmt_strlen(empty_str));
      TEST_ASSERT_EQUAL_INT(5, fmt_strlen(short_str));
      TEST_ASSERT_EQUAL_INT(21, fmt_strlen(long_str));
  }
  
  static void test_fmt_str(void)
  {
      const char *string1 = "string1";
      char string2[]      = "StRiNg2";
  
      TEST_ASSERT_EQUAL_INT(fmt_strlen(string1), fmt_str(&string2[0], string1));
      TEST_ASSERT_EQUAL_STRING(string1, &string2[0]);
  }
  
  static void test_scn_u32_dec(void)
  {
      const char *string1 = "123456789";
      uint32_t val1 = 123456789;
      uint32_t val2 = 12345;
  
      TEST_ASSERT_EQUAL_INT(val1, scn_u32_dec(string1, 9));
      TEST_ASSERT_EQUAL_INT(val2, scn_u32_dec(string1, 5));
  }
  
  Test *tests_fmt_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_fmt_byte_hex),
          new_TestFixture(test_fmt_bytes_hex_reverse),
          new_TestFixture(test_fmt_u32_hex),
          new_TestFixture(test_fmt_u64_hex),
          new_TestFixture(test_fmt_u32_dec),
          new_TestFixture(test_fmt_u64_dec_a),
          new_TestFixture(test_fmt_u64_dec_b),
          new_TestFixture(test_fmt_u64_dec_c),
          new_TestFixture(test_fmt_u16_dec),
          new_TestFixture(test_fmt_s32_dec),
          new_TestFixture(test_rmt_s16_dec),
          new_TestFixture(test_rmt_s16_dfp),
          new_TestFixture(test_fmt_strlen),
          new_TestFixture(test_fmt_str),
          new_TestFixture(test_scn_u32_dec),
      };
  
      EMB_UNIT_TESTCALLER(fmt_tests, NULL, NULL, fixtures);
  
      return (Test *)&fmt_tests;
  }
  
  void tests_fmt(void)
  {
      TESTS_RUN(tests_fmt_tests());
  }
  /** @} */