Blame view

RIOT/tests/unittests/tests-printf_float/tests-printf_float.c 1.73 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
  /*
   * 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 printing floating point numbers
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @}
   */
  
  #include <errno.h>
  #include <stdio.h>
  #include <string.h>
  
  #include "embUnit/embUnit.h"
  
  #include "tests-printf_float.h"
  
  #define BUFSIZE             (10)
  
  static const double in0 = 2016.0349;
  static const double in1 = 123.4567;
  static const double in2 = 0.0;
  
  static void sfprintf_float(void)
  {
      char tmp[BUFSIZE];
      char *str = tmp;
  
      snprintf(str, BUFSIZE, "%f", in0);
      TEST_ASSERT_EQUAL_STRING("2016.0349", str);
  
      snprintf(str, BUFSIZE, "%.2f", in0);
      TEST_ASSERT_EQUAL_STRING("2016.03", str);
  
      snprintf(str, BUFSIZE, "%.f", in0);
      TEST_ASSERT_EQUAL_STRING("2016", str);
  
      snprintf(str, BUFSIZE, "%.4f", in1);
      TEST_ASSERT_EQUAL_STRING("123.4567", str);
  
      snprintf(str, BUFSIZE, "%.2f", in1);
      TEST_ASSERT_EQUAL_STRING("123.46", str);
  
      snprintf(str, BUFSIZE, "%4.f", in2);
      TEST_ASSERT_EQUAL_STRING("   0", str);
  
      snprintf(str, BUFSIZE, "%.3f", in2);
      TEST_ASSERT_EQUAL_STRING("0.000", str);
  
      snprintf(str, BUFSIZE, "%2.04f", in2);
      TEST_ASSERT_EQUAL_STRING("0.0000", str);
  }
  
  Test *tests_printf_float_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(sfprintf_float)
      };
  
      EMB_UNIT_TESTCALLER(pkt_tests, NULL, NULL, fixtures);
  
      return (Test *)&pkt_tests;
  }
  
  void tests_printf_float(void)
  {
      TESTS_RUN(tests_printf_float_tests());
  }