1 // Copyright (c) 2012, Robert Escriva
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of this project nor the names of its contributors may
13 // be used to endorse or promote products derived from this software
14 // without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27
28 #pragma GCC diagnostic ignored "-Wfloat-equal"
29
30 // C
31 #include <string.h>
32
33 // e
34 #include "th.h"
35 #include "e/endian.h"
36
37 #define ASSERT_MEMCMP(X, Y, S) ASSERT_EQ(0, memcmp(X, Y, S))
38
39 namespace
40 {
41
TEST(EndianTest,Pack)42 TEST(EndianTest, Pack)
43 {
44 uint8_t buffer[sizeof(int64_t)];
45
46 e::pack8be(uint8_t(0xde), buffer);
47 ASSERT_MEMCMP(buffer, "\xde", 1);
48 e::pack8le(uint8_t(0xde), buffer);
49 ASSERT_MEMCMP(buffer, "\xde", 1);
50 e::pack8be(int8_t(0xde), buffer);
51 ASSERT_MEMCMP(buffer, "\xde", 1);
52 e::pack8le(int8_t(0xde), buffer);
53 ASSERT_MEMCMP(buffer, "\xde", 1);
54
55 e::pack16be(uint16_t(0xdead), buffer);
56 ASSERT_MEMCMP(buffer, "\xde\xad", 2);
57 e::pack16le(uint16_t(0xdead), buffer);
58 ASSERT_MEMCMP(buffer, "\xad\xde", 2);
59 e::pack16be(int16_t(0xdead), buffer);
60 ASSERT_MEMCMP(buffer, "\xde\xad", 2);
61 e::pack16le(int16_t(0xdead), buffer);
62 ASSERT_MEMCMP(buffer, "\xad\xde", 2);
63
64 e::pack32be(uint32_t(0xdeadbeefUL), buffer);
65 ASSERT_MEMCMP(buffer, "\xde\xad\xbe\xef", 4);
66 e::pack32le(uint32_t(0xdeadbeefUL), buffer);
67 ASSERT_MEMCMP(buffer, "\xef\xbe\xad\xde", 4);
68 e::pack32be(int32_t(0xdeadbeefUL), buffer);
69 ASSERT_MEMCMP(buffer, "\xde\xad\xbe\xef", 4);
70 e::pack32le(int32_t(0xdeadbeefUL), buffer);
71 ASSERT_MEMCMP(buffer, "\xef\xbe\xad\xde", 4);
72
73 e::pack64be(uint64_t(0xdeadbeefcafebabeULL), buffer);
74 ASSERT_MEMCMP(buffer, "\xde\xad\xbe\xef\xca\xfe\xba\xbe", 8);
75 e::pack64le(uint64_t(0xdeadbeefcafebabeULL), buffer);
76 ASSERT_MEMCMP(buffer, "\xbe\xba\xfe\xca\xef\xbe\xad\xde", 8);
77 e::pack64be(int64_t(0xdeadbeefcafebabeULL), buffer);
78 ASSERT_MEMCMP(buffer, "\xde\xad\xbe\xef\xca\xfe\xba\xbe", 8);
79 e::pack64le(int64_t(0xdeadbeefcafebabeULL), buffer);
80 ASSERT_MEMCMP(buffer, "\xbe\xba\xfe\xca\xef\xbe\xad\xde", 8);
81
82 float f = 16711938.0;
83 e::packfloatbe(f, buffer);
84 ASSERT_MEMCMP(buffer, "\x4b\x7f\x01\x02", 4);
85 e::packfloatle(f, buffer);
86 ASSERT_MEMCMP(buffer, "\x02\x01\x7f\x4b", 4);
87
88 double d = 9006104071832581.0;
89 e::packdoublebe(d, buffer);
90 ASSERT_MEMCMP(buffer, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8);
91 e::packdoublele(d, buffer);
92 ASSERT_MEMCMP(buffer, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8);
93 }
94
TEST(EndianTest,Unpack)95 TEST(EndianTest, Unpack)
96 {
97 const uint8_t buffer8[] = "\xde";
98 const uint8_t buffer16[] = "\xde\xad";
99 const uint8_t buffer32[] = "\xde\xad\xbe\xef";
100 const uint8_t buffer64[] = "\xde\xad\xbe\xef\xca\xfe\xba\xbe";
101 const uint8_t bufferfloatbe[] = "\x4b\x7f\x01\x02";
102 const uint8_t bufferfloatle[] = "\x02\x01\x7f\x4b";
103 const uint8_t bufferdoublebe[] = "\x43\x3f\xff\x01\x02\x03\x04\x05";
104 const uint8_t bufferdoublele[] = "\x05\x04\x03\x02\x01\xff\x3f\x43";
105
106 uint8_t unsigned8;
107 int8_t signed8;
108 uint16_t unsigned16;
109 int16_t signed16;
110 uint32_t unsigned32;
111 int32_t signed32;
112 uint64_t unsigned64;
113 int64_t signed64;
114 float f;
115 double d;
116
117 e::unpack8be(buffer8, &unsigned8);
118 ASSERT_EQ(0xde, unsigned8);
119 e::unpack8le(buffer8, &unsigned8);
120 ASSERT_EQ(0xde, unsigned8);
121 e::unpack8be(buffer8, &signed8);
122 ASSERT_EQ(int8_t(0xde), signed8);
123 e::unpack8le(buffer8, &signed8);
124 ASSERT_EQ(int8_t(0xde), signed8);
125
126 e::unpack16be(buffer16, &unsigned16);
127 ASSERT_EQ(0xdead, unsigned16);
128 e::unpack16le(buffer16, &unsigned16);
129 ASSERT_EQ(0xadde, unsigned16);
130 e::unpack16be(buffer16, &signed16);
131 ASSERT_EQ(int16_t(0xdead), signed16);
132 e::unpack16le(buffer16, &signed16);
133 ASSERT_EQ(int16_t(0xadde), signed16);
134
135 e::unpack32be(buffer32, &unsigned32);
136 ASSERT_EQ(0xdeadbeefUL, unsigned32);
137 e::unpack32le(buffer32, &unsigned32);
138 ASSERT_EQ(0xefbeaddeUL, unsigned32);
139 e::unpack32be(buffer32, &signed32);
140 ASSERT_EQ(int32_t(0xdeadbeefUL), signed32);
141 e::unpack32le(buffer32, &signed32);
142 ASSERT_EQ(int32_t(0xefbeaddeUL), signed32);
143
144 e::unpack64be(buffer64, &unsigned64);
145 ASSERT_EQ(0xdeadbeefcafebabeULL, unsigned64);
146 e::unpack64le(buffer64, &unsigned64);
147 ASSERT_EQ(0xbebafecaefbeaddeULL, unsigned64);
148 e::unpack64be(buffer64, &signed64);
149 ASSERT_EQ(int64_t(0xdeadbeefcafebabeULL), signed64);
150 e::unpack64le(buffer64, &signed64);
151 ASSERT_EQ(int64_t(0xbebafecaefbeaddeULL), signed64);
152
153 e::unpackfloatbe(bufferfloatbe, &f);
154 ASSERT_EQ(16711938.0, f);
155 e::unpackfloatle(bufferfloatle, &f);
156 ASSERT_EQ(16711938.0, f);
157
158 e::unpackdoublebe(bufferdoublebe, &d);
159 ASSERT_EQ(9006104071832581.0, d);
160 e::unpackdoublele(bufferdoublele, &d);
161 ASSERT_EQ(9006104071832581.0, d);
162 }
163
164 } // namespace
165