CMS 3D CMS Logo

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