Blame view

RIOT/tests/unittests/tests-core/tests-core-byteorder.c 3.38 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
  /*
   * Copyright (C) 2014 René Kijewski
   *
   * 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 "embUnit.h"
  
  #include "byteorder.h"
  
  #include "tests-core.h"
  
  static void test_byteorder_little_to_big_16(void)
  {
      le_uint16_t little = { .u8 = { 0x12, 0x34 } };
      be_uint16_t big    = { .u8 = { 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(big.u16, byteorder_ltobs(little).u16);
  }
  
  static void test_byteorder_big_to_little_16(void)
  {
      le_uint16_t little = { .u8 = { 0x12, 0x34 } };
      be_uint16_t big    = { .u8 = { 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(little.u16, byteorder_btols(big).u16);
  }
  
  static void test_byteorder_little_to_big_32(void)
  {
      le_uint32_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78 } };
      be_uint32_t big    = { .u8 = { 0x78, 0x56, 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(big.u32, byteorder_ltobl(little).u32);
  }
  
  static void test_byteorder_big_to_little_32(void)
  {
      le_uint32_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78 } };
      be_uint32_t big    = { .u8 = { 0x78, 0x56, 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(little.u32, byteorder_btoll(big).u32);
  }
  
  static void test_byteorder_little_to_big_64(void)
  {
      le_uint64_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
      be_uint64_t big    = { .u8 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(big.u64, byteorder_ltobll(little).u64);
  }
  
  static void test_byteorder_big_to_little_64(void)
  {
      le_uint64_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
      be_uint64_t big    = { .u8 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 } };
      TEST_ASSERT_EQUAL_INT(little.u64, byteorder_btolll(big).u64);
  }
  
  static void test_byteorder_host_to_network_16(void)
  {
      static const uint16_t host = 0x1234;
      network_uint16_t network = { .u8 = { 0x12, 0x34 } };
      TEST_ASSERT_EQUAL_INT(network.u16, byteorder_htons(host).u16);
      TEST_ASSERT_EQUAL_INT(host, byteorder_ntohs(network));
  }
  
  static void test_byteorder_host_to_network_32(void)
  {
      static const uint32_t host = 0x12345678ul;
      network_uint32_t network = { .u8 = { 0x12, 0x34, 0x56, 0x78 } };
      TEST_ASSERT_EQUAL_INT(network.u32, byteorder_htonl(host).u32);
      TEST_ASSERT_EQUAL_INT(host, byteorder_ntohl(network));
  }
  
  static void test_byteorder_host_to_network_64(void)
  {
      static const uint64_t host = 0x123456789abcdef0ull;
      network_uint64_t network = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
      TEST_ASSERT_EQUAL_INT(network.u64, byteorder_htonll(host).u64);
      TEST_ASSERT_EQUAL_INT(host, byteorder_ntohll(network));
  }
  
  Test *tests_core_byteorder_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_byteorder_little_to_big_16),
          new_TestFixture(test_byteorder_big_to_little_16),
          new_TestFixture(test_byteorder_little_to_big_32),
          new_TestFixture(test_byteorder_big_to_little_32),
          new_TestFixture(test_byteorder_little_to_big_64),
          new_TestFixture(test_byteorder_big_to_little_64),
          new_TestFixture(test_byteorder_host_to_network_16),
          new_TestFixture(test_byteorder_host_to_network_32),
          new_TestFixture(test_byteorder_host_to_network_64),
      };
  
      EMB_UNIT_TESTCALLER(core_byteorder_tests, NULL, NULL, fixtures);
      return (Test *)&core_byteorder_tests;
  }