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 "pw_unit_test/framework.h"
22
23 namespace pw::bytes {
24 namespace {
25
26 // SignExtend unsigned integer values into signed integers.
27 constexpr uint32_t kNegative24Bits = 0x00FACADE;
28 constexpr int32_t kExtendedNegative24Bits = SignExtend<24>(kNegative24Bits);
29 static_assert(kExtendedNegative24Bits == static_cast<int32_t>(0xFFFACADE));
30
31 constexpr uint32_t kPositive20Bits = 0x00000ACE;
32 constexpr int32_t kExtendedPositive20Bits = SignExtend<20>(kPositive20Bits);
33 static_assert(kExtendedPositive20Bits == static_cast<int32_t>(0x00000ACE));
34
35 constexpr uint32_t kNegative12Bits = 0x00000ACE;
36 constexpr int32_t kExtendedNegative12Bits = SignExtend<12>(kNegative12Bits);
37 static_assert(kExtendedNegative12Bits == static_cast<int32_t>(0xFFFFFACE));
38
39 constexpr uint32_t k32Bits = 0b10100000101000001010000010100000;
40 // ExtractBits: extract single bit.
41 constexpr uint8_t kZeroBit = ExtractBits<uint8_t, 4, 4>(k32Bits);
42 static_assert(kZeroBit == 0);
43 constexpr uint8_t kOneBit = ExtractBits<uint8_t, 5, 5>(k32Bits);
44 static_assert(kOneBit == 1);
45 // ExtractBits: extract 16 bits from uint32_t to uint16_t.
46 constexpr uint16_t kExtracted16Bits = 0b1010000010100000;
47 constexpr uint16_t k15To0Bits = ExtractBits<uint32_t, 15, 0>(k32Bits);
48 static_assert(k15To0Bits == kExtracted16Bits);
49 constexpr uint16_t k23To8Bits = ExtractBits<uint16_t, 23, 8>(k32Bits);
50 static_assert(k23To8Bits == kExtracted16Bits);
51 constexpr uint16_t k31To16Bits = ExtractBits<uint16_t, 31, 16>(k32Bits);
52 static_assert(k31To16Bits == kExtracted16Bits);
53 // ExtractBits: extract 31 bits.
54 constexpr uint32_t kExtracted31Bits = 0b1010000010100000101000001010000;
55 constexpr uint32_t k31To1Bits = ExtractBits<uint32_t, 31, 1>(k32Bits);
56 static_assert(k31To1Bits == kExtracted31Bits);
57 // ExtractBits: extract all bits.
58 constexpr uint32_t k31To0Bits = ExtractBits<uint32_t, 31, 0>(k32Bits);
59 static_assert(k31To0Bits == k32Bits);
60
TEST(Endian,NativeIsBigOrLittle)61 TEST(Endian, NativeIsBigOrLittle) {
62 EXPECT_TRUE(endian::native == endian::little ||
63 endian::native == endian::big);
64 }
65
TEST(Endian,NativeIsCorrect)66 TEST(Endian, NativeIsCorrect) {
67 constexpr uint32_t kInteger = 0x11223344u;
68 int8_t bytes[sizeof(kInteger)] = {};
69 std::memcpy(bytes, &kInteger, sizeof(kInteger));
70
71 if (endian::native == endian::little) {
72 EXPECT_EQ(bytes[0], 0x44);
73 EXPECT_EQ(bytes[1], 0x33);
74 EXPECT_EQ(bytes[2], 0x22);
75 EXPECT_EQ(bytes[3], 0x11);
76 } else {
77 EXPECT_EQ(bytes[0], 0x11);
78 EXPECT_EQ(bytes[1], 0x22);
79 EXPECT_EQ(bytes[2], 0x33);
80 EXPECT_EQ(bytes[3], 0x44);
81 }
82 }
83
84 } // namespace
85 } // namespace pw::bytes
86