Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions tests/ut/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ set(A2A3_RUNTIME_DIR ${CMAKE_SOURCE_DIR}/../../../src/a2a3/runtime/tensormap_and
set(A2A3_STUB_SOURCES ${CMAKE_SOURCE_DIR}/stubs/test_stubs.cpp)
set(A2A3_RUNTIME_SOURCES
${A2A3_RUNTIME_DIR}/pto_ring_buffer.cpp
${A2A3_RUNTIME_DIR}/pto_shared_memory.cpp
${A2A3_RUNTIME_DIR}/pto_scheduler.cpp
${A2A3_RUNTIME_DIR}/shared/pto_shared_memory.cpp
${A2A3_RUNTIME_DIR}/scheduler/pto_scheduler.cpp
${A2A3_RUNTIME_DIR}/pto_tensormap.cpp
)

Expand Down Expand Up @@ -230,6 +230,48 @@ add_task_interface_test(test_child_memory types/test_child_memory.cpp)
# ---------------------------------------------------------------------------
add_a2a3_test(test_a2a3_fatal a2a3/test_a2a3_fatal.cpp)

# PTO2 runtime-linked tests
add_a2a3_runtime_test(test_task_allocator
SOURCES a2a3/test_task_allocator.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_dep_list_pool
SOURCES a2a3/test_dep_list_pool.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_scheduler_state
SOURCES a2a3/test_scheduler_state.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_task_state
SOURCES a2a3/test_task_state.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_ready_queue
SOURCES a2a3/test_ready_queue.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_shared_memory
SOURCES a2a3/test_shared_memory.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_a2a3_tensormap
SOURCES a2a3/test_tensormap.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_fanin_pool
SOURCES a2a3/test_fanin_pool.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_spsc_queue
SOURCES a2a3/test_spsc_queue.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)
add_a2a3_runtime_test(test_wiring
SOURCES a2a3/test_wiring.cpp
EXTRA_SOURCES ${A2A3_RUNTIME_SOURCES}
)

# ---------------------------------------------------------------------------
# A5 tests (src/a5/runtime/tensormap_and_ringbuffer/)
# ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions tests/ut/cpp/a2a3/test_a2a3_fatal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
* See LICENSE in the root of the software repository for the full text of the License.
* -----------------------------------------------------------------------------------------------------------
*/
/**
* Unit tests for PTO2 A2A3 fatal error handling.
*
* Tests API short-circuit after fatal state, explicit fatal routing,
* and allocation with invalid arguments.
*/

#include <gtest/gtest.h>

Expand Down
168 changes: 168 additions & 0 deletions tests/ut/cpp/a2a3/test_dep_list_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) PyPTO Contributors.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
* -----------------------------------------------------------------------------------------------------------
*/
/**
* Unit tests for PTO2DepListPool from pto_ring_buffer.h
*
* Tests dependency list pool allocation, prepend chaining, overflow detection,
* tail advancement, and high-water mark tracking.
*
* Design contracts:
*
* - advance_tail(new_tail) only advances if new_tail > tail; it does
* not validate new_tail <= top. Caller contract (monotonic,
* top-bounded).
*
* - The list terminator is literal nullptr. base[0] is a normal pool entry;
* init clearing it is incidental, not an invariant.
*/

#include <gtest/gtest.h>

#include <atomic>
#include <cstring>
#include <vector>

#include "pto_ring_buffer.h"

// =============================================================================
// Fixture
// =============================================================================

class DepListPoolTest : public ::testing::Test {
protected:
static constexpr int32_t POOL_CAP = 8;
PTO2DepListEntry entries[POOL_CAP]{};
std::atomic<int32_t> error_code{PTO2_ERROR_NONE};
PTO2DepListPool pool{};

void SetUp() override {
std::memset(entries, 0, sizeof(entries));
error_code.store(PTO2_ERROR_NONE);
pool.init(entries, POOL_CAP, &error_code);
}
};

// =============================================================================
// Normal path
// =============================================================================

TEST_F(DepListPoolTest, InitialState) {
EXPECT_EQ(pool.used(), 0);
EXPECT_EQ(pool.available(), POOL_CAP);
}

TEST_F(DepListPoolTest, SingleAlloc) {
PTO2DepListEntry *entry = pool.alloc();
ASSERT_NE(entry, nullptr);
EXPECT_EQ(pool.used(), 1);
EXPECT_EQ(pool.available(), POOL_CAP - 1);
}

TEST_F(DepListPoolTest, OverflowDetection) {
for (int i = 0; i < POOL_CAP; i++) {
PTO2DepListEntry *e = pool.alloc();
ASSERT_NE(e, nullptr) << "Unexpected failure at alloc " << i;
}
EXPECT_EQ(pool.used(), POOL_CAP);
EXPECT_EQ(pool.available(), 0);

PTO2DepListEntry *overflow = pool.alloc();
EXPECT_EQ(overflow, nullptr);
EXPECT_EQ(error_code.load(), PTO2_ERROR_DEP_POOL_OVERFLOW);
}

// Prepend builds LIFO linked list: verify each slot_state pointer.
TEST_F(DepListPoolTest, PrependChainCorrectness) {
PTO2TaskSlotState slots[5]{};
PTO2DepListEntry *head = nullptr;

for (int i = 0; i < 5; i++) {
head = pool.prepend(head, &slots[i]);
ASSERT_NE(head, nullptr);
}

// LIFO order: head -> slots[4] -> slots[3] -> ... -> slots[0] -> nullptr.
PTO2DepListEntry *cur = head;
for (int i = 4; i >= 0; i--) {
ASSERT_NE(cur, nullptr);
EXPECT_EQ(cur->slot_state, &slots[i]) << "Entry " << (4 - i) << " should point to slots[" << i << "]";
cur = cur->next;
}
EXPECT_EQ(cur, nullptr) << "Chain should terminate with nullptr";
}

TEST_F(DepListPoolTest, AdvanceTail) {
for (int i = 0; i < 4; i++) {
pool.alloc();
}
EXPECT_EQ(pool.used(), 4);
EXPECT_EQ(pool.available(), POOL_CAP - 4);

pool.advance_tail(4);
EXPECT_EQ(pool.used(), 1);
EXPECT_EQ(pool.available(), POOL_CAP - 1);
}

TEST_F(DepListPoolTest, AdvanceTailBackwardsNoop) {
pool.alloc();
pool.alloc();
pool.advance_tail(3);
int32_t used_after = pool.used();

pool.advance_tail(2);
EXPECT_EQ(pool.used(), used_after);

pool.advance_tail(3);
EXPECT_EQ(pool.used(), used_after);
}

TEST_F(DepListPoolTest, HighWaterAccuracy) {
for (int i = 0; i < 5; i++)
pool.alloc();
EXPECT_EQ(pool.high_water, 5);

pool.advance_tail(4);
EXPECT_EQ(pool.high_water, 5) << "High water never decreases";

for (int i = 0; i < 3; i++)
pool.alloc();
EXPECT_GE(pool.high_water, 5);
}

// =============================================================================
// Boundary conditions
// =============================================================================

// Prepend chain integrity under pool exhaustion: chain must be walkable.
TEST_F(DepListPoolTest, PrependUnderExhaustion) {
PTO2TaskSlotState slots[POOL_CAP]{};
PTO2DepListEntry *head = nullptr;

int count = 0;
while (count < POOL_CAP + 5) {
PTO2DepListEntry *new_head = pool.prepend(head, &slots[count % POOL_CAP]);
if (!new_head) break;
head = new_head;
count++;
}

int walk = 0;
PTO2DepListEntry *cur = head;
while (cur) {
walk++;
cur = cur->next;
if (walk > count + 1) {
FAIL() << "Chain has cycle -- walked more entries than allocated";
break;
}
}
EXPECT_EQ(walk, count);
}
Loading
Loading