Added interface file includes for C++ types supported by swig (#1853)

* Added interface file includes for C++ types supported by swig

* Testing swig interface includes

* Inital pass at testing STL access via input file for swig interface file update

* Updated test comparison for keys() return function that swig generates for std::map. Added SIM_test_ip_stl to the trickops yml file

* Updated trick_swig.i to remove redundant includes

* Renamed input file for SIM_test_ip_stL

* Added typemap so swig can convert from C++ std::string pointer to python string

* Commented out some %include .i files. Also commented out %import for unordered_map as not supported by swig3 in the test code.

* Removed duplicated includes and added checking swig version for some .i files.

* Moved std_sstream.i to the right place as the order matters.

* Moved all work in progress .i files in swig 3 to swig 4 block.

* Removed unused files and added proper purposes for header files.

* Updated vector of char16_t and char32_t tests.

---------

Co-authored-by: Hong Chen <hchen99@users.noreply.github.com>
Co-authored-by: mherr <mherr@pop-os.lan>
This commit is contained in:
M-Herr 2025-05-20 15:24:03 -05:00 committed by GitHub
parent ff58ed109c
commit 165c3a690c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 638 additions and 5 deletions

View File

@ -1,9 +1,54 @@
/* include support for STLs */
%include "std_alloc.i"
%include "std_char_traits.i"
%include "std_complex.i"
%include "std_deque.i"
%include "std_except.i"
%include "std_list.i"
%include "std_map.i"
%include "std_multimap.i"
%include "std_multiset.i"
%include "std_pair.i"
%include "std_set.i"
#if SWIG_VERSION >= 0x040000
// std_sstream.i is not fully supported and reliable in swig 3.0.x and is fully supported in swig 4.x
%include "std_sstream.i"
#endif
%include "std_string.i"
%include "std_iostream.i"
%include "std_vector.i"
%include "std_array.i"
%include "std_shared_ptr.i"
%include "std_unique_ptr.i"
%include "std_container.i"
// Both std_queue.i and std_stack.i coming with swig are wrappers for Ruby.
// We need to come up with our own wrappers for these if needed for Python.
//%include "std_queue.i"
//%include "std_stack.i"
#if SWIG_VERSION >= 0x040000
// All following std_unordered_.i files were work in progress in swig 3.0.x and are fully supported in swig 4.x
%include "std_unordered_map.i"
%include "std_unordered_multimap.i"
%include "std_unordered_multiset.i"
%include "std_unordered_set.i"
#endif
%include "std_vectora.i"
#if SWIG_VERSION >= 0x040000
// std_wstring.i was introduced in swig 3.0.8 but its support for Python was incomplete
// std_wstring.i works properly in swig 4.x
%include "std_wstring.i"
#endif
%include "factory.i"
%include "trick/swig/swig_extend_str.i"
@ -24,7 +69,6 @@
%{
#include <sstream>
#include "trick/UnitsMap.hh"
#include "trick/MemoryManager.hh"
#include "trick/reference.h"

View File

@ -2972,11 +2972,11 @@ def main():
test_so.obj.msi['key1'] = 50
test_so.obj.msi['key2'] = 60
test_so.obj.msi['key3'] = 70
TRICK_EXPECT_EQ( test_so.obj.msi.empty(), 0, test_suite , "STL map empty false" )
TRICK_EXPECT_EQ( test_so.obj.msi['key1'], 50, test_suite , "STL map key/data insertion/access" )
TRICK_EXPECT_EQ( str(test_so.obj.msi.keys()), "['key1', 'key2', 'key3']", test_suite , "STL map keys command" )
TRICK_EXPECT_EQ( str(test_so.obj.msi.values()), "[50, 60, 70]", test_suite , "STL map values command" )
TRICK_EXPECT_EQ( list(str(k) for k in test_so.obj.msi.keys()), ['key1', 'key2', 'key3'], test_suite , "STL map keys command" )
TRICK_EXPECT_EQ( test_so.obj.msi.values(), [50, 60, 70], test_suite , "STL map values command" )
TRICK_EXPECT_EQ( test_so.obj.msi.has_key('key1'), 1, test_suite , "STL map has_key true" )
TRICK_EXPECT_EQ( test_so.obj.msi.has_key('key4'), 0, test_suite , "STL map has_key false" )
TRICK_EXPECT_EQ( test_so.obj.msi.size(), 3, test_suite , "STL map size command" )

View File

@ -0,0 +1,172 @@
from trick.unit_test import *
def array_tests(stl_so, test_suite):
test_name = "Char Arrays"
stl_so.stl_ip_test.char_types.array[0] = 'a'
stl_so.stl_ip_test.char_types.array[1] = 'b'
stl_so.stl_ip_test.char_types.array[2] = 'c'
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.char_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.array[0], 'a', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.array[1], 'b', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.array[2], 'c', test_suite, test_name)
try:
test_name = "Char Arrays"
stl_so.stl_ip_test.char_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<char, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "Signed Char Arrays"
stl_so.stl_ip_test.signed_char_types.array[0] = -1
stl_so.stl_ip_test.signed_char_types.array[1] = -2
stl_so.stl_ip_test.signed_char_types.array[2] = -3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.signed_char_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.array[0], -1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.array[1], -2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.array[2], -3, test_suite, test_name)
try:
test_name = "Signed Char Arrays"
stl_so.stl_ip_test.signed_char_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<signed char, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "Unsigned Char Arrays"
stl_so.stl_ip_test.unsigned_char_types.array[0] = 1
stl_so.stl_ip_test.unsigned_char_types.array[1] = 2
stl_so.stl_ip_test.unsigned_char_types.array[2] = 3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.unsigned_char_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.array[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.array[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.array[2], 3, test_suite, test_name)
try:
test_name = "Unsigned Char Arrays"
stl_so.stl_ip_test.unsigned_char_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<unsigned char, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
#TODO: Fix test for char16_t
'''
test_name = "char16_t Arrays"
stl_so.stl_ip_test.char16_t_types.array[0] = stl_so.stl_ip_test.to_char16_t('a')
stl_so.stl_ip_test.char16_t_types.array[1] = stl_so.stl_ip_test.to_char16_t('b')
stl_so.stl_ip_test.char16_t_types.array[2] = stl_so.stl_ip_test.to_char16_t('c')
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.char16_t_types.array), 3, test_suite, test_name)
try:
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char16_t_types.array[0], stl_so.stl_ip_test.to_char16_t('a'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char16_t_types.array[1], stl_so.stl_ip_test.to_char16_t('b'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char16_t_types.array[2], stl_so.stl_ip_test.to_char16_t('c'), test_suite, test_name)
except Exception as e:
print(f"Exception {e}")
try:
test_name = "char16_t Arrays"
stl_so.stl_ip_test.char16_t_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<char16_t, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "char32_t Arrays"
stl_so.stl_ip_test.char32_t_types.array[0] = 'a'
stl_so.stl_ip_test.char32_t_types.array[1] = 'b'
stl_so.stl_ip_test.char32_t_types.array[2] = 'c'
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.char32_t_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char32_t_types.array[0], 'a', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char32_t_types.array[1], 'b', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char32_t_types.array[2], 'c', test_suite, test_name)
try:
test_name = "char32_t Arrays"
stl_so.stl_ip_test.char32_t_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<char32_t, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
'''
test_name = "short int Arrays"
stl_so.stl_ip_test.short_int_types.array[0] = 1
stl_so.stl_ip_test.short_int_types.array[1] = 2
stl_so.stl_ip_test.short_int_types.array[2] = 3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.short_int_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.array[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.array[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.array[2], 3, test_suite, test_name)
try:
test_name = "short int Arrays"
stl_so.stl_ip_test.short_int_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<short int, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "unsigned short int Arrays"
stl_so.stl_ip_test.unsigned_short_int_types.array[0] = 1
stl_so.stl_ip_test.unsigned_short_int_types.array[1] = 2
stl_so.stl_ip_test.unsigned_short_int_types.array[2] = 3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.unsigned_short_int_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.array[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.array[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.array[2], 3, test_suite, test_name)
#Pass invalid type(struct)
try:
test_name = "unsigned short int Arrays"
stl_so.stl_ip_test.unsigned_short_int_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<unsigned short int, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
#Pass invalid type(signed value)
try:
test_name = "unsigned short int Arrays"
stl_so.stl_ip_test.unsigned_short_int_types.array[0] = -1
except Exception as e:
print(f"Expected exception due to passing signed value into std::array<unsigned short int, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "int Arrays"
stl_so.stl_ip_test.int_types.array[0] = 1
stl_so.stl_ip_test.int_types.array[1] = 2
stl_so.stl_ip_test.int_types.array[2] = 3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.int_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.array[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.array[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.array[2], 3, test_suite, test_name)
try:
test_name = "int Arrays"
stl_so.stl_ip_test.int_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<int, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "unsigned int Arrays"
stl_so.stl_ip_test.unsigned_int_types.array[0] = 1
stl_so.stl_ip_test.unsigned_int_types.array[1] = 2
stl_so.stl_ip_test.unsigned_int_types.array[2] = 3
TRICK_EXPECT_EQ(len(stl_so.stl_ip_test.unsigned_int_types.array), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.array[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.array[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.array[2], 3, test_suite, test_name)
#Pass invalid type(struct)
try:
test_name = "unsigned int Arrays"
stl_so.stl_ip_test.unsigned_int_types.array[0] = trick.TestStructure()
except Exception as e:
print(f"Expected exception due to passing TestStruct into std::array<unsigned int, 3> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)

View File

@ -0,0 +1,179 @@
from trick.unit_test import *
def vector_tests(stl_so, test_suite):
test_name = "Char Vectors"
stl_so.stl_ip_test.char_types.vector.push_back('a')
stl_so.stl_ip_test.char_types.vector.push_back('b')
stl_so.stl_ip_test.char_types.vector.push_back('c')
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.vector[0], 'a', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.vector[1], 'b', test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char_types.vector[2], 'c', test_suite, test_name)
try:
test_name = "Char Vectors"
stl_so.stl_ip_test.char_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<char> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "Signed Char Vectors"
stl_so.stl_ip_test.signed_char_types.vector.push_back(-1)
stl_so.stl_ip_test.signed_char_types.vector.push_back(-2)
stl_so.stl_ip_test.signed_char_types.vector.push_back(-3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.vector[0], -1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.vector[1], -2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.signed_char_types.vector[2], -3, test_suite, test_name)
try:
test_name = "Signed Char Vectors"
stl_so.stl_ip_test.char_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<signed char> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "Unsigned Char Vectors"
stl_so.stl_ip_test.unsigned_char_types.vector.push_back(1)
stl_so.stl_ip_test.unsigned_char_types.vector.push_back(2)
stl_so.stl_ip_test.unsigned_char_types.vector.push_back(3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.vector[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.vector[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_char_types.vector[2], 3, test_suite, test_name)
try:
test_name = "Unsigned Char Vectors"
stl_so.stl_ip_test.char_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<unsigned char> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "char16_t Vectors"
'''
# Calling push_back on char16_t vector directly here causes issues due to char16_t not supported in python
stl_so.stl_ip_test.char16_t_types.vector.push_back(stl_so.stl_ip_test.to_char16_t('a'))
stl_so.stl_ip_test.char16_t_types.vector.push_back(stl_so.stl_ip_test.to_char16_t('b'))
stl_so.stl_ip_test.char16_t_types.vector.push_back(stl_so.stl_ip_test.to_char16_t('c'))
'''
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char16_t_types.vector, ord('a'))
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char16_t_types.vector, ord('b'))
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char16_t_types.vector, ord('c'))
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char16_t_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char16_t_types.vector, 0), ord('a'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char16_t_types.vector, 1), ord('b'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char16_t_types.vector, 2), ord('c'), test_suite, test_name)
try:
test_name = "char16_t Vectors"
stl_so.stl_ip_test.char16_t_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<char16_t> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "char32_t Vectors"
'''
# Calling push_back on char32_t vector directly here causes issues due to char32_t not supported in python
stl_so.stl_ip_test.char32_t_types.vector.push_back(stl_so.stl_ip_test.to_char32_t(ord('a')))
stl_so.stl_ip_test.char32_t_types.vector.push_back(stl_so.stl_ip_test.to_char32_t(ord('b')))
stl_so.stl_ip_test.char32_t_types.vector.push_back(stl_so.stl_ip_test.to_char32_t(ord('c')))
'''
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char32_t_types.vector, ord('a'))
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char32_t_types.vector, ord('b'))
stl_so.stl_ip_test.addCharToVector(stl_so.stl_ip_test.char32_t_types.vector, ord('c'))
TRICK_EXPECT_EQ(stl_so.stl_ip_test.char32_t_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char32_t_types.vector, 0), ord('a'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char32_t_types.vector, 1), ord('b'), test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.getCharFromVector(stl_so.stl_ip_test.char32_t_types.vector, 2), ord('c'), test_suite, test_name)
try:
test_name = "char32_t Vectors"
stl_so.stl_ip_test.char32_t_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<char32_t> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "short int Vectors"
stl_so.stl_ip_test.short_int_types.vector.push_back(1)
stl_so.stl_ip_test.short_int_types.vector.push_back(2)
stl_so.stl_ip_test.short_int_types.vector.push_back(3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.vector[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.vector[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.short_int_types.vector[2], 3, test_suite, test_name)
try:
test_name = "short int Vectors"
stl_so.stl_ip_test.short_int_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<short int> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "unsigned short int Vectors"
stl_so.stl_ip_test.unsigned_short_int_types.vector.push_back(1)
stl_so.stl_ip_test.unsigned_short_int_types.vector.push_back(2)
stl_so.stl_ip_test.unsigned_short_int_types.vector.push_back(3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.vector[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.vector[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_short_int_types.vector[2], 3, test_suite, test_name)
#Pass invalid type(struct)
try:
test_name = "unsigned short int Vectors"
stl_so.stl_ip_test.unsigned_short_int_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<unsigned short int> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
#Pass invalid type(signed value)
try:
test_name = "unsigned short int Vectors"
stl_so.stl_ip_test.unsigned_short_int_types.vector.push_back(-1)
except Exception as e:
print(f"Expected exception due to passing signed value into push_back for std::vector<unsigned short int> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "int Vectors"
stl_so.stl_ip_test.int_types.vector.push_back(1)
stl_so.stl_ip_test.int_types.vector.push_back(2)
stl_so.stl_ip_test.int_types.vector.push_back(3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.vector[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.vector[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.int_types.vector[2], 3, test_suite, test_name)
try:
test_name = "int Vectors"
stl_so.stl_ip_test.int_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<int> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)
test_name = "unsigned int Vectors"
stl_so.stl_ip_test.unsigned_int_types.vector.push_back(1)
stl_so.stl_ip_test.unsigned_int_types.vector.push_back(2)
stl_so.stl_ip_test.unsigned_int_types.vector.push_back(3)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.vector.size(), 3, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.vector[0], 1, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.vector[1], 2, test_suite, test_name)
TRICK_EXPECT_EQ(stl_so.stl_ip_test.unsigned_int_types.vector[2], 3, test_suite, test_name)
#Pass invalid type(struct)
try:
test_name = "unsigned int Vectors"
stl_so.stl_ip_test.unsigned_int_types.vector.push_back(trick.TestStructure())
except Exception as e:
print(f"Expected exception due to passing TestStruct into push_back for std::vector<unsigned int> : {e}")
TRICK_EXPECT_GT(len(e.__str__()), 0, test_suite, test_name)

View File

@ -0,0 +1,20 @@
import trick
from trick.unit_test import *
trick_utest.unit_tests.enable()
trick_utest.unit_tests.set_file_name( os.getenv("TRICK_HOME") + "/trick_test/SIM_test_ip_stl.xml" )
trick_utest.unit_tests.set_test_name( "InputFileSTLTests" )
test_suite = "STL input tests"
from RUN_test.test_arrays import *
from RUN_test.test_vectors import *
array_tests(stl_so, test_suite)
vector_tests(stl_so, test_suite)
trick.stop(10)

View File

@ -0,0 +1,17 @@
#include "sim_objects/default_trick_sys.sm"
##include "STLIPTest.hh"
class STLSimObject : public Trick::SimObject
{
public:
STLIPTest stl_ip_test;
STLSimObject()
{
}
};
STLSimObject stl_so;

View File

@ -0,0 +1,2 @@
TRICK_CFLAGS += -I./models
TRICK_CXXFLAGS += -I./models

View File

@ -0,0 +1,104 @@
/*
PURPOSE: (Defines the STLIPTest class, which serves as a testbed for various STL
container types instantiated with C++ types such as int, double, char, etc.)
LIBRARY_DEPENDENCIES: (
()
)
*/
#ifndef STL_IP_TEST_HH
#define STL_IP_TEST_HH
#include <iostream>
#include <cstdint>
#include <cuchar>
#include <list>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <array>
#include <vector>
#include <memory>
#include <string>
#include <cstddef>
#include "test_classes/include/container_types.hh"
struct TestStructure
{
int a;
double b;
};
class STLIPTest
{
friend class InputProcessor;
public:
STLIPTest() = default;
~STLIPTest() = default;
//STLs
//Char Types
//inline char16_t to_char16_t(char c) {return static_cast<char16_t>(c);}
// Python doesn't have a char16_t type, so we use int for compatibility
// Add a character to the vector (accepts int for Python compatibility)
void addCharToVector(std::vector<char16_t>& vec, int value) {
vec.push_back(static_cast<char16_t>(value));
}
// Get a character from the vector (returns as int for Python compatibility)
int getCharFromVector(std::vector<char16_t>& vec, size_t index) {
if (index >= vec.size()) {
// Handle out of bounds access
throw std::out_of_range("Index out of bounds");
}
// Convert char16_t to int when returning to Python
return static_cast<int>(vec[index]);
}
// Python doesn't have a char32_t type, so we use int for compatibility
// Add a character to the vector (accepts int for Python compatibility)
void addCharToVector(std::vector<char32_t>& vec, int value) {
vec.push_back(static_cast<char32_t>(value));
}
// Get a character from the vector (returns as int for Python compatibility)
int getCharFromVector(std::vector<char32_t>& vec, size_t index) {
if (index >= vec.size()) {
// Handle out of bounds access
throw std::out_of_range("Index out of bounds");
}
// Convert char16_t to int when returning to Python
return static_cast<int>(vec[index]);
}
STLContainerTypes<char> char_types;
STLContainerTypes<signed char> signed_char_types;
STLContainerTypes<unsigned char> unsigned_char_types;
STLContainerTypes<char16_t> char16_t_types;
STLContainerTypes<char32_t> char32_t_types;
STLContainerTypes<short int> short_int_types;
STLContainerTypes<unsigned short int> unsigned_short_int_types;
STLContainerTypes<int> int_types;
STLContainerTypes<unsigned int> unsigned_int_types;
STLContainerTypes<long int> long_int_types;
STLContainerTypes<unsigned long int> unsigned_long_int_types;
STLContainerTypes<long long int> long_long_int_types;
STLContainerTypes<unsigned long long int> unsigned_long_long_int_types;
STLContainerTypes<float> float_types;
STLContainerTypes<double> double_types;
STLContainerTypes<long double> long_double_types;
};
#endif

View File

@ -0,0 +1,88 @@
/*
PURPOSE: (Provides a convenient way to instantiate and use a variety of STL containers
for C++ types/language bindings with SWIG)
LIBRARY_DEPENDENCIES: (
()
)
*/
#include <iostream>
#include <string>
#include <cstdint>
#include <cuchar>
#include <list>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <array>
#include <vector>
#include <stack>
#ifdef SWIG
%define DECLARE_SWIG_CONTAINER_TYPES(ALIAS, TYPE)
%template(ALIAS##Array) std::array<TYPE, 3>;
%template(ALIAS##Vector) std::vector<TYPE>;
// Both std_queue.i and std_stack.i coming with swig are wrappers for Ruby.
// We need to come up with our own wrappers for these if needed for Python.
//%template(ALIAS##Queue) std::queue<TYPE>;
//%template(ALIAS##Stack) std::stack<TYPE>;
%template(ALIAS##Deque) std::deque<TYPE>;
%template(ALIAS##Set) std::set<TYPE>;
%template(ALIAS##Map) std::map<TYPE, TYPE>;
#if SWIG_VERSION >= 0x040000
%template(ALIAS##UnorderedMap) std::unordered_map<TYPE, TYPE>;
#endif
%enddef
DECLARE_SWIG_CONTAINER_TYPES(Char, char)
DECLARE_SWIG_CONTAINER_TYPES(SignedChar, signed char)
DECLARE_SWIG_CONTAINER_TYPES(UnsignedChar, unsigned char)
DECLARE_SWIG_CONTAINER_TYPES(Char16T, char16_t)
DECLARE_SWIG_CONTAINER_TYPES(Char32T, char32_t)
DECLARE_SWIG_CONTAINER_TYPES(ShortInt, short int)
DECLARE_SWIG_CONTAINER_TYPES(UnsignedShortInt, unsigned short int)
DECLARE_SWIG_CONTAINER_TYPES(Int, int)
DECLARE_SWIG_CONTAINER_TYPES(UnsignedInt, unsigned int)
DECLARE_SWIG_CONTAINER_TYPES(LongInt, long int)
DECLARE_SWIG_CONTAINER_TYPES(UnsignedLongInt, unsigned long int)
DECLARE_SWIG_CONTAINER_TYPES(LongLongInt, long long int)
DECLARE_SWIG_CONTAINER_TYPES(UnsignedLongLongInt, unsigned long long int)
DECLARE_SWIG_CONTAINER_TYPES(Float, float)
DECLARE_SWIG_CONTAINER_TYPES(Double, double)
DECLARE_SWIG_CONTAINER_TYPES(LongDouble, long double)
#endif
template<typename T>
struct STLContainerTypes
{
using ArrayType = std::array<T, 3>;
using VectorType = std::vector<T>;
using QueueType = std::queue<T>;
using DequeType = std::deque<T>;
using SetType = std::set<T>;
using MapType = std::map<T, T>;
using UnorderedMapType = std::unordered_map<T, T>;
using StackType = std::stack<T>;
ArrayType array;
VectorType vector;
QueueType queue;
DequeType deque;
SetType set;
MapType map;
UnorderedMapType unordered_map;
StackType stack;
};

View File

@ -93,6 +93,13 @@ SIM_test_ip:
runs:
RUN_test/unit_test.py:
returns: 0
SIM_test_ip_stl:
path: test/SIM_test_ip_stl
build_args: "-t"
binary: "T_main_{cpu}_test.exe"
runs:
RUN_test/unit_test.py:
returns: 0
SIM_test_multidt:
path: test/SIM_test_multidt
build_args: "-t"