| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include "test/nextgen/common/poly.hpp" |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #include "kai/kai_common.h" |
| 12 | #include "test/nextgen/common/test_registry.hpp" |
| 13 | |
| 14 | namespace kai::test { |
| 15 | namespace { |
| 16 | |
| 17 | enum class PolyTestId { BASE, A, B }; |
| 18 | inline constexpr size_t pad_size = 8; |
| 19 | |
| 20 | class PolyTestBase { |
| 21 | public: |
| 22 | virtual ~PolyTestBase() = default; |
| 23 | virtual PolyTestId id() const { |
| 24 | return PolyTestId::BASE; |
| 25 | } |
| 26 | virtual void assign_all() = 0; |
| 27 | virtual bool verify_match() const = 0; |
| 28 | }; |
| 29 | |
| 30 | class PolyTestDeriveA : public PolyTestBase { |
| 31 | public: |
| 32 | static inline int constructed = 0; |
| 33 | static inline int destroyed = 0; |
| 34 | std::array<uint8_t, pad_size> padding{}; |
| 35 | int m_key; |
| 36 | |
| 37 | PolyTestDeriveA(int key) : m_key{key} { |
| 38 | constructed++; |
| 39 | } |
| 40 | |
| 41 | PolyTestId id() const override { |
| 42 | return PolyTestId::A; |
| 43 | } |
| 44 | |
| 45 | void assign_all() override { |
| 46 | std::fill(std::begin(padding), std::end(padding), 0x01); |
| 47 | } |
| 48 | bool verify_match() const override { |
| 49 | return std::all_of( |
| 50 | std::begin(padding), // |
| 51 | std::end(padding), // |
| 52 | [](uint8_t v) { return v == 0x01; }); |
| 53 | } |
| 54 | |
| 55 | ~PolyTestDeriveA() override { |
| 56 | destroyed++; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | class PolyTestDeriveB final : public PolyTestBase { |
| 61 | public: |
| 62 | static inline int constructed = 0; |
| 63 | static inline int destroyed = 0; |
| 64 | std::array<uint8_t, pad_size * 2> padding{}; |
| 65 | int m_key; |
| 66 | |
| 67 | PolyTestDeriveB(int key) : m_key{key} { |
| 68 | constructed++; |
| 69 | } |
| 70 | |
| 71 | PolyTestId id() const override { |
| 72 | return PolyTestId::B; |
| 73 | } |
| 74 | |
| 75 | void assign_all() override { |
| 76 | std::fill(std::begin(padding), std::end(padding), 0x02); |
| 77 | } |
| 78 | bool verify_match() const override { |
| 79 | return std::all_of( |
| 80 | std::begin(padding), // |
| 81 | std::end(padding), // |
| 82 | [](uint8_t v) { return v == 0x02; }); |
| 83 | } |
| 84 | |
| 85 | ~PolyTestDeriveB() override { |
| 86 | destroyed++; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | class PolyFixture : public ::testing::Test { |
| 91 | public: |
| 92 | PolyFixture() = default; |
| 93 | }; |
| 94 | |
| 95 | class PolyDestructorTest : public PolyFixture { |
| 96 | public: |
| 97 | void TestBody() override { |
| 98 | PolyTestDeriveA::destroyed = 0; |
| 99 | PolyTestDeriveB::destroyed = 0; |
| 100 | const int key_a = 12; |
| 101 | const int key_b = 21; |
| 102 | |
| 103 | // Scope lifetime objects under test |
| 104 | { |
| 105 | Poly<PolyTestBase> a = kai::test::make_poly<PolyTestDeriveA>(key_a); |
| 106 | Poly<PolyTestBase> b = kai::test::make_poly<PolyTestDeriveB>(key_b); |
| 107 | |
| 108 | // Cast check |
| 109 | auto& ref_a = dynamic_cast<PolyTestDeriveA&>(*a); |
| 110 | auto& ref_b = dynamic_cast<PolyTestDeriveB&>(*b); |
| 111 | |
| 112 | // Check derived type using different accessors |
| 113 | ASSERT_EQ(a->id(), PolyTestId::A); |
| 114 | ASSERT_EQ(b->id(), PolyTestId::B); |
| 115 | ASSERT_EQ(ref_a.id(), PolyTestId::A); |
| 116 | ASSERT_EQ(ref_b.id(), PolyTestId::B); |
| 117 | |
| 118 | // Check assigned key values |
| 119 | ASSERT_EQ(ref_a.m_key, key_a); |
| 120 | ASSERT_EQ(ref_b.m_key, key_b); |
| 121 | |
| 122 | // Assign values to different sized derived types |
| 123 | a->assign_all(); |
| 124 | b->assign_all(); |
| 125 | |
| 126 | // Check different sized derived types populated correctly |
| 127 | KAI_ASSERT(ref_a.verify_match()); |
| 128 | KAI_ASSERT(ref_b.verify_match()); |
| 129 | } |
| 130 | |
| 131 | // Out of scope |
| 132 | // Check constructor calls |
| 133 | ASSERT_EQ(PolyTestDeriveA::constructed, 1); |
| 134 | ASSERT_EQ(PolyTestDeriveB::constructed, 1); |
| 135 | |
| 136 | // Check destructor calls |
| 137 | ASSERT_EQ(PolyTestDeriveA::destroyed, 1); |
| 138 | ASSERT_EQ(PolyTestDeriveB::destroyed, 1); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | const auto poly_tests_setup = TestRegistry::register_setup([]() { |
| 143 | const char* test_suite_name = "Poly"; |
| 144 | const std::string test_name = "Poly_test"; |
| 145 | |
| 146 | KAI_REGISTER_TEST(PolyFixture, PolyDestructorTest, test_suite_name, test_name.c_str()); |
| 147 | }); |
| 148 | |
| 149 | } // namespace |
| 150 | } // namespace kai::test |