/* * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD * * SPDX-License-Identifier: MIT */ /* UnitTest for M5UnitComponent */ #include #include #include "unit_dummy.hpp" TEST(Component, Children) { m5::unit::UnitDummy u0, u1, u2, u3; // EXPECT_FALSE(u0.hasParent()); EXPECT_FALSE(u0.hasSiblings()); EXPECT_FALSE(u0.hasChildren()); EXPECT_EQ(0U, u0.childrenSize()); EXPECT_FALSE(u0.add(u1, 0)); // add 1 auto cfg = u0.component_config(); cfg.max_children = 1; 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)); 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()); // add 2 cfg = u0.component_config(); cfg.max_children = 2; u0.component_config(cfg); EXPECT_LT(u2.channel(), 0); EXPECT_FALSE(u0.add(u2, 0)); // same channel (failed) EXPECT_LT(u2.channel(), 0); EXPECT_TRUE(u0.add(u2, 3)); 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); EXPECT_FALSE(u0.add(u3, 2)); // max = 2 (failed) EXPECT_LT(u3.channel(), 0); // 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 m5::unit::UnitDummy* ptr[] = {&u1, &u2}; size_t i = 0; 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"); }