CMS 3D CMS Logo

Test.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <iostream>
5 #include <fstream>
6 #include <utility>
7 #include <stdexcept>
8 
11 
12 // The compiler knows our default-constructed objects' members
13 // may not be initialized when we serialize them.
14 
15 #if !defined(__clang__)
16 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
17 #endif
18 
19 // The main test: constructs an object using the default constructor,
20 // serializes it, deserializes it and finally checks whether they are equal.
21 // Mainly used to see if all the required templates compile.
22 //
23 // It is not possible to use (easily) the Boost text archive because:
24 //
25 // * Uninitialized booleans are serialized as a byte (unsigned char),
26 // while on deserialization Boost asserts on the value being 0 or 1.
27 //
28 // * Uninitialized floating point variables may be NaN (or Inf),
29 // which are serialized as "nan" and Boost fails to deserialize them.
30 //
31 // While the problems could be solved adding default constructors
32 // or modifying them (C++11's in-class member initializers cannot be used
33 // due to genreflex in ROOT 5 which would have been a bit easier),
34 // for the floating point case there are too many members and classes
35 // to modify. Instead, the Boost binary archive can be used, which does not
36 // assert. However, this means it is required to support comparing NaNs and
37 // Infs properly in cond::serialization::equal.
38 //
39 // The EOS' portable binary archive has the same problem with uninitialized
40 // booleans as the Boost text archive, but not the uninitialized floating
41 // point variables. Since the number of boolean members in CondFormats
42 // is small, we initialized them in the default constructor or disabled
43 // the test in other cases.
44 //
45 // Note that classes with STL containers of other classes may not be tested
46 // (at runtime, since they have size 0 by default), unless they are explicitly
47 // tested by themselves (which should be the case, since in the XML it was
48 // required to write the "dependencies").
49 template <typename T>
51  const std::string filename(std::string(typeid(T).name()) + ".bin");
52 
53  // C++ does not allow to construct const objects
54  // of non-POD types without user-provided default constructor
55  // (since it would be uninitialized), so we always create
56  // a non-const object.
57  T originalObject{};
58  const T& originalObjectRef = originalObject;
59  {
60  std::ofstream ofs(filename, std::ios::out | std::ios::binary);
62  std::cout << "Serializing " << typeid(T).name() << " ..." << std::endl;
63  oa << originalObjectRef;
64  }
65 
66  T deserializedObject;
67  {
68  std::ifstream ifs(filename, std::ios::in | std::ios::binary);
70  std::cout << "Deserializing " << typeid(T).name() << " ..." << std::endl;
71  ia >> deserializedObject;
72  }
73 
74  // TODO: First m,ake the Boost IO compile and run properly,
75  // then focus again on the equal() functions.
76  //std::cout << "Checking " << typeid(T).name() << " ..." << std::endl;
77  //if (not cond::serialization::equal(originalObject, deserializedObject))
78  // throw std::logic_error("Object is not equal.");
79 }
eos::portable_oarchive OutputArchive
Definition: Archive.h:18
eos::portable_iarchive InputArchive
Definition: Archive.h:17
void testSerialization()
Definition: Test.h:50
long double T