Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:14

0001 #include "catch.hpp"
0002 #include <utility>
0003 #include <algorithm>
0004 #include <iterator>
0005 
0006 #include "FWCore/SharedMemory/interface/ROOTSerializer.h"
0007 #include "FWCore/SharedMemory/interface/ROOTDeserializer.h"
0008 
0009 #include "DataFormats/TestObjects/interface/Thing.h"
0010 
0011 namespace {
0012   struct ReadWriteTestBuffer {
0013     std::pair<char*, std::size_t> buffer() { return std::pair(&buffer_.front(), size()); }
0014 
0015     int bufferIdentifier() { return bufferIdentifier_; }
0016 
0017     void copyToBuffer(char* iStart, std::size_t iLength) {
0018       buffer_.clear();
0019       if (iLength > buffer_.capacity()) {
0020         buffer_.reserve(iLength);
0021         ++bufferIdentifier_;
0022       }
0023       std::copy(iStart, iStart + iLength, std::back_insert_iterator(buffer_));
0024     }
0025 
0026     std::size_t size() const { return buffer_.size(); }
0027 
0028     std::vector<char> buffer_;
0029     int bufferIdentifier_ = 1;
0030   };
0031 
0032   bool compare(std::vector<edmtest::Thing> const& iLHS, std::vector<edmtest::Thing> const& iRHS) {
0033     if (iLHS.size() != iRHS.size()) {
0034       return false;
0035     }
0036 
0037     for (size_t i = 0; i < iLHS.size(); ++i) {
0038       if (iLHS[i].a != iRHS[i].a) {
0039         return false;
0040       }
0041     }
0042     return true;
0043   }
0044 }  // namespace
0045 using namespace edm::shared_memory;
0046 TEST_CASE("test De/ROOTSerializer", "[ROOTSerializer]") {
0047   SECTION("Process edmtest::Thing") {
0048     ReadWriteTestBuffer buffer;
0049 
0050     ROOTSerializer<edmtest::Thing, ReadWriteTestBuffer> serializer(buffer);
0051     ROOTDeserializer<edmtest::Thing, ReadWriteTestBuffer> deserializer(buffer);
0052 
0053     edmtest::Thing t;
0054     t.a = 42;
0055 
0056     serializer.serialize(t);
0057     REQUIRE(buffer.bufferIdentifier() == 2);
0058 
0059     auto newT = deserializer.deserialize();
0060 
0061     REQUIRE(t.a == newT.a);
0062     SECTION("Reuse buffer") {
0063       t.a = 12;
0064       serializer.serialize(t);
0065       REQUIRE(buffer.bufferIdentifier() == 2);
0066 
0067       auto newT = deserializer.deserialize();
0068 
0069       REQUIRE(t.a == newT.a);
0070     }
0071   }
0072 
0073   SECTION("Process std::vector<edmtest::Thing>") {
0074     ReadWriteTestBuffer buffer;
0075 
0076     ROOTSerializer<std::vector<edmtest::Thing>, ReadWriteTestBuffer> serializer(buffer);
0077     ROOTDeserializer<std::vector<edmtest::Thing>, ReadWriteTestBuffer> deserializer(buffer);
0078 
0079     std::vector<edmtest::Thing> t;
0080     t.reserve(4);
0081     for (int i = 0; i < 4; ++i) {
0082       edmtest::Thing temp;
0083       temp.a = i;
0084       t.push_back(temp);
0085     }
0086 
0087     serializer.serialize(t);
0088     REQUIRE(buffer.bufferIdentifier() == 2);
0089 
0090     auto newT = deserializer.deserialize();
0091 
0092     REQUIRE(compare(t, newT));
0093     SECTION("Reuse edtest::Thing buffer") {
0094       for (auto& v : t) {
0095         v.a += 1;
0096       }
0097       serializer.serialize(t);
0098       REQUIRE(buffer.bufferIdentifier() == 2);
0099 
0100       auto newT = deserializer.deserialize();
0101 
0102       REQUIRE(compare(t, newT));
0103     }
0104 
0105     SECTION("Grow") {
0106       t.emplace_back();
0107       serializer.serialize(t);
0108       REQUIRE(buffer.bufferIdentifier() == 3);
0109 
0110       auto newT = deserializer.deserialize();
0111 
0112       REQUIRE(compare(t, newT));
0113     }
0114 
0115     SECTION("Shrink") {
0116       t.pop_back();
0117       t.pop_back();
0118 
0119       serializer.serialize(t);
0120       REQUIRE(buffer.bufferIdentifier() == 2);
0121 
0122       auto newT = deserializer.deserialize();
0123 
0124       REQUIRE(compare(t, newT));
0125     }
0126   }
0127 }