|
| 1 | +// Copyright 2009-2025 NTESS. Under the terms |
| 2 | +// of Contract DE-NA0003525 with NTESS, the U.S. |
| 3 | +// Government retains certain rights in this software. |
| 4 | +// |
| 5 | +// Copyright (c) 2009-2025, NTESS |
| 6 | +// All rights reserved. |
| 7 | +// |
| 8 | +// This file is part of the SST software package. For license |
| 9 | +// information, see the LICENSE file in the top level directory of the |
| 10 | +// distribution. |
| 11 | + |
| 12 | +#include "sst_config.h" |
| 13 | + |
| 14 | +#include "sst/core/serialization/serialize.h" |
| 15 | + |
| 16 | +#include <array> |
| 17 | +#include <bitset> |
| 18 | +#include <complex> |
| 19 | +#include <cstddef> |
| 20 | +#include <cstdint> |
| 21 | +#include <type_traits> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace SST::Core::Serialization::unittest { |
| 26 | + |
| 27 | +// Compile-time unit tests of trivially serializable types |
| 28 | + |
| 29 | +template <typename TYPE, bool EXPECTED> |
| 30 | +void |
| 31 | +trivially_serializable_test() |
| 32 | +{ |
| 33 | + static_assert(is_trivially_serializable_v<TYPE> == EXPECTED); |
| 34 | +} |
| 35 | + |
| 36 | +template <typename TYPE> |
| 37 | +void |
| 38 | +test_trivially_serializable_type_assumptions() |
| 39 | +{ |
| 40 | + static_assert(std::is_trivially_copyable_v<TYPE> && std::is_standard_layout_v<TYPE>); |
| 41 | +} |
| 42 | + |
| 43 | +template void trivially_serializable_test<std::byte, true>(); |
| 44 | +template void trivially_serializable_test<int8_t, true>(); |
| 45 | +template void trivially_serializable_test<uint8_t, true>(); |
| 46 | +template void trivially_serializable_test<int16_t, true>(); |
| 47 | +template void trivially_serializable_test<uint16_t, true>(); |
| 48 | +template void trivially_serializable_test<int32_t, true>(); |
| 49 | +template void trivially_serializable_test<uint32_t, true>(); |
| 50 | +template void trivially_serializable_test<int64_t, true>(); |
| 51 | +template void trivially_serializable_test<uint64_t, true>(); |
| 52 | +template void trivially_serializable_test<float, true>(); |
| 53 | +template void trivially_serializable_test<double, true>(); |
| 54 | +template void trivially_serializable_test<long double, true>(); |
| 55 | + |
| 56 | +enum class test_enum { X, Y, Z }; |
| 57 | +enum class test_enum_int8_t : int8_t { X, Y, Z, A, B, C }; |
| 58 | +enum class test_enum_uint8_t : uint8_t { X, Y, Z, A, B, C, J, K, L }; |
| 59 | +enum class test_enum_int16_t : int16_t { X, Y, Z, A, B, C }; |
| 60 | +enum class test_enum_uint16_t : uint16_t { X, Y, Z, A, B, C, J, K, L }; |
| 61 | +enum class test_enum_int32_t : int32_t { X, Y, Z, A, B, C }; |
| 62 | +enum class test_enum_uint32_t : uint32_t { X, Y, Z, A, B, C, J, K, L }; |
| 63 | +enum class test_enum_int64_t : int64_t { X, Y, Z, A, B, C }; |
| 64 | +enum class test_enum_uint64_t : uint64_t { X, Y, Z, A, B, C, J, K, L }; |
| 65 | + |
| 66 | +template void trivially_serializable_test<test_enum, true>(); |
| 67 | +template void trivially_serializable_test<test_enum_int8_t, true>(); |
| 68 | +template void trivially_serializable_test<test_enum_uint8_t, true>(); |
| 69 | +template void trivially_serializable_test<test_enum_int16_t, true>(); |
| 70 | +template void trivially_serializable_test<test_enum_uint16_t, true>(); |
| 71 | +template void trivially_serializable_test<test_enum_int32_t, true>(); |
| 72 | +template void trivially_serializable_test<test_enum_uint32_t, true>(); |
| 73 | +template void trivially_serializable_test<test_enum_int64_t, true>(); |
| 74 | +template void trivially_serializable_test<test_enum_uint64_t, true>(); |
| 75 | + |
| 76 | +struct test_complex_float |
| 77 | +{ |
| 78 | + float r, i; |
| 79 | +}; |
| 80 | +struct test_complex_double |
| 81 | +{ |
| 82 | + double r, i; |
| 83 | +}; |
| 84 | + |
| 85 | +template void trivially_serializable_test<test_complex_float, true>(); |
| 86 | +template void trivially_serializable_test<test_complex_double, true>(); |
| 87 | +template void trivially_serializable_test<std::complex<float>, true>(); |
| 88 | +template void trivially_serializable_test<std::complex<double>, true>(); |
| 89 | + |
| 90 | +struct test_complex_mix |
| 91 | +{ |
| 92 | + float _Complex cf; |
| 93 | + double _Complex cd; |
| 94 | + long double _Complex cl; |
| 95 | + std::complex<double> cfa[10]; |
| 96 | +}; |
| 97 | + |
| 98 | +template void trivially_serializable_test<test_complex_mix, true>(); |
| 99 | + |
| 100 | +struct test_complex_double_array |
| 101 | +{ |
| 102 | + test_complex_double ary[1000]; |
| 103 | +}; |
| 104 | + |
| 105 | +template void trivially_serializable_test<test_complex_double_array, true>(); |
| 106 | + |
| 107 | +struct test_struct |
| 108 | +{ |
| 109 | + test_complex_float z[8]; |
| 110 | + std::bitset<100> bs; |
| 111 | + test_complex_float test_struct::* mem_ptr; |
| 112 | + std::array<size_t, 100> sizes; |
| 113 | + union { |
| 114 | + int i; |
| 115 | + float f; |
| 116 | + }; |
| 117 | +}; |
| 118 | + |
| 119 | +template void trivially_serializable_test<test_struct, true>(); |
| 120 | + |
| 121 | +union test_union_ptr_2nd { |
| 122 | + uintptr_t iptr; |
| 123 | + void* ptr; |
| 124 | +}; |
| 125 | + |
| 126 | +template void trivially_serializable_test<test_union_ptr_2nd, true>(); |
| 127 | + |
| 128 | +// Tests of types which are not trivially serializable |
| 129 | + |
| 130 | +template void trivially_serializable_test<void, false>(); |
| 131 | +template void trivially_serializable_test<void*, false>(); |
| 132 | + |
| 133 | +typedef void (*fptr)(int, int); |
| 134 | +template void trivially_serializable_test<fptr, false>(); |
| 135 | + |
| 136 | +typedef test_complex_double* test_complex_double_array_ptr[1000]; |
| 137 | +template void trivially_serializable_test<test_complex_double_array_ptr, false>(); |
| 138 | + |
| 139 | +template void trivially_serializable_test<std::vector<int>, false>(); |
| 140 | + |
| 141 | +struct test_struct_ref |
| 142 | +{ |
| 143 | + test_complex_float z[8]; |
| 144 | + double& dref; |
| 145 | +}; |
| 146 | + |
| 147 | +template void trivially_serializable_test<test_struct_ref, false>(); |
| 148 | + |
| 149 | +struct test_struct_ptr |
| 150 | +{ |
| 151 | + int i; |
| 152 | + int* iptr; |
| 153 | + double d; |
| 154 | +}; |
| 155 | + |
| 156 | +template void trivially_serializable_test<test_struct_ptr, false>(); |
| 157 | + |
| 158 | +struct test_serialize_order |
| 159 | +{ |
| 160 | + std::complex<double> cfa[10]; |
| 161 | + void serialize_order(serializer& ser) { SST_SER(cfa); } |
| 162 | +}; |
| 163 | + |
| 164 | +template void trivially_serializable_test<test_serialize_order, false>(); |
| 165 | + |
| 166 | +struct test_mix |
| 167 | +{ |
| 168 | + test_complex_float z; |
| 169 | + test_struct_ref refs; |
| 170 | +}; |
| 171 | + |
| 172 | +template void trivially_serializable_test<test_mix, false>(); |
| 173 | + |
| 174 | +union test_union_ptr_1st { |
| 175 | + void* ptr; |
| 176 | + uintptr_t iptr; |
| 177 | +}; |
| 178 | + |
| 179 | +template void trivially_serializable_test<test_union_ptr_1st, false>(); |
| 180 | + |
| 181 | +struct test_container_struct |
| 182 | +{ |
| 183 | + int size; |
| 184 | + std::vector<double> v; |
| 185 | +}; |
| 186 | + |
| 187 | +template void trivially_serializable_test<test_container_struct, false>(); |
| 188 | + |
| 189 | +template void trivially_serializable_test<std::pair<int, int>, false>(); |
| 190 | + |
| 191 | +template void trivially_serializable_test<std::tuple<float, float, float>, false>(); |
| 192 | + |
| 193 | +// Test assumptions which are made by is_trivially_serializable_v<TYPE> |
| 194 | +template void test_trivially_serializable_type_assumptions<std::bitset<1>>(); |
| 195 | +template void test_trivially_serializable_type_assumptions<std::bitset<8>>(); |
| 196 | +template void test_trivially_serializable_type_assumptions<std::bitset<100>>(); |
| 197 | +template void test_trivially_serializable_type_assumptions<std::bitset<200>>(); |
| 198 | +template void test_trivially_serializable_type_assumptions<std::bitset<400>>(); |
| 199 | +template void test_trivially_serializable_type_assumptions<std::bitset<800>>(); |
| 200 | +template void test_trivially_serializable_type_assumptions<std::bitset<1600>>(); |
| 201 | + |
| 202 | +template void test_trivially_serializable_type_assumptions<std::complex<float>>(); |
| 203 | +template void test_trivially_serializable_type_assumptions<std::complex<double>>(); |
| 204 | +template void test_trivially_serializable_type_assumptions<std::complex<long double>>(); |
| 205 | + |
| 206 | +} // namespace SST::Core::Serialization::unittest |
0 commit comments