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
1 change: 1 addition & 0 deletions projects/rocsparse/clients/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ set(ROCSPARSE_CLIENTS_TESTINGS
../testings/testing_check_matrix_hyb.cpp
../testings/testing_check_spmat.cpp
../testings/testing_bsrpad_value.cpp
rocsparse_test_listeners.cpp
)


Expand Down
272 changes: 272 additions & 0 deletions projects/rocsparse/clients/tests/rocsparse_test_listeners.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*! \file */
/* ************************************************************************
* Copyright (C) 2025 Advanced Micro Devices, Inc. All rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ************************************************************************ */
#include "rocsparse_test_listeners.hpp"

rocsparse_clients::configurable_event_listener::configurable_event_listener(
testing::TestEventListener* theEventListener)
: eventListener(theEventListener)
, showTestCases(true)
, showTestNames(true)
, showSuccesses(true)
, showInlineFailures(true)
, showEnvironment(true)
{
}

rocsparse_clients::configurable_event_listener::~configurable_event_listener()
{
delete eventListener;
}

void rocsparse_clients::configurable_event_listener::OnTestProgramStart(
const testing::UnitTest& unit_test)
{
eventListener->OnTestProgramStart(unit_test);
}

void rocsparse_clients::configurable_event_listener::OnTestIterationStart(
const testing::UnitTest& unit_test, int iteration)
{
eventListener->OnTestIterationStart(unit_test, iteration);
}

void rocsparse_clients::configurable_event_listener::OnEnvironmentsSetUpStart(
const testing::UnitTest& unit_test)
{
if(showEnvironment)
{
eventListener->OnEnvironmentsSetUpStart(unit_test);
}
}

void rocsparse_clients::configurable_event_listener::OnEnvironmentsSetUpEnd(
const testing::UnitTest& unit_test)
{
if(showEnvironment)
{
eventListener->OnEnvironmentsSetUpEnd(unit_test);
}
}

void rocsparse_clients::configurable_event_listener::OnTestCaseStart(
const testing::TestCase& test_case)
{
if(showTestCases)
{
eventListener->OnTestCaseStart(test_case);
}
}

void rocsparse_clients::configurable_event_listener::OnTestStart(const testing::TestInfo& test_info)
{
if(showTestNames)
{
eventListener->OnTestStart(test_info);
}
}

void rocsparse_clients::configurable_event_listener::OnTestPartResult(
const testing::TestPartResult& result)
{
eventListener->OnTestPartResult(result);
}

void rocsparse_clients::configurable_event_listener::OnTestEnd(const testing::TestInfo& test_info)
{
if(test_info.result()->Failed() ? showInlineFailures : showSuccesses)
{
eventListener->OnTestEnd(test_info);
}
}

void rocsparse_clients::configurable_event_listener::OnTestCaseEnd(
const testing::TestCase& test_case)
{
if(showTestCases)
{
eventListener->OnTestCaseEnd(test_case);
}
}

void rocsparse_clients::configurable_event_listener::OnEnvironmentsTearDownStart(
const testing::UnitTest& unit_test)
{
if(showEnvironment)
{
eventListener->OnEnvironmentsTearDownStart(unit_test);
}
}

void rocsparse_clients::configurable_event_listener::OnEnvironmentsTearDownEnd(
const testing::UnitTest& unit_test)
{
if(showEnvironment)
{
eventListener->OnEnvironmentsTearDownEnd(unit_test);
}
}

void rocsparse_clients::configurable_event_listener::OnTestIterationEnd(
const testing::UnitTest& unit_test, int iteration)
{
eventListener->OnTestIterationEnd(unit_test, iteration);
}

void rocsparse_clients::configurable_event_listener::OnTestProgramEnd(
const testing::UnitTest& unit_test)
{
eventListener->OnTestProgramEnd(unit_test);
}

void rocsparse_clients::stream_redirector::redirect()
{
// Save original buffers
this->m_old_cout_buf = std::cout.rdbuf();
this->m_old_cerr_buf = std::cerr.rdbuf();

// Redirect to our ostringstream
std::cout.rdbuf(this->m_stream.rdbuf());
std::cerr.rdbuf(this->m_stream.rdbuf());
}

void rocsparse_clients::stream_redirector::restore()
{
// Restore original buffers
std::cout.rdbuf(this->m_old_cout_buf);
std::cerr.rdbuf(this->m_old_cerr_buf);
}

std::ostringstream& rocsparse_clients::stream_redirector::get_stream()
{
return this->m_stream;
}

void rocsparse_clients::stream_redirector::clear()
{
this->m_stream.clear();
}

rocsparse_clients::output_redirect_listener::output_redirect_listener(
::testing::TestEventListener* listener)
: m_default_listener(listener)
{
}

rocsparse_clients::output_redirect_listener::~output_redirect_listener()
{
delete this->m_default_listener;
}

void rocsparse_clients::output_redirect_listener::OnTestProgramStart(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnTestProgramStart(unit_test);
}

void rocsparse_clients::output_redirect_listener::OnTestIterationStart(
const ::testing::UnitTest& unit_test, int iteration)
{
this->m_default_listener->OnTestIterationStart(unit_test, iteration);
}

void rocsparse_clients::output_redirect_listener::OnEnvironmentsSetUpStart(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnEnvironmentsSetUpStart(unit_test);
}

void rocsparse_clients::output_redirect_listener::OnEnvironmentsSetUpEnd(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnEnvironmentsSetUpEnd(unit_test);
}

void rocsparse_clients::output_redirect_listener::OnTestCaseStart(
const ::testing::TestCase& test_case)
{
this->m_default_listener->OnTestCaseStart(test_case);
}

void rocsparse_clients::output_redirect_listener::OnTestStart(const ::testing::TestInfo& test_info)
{
// Clear and redirect streams before each test
this->m_redirector.clear();
this->m_redirector.redirect();
this->m_default_listener->OnTestStart(test_info);
}

void rocsparse_clients::output_redirect_listener::OnTestPartResult(
const ::testing::TestPartResult& test_part_result)
{
this->m_default_listener->OnTestPartResult(test_part_result);
}

void rocsparse_clients::output_redirect_listener::OnTestEnd(const ::testing::TestInfo& test_info)
{
// Restore streams after test
this->m_redirector.restore();

// Check if test failed
if(test_info.result()->Failed())
{
const std::string content = this->m_redirector.get_stream().str();

if(!content.empty())
{
std::cerr << content << std::endl;
}
Comment thread
kliegeois marked this conversation as resolved.
}

this->m_default_listener->OnTestEnd(test_info);
}

void rocsparse_clients::output_redirect_listener::OnTestCaseEnd(
const ::testing::TestCase& test_case)
{
this->m_default_listener->OnTestCaseEnd(test_case);
}

void rocsparse_clients::output_redirect_listener::OnEnvironmentsTearDownStart(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnEnvironmentsTearDownStart(unit_test);
}

void rocsparse_clients::output_redirect_listener::OnEnvironmentsTearDownEnd(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnEnvironmentsTearDownEnd(unit_test);
}

void rocsparse_clients::output_redirect_listener::OnTestIterationEnd(
const ::testing::UnitTest& unit_test, int iteration)
{
this->m_default_listener->OnTestIterationEnd(unit_test, iteration);
}

void rocsparse_clients::output_redirect_listener::OnTestProgramEnd(
const ::testing::UnitTest& unit_test)
{
this->m_default_listener->OnTestProgramEnd(unit_test);
}
Loading
Loading