-
Notifications
You must be signed in to change notification settings - Fork 89
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
OmarDuran
wants to merge
19
commits into
develop
Choose a base branch
from
test/omarduran/unitest_rttypes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8c05d0f
test: Add unit tests for RTType.hpp
OmarDuran a7cceae
style: update testRTTypes.cpp
OmarDuran 46f72d3
refactor: test both base and derived types
OmarDuran c2be66f
refactor: making derived class final
OmarDuran 33c2815
test: adding test for geos wrapper
OmarDuran 933899d
Merge branch 'develop' into test/omarduran/unitest_rttypes
OmarDuran afb3081
test: Add unit tests for RTType.hpp
OmarDuran c6c3ac3
style: update testRTTypes.cpp
OmarDuran e0bdf81
refactor: test both base and derived types
OmarDuran 665e1cb
refactor: making derived class final
OmarDuran 78353b1
test: adding test for geos wrapper
OmarDuran d0099a1
Merge branch 'test/omarduran/unitest_rttypes' of https://github.com/G…
OmarDuran c354e77
Merge branch 'develop' into test/omarduran/unitest_rttypes
OmarDuran 084ddcd
Merge branch 'develop' into test/omarduran/unitest_rttypes
OmarDuran 3460daf
refactor: removing final keyword from Wrapper.hpp
OmarDuran e9d56f8
style: linter testRTTypes.cpp
OmarDuran 0aeca31
Merge branch 'develop' into test/omarduran/unitest_rttypes
OmarDuran 7ec6018
Merge branch 'develop' into test/omarduran/unitest_rttypes
OmarDuran 1c08e81
refactor: mock classes for failing apple clang dynamic cast
OmarDuran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/coreComponents/codingUtilities/tests/testRTTypes.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "codingUtilities/RTTypes.hpp" | ||
#include "dataRepository/Group.hpp" | ||
#include "dataRepository/Wrapper.hpp" | ||
|
||
// TPL includes | ||
#include <gtest/gtest.h> | ||
#include <conduit.hpp> | ||
|
||
using namespace geos; | ||
using namespace dataRepository; | ||
|
||
// Mock classes to test dynamic casting | ||
class Base { | ||
public: | ||
virtual ~Base() = default; // Needed for RTTI | ||
}; | ||
|
||
class Derived final : public Base { | ||
public: | ||
void show() { | ||
std::cout << "Derived class method." << std::endl; | ||
} | ||
}; | ||
|
||
// Test for dynamicCast with pointer | ||
TEST(DynamicCastTests, Pointer_Casting_Success) { | ||
Base* base = new Derived(); | ||
Derived* derived = geos::dynamicCast<Derived*>(base); | ||
ASSERT_NE(derived, nullptr) << "Expected successful cast from Base to Derived."; | ||
delete base; // Clean up allocated memory | ||
} | ||
|
||
TEST(DynamicCastTests, Pointer_Casting_Failure) { | ||
Base* base = new Base(); | ||
Derived* derived = geos::dynamicCast<Derived*>(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) { | ||
Derived derived; | ||
Base& base_ref = derived; | ||
Derived& derived_ref = geos::dynamicCast<Derived&>(base_ref); | ||
ASSERT_EQ(&derived_ref, &derived) << "Expected successful cast from Base to Derived."; | ||
} | ||
|
||
TEST(DynamicCastTests, Reference_Casting_Failure) { | ||
Base base; | ||
Base& base_ref = base; | ||
|
||
Base& derived_base_ref = geos::dynamicCast<Base&>(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(); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need these 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the wrapper constructor nees a group... But what's the conduit node for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah... you need to make a Group.. okay I was hoping we could avoid that include.