1
2 // Copyright 2022 The Pigweed Authors
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 // use this file except in compliance with the License. You may obtain a copy of
6 // the License at
7 //
8 // https://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, WITHOUT
12 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 // License for the specific language governing permissions and limitations under
14 // the License.
15
16 #include "pw_bytes/bit.h"
17
18 #include <cstdint>
19 #include <cstring>
20
21 #include "gtest/gtest.h"
22
23 namespace pw::bytes {
24 namespace {
25
TEST(Endian,NativeIsBigOrLittle)26 TEST(Endian, NativeIsBigOrLittle) {
27 EXPECT_TRUE(endian::native == endian::little ||
28 endian::native == endian::big);
29 }
30
TEST(Endian,NativeIsCorrect)31 TEST(Endian, NativeIsCorrect) {
32 constexpr uint32_t kInteger = 0x11223344u;
33 int8_t bytes[sizeof(kInteger)] = {};
34 std::memcpy(bytes, &kInteger, sizeof(kInteger));
35
36 if (endian::native == endian::little) {
37 EXPECT_EQ(bytes[0], 0x44);
38 EXPECT_EQ(bytes[1], 0x33);
39 EXPECT_EQ(bytes[2], 0x22);
40 EXPECT_EQ(bytes[3], 0x11);
41 } else {
42 EXPECT_EQ(bytes[0], 0x11);
43 EXPECT_EQ(bytes[1], 0x22);
44 EXPECT_EQ(bytes[2], 0x33);
45 EXPECT_EQ(bytes[3], 0x44);
46 }
47 }
48
49 } // namespace
50 } // namespace pw::bytes
51