ardupilot/libraries/AP_Common/tests/test_bitmask.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.1 KiB
C++
Raw Normal View History

2018-11-02 18:29:18 -03:00
#include <AP_gtest.h>
2021-06-19 12:04:17 -03:00
#include <AP_HAL/AP_HAL.h>
2018-11-02 18:29:18 -03:00
#include <AP_Common/Bitmask.h>
2021-06-19 12:04:17 -03:00
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
template<int N>
void bitmask_tests(void)
2018-11-02 18:29:18 -03:00
{
Bitmask<N> x;
2021-06-19 12:04:17 -03:00
EXPECT_EQ(0, x.count());
2018-11-02 18:29:18 -03:00
EXPECT_EQ(-1, x.first_set());
x.set(5);
2021-06-19 12:04:17 -03:00
EXPECT_EQ(1, x.count());
2018-11-02 18:29:18 -03:00
EXPECT_EQ(5, x.first_set());
x.clear(5);
2021-06-19 12:04:17 -03:00
EXPECT_EQ(0, x.count());
2018-11-02 18:29:18 -03:00
EXPECT_EQ(-1, x.first_set());
EXPECT_EQ(-1, x.first_set());
x.set(N-7);
EXPECT_EQ(N-7, x.first_set());
x.clear(N-7);
2018-11-02 18:29:18 -03:00
EXPECT_EQ(-1, x.first_set());
EXPECT_EQ(-1, x.first_set());
x.set(0);
x.set(5);
x.set(6);
x.set(N-1);
2021-06-19 12:04:17 -03:00
EXPECT_EQ(4, x.count());
2018-11-02 18:29:18 -03:00
EXPECT_EQ(0, x.first_set());
EXPECT_EQ(0, x.first_set());
x.clear(0);
EXPECT_EQ(5, x.first_set());
EXPECT_EQ(5, x.first_set());
x.clear(5);
EXPECT_EQ(6, x.first_set());
EXPECT_EQ(6, x.first_set());
x.clear(6);
EXPECT_EQ(N-1, x.first_set());
EXPECT_EQ(N-1, x.first_set());
x.clear(N-1);
2018-11-02 18:29:18 -03:00
EXPECT_EQ(-1, x.first_set());
2021-06-19 12:04:17 -03:00
Bitmask<N> x2;
2021-06-19 12:04:17 -03:00
x2 = x;
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
x.clear(N+1);
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
EXPECT_EXIT(x.clear(N+1), testing::KilledBySignal(SIGABRT), "AP_InternalError::error_t::bitmask_range");
#endif
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
x.set(N+1);
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
EXPECT_EXIT(x.set(N+1), testing::KilledBySignal(SIGABRT), "AP_InternalError::error_t::bitmask_range");
#endif
for (uint8_t i=0; i<N; i++) {
2021-06-19 12:04:17 -03:00
EXPECT_EQ(x2.get(i), x.get(i));
}
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
EXPECT_EQ(false, x.get(N+1));
EXPECT_EQ(false, x2.get(N+1));
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
EXPECT_EXIT(x2.get(N+1), testing::KilledBySignal(SIGABRT), "AP_InternalError::error_t::bitmask_range");
#endif
2018-11-02 18:29:18 -03:00
}
// bitmasks are composed of 32 bit words, so test those boundaries
TEST(Bitmask, Tests31) { bitmask_tests<31>(); }
TEST(Bitmask, Tests32) { bitmask_tests<32>(); }
TEST(Bitmask, Tests33) { bitmask_tests<33>(); }
TEST(Bitmask, Tests47) { bitmask_tests<47>(); }
TEST(Bitmask, Tests48) { bitmask_tests<48>(); }
TEST(Bitmask, Tests49) { bitmask_tests<49>(); }
TEST(Bitmask, Tests63) { bitmask_tests<63>(); }
TEST(Bitmask, Tests64) { bitmask_tests<64>(); }
TEST(Bitmask, Tests65) { bitmask_tests<65>(); }
template<int N>
void bitmask_setall(void)
2018-11-02 18:29:18 -03:00
{
Bitmask<N> x;
2018-11-02 18:29:18 -03:00
EXPECT_EQ(-1, x.first_set());
EXPECT_EQ(false, x.get(N-4));
EXPECT_EQ(0, x.count());
2018-11-02 18:29:18 -03:00
x.setall();
EXPECT_EQ(0, x.first_set());
EXPECT_EQ(N, x.count());
2018-11-02 18:29:18 -03:00
x.clear(0);
EXPECT_EQ(1, x.first_set());
x.clear(1);
EXPECT_EQ(2, x.first_set());
EXPECT_EQ(true, x.get(N-4));
2018-11-02 18:29:18 -03:00
EXPECT_EQ(false, x.empty());
x.clearall();
EXPECT_EQ(-1, x.first_set());
EXPECT_EQ(false, x.get(N-4));
2018-11-02 18:29:18 -03:00
EXPECT_EQ(true, x.empty());
EXPECT_EQ(0, x.count());
2018-11-02 18:29:18 -03:00
}
TEST(Bitmask, SetAll31) { bitmask_setall<31>(); }
TEST(Bitmask, SetAll32) { bitmask_setall<32>(); }
TEST(Bitmask, SetAll33) { bitmask_setall<33>(); }
TEST(Bitmask, SetAll47) { bitmask_setall<47>(); }
TEST(Bitmask, SetAll48) { bitmask_setall<48>(); }
TEST(Bitmask, SetAll49) { bitmask_setall<49>(); }
TEST(Bitmask, SetAll63) { bitmask_setall<63>(); }
TEST(Bitmask, SetAll64) { bitmask_setall<64>(); }
TEST(Bitmask, SetAll65) { bitmask_setall<65>(); }
template<int N>
void bitmask_assignment(void)
{
Bitmask<N> x;
2018-11-02 18:29:18 -03:00
x.set(0);
x.set(5);
x.set(6);
x.set(N-1);
2018-11-02 18:29:18 -03:00
Bitmask<N> y;
2018-11-02 18:29:18 -03:00
y = x;
EXPECT_EQ(true, x == y);
2018-11-02 18:29:18 -03:00
x.clear(0);
EXPECT_EQ(true, y.get(0));
EXPECT_EQ(true, y.get(5));
EXPECT_EQ(true, y.get(6));
EXPECT_EQ(true, y.get(N-1));
}
TEST(Bitmask, Assignment31) { bitmask_assignment<31>(); }
TEST(Bitmask, Assignment32) { bitmask_assignment<32>(); }
TEST(Bitmask, Assignment33) { bitmask_assignment<33>(); }
TEST(Bitmask, Assignment47) { bitmask_assignment<47>(); }
TEST(Bitmask, Assignment48) { bitmask_assignment<48>(); }
TEST(Bitmask, Assignment49) { bitmask_assignment<49>(); }
TEST(Bitmask, Assignment63) { bitmask_assignment<63>(); }
TEST(Bitmask, Assignment64) { bitmask_assignment<64>(); }
TEST(Bitmask, Assignment65) { bitmask_assignment<65>(); }
2018-11-02 18:29:18 -03:00
AP_GTEST_PANIC()
2018-11-02 18:29:18 -03:00
AP_GTEST_MAIN()