test/nextgen/tests/poly_test.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 4 | 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 | 2 | std::array<uint8_t, pad_size> padding{}; | |
| 35 | int m_key; | ||
| 36 | |||
| 37 | 5 | PolyTestDeriveA(int key) : m_key{key} { | |
| 38 | 2 | constructed++; | |
| 39 | 3 | } | |
| 40 | |||
| 41 | 4 | PolyTestId id() const override { | |
| 42 | 4 | return PolyTestId::A; | |
| 43 | } | ||
| 44 | |||
| 45 | 2 | void assign_all() override { | |
| 46 | 2 | std::fill(std::begin(padding), std::end(padding), 0x01); | |
| 47 | 2 | } | |
| 48 | 2 | bool verify_match() const override { | |
| 49 | 2 | return std::all_of( | |
| 50 | 2 | std::begin(padding), // | |
| 51 | 2 | std::end(padding), // | |
| 52 | 16 | [](uint8_t v) { return v == 0x01; }); | |
| 53 | } | ||
| 54 | |||
| 55 | 3 | ~PolyTestDeriveA() override { | |
| 56 | 2 | destroyed++; | |
| 57 | 3 | } | |
| 58 | }; | ||
| 59 | |||
| 60 | class PolyTestDeriveB final : public PolyTestBase { | ||
| 61 | public: | ||
| 62 | static inline int constructed = 0; | ||
| 63 | static inline int destroyed = 0; | ||
| 64 | 2 | std::array<uint8_t, pad_size * 2> padding{}; | |
| 65 | int m_key; | ||
| 66 | |||
| 67 | 5 | PolyTestDeriveB(int key) : m_key{key} { | |
| 68 | 2 | constructed++; | |
| 69 | 3 | } | |
| 70 | |||
| 71 | 4 | PolyTestId id() const override { | |
| 72 | 4 | return PolyTestId::B; | |
| 73 | } | ||
| 74 | |||
| 75 | 2 | void assign_all() override { | |
| 76 | 2 | std::fill(std::begin(padding), std::end(padding), 0x02); | |
| 77 | 2 | } | |
| 78 | 2 | bool verify_match() const override { | |
| 79 | 2 | return std::all_of( | |
| 80 | 2 | std::begin(padding), // | |
| 81 | 2 | std::end(padding), // | |
| 82 | 32 | [](uint8_t v) { return v == 0x02; }); | |
| 83 | } | ||
| 84 | |||
| 85 | 3 | ~PolyTestDeriveB() override { | |
| 86 | 2 | destroyed++; | |
| 87 | 3 | } | |
| 88 | }; | ||
| 89 | |||
| 90 | class PolyFixture : public ::testing::Test { | ||
| 91 | public: | ||
| 92 | 2 | PolyFixture() = default; | |
| 93 | }; | ||
| 94 | |||
| 95 | class PolyDestructorTest : public PolyFixture { | ||
| 96 | public: | ||
| 97 | 2 | void TestBody() override { | |
| 98 | 2 | PolyTestDeriveA::destroyed = 0; | |
| 99 | 2 | PolyTestDeriveB::destroyed = 0; | |
| 100 | 2 | const int key_a = 12; | |
| 101 | 2 | const int key_b = 21; | |
| 102 | |||
| 103 | // Scope lifetime objects under test | ||
| 104 | { | ||
| 105 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | Poly<PolyTestBase> a = kai::test::make_poly<PolyTestDeriveA>(key_a); |
| 106 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | Poly<PolyTestBase> b = kai::test::make_poly<PolyTestDeriveB>(key_b); |
| 107 | |||
| 108 | // Cast check | ||
| 109 |
2/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | auto& ref_a = dynamic_cast<PolyTestDeriveA&>(*a); |
| 110 |
2/8✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | auto& ref_b = dynamic_cast<PolyTestDeriveB&>(*b); |
| 111 | |||
| 112 | // Check derived type using different accessors | ||
| 113 |
6/20✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 2 times.
|
2 | ASSERT_EQ(a->id(), PolyTestId::A); |
| 114 |
6/20✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 2 times.
|
2 | ASSERT_EQ(b->id(), PolyTestId::B); |
| 115 |
5/18✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 2 times.
|
2 | ASSERT_EQ(ref_a.id(), PolyTestId::A); |
| 116 |
5/18✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 2 times.
|
2 | ASSERT_EQ(ref_b.id(), PolyTestId::B); |
| 117 | |||
| 118 | // Check assigned key values | ||
| 119 |
4/16✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
|
2 | ASSERT_EQ(ref_a.m_key, key_a); |
| 120 |
4/16✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
|
2 | ASSERT_EQ(ref_b.m_key, key_b); |
| 121 | |||
| 122 | // Assign values to different sized derived types | ||
| 123 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | a->assign_all(); |
| 124 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | 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 | 2 | } | |
| 130 | |||
| 131 | // Out of scope | ||
| 132 | // Check constructor calls | ||
| 133 |
3/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
|
2 | ASSERT_EQ(PolyTestDeriveA::constructed, 1); |
| 134 |
3/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
|
2 | ASSERT_EQ(PolyTestDeriveB::constructed, 1); |
| 135 | |||
| 136 | // Check destructor calls | ||
| 137 |
3/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
|
2 | ASSERT_EQ(PolyTestDeriveA::destroyed, 1); |
| 138 |
3/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
|
2 | ASSERT_EQ(PolyTestDeriveB::destroyed, 1); |
| 139 | 2 | } | |
| 140 | }; | ||
| 141 | |||
| 142 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 | const auto poly_tests_setup = TestRegistry::register_setup([]() { |
| 143 | 3 | const char* test_suite_name = "Poly"; | |
| 144 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
3 | const std::string test_name = "Poly_test"; |
| 145 | |||
| 146 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
5 | KAI_REGISTER_TEST(PolyFixture, PolyDestructorTest, test_suite_name, test_name.c_str()); |
| 147 | 3 | }); | |
| 148 | |||
| 149 | } // namespace | ||
| 150 | } // namespace kai::test | ||
| 151 |