Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add unit tests for RTType.hpp #3452

Draft
wants to merge 19 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions src/coreComponents/codingUtilities/tests/BaseClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// BaseClass.cpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#include "BaseClass.hpp"

BaseClass::BaseClass() {

};

BaseClass::~BaseClass() {

};
20 changes: 20 additions & 0 deletions src/coreComponents/codingUtilities/tests/BaseClass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// BaseClass.hpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#ifndef BaseClass_hpp
#define BaseClass_hpp

#include <stdio.h>

class BaseClass
{
public:
explicit BaseClass();
virtual ~BaseClass();
};

#endif /* BaseClass_hpp */
17 changes: 14 additions & 3 deletions src/coreComponents/codingUtilities/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
set( testSources
testGeosxTraits.cpp
testParsing.cpp
testRTTypes.cpp
testUtilities.cpp )

set( dependencyList codingUtilities ${parallelDeps} )
set(mock_object_headers
BaseClass.hpp
DerivedFinalClass.hpp
)

set(mock_object_impl
BaseClass.cpp
DerivedFinalClass.cpp
)

set( dependencyList codingUtilities ${parallelDeps})

geos_decorate_link_dependencies( LIST decoratedDependencies
DEPENDENCIES ${dependencyList} )
Expand All @@ -13,9 +24,9 @@ foreach( test ${testSources} )

get_filename_component( test_name ${test} NAME_WE )
blt_add_executable( NAME ${test_name}
SOURCES ${test}
SOURCES ${test} ${mock_object_headers} ${mock_object_impl}
OUTPUT_DIR ${TEST_OUTPUT_DIRECTORY}
DEPENDS_ON ${decoratedDependencies} gtest )
DEPENDS_ON ${decoratedDependencies} gtest dataRepository )

geos_add_test( NAME ${test_name}
COMMAND ${test_name} )
Expand Down
11 changes: 11 additions & 0 deletions src/coreComponents/codingUtilities/tests/DerivedClassFinal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// DerivedClassFinal.cpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#include "DerivedClassFinal.hpp"

Derived::Derived() = default;

23 changes: 23 additions & 0 deletions src/coreComponents/codingUtilities/tests/DerivedClassFinal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// DerivedClassFinal.hpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#ifndef DerivedClassFinal_hpp
#define DerivedClassFinal_hpp

#include <stdio.h>

class Derived final : public Base
{
public:
explicit Derived();

virtual ~Derived() noexcept override {

};
};

#endif /* DerivedClassFinal_hpp */
10 changes: 10 additions & 0 deletions src/coreComponents/codingUtilities/tests/DerivedFinalClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// DerivedFinalClass.cpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#include "DerivedFinalClass.hpp"

DerivedFinalClass::DerivedFinalClass() = default;
24 changes: 24 additions & 0 deletions src/coreComponents/codingUtilities/tests/DerivedFinalClass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// DerivedFinalClass.hpp
// testRTTypes
//
// Created by Omar Duran on 12/16/24.
//

#ifndef DerivedFinalClass_hpp
#define DerivedFinalClass_hpp

#include <stdio.h>
#include "BaseClass.hpp"

class DerivedFinalClass final : public BaseClass
{
public:
explicit DerivedFinalClass();

virtual ~DerivedFinalClass() noexcept override {

};
};

#endif /* DerivedFinalClass_hpp */
174 changes: 174 additions & 0 deletions src/coreComponents/codingUtilities/tests/testRTTypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 Total, S.A
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/

#include <iostream>
#include "codingUtilities/RTTypes.hpp"
#include "dataRepository/Group.hpp"
#include "dataRepository/Wrapper.hpp"

#include <gtest/gtest.h>

// TPL includes
#include <conduit.hpp>

// Mock classes to test dynamic casting
#include "BaseClass.hpp"
#include "DerivedFinalClass.hpp"

using namespace geos;
using namespace dataRepository;

// Test for dynamicCast with pointer
TEST( DynamicCastTests, Pointer_Casting_Success )
{
BaseClass * base = new DerivedFinalClass();
DerivedFinalClass * derived = geos::dynamicCast< DerivedFinalClass * >( base );
ASSERT_NE( derived, nullptr ) << "Expected successful cast from Base to Derived.";
delete base; // Clean up allocated memory
}

TEST( DynamicCastTests, Pointer_Casting_Failure )
{
BaseClass * base = new BaseClass();
DerivedFinalClass * derived = geos::dynamicCast< DerivedFinalClass * >( base );
ASSERT_EQ( derived, nullptr ) << "Expected nullptr due to failed cast from Base to Derived.";
delete base; // Clean up allocated memory
}

// Test for dynamicCast with reference
TEST( DynamicCastTests, Reference_Casting_Success )
{
DerivedFinalClass derived;
BaseClass & base_ref = derived;
DerivedFinalClass & derived_ref = geos::dynamicCast< DerivedFinalClass & >( base_ref );
ASSERT_EQ( &derived_ref, &derived ) << "Expected successful cast from Base to Derived.";
}

TEST( DynamicCastTests, Reference_Casting_Failure )
{
BaseClass base;
BaseClass & base_ref = base;

BaseClass & derived_base_ref = geos::dynamicCast< BaseClass & >( base_ref );
ASSERT_EQ( &derived_base_ref, &base ) << "Expected successful cast from Base to Base.";

}


//// Typed test for geos wrapper
//template< typename T >
//class WrapperMock : public ::testing::Test
//{
//public:
// WrapperMock():
// m_node(),
// m_group( "root", m_node ),
// m_wrapper( "wrapper", m_group ),
// m_wrapperBase( m_wrapper )
// {}
//
// void testDynamicCastWithPointer( )
// {
// {
// WrapperBase * base_pointer = &m_wrapperBase;
// Wrapper< T > * derived = geos::dynamicCast< Wrapper< T > * >( base_pointer );
// ASSERT_NE( derived, nullptr ) << "Expected successful cast from Base to Derived.";
// }
// {
// WrapperBase * base_pointer = &m_wrapperBase;
// WrapperBase * derived = geos::dynamicCast< WrapperBase * >( base_pointer );
// ASSERT_NE( derived, nullptr ) << "Expected successful cast from Base to Base.";
// }
// {
// Wrapper< T > * defived_pointer = &m_wrapper;
// Wrapper< T > * derived = geos::dynamicCast< Wrapper< T > * >( defived_pointer );
// ASSERT_NE( derived, nullptr ) << "Expected successful cast from Derived to Derived.";
// }
// }
//
// void testDynamicCastWithReference( )
// {
// {
// WrapperBase & base_reference = m_wrapperBase;
// Wrapper< T > & derived = geos::dynamicCast< Wrapper< T > & >( base_reference );
// ASSERT_EQ( &derived, &base_reference ) << "Expected successful cast from Base to Derived.";
// }
// {
// WrapperBase & base_reference = m_wrapperBase;
// WrapperBase & derived = geos::dynamicCast< WrapperBase & >( base_reference );
// ASSERT_EQ( &derived, &base_reference ) << "Expected successful cast from Base to Base.";
// }
// {
// Wrapper< T > & defived_reference = m_wrapper;
// Wrapper< T > & derived = geos::dynamicCast< Wrapper< T > & >( defived_reference );
// ASSERT_EQ( &derived, &defived_reference ) << "Expected successful cast from Derived to Derived.";
// }
// }
//
//
//private:
// conduit::Node m_node;
// Group m_group;
// Wrapper< T > m_wrapper;
// WrapperBase & m_wrapperBase;
//};
//
//using WrapperMockTypes = ::testing::Types< int, array1d< real64 >, array1d< array1d< int > >, void *, std::function< void (void) > >;
//TYPED_TEST_SUITE( WrapperMock, WrapperMockTypes, );
//
//TYPED_TEST( WrapperMock, DynamicCastWithPointer )
//{
// this->testDynamicCastWithPointer( );
//}
//
//TYPED_TEST( WrapperMock, DynamicCastWithReference )
//{
// this->testDynamicCastWithReference( );
//}
//
//// Test Regex constructor
//TEST( RegexTests, Constructor )
//{
// geos::Regex regex( "^[0-9]+$", "Input must be a number." );
// ASSERT_EQ( regex.m_regexStr, "^[0-9]+$" ) << "Regex string is incorrect.";
// ASSERT_EQ( regex.m_formatDescription, "Input must be a number." ) << "Format description is incorrect.";
//}
//
//TEST( RtTypesTests, GetTypeName )
//{
// {
// std::type_index typeIndex( typeid(Base));
// auto typeName = geos::rtTypes::getTypeName( typeIndex );
// EXPECT_EQ( typeName, std::string( "Base" )); // Expected Base
// }
// {
// std::type_index typeIndex( typeid(Derived));
// auto typeName = geos::rtTypes::getTypeName( typeIndex );
// EXPECT_EQ( typeName, std::string( "Derived" )); // Expected Derived
// }
//}
//
//// Additional tests to validate the functionality of getTypeRegex
//TEST( RtTypesTests, GetTypeRegex_Default )
//{
// geos::Regex regex = geos::rtTypes::getTypeRegex< int >(); // Assuming int has a default regex defined
// ASSERT_NE( regex.m_regexStr.empty(), true ) << "Expected non-empty regex for int.";
//}

int main( int argc, char * *argv )
{
testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}
2 changes: 1 addition & 1 deletion src/coreComponents/dataRepository/Wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace dataRepository
* @tparam T is any type that is to be wrapped by Wrapper
*/
template< typename T >
class Wrapper final : public WrapperBase
class Wrapper : public WrapperBase
{
public:

Expand Down
Loading