duckhandy/test/unit/bitfield_pack_test.cpp

80 lines
2.1 KiB
C++

/* Copyright 2016-2020 Michele Santullo
* This file is part of "duckhandy".
*
* "duckhandy" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "duckhandy" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch.hpp"
#include "duckhandy/bitfield_pack.hpp"
#include <cstdint>
TEST_CASE ("Check correctness in BitfieldPack", "[BitfieldPack][containers]") {
using dhandy::set;
using dhandy::get;
{
dhandy::BitfieldPack<uint8_t, 4, 7, 1> pack(0xAA);
CHECK(sizeof(pack) == sizeof(uint8_t) * 2);
CHECK(get<0>(pack) == 0b1010U);
CHECK(get<1>(pack) == 0b0101010U);
CHECK(get<2>(pack) == 0b1U);
set<0>(pack, 2);
CHECK(get<0>(pack) == 2);
set<1>(pack, 50);
CHECK(get<1>(pack) == 50);
CHECK(get<2>(pack) == 0b1U);
CHECK(get<0>(pack) == 2);
}
{
dhandy::BitfieldPack<uint16_t, 16, 5, 8, 11, 3, 1, 1> pack;
set<6>(pack, 1);
CHECK(get<0>(pack) == 0);
CHECK(get<1>(pack) == 0);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
set<6>(pack, 0);
CHECK(get<6>(pack) == 0);
set<6>(pack, 1);
set<1>(pack, 5);
CHECK(get<0>(pack) == 0);
CHECK(get<1>(pack) == 5);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
set<1>(pack, 0);
CHECK(get<1>(pack) == 0);
set<6>(pack, 1);
set<1>(pack, 5);
set<3>(pack, 0b10011001101);
CHECK(get<0>(pack) == 0);
CHECK(get<1>(pack) == 5);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0b10011001101);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
}
}