Skip to content

Commit af745d3

Browse files
authored
add array serialization tests, fix bugs (#1319)
fix error in non-instantiated template
1 parent 5a53c90 commit af745d3

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

src/sst/core/serialization/impl/serialize_array.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ struct serialize_impl_fixed_array
9797
case serializer::UNPACK:
9898
if constexpr ( std::is_pointer_v<OBJ_TYPE> ) {
9999
// for pointers to fixed arrays, we allocate the storage
100-
if constexpr ( std::is_same_v<OBJ_TYPE, ELEM_T(*)[SIZE]> )
101-
ary = new ELEM_T[SIZE];
102-
else
103-
ary = new std::remove_pointer_t<OBJ_TYPE>;
100+
ary = reinterpret_cast<OBJ_TYPE>(new std::remove_pointer_t<OBJ_TYPE>);
104101
}
105102
[[fallthrough]];
106103

src/sst/core/serialization/serializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class serializer
131131
switch ( mode_ ) {
132132
case SIZER:
133133
sizer_.add(sizeof(SIZE_T));
134-
sizer_.add(size);
134+
sizer_.add(size * sizeof(ELEM_T));
135135
break;
136136
case PACK:
137137
if ( buffer ) {

src/sst/core/testElements/coreTest_Serialization.cc

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ namespace SST::CoreTestSerialization {
4040
using SST::Core::Serialization::get_size;
4141

4242
template <typename T>
43-
static void
44-
serializeDeserialize(T& input, T& output, bool with_tracking = false)
43+
void
44+
serializeDeserialize(T&& input, T&& output, bool with_tracking = false)
4545
{
4646
// Set up serializer and buffers
4747
char* buffer;
@@ -220,6 +220,57 @@ checkContainerSerializeDeserialize(T*& data)
220220
return true;
221221
};
222222

223+
// Arrays
224+
225+
template <typename>
226+
constexpr size_t array_size = 0;
227+
228+
template <typename T, size_t S>
229+
constexpr size_t array_size<T[S]> = S;
230+
231+
template <typename T, size_t S>
232+
constexpr size_t array_size<std::array<T, S>> = S;
233+
234+
template <typename T>
235+
bool
236+
checkFixedArraySerializeDeserialize(T& data)
237+
{
238+
T result;
239+
serializeDeserialize(data, result);
240+
241+
for ( size_t i = 0; i < array_size<std::remove_pointer_t<T>>; ++i ) {
242+
if constexpr ( std::is_pointer_v<T> ) {
243+
if ( (*data)[i] != (*result)[i] ) return false;
244+
}
245+
else {
246+
if ( data[i] != result[i] ) return false;
247+
}
248+
}
249+
if constexpr ( std::is_pointer_v<T> ) delete[] result;
250+
return true;
251+
};
252+
253+
template <typename T>
254+
bool
255+
checkArraySerializeDeserialize(T* data, size_t dataSize)
256+
{
257+
using SST::Core::Serialization::array;
258+
259+
T* result = nullptr;
260+
size_t resultSize = ~size_t {};
261+
262+
serializeDeserialize(array(data, dataSize), array(result, resultSize));
263+
264+
if ( resultSize != dataSize ) return false;
265+
266+
for ( size_t i = 0; i < dataSize; ++i ) {
267+
if ( data[i] != result[i] ) return false;
268+
}
269+
270+
delete[] result;
271+
272+
return true;
273+
};
223274

224275
// For ordered but non-iterable contaienrs
225276
template <typename T>
@@ -558,6 +609,47 @@ coreTestSerialization::coreTestSerialization(ComponentId_t id, Params& params) :
558609
checkSimpleSerializeDeserialize<double*>::check_all(rng->nextUniform() * 1000000, out, "double*");
559610
checkSimpleSerializeDeserialize<std::string*>::check_all("test_string", out, "std::string*");
560611
}
612+
else if ( test == "array" ) {
613+
{
614+
int32_t array_in[10];
615+
for ( size_t i = 0; i < 10; ++i )
616+
array_in[i] = rng->generateNextInt32();
617+
passed = checkFixedArraySerializeDeserialize(array_in);
618+
if ( !passed ) out.output("ERROR: int32_t[10] did not serialize/deserialize properly\n");
619+
}
620+
{
621+
std::array<int32_t, 10> array_in;
622+
for ( size_t i = 0; i < 10; ++i )
623+
array_in[i] = rng->generateNextInt32();
624+
passed = checkFixedArraySerializeDeserialize(array_in);
625+
if ( !passed ) out.output("ERROR: std::array<int32_t, 10> did not serialize/deserialize properly\n");
626+
}
627+
{
628+
int32_t(*array_in)[10] = reinterpret_cast<int32_t(*)[10]>(new int32_t[10]);
629+
for ( size_t i = 0; i < 10; ++i )
630+
(*array_in)[i] = rng->generateNextInt32();
631+
passed = checkFixedArraySerializeDeserialize(array_in);
632+
if ( !passed ) out.output("ERROR: int32_t[10] did not serialize/deserialize properly\n");
633+
delete[] array_in;
634+
}
635+
{
636+
std::array<int32_t, 10>* array_in = new std::array<int32_t, 10>;
637+
for ( size_t i = 0; i < 10; ++i )
638+
(*array_in)[i] = rng->generateNextInt32();
639+
passed = checkFixedArraySerializeDeserialize(array_in);
640+
if ( !passed ) out.output("ERROR: std::array<int32_t, 10> did not serialize/deserialize properly\n");
641+
delete array_in;
642+
}
643+
{
644+
size_t size = 100;
645+
int32_t* array_in = new int32_t[size];
646+
for ( size_t i = 0; i < size; ++i )
647+
array_in[i] = rng->generateNextInt32();
648+
passed = checkArraySerializeDeserialize(array_in, size);
649+
if ( !passed ) out.output("ERROR: std::array<int32_t, %zu> did not serialize/deserialize properly\n", size);
650+
delete[] array_in;
651+
}
652+
}
561653
else if ( test == "ordered_containers" ) {
562654
// Ordered Containers
563655
// map, set, vector, vector<bool>, list, deque

tests/testsuite_default_Serialization.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def test_Serialization_pod(self):
3636
def test_Serialization_pod_ptr(self):
3737
self.serialization_test_template("pod_ptr")
3838

39+
def test_Serialization_array(self):
40+
self.serialization_test_template("array")
41+
3942
def test_Serialization_ordered_containers(self):
4043
self.serialization_test_template("ordered_containers")
4144

@@ -82,4 +85,3 @@ def serialization_test_template(self, testtype, default_reffile = True):
8285
filter1 = StartsWithFilter("WARNING: No components are")
8386
cmp_result = testing_compare_filtered_diff("serialization", outfile, reffile, True, [filter1])
8487
self.assertTrue(cmp_result, "Output/Compare file {0} does not match Reference File {1}".format(outfile, reffile))
85-

0 commit comments

Comments
 (0)