Blame view

RIOT/tests/unittests/tests-checksum/tests-checksum-crc16-ccitt.c 3.41 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
  /*
   * Copyright 2016 Ludwig KnΓΌpfer <ludwig.knuepfer@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 <stdint.h>
  
  #include "embUnit/embUnit.h"
  
  #include "checksum/crc16_ccitt.h"
  
  #include "tests-checksum.h"
  
  static int calc_and_compare_crc_with_update(const unsigned char *buf,
          size_t len, size_t split, uint16_t expected)
  {
      uint16_t result = crc16_ccitt_calc(buf, split);
  
      result = crc16_ccitt_update(result, buf + split, len - split);
  
      return result == expected;
  }
  
  static int calc_and_compare_crc(const unsigned char *buf, size_t len,
          uint16_t expected)
  {
      uint16_t result = crc16_ccitt_calc(buf, len);
  
      return result == expected;
  }
  
  static void test_checksum_crc16_ccitt_sequence_empty(void)
  {
      unsigned char buf[] = "";
      uint16_t expect = 0x1D0F;
  
      TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
      TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
                  (sizeof(buf) - 1) / 2, expect));
  }
  
  static void test_checksum_crc16_ccitt_sequence_1a(void)
  {
      unsigned char buf[] = "A";
      uint16_t expect = 0x9479;
  
      TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
      TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
                  (sizeof(buf) - 1) / 2, expect));
  }
  
  static void test_checksum_crc16_ccitt_sequence_256a(void)
  {
      unsigned char buf[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                            "AAAA";
      uint16_t expect = 0xE938;
  
      TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
      TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
                  (sizeof(buf) - 1) / 2, expect));
  }
  
  static void test_checksum_crc16_ccitt_sequence_1to9(void)
  {
      unsigned char buf[] = "123456789";
      uint16_t expect = 0xE5CC;
  
      TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
      TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf)
                  - 1, (sizeof(buf) - 1) / 2, expect));
  }
  
  static void test_checksum_crc16_ccitt_sequence_4bytes(void)
  {
      unsigned char buf[] = { 0x12, 0x34, 0x56, 0x78 };
      uint16_t expect = 0xBA3C;
  
      TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf), expect));
      TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf),
                  sizeof(buf) / 2, expect));
  }
  
  Test *tests_checksum_crc16_ccitt_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          /* Reference values according to
           * http://srecord.sourceforge.net/crc16-ccitt.html */
          new_TestFixture(test_checksum_crc16_ccitt_sequence_empty),
          new_TestFixture(test_checksum_crc16_ccitt_sequence_1a),
          new_TestFixture(test_checksum_crc16_ccitt_sequence_256a),
          new_TestFixture(test_checksum_crc16_ccitt_sequence_1to9),
          new_TestFixture(test_checksum_crc16_ccitt_sequence_4bytes),
      };
  
      EMB_UNIT_TESTCALLER(checksum_crc16_ccitt_tests, NULL, NULL, fixtures);
  
      return (Test *)&checksum_crc16_ccitt_tests;
  }