2020-04-16 09:18:19 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017-2020 The Project X-Ray Authors.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a ISC-style
|
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
|
* https://opensource.org/licenses/ISC
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
|
*/
|
2017-12-01 17:47:21 +01:00
|
|
|
#include <prjxray/bit_ops.h>
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
|
|
TEST(BitMaskTest, Bit0) {
|
|
|
|
|
uint32_t expected = prjxray::bit_mask<uint32_t>(0);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0x1), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitMaskTest, Bit3) {
|
|
|
|
|
uint32_t expected = prjxray::bit_mask<uint32_t>(3);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0x8), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitMaskRange, SingleBit) {
|
|
|
|
|
uint32_t expected = prjxray::bit_mask_range<uint32_t>(23, 23);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0x800000), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitMaskRange, DownToZero) {
|
|
|
|
|
uint32_t expected = prjxray::bit_mask_range<uint32_t>(7, 0);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0xFF), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitMaskRange, MiddleBits) {
|
|
|
|
|
uint32_t expected = prjxray::bit_mask_range<uint32_t>(18, 8);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0x7FF00), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldGetTest, OneSelectedBit) {
|
|
|
|
|
uint32_t expected = prjxray::bit_field_get(0xFFFFFFFF, 23, 23);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(1), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldGetTest, SelectDownToZero) {
|
|
|
|
|
uint32_t expected = prjxray::bit_field_get(0xFFCCBBAA, 7, 0);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0xAA), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldGetTest, SelectMidway) {
|
|
|
|
|
uint32_t expected = prjxray::bit_field_get(0xFFCCBBAA, 18, 8);
|
|
|
|
|
EXPECT_EQ(static_cast<uint32_t>(0x4BB), expected);
|
|
|
|
|
}
|
2017-12-04 20:02:55 +01:00
|
|
|
|
|
|
|
|
TEST(BitFieldSetTest, WriteOneBit) {
|
|
|
|
|
uint32_t actual = prjxray::bit_field_set(
|
2018-01-08 22:30:02 +01:00
|
|
|
static_cast<uint32_t>(0x0), 23, 23, static_cast<uint32_t>(0x1));
|
2017-12-04 20:02:55 +01:00
|
|
|
EXPECT_EQ(actual, static_cast<uint32_t>(0x800000));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldSetTest, WriteOneBitWithOutOfRangeValue) {
|
|
|
|
|
uint32_t actual = prjxray::bit_field_set(
|
2018-01-08 22:30:02 +01:00
|
|
|
static_cast<uint32_t>(0x0), 23, 23, static_cast<uint32_t>(0x3));
|
2017-12-04 20:02:55 +01:00
|
|
|
EXPECT_EQ(actual, static_cast<uint32_t>(0x800000));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldSetTest, WriteMultipleBits) {
|
|
|
|
|
uint32_t actual = prjxray::bit_field_set(
|
2018-01-08 22:30:02 +01:00
|
|
|
static_cast<uint32_t>(0x0), 18, 8, static_cast<uint32_t>(0x123));
|
2017-12-04 20:02:55 +01:00
|
|
|
EXPECT_EQ(actual, static_cast<uint32_t>(0x12300));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BitFieldSetTest, WriteMultipleBitsWithOutOfRangeValue) {
|
|
|
|
|
uint32_t actual = prjxray::bit_field_set(
|
2018-01-08 22:30:02 +01:00
|
|
|
static_cast<uint32_t>(0x0), 18, 8, static_cast<uint32_t>(0x1234));
|
2017-12-04 20:02:55 +01:00
|
|
|
EXPECT_EQ(actual, static_cast<uint32_t>(0x23400));
|
|
|
|
|
}
|