OTest2
A C++ testing framework
testmarkmap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <testmarkmap.h>
20 
21 #include <assert.h>
22 
23 #include <testmarkhash.h>
24 #include <testmarkin.h>
25 #include <testmarkout.h>
26 
27 namespace OTest2 {
28 
29 namespace {
30 
31 const char SERIALIZE_TYPE_MARK[] = "ot2:map";
32 
33 } /* -- namespace */
34 
36  TestMarkPrefix(SERIALIZE_TYPE_MARK, ""),
37  map() {
38 
39 }
40 
42  const std::string& prefix_) :
43  TestMarkPrefix(SERIALIZE_TYPE_MARK, prefix_),
44  map() {
45 
46 }
47 
49  const CtorMark* ctor_mark_) :
50  TestMarkPrefix(ctor_mark_, SERIALIZE_TYPE_MARK),
51  map() {
52 
53 }
54 
56 
57 }
58 
59 const char* TestMarkMap::typeMark() {
60  return SERIALIZE_TYPE_MARK;
61 }
62 
63 bool TestMarkMap::doIsEqualPrefixed(
64  const TestMark& other_,
65  long double precision_) const {
66  const TestMarkMap* o_(static_cast<const TestMarkMap*>(&other_));
67 
68  if(map.size() != o_->map.size())
69  return false;
70 
71  auto liter_(map.begin());
72  auto riter_(o_->map.begin());
73  for(; liter_ != map.end(); ++liter_, ++riter_) {
74  if((*liter_).first != (*riter_).first)
75  return false;
76  if(!(*liter_).second->isEqual(*(*riter_).second, precision_))
77  return false;
78  }
79 
80  return true;
81 }
82 
83 void TestMarkMap::doDiffArray(
84  int level_,
85  std::vector<LinearizedRecord>& array_) const
86 {
87  for(const auto& item_ : map) {
88  array_.push_back({level_, item_.second.get(), item_.first});
89  }
90 }
91 
92 void TestMarkMap::doLinearizedMark(
93  int level_,
94  const std::string& label_,
95  std::vector<LinearizedRecord>& array_) const {
96  array_.push_back({level_, this, label_});
97  for(const auto& item_ : map) {
98  item_.second->doLinearizedMark(level_ + 1, item_.first, array_);
99  }
100 }
101 
102 const char* TestMarkMap::getParenthesis() const {
103  return "{}";
104 }
105 
106 void TestMarkMap::serializeItems(
107  TestMarkOut& serializer_) const {
108  serializer_.writeInt(map.size());
109  for(const auto& item_ : map) {
110  serializer_.writeString(item_.first);
111  item_.second->serializeMark(serializer_);
112  }
113 }
114 
115 void TestMarkMap::deserializeItems(
116  TestMarkFactory& factory_,
117  TestMarkIn& deserializer_) {
118  const std::int64_t size_(deserializer_.readInt());
119  for(std::int64_t i_(0); i_ < size_; ++i_) {
120  std::string key_(deserializer_.readString());
121  TestMarkPtr item_(TestMarkIn::deserialize(factory_, deserializer_));
122  append(key_, item_);
123  }
124 }
125 
127  const std::string& key_,
128  TestMarkPtr mark_) {
129  assert(!key_.empty() && mark_ != nullptr);
130 
131  map.insert(Map::value_type(key_, mark_));
132  hash.addHashCode(mark_->getHashCode());
134 }
135 
136 } /* namespace OTest2 */
OTest2::TestMarkHash::addTerminator
void addTerminator()
Append a terminataor character into the hash.
Definition: testmarkhash.cpp:51
testmarkmap.h
testmarkhash.h
OTest2::TestMarkMap::TestMarkMap
TestMarkMap()
Ctor.
Definition: testmarkmap.cpp:35
OTest2::TestMarkIn::deserialize
static TestMarkPtr deserialize(TestMarkFactory &factory_, TestMarkIn &deserializer_)
Deserialize a testmark.
Definition: testmarkin.cpp:37
OTest2::TestMarkPrefix
A generic test mark with string prefix.
Definition: testmarkprefix.h:35
OTest2::TestMarkPrefix::hash
TestMarkHash hash
Definition: testmarkprefix.h:41
OTest2::TestMarkMap
Unordered indexed multimap of test marks.
Definition: testmarkmap.h:35
OTest2::TestMarkMap::typeMark
static const char * typeMark()
Get serialization typemark.
Definition: testmarkmap.cpp:59
OTest2::TestMarkPtr
std::shared_ptr< TestMark > TestMarkPtr
Definition: testmarkptr.h:26
OTest2
Definition: assertbean.h:25
testmarkin.h
OTest2::TestMarkMap::append
void append(const std::string &key_, TestMarkPtr mark_)
Append a mark.
Definition: testmarkmap.cpp:126
testmarkout.h
OTest2::TestMarkMap::~TestMarkMap
virtual ~TestMarkMap()
Dtor.
Definition: testmarkmap.cpp:55
OTest2::TestMark
Generic interface of a test mark node.
Definition: testmark.h:41
OTest2::TestMarkHash::addHashCode
void addHashCode(TestMarkHashCode code_)
Append a hash code of another testmark.
Definition: testmarkhash.cpp:56