Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / nlio / repo / tests / nlbyteorder-test-cxx.cpp
1 /**
2  *    Copyright 2012-2016 Nest Labs Inc. All Rights Reserved.
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 /**
18  *    @file
19  *      This file implements a unit test suite for the C++ language
20  *      binding of the Nest Labs byte ordering functions and macros.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <nlbyteorder.hpp>
28
29 #include <nlunit-test.h>
30
31 #include "nlbyteorder-test.h"
32
33 using namespace nl::ByteOrder;
34
35 static void CheckPreprocessorDefinitions(nlTestSuite *inSuite, void *inContext)
36 {
37     NL_TEST_ASSERT(inSuite, NLBYTEORDER_LITTLE_ENDIAN  == 0x1234);
38     NL_TEST_ASSERT(inSuite, NLBYTEORDER_BIG_ENDIAN     == 0x4321);
39     NL_TEST_ASSERT(inSuite, NLBYTEORDER_UNKNOWN_ENDIAN == 0xFFFF);
40     NL_TEST_ASSERT(inSuite, NLBYTEORDER != NLBYTEORDER_UNKNOWN_ENDIAN);
41 }
42
43 static void CheckEnumerations(nlTestSuite *inSuite, void *inContext)
44 {
45     NL_TEST_ASSERT(inSuite, Unknown      == static_cast<ByteOrder>(nlByteOrderUnknown));
46     NL_TEST_ASSERT(inSuite, LittleEndian == static_cast<ByteOrder>(nlByteOrderLittleEndian));
47     NL_TEST_ASSERT(inSuite, BigEndian    == static_cast<ByteOrder>(nlByteOrderBigEndian));
48 }
49
50 static void CheckByteOrder(nlTestSuite *inSuite, void *inContext)
51 {
52     // Check that we get a meaningful value for the current byte order.
53
54     NL_TEST_ASSERT(inSuite, GetCurrent() != Unknown);
55 }
56
57 static void CheckConstantMacros(nlTestSuite *inSuite, void *inContext)
58 {
59     const uint16_t v16 = nlByteOrderConstantSwap16(MAGIC16);
60     const uint32_t v32 = nlByteOrderConstantSwap32(MAGIC32);
61     const uint64_t v64 = nlByteOrderConstantSwap64(MAGIC64);
62
63     // Check that the constant swap macros work.
64
65     NL_TEST_ASSERT(inSuite, v16 == MAGIC_SWAP16);
66     NL_TEST_ASSERT(inSuite, v32 == MAGIC_SWAP32);
67     NL_TEST_ASSERT(inSuite, v64 == MAGIC_SWAP64);
68 }
69
70 static void CheckValueInlines(nlTestSuite *inSuite, void *inContext)
71 {
72     // Check that the constant swap inline free functions work.
73
74     NL_TEST_ASSERT(inSuite, Swap16(MAGIC16) == MAGIC_SWAP16);
75     NL_TEST_ASSERT(inSuite, Swap32(MAGIC32) == MAGIC_SWAP32);
76     NL_TEST_ASSERT(inSuite, Swap64(MAGIC64) == MAGIC_SWAP64);
77 }
78
79 static void CheckInPlaceInlines(nlTestSuite *inSuite, void *inContext)
80 {
81     uint16_t v16 = MAGIC16;
82     uint32_t v32 = MAGIC32;
83     uint64_t v64 = MAGIC64;
84
85     Swap16(&v16);
86     NL_TEST_ASSERT(inSuite, v16 == MAGIC_SWAP16);
87
88     Swap32(&v32);
89     NL_TEST_ASSERT(inSuite, v32 == MAGIC_SWAP32);
90
91     Swap64(&v64);
92     NL_TEST_ASSERT(inSuite, v64 == MAGIC_SWAP64);
93 }
94
95 static void CheckSwapByValue(nlTestSuite *inSuite, void *inContext)
96 {
97     uint16_t v16_in = MAGIC16;
98     uint32_t v32_in = MAGIC32;
99     uint64_t v64_in = MAGIC64;
100     uint16_t v16_out;
101     uint32_t v32_out;
102     uint64_t v64_out;
103
104     // Check little-to-host and host-to-little swap by value.
105
106     v16_out = Swap16LittleToHost(v16_in);
107     v32_out = Swap32LittleToHost(v32_in);
108     v64_out = Swap64LittleToHost(v64_in);
109
110 #if NLBYTEORDER == NLBYTEORDER_LITTLE_ENDIAN
111     NL_TEST_ASSERT(inSuite, v16_out == v16_in);
112     NL_TEST_ASSERT(inSuite, v32_out == v32_in);
113     NL_TEST_ASSERT(inSuite, v64_out == v64_in);
114
115 #elif NLBYTEORDER == NLBYTEORDER_BIG_ENDIAN
116     NL_TEST_ASSERT(inSuite, v16_out == MAGIC_SWAP16);
117     NL_TEST_ASSERT(inSuite, v32_out == MAGIC_SWAP32);
118     NL_TEST_ASSERT(inSuite, v64_out == MAGIC_SWAP64);
119
120 #endif
121
122     v16_in = Swap16HostToLittle(v16_out);
123     v32_in = Swap32HostToLittle(v32_out);
124     v64_in = Swap64HostToLittle(v64_out);
125
126 #if NLBYTEORDER == NLBYTEORDER_LITTLE_ENDIAN
127     NL_TEST_ASSERT(inSuite, v16_out == v16_in);
128     NL_TEST_ASSERT(inSuite, v32_out == v32_in);
129     NL_TEST_ASSERT(inSuite, v64_out == v64_in);
130
131 #elif NLBYTEORDER == NLBYTEORDER_BIG_ENDIAN
132     NL_TEST_ASSERT(inSuite, v16_in == MAGIC16);
133     NL_TEST_ASSERT(inSuite, v32_in == MAGIC32);
134     NL_TEST_ASSERT(inSuite, v64_in == MAGIC64);
135
136 #endif
137
138     // Check big-to-host and host-to-big swap by value.
139
140     v16_out = Swap16BigToHost(v16_in);
141     v32_out = Swap32BigToHost(v32_in);
142     v64_out = Swap64BigToHost(v64_in);
143
144 #if NLBYTEORDER == NLBYTEORDER_BIG_ENDIAN
145     NL_TEST_ASSERT(inSuite, v16_out == v16_in);
146     NL_TEST_ASSERT(inSuite, v32_out == v32_in);
147     NL_TEST_ASSERT(inSuite, v64_out == v64_in);
148
149 #elif NLBYTEORDER == NLBYTEORDER_LITTLE_ENDIAN
150     NL_TEST_ASSERT(inSuite, v16_out == MAGIC_SWAP16);
151     NL_TEST_ASSERT(inSuite, v32_out == MAGIC_SWAP32);
152     NL_TEST_ASSERT(inSuite, v64_out == MAGIC_SWAP64);
153
154 #endif
155
156     v16_in = Swap16HostToBig(v16_out);
157     v32_in = Swap32HostToBig(v32_out);
158     v64_in = Swap64HostToBig(v64_out);
159
160 #if NLBYTEORDER == NLBYTEORDER_BIG_ENDIAN
161     NL_TEST_ASSERT(inSuite, v16_out == v16_in);
162     NL_TEST_ASSERT(inSuite, v32_out == v32_in);
163     NL_TEST_ASSERT(inSuite, v64_out == v64_in);
164
165 #elif NLBYTEORDER == NLBYTEORDER_LITTLE_ENDIAN
166     NL_TEST_ASSERT(inSuite, v16_in == MAGIC16);
167     NL_TEST_ASSERT(inSuite, v32_in == MAGIC32);
168     NL_TEST_ASSERT(inSuite, v64_in == MAGIC64);
169
170 #endif
171 }
172
173 static const nlTest sTests[] = {
174     NL_TEST_DEF("preprocessor definitions", CheckPreprocessorDefinitions),
175     NL_TEST_DEF("enumerations",             CheckEnumerations),
176     NL_TEST_DEF("byte order",               CheckByteOrder),
177     NL_TEST_DEF("constant macros",          CheckConstantMacros),
178     NL_TEST_DEF("value inlines",            CheckValueInlines),
179     NL_TEST_DEF("in-place inlines",         CheckInPlaceInlines),
180     NL_TEST_DEF("swap-by-value",            CheckSwapByValue),
181     NL_TEST_SENTINEL()
182 };
183
184 int main(void)
185 {
186     nlTestSuite theSuite = {
187         "nlbyteorder",
188         &sTests[0]
189     };
190
191     nl_test_set_output_style(OUTPUT_CSV);
192
193     nlTestRunner(&theSuite, NULL);
194
195     return nlTestRunnerStats(&theSuite);
196 }