OTest2
A C++ testing framework
assertionsitemwiseimpl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 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 
20 #ifndef OTest2_INCLUDE_OTEST2_ASSERTIONSITEMWISEIMPL_H_
21 #define OTest2_INCLUDE_OTEST2_ASSERTIONSITEMWISEIMPL_H_
22 
24 
25 #include <assert.h>
26 #include <cstdint>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <type_traits>
31 #include <utility>
32 #include <vector>
33 
34 #include <otest2/assertstream.h>
35 #include <otest2/comparisons.h>
36 #include <otest2/containertrait.h>
37 #include <otest2/printtraits.h>
38 #include <otest2/printutils.h>
39 #include <otest2/typetraits.h>
40 
41 namespace OTest2 {
42 
43 namespace Private {
44 
45 template<
46  template<typename, typename> class Compare_,
47  typename IterA_,
48  typename IterB_>
50  std::vector<std::string>& messages_,
51  IterA_ begin_a_,
52  IterA_ end_a_,
53  IterB_ begin_b_,
54  IterB_ end_b_) {
55  typedef typename AssertionParameter<decltype(*begin_a_)>::Type AType_;
56  typedef typename AssertionParameter<decltype(*begin_b_)>::Type BType_;
57  typedef Compare_<AType_, BType_> Comparator_;
58 
59  /* -- iterate and check items */
60  Comparator_ cmp_;
61  int index_(0);
62  while(begin_a_ != end_a_ && begin_b_ != end_b_) {
63  bool condition_(cmp_(*begin_a_, *begin_b_));
64  if(!condition_) {
65  /* -- report the failure */
66  std::ostringstream sos_;
67  sos_ << "the ";
68  printOrdinalNumber(sos_, index_);
69  sos_ << " item has failed: a[" << index_ << "] ";
71  sos_ << " b[" << index_ << "]";
72  messages_.push_back(sos_.str());
73 
74  sos_.str("");
75  sos_ << "a[" << index_ << "] = ";
76  PrintTrait<AType_>::print(sos_, *begin_a_);
77  messages_.push_back(sos_.str());
78 
79  sos_.str("");
80  sos_ << "b[" << index_ << "] = ";
81  PrintTrait<BType_>::print(sos_, *begin_b_);
82  messages_.push_back(sos_.str());
83 
84  return false;
85  }
86 
87  /* -- move to next item */
88  ++begin_a_;
89  ++begin_b_;
90  ++index_;
91  }
92 
93  /* -- there are some other items in the left container */
94  if(begin_a_ != end_a_) {
95  messages_.push_back("left container is bigger then the right one");
96  return false;
97  }
98 
99  /* -- there are some other items in the right container */
100  if(begin_b_ != end_b_) {
101  messages_.push_back("left container is smaller then the right one");
102  return false;
103  }
104 
105  messages_.push_back("the check has passed");
106  return true;
107 }
108 
109 } /* -- namespace Private */
110 
111 template<
112  template<typename, typename> class Compare_,
113  typename IterA_,
114  typename IterB_>
115 bool ItemWiseAssertion::testAssertItemWiseList(
116  IterA_ begin_a_,
117  IterA_ end_a_,
118  IterB_ begin_b_,
119  IterB_ end_b_) {
120  /* -- check the container */
121  std::vector<std::string> messages_;
122  bool result_(Private::compareListsItemWise<Compare_>(
123  messages_, begin_a_, end_a_, begin_b_, end_b_));
124  assert(messages_.size() > 0);
125 
126  /* -- report the result */
127  AssertStream report_(enterAssertion(result_));
128  for(const auto& message_ : messages_)
129  report_ << message_ << commitMsg();
130  return report_.getResult();
131 }
132 
133 template<template<typename, typename> class Compare_, typename IterA_, typename IterB_>
135  IterA_ begin_a_,
136  IterA_ end_a_,
137  IterB_ begin_b_,
138  IterB_ end_b_) {
139  return testAssertItemWiseList<Compare_>(begin_a_, end_a_, begin_b_, end_b_);
140 }
141 
142 template<template<typename, typename> class Compare_, typename ContainerA_, typename IterB_>
144  const ContainerA_& a_,
145  IterB_ begin_b_,
146  IterB_ end_b_) {
147  return testAssertItemWiseList<Compare_>(
150  begin_b_,
151  end_b_);
152 }
153 
154 template<template<typename, typename> class Compare_, typename IterA_, typename ContainerB_>
156  IterA_ begin_a_,
157  IterA_ end_a_,
158  const ContainerB_& b_) {
159  return testAssertItemWiseList<Compare_>(
160  begin_a_,
161  end_a_,
164 }
165 
166 template<template<typename, typename> class Compare_, typename ContainerA_, typename ContainerB_>
168  const ContainerA_& a_,
169  const ContainerB_& b_) {
170  return testAssertItemWiseList<Compare_>(
175 }
176 
177 } /* -- namespace OTest2 */
178 
179 #endif /* -- OTest2_INCLUDE_OTEST2_ASSERTIONSITEMWISEIMPL_H_ */
assertstream.h
typetraits.h
OTest2::AssertContext::enterAssertion
AssertStream enterAssertion(bool result_)
Enter an assertion.
Definition: assertcontext.cpp:45
OTest2::commitMsg
Private::Manipulator commitMsg()
Commit current assertion message.
Definition: assertstream.cpp:201
printtraits.h
printutils.h
containertrait.h
OTest2::PrintTrait::print
static std::ostream & print(std::ostream &os_, typename TypeTrait< Type_ >::BestArg value_)
Definition: printtraits.h:39
assertionsitemwise.h
OTest2
Definition: assertbean.h:25
comparisons.h
OTest2::ItemWiseAssertion::testAssertItemWise
bool testAssertItemWise(IterA_ begin_a_, IterA_ end_a_, IterB_ begin_b_, IterB_ end_b_)
Definition: assertionsitemwiseimpl.h:134
OTest2::Private::compareListsItemWise
bool compareListsItemWise(std::vector< std::string > &messages_, IterA_ begin_a_, IterA_ end_a_, IterB_ begin_b_, IterB_ end_b_)
Definition: assertionsitemwiseimpl.h:49
OTest2::ListContainerTrait
A simple trait allowing access to list container's iterators.
Definition: containertrait.h:45
OTest2::AssertionParameter
A helper trait - get normalized type of a captured assertion parameter.
Definition: typetraits.h:81
OTest2::printOrdinalNumber
void printOrdinalNumber(std::ostream &os_, int number_)
Format the ordinal number with appropriate suffix.
Definition: printutils.cpp:25