Files

119 lines
3.0 KiB
C++
Raw Permalink Normal View History

2024-08-08 18:11:43 +09:00
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
UnitTest for M5UnitComponent
2024-05-09 18:51:54 +09:00
*/
#include <gtest/gtest.h>
#include <M5UnitComponent.hpp>
#include "unit_dummy.hpp"
2024-09-25 18:18:05 +09:00
TEST(Component, Children)
{
2024-08-09 18:15:52 +09:00
m5::unit::UnitDummy u0, u1, u2, u3;
2024-05-09 18:51:54 +09:00
//
EXPECT_FALSE(u0.hasParent());
EXPECT_FALSE(u0.hasSiblings());
EXPECT_FALSE(u0.hasChildren());
2024-05-09 18:51:54 +09:00
EXPECT_EQ(0U, u0.childrenSize());
EXPECT_FALSE(u0.add(u1, 0));
// add 1
2024-08-08 18:11:43 +09:00
auto cfg = u0.component_config();
cfg.max_children = 1;
2024-08-08 18:11:43 +09:00
u0.component_config(cfg);
EXPECT_LT(u1.channel(), 0);
EXPECT_TRUE(u0.add(u1, 0));
EXPECT_FALSE(u0.add(u1, 1));
EXPECT_FALSE(u0.add(u2, 1));
2024-05-09 18:51:54 +09:00
EXPECT_FALSE(u0.hasParent());
EXPECT_FALSE(u0.hasSiblings());
EXPECT_TRUE(u0.hasChildren());
EXPECT_EQ(1U, u0.childrenSize());
EXPECT_EQ(0, u1.channel());
EXPECT_TRUE(u1.hasParent());
EXPECT_FALSE(u1.hasSiblings());
EXPECT_FALSE(u1.hasChildren());
2024-05-09 18:51:54 +09:00
// add 2
2024-08-08 18:11:43 +09:00
cfg = u0.component_config();
cfg.max_children = 2;
2024-08-08 18:11:43 +09:00
u0.component_config(cfg);
EXPECT_LT(u2.channel(), 0);
2024-05-09 18:51:54 +09:00
EXPECT_FALSE(u0.add(u2, 0)); // same channel (failed)
EXPECT_LT(u2.channel(), 0);
EXPECT_TRUE(u0.add(u2, 3));
2024-05-09 18:51:54 +09:00
EXPECT_FALSE(u0.hasParent());
EXPECT_FALSE(u0.hasSiblings());
EXPECT_TRUE(u0.hasChildren());
EXPECT_EQ(2U, u0.childrenSize());
EXPECT_TRUE(u1.hasParent());
EXPECT_TRUE(u1.hasSiblings());
EXPECT_FALSE(u1.hasChildren());
EXPECT_TRUE(u2.hasParent());
EXPECT_TRUE(u2.hasSiblings());
EXPECT_FALSE(u2.hasChildren());
EXPECT_EQ(3, u2.channel());
EXPECT_LT(u3.channel(), 0);
2024-05-09 18:51:54 +09:00
EXPECT_FALSE(u0.add(u3, 2)); // max = 2 (failed)
EXPECT_LT(u3.channel(), 0);
2024-05-09 18:51:54 +09:00
// parent
EXPECT_EQ(u1.parent(), &u0);
EXPECT_EQ(u2.parent(), &u0);
EXPECT_EQ(u0.parent(), nullptr);
// existsChild / child
EXPECT_TRUE(u0.existsChild(0));
EXPECT_TRUE(u0.existsChild(3));
EXPECT_FALSE(u0.existsChild(1));
EXPECT_FALSE(u0.existsChild(2));
EXPECT_EQ(u0.child(0), &u1);
EXPECT_EQ(u0.child(3), &u2);
EXPECT_EQ(u0.child(1), nullptr);
// iteration
2024-08-09 18:15:52 +09:00
m5::unit::UnitDummy* ptr[] = {&u1, &u2};
size_t i = 0;
2024-05-09 18:51:54 +09:00
for (auto it = u0.childBegin(); it != u0.childEnd(); ++it) {
EXPECT_EQ(&(*it), ptr[i++]);
}
EXPECT_EQ(i, u0.childrenSize());
}
TEST(Component, DefaultProperties)
{
m5::unit::UnitDummy u;
// category defaults to None
EXPECT_EQ(u.category(), m5::unit::types::category_t::None);
// not in periodic measurement by default
EXPECT_FALSE(u.inPeriodic());
EXPECT_FALSE(u.updated());
// order is 0 before registration
EXPECT_EQ(u.order(), 0U);
// adapter exists but Type::Unknown before assignment
EXPECT_NE(u.adapter(), nullptr);
EXPECT_EQ(u.adapter()->type(), m5::unit::Adapter::Type::Unknown);
// address
EXPECT_EQ(u.address(), m5::unit::DUMMY_I2C_ADDR);
// deviceName
EXPECT_STREQ(u.deviceName(), "UnitDummy");
}