Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestSerialize.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <gtest/gtest.h>
8
9#include "arccore/serialize/BasicSerializer.h"
10
11#include "arccore/base/Ref.h"
12#include "arccore/base/FatalErrorException.h"
13#include "arccore/base/ValueFiller.h"
15#include "arccore/base/Float128.h"
16#include "arccore/base/Int128.h"
17
18using namespace Arccore;
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
24{
25 public:
26
27 virtual ~ISerializeValue() = default;
28
29 virtual void serialize(ISerializer* s) = 0;
30 virtual void checkValid() = 0;
31};
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36namespace
37{
38using Float64 = Real;
39template <typename T> class ValueTraits;
40#define VALUE_TRAITS(type_name, basic_type_name) \
41 template <> class ValueTraits<type_name> \
42 { \
43 public: \
44\
45 static eBasicDataType dataType() { return eBasicDataType::basic_type_name; } \
46 static void getValue(ISerializer* s, type_name& v) { v = s->get##type_name(); } \
47 };
48
49#define VALUE_TRAITS2(type_name) VALUE_TRAITS(type_name, type_name)
50
51VALUE_TRAITS2(Byte);
52VALUE_TRAITS2(Int8);
53VALUE_TRAITS2(Int16);
54VALUE_TRAITS2(Int32)
55VALUE_TRAITS2(Int64);
56VALUE_TRAITS2(Float16);
57VALUE_TRAITS2(Float32);
58VALUE_TRAITS2(BFloat16);
59VALUE_TRAITS(Real, Float64);
60VALUE_TRAITS2(Float128);
61VALUE_TRAITS2(Int128);
62
63} // namespace
64
65/*---------------------------------------------------------------------------*/
66/*---------------------------------------------------------------------------*/
67
68template <typename DataType>
69class SerializeValue
70: public ISerializeValue
71{
72 using ValueTraitsType = ValueTraits<DataType>;
73
74 public:
75
76 SerializeValue()
77 : m_data_type(ValueTraitsType::dataType())
78 {}
79
80 public:
81
82 void serialize(ISerializer* s) override
83 {
84 Int64 size = m_array_values.size();
85 switch (s->mode()) {
86 case ISerializer::ModeReserve:
87 std::cout << "ReserveArray type=" << m_data_type << " size=" << m_array_values.size() << "\n";
88 s->reserveArray(m_array_values);
89 if (size > 0)
90 s->reserve(m_data_type, 1);
91 break;
93 std::cout << "PutArray type=" << m_data_type << " size=" << m_array_values.size() << "\n";
94 s->putArray(m_array_values);
95 if (size > 0)
96 s->put(m_array_values[0]);
97 break;
99 s->getArray(m_result_array_values);
100 if (size > 0)
101 ValueTraitsType::getValue(s, m_unique_value);
102 }
103 }
104
105 void checkValid() override
106 {
107 std::cout << "ref_size=" << m_array_values.size()
108 << " result_size=" << m_result_array_values.size() << "\n";
109 ASSERT_EQ(m_array_values, m_result_array_values);
110 ASSERT_EQ(m_array_values[0], m_unique_value);
111 }
112
113 void resizeAndFill(Int32 size)
114 {
115 m_array_values.resize(size);
116 ValueFiller::fillRandom(542, m_array_values.span());
117 }
118
119 public:
120
121 UniqueArray<DataType> m_array_values;
122 UniqueArray<DataType> m_result_array_values;
123 DataType m_unique_value = {};
125};
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130class StringSerializeValue
131: public ISerializeValue
132{
133 public:
134
135 explicit StringSerializeValue(const String& v)
136 : m_ref_string(v)
137 {}
138
139 public:
140
141 void serialize(ISerializer* s) override
142 {
143 switch (s->mode()) {
144 case ISerializer::ModeReserve:
145 s->reserve(m_ref_string);
146 break;
148 s->put(m_ref_string);
149 break;
151 s->get(m_result_string);
152 }
153 }
154
155 void checkValid() override
156 {
157 ASSERT_EQ(m_ref_string, m_result_string);
158 }
159
160 public:
161
162 String m_ref_string;
163 String m_result_string;
164};
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
170{
171 public:
172
174 {
175 for (ISerializeValue* v : m_values)
176 delete v;
177 }
178 void doSerialize(ISerializer* s)
179 {
180 for (ISerializeValue* v : m_values)
181 v->serialize(s);
182 }
183 void checkValid()
184 {
185 for (ISerializeValue* v : m_values)
186 v->checkValid();
187 }
188
189 template <typename DataType> void add(Int32 size)
190 {
191 auto* sval = new SerializeValue<DataType>();
192 sval->resizeAndFill(size);
193 m_values.add(sval);
194 }
195 void addString(const String& v)
196 {
197 m_values.add(new StringSerializeValue(v));
198 }
199
200 public:
201
203};
204
205/*---------------------------------------------------------------------------*/
206/*---------------------------------------------------------------------------*/
207
208void _doMisc()
209{
210 Ref<ISerializer> serializer_ref = createSerializer();
211 ISerializer* serializer = serializer_ref.get();
212
213 SerializeValueList values;
214
215 values.add<Float16>(12679);
216 values.add<BFloat16>(3212);
217 values.add<Int8>(6357);
218 values.add<Float32>(983);
219 values.add<Int16>(16353);
220 values.add<Real>(29123);
221 values.add<Int32>(16);
222 values.add<Byte>(3289);
223 values.add<Int64>(12932);
224 values.add<Float128>(19328);
225 values.add<Int128>(32422);
226 values.addString("Ceci est un test de chaîne de caractères");
227
228 serializer->setMode(ISerializer::ModeReserve);
229 values.doSerialize(serializer);
230 serializer->allocateBuffer();
231 serializer->setMode(ISerializer::ModePut);
232 values.doSerialize(serializer);
233 serializer->setMode(ISerializer::ModeGet);
234 values.doSerialize(serializer);
235
236 values.checkValid();
237}
238
239TEST(Serialize, Misc)
240{
241 try {
242 _doMisc();
243 }
244 catch (const Exception& e) {
245 e.write(std::cerr);
246 throw;
247 }
248}
File containing definitions of basic data types managed by Arccore.
Management of references to a C++ class.
virtual void allocateBuffer()=0
Allocates the serializer memory.
virtual void setMode(eMode new_mode)=0
Sets the current mode.
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
1D data vector with value semantics (STL style).
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual void putArray(Span< const Real > values)=0
Save the number of elements and the values elements.
virtual void reserveArray(Span< const Real > values)=0
Reserve to save the number of elements and the values elements.
virtual eMode mode() const =0
Current operating mode.
virtual void getArray(Array< Real > &values)=0
Resize and fill values.
virtual void put(Span< const Real > values)=0
Add the array values.
virtual void get(ArrayView< Real > values)=0
Retrieve the array values.
eBasicDataType
Type of a basic data item.
@ Unknown
Unknown or uninitialized data type.
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
Namespace of Arccore.
Arcane::Float128 Float128
Type representing a 128-bit float.
Arcane::BFloat16 BFloat16
Type 'Brain Float16'.
float Float32
IEEE-753 single precision floating point type (binary32).
Arcane::Float16 Float16
Type 'Float16' (binary16).
ARCCORE_SERIALIZE_EXPORT Ref< ISerializer > createSerializer()
Creates an instance of ISerializer.
Arcane::Int8 Int8
Type representing an 8-bit integer.
Arcane::Int128 Int128
Type representing a 128-bit integer.