Tawara  0.1.0
test_utils.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, 2012, Geoffrey Biggs, geoffrey.biggs@aist.go.jp
5  * RT-Synthesis Research Group
6  * Intelligent Systems Research Institute,
7  * National Institute of Advanced Industrial Science and Technology (AIST),
8  * Japan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above
18  * copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided
20  * with the distribution.
21  * * Neither the name of Geoffrey Biggs nor AIST, nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #if !defined(TAWARA_TEST_UTILS_H_)
40 #define TAWARA_TEST_UTILS_H_
41 
42 #include <boost/shared_ptr.hpp>
43 #include <functional>
44 #include <gtest/gtest.h>
45 #include <stdint.h>
46 #include <string>
47 #include <tawara/element.h>
48 #include <tawara/prim_element.h>
49 #include <vector>
50 
51 
52 namespace test_utils
53 {
54 
55 typedef boost::shared_ptr<tawara::Element> ElPtr;
56 
57 // Adds the total size of an element to a value
59  : public std::binary_function<uint64_t, ElPtr, uint64_t>
60 {
61  uint64_t operator()(uint64_t total, ElPtr el)
62  {
63  return total + el->size();
64  }
65 };
66 
67 
68 // Tests if two std::basic_string<uint8_t> byte buffers are equal.
69 ::testing::AssertionResult std_buffers_eq(char const* b1_expr,
70  char const* b2_expr,
71  std::basic_string<uint8_t> const& b1,
72  std::basic_string<uint8_t> const& b2);
73 
74 // Tests if two std::string buffers are equal.
75 ::testing::AssertionResult std_buffers_eq(char const* b1_expr,
76  char const* b2_expr, std::string const& b1, std::string const& b2);
77 
78 // Tests if two std::vector<char> buffers are equal.
79 ::testing::AssertionResult std_vectors_eq(char const* b1_expr,
80  char const* b2_expr, std::vector<char> const& b1,
81  std::vector<char> const& b2);
82 
83 // Tests if two std::pair values are equal because gtest can't figure it out
84 // for itself nor print the result.
85 template<typename T1, typename T2>
86 ::testing::AssertionResult pairs_eq(char const* p1_expr,
87  char const* p2_expr, typename std::pair<uint64_t, T1> const& p1,
88  typename std::pair<uint64_t, T2> const& p2)
89 {
90  if (p1.first != p2.first || p1.second != p2.second)
91  {
92  return ::testing::AssertionFailure() << p1_expr << " (" << p1.first <<
93  ", " << p1.second << ") != " << p2_expr << " (" << p2.first <<
94  ", " << p2.second << ")";
95  }
96  return ::testing::AssertionSuccess();
97 }
98 
99 // Returns a blob of binary data, with a length that increases by 5 bytes
100 // each time this function is called.
101 boost::shared_ptr<std::vector<char> > make_blob(size_t size);
102 
103 }; // test_utils
104 
105 #endif // TAWARA_TEST_UTILS_H_
106 
::testing::AssertionResult pairs_eq(char const *p1_expr, char const *p2_expr, typename std::pair< uint64_t, T1 > const &p1, typename std::pair< uint64_t, T2 > const &p2)
Definition: test_utils.h:86
boost::shared_ptr< tawara::Element > ElPtr
Definition: test_utils.h:55
std::streamsize size(ID id)
Get the number of bytes required by an ID.
::testing::AssertionResult std_buffers_eq(char const *b1_expr, char const *b2_expr, std::basic_string< uint8_t > const &b1, std::basic_string< uint8_t > const &b2)
boost::shared_ptr< std::vector< char > > make_blob(size_t size)
uint64_t operator()(uint64_t total, ElPtr el)
Definition: test_utils.h:61
::testing::AssertionResult std_vectors_eq(char const *b1_expr, char const *b2_expr, std::vector< char > const &b1, std::vector< char > const &b2)