utest.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
33 #include <iostream>
34 #include <gtest/gtest.h>
35 
36 using namespace industrial_robot_client::utils;
37 
38 
39 TEST(IndustrialUtilsSuite, vector_within_range)
40 {
41 
42  std::vector<double> v1;
43  std::vector<double> v2;
44  const double range = 0.01;
45  const double half_range = range / 2.0;
46  const double outside_range = 1.1 * half_range;
47  const double inside_range = 0.9 * half_range;
48  const double a = 9.65;
49  const double b = 1324.239;
50  const double c = 90204987243.1433223;
51 
52  //NOTE: Asserts are used because each step builds upon the the prevoius step
53  EXPECT_TRUE(isWithinRange(v1, v2, range));
54 
55  v1.push_back(a);
56  ASSERT_FALSE(isWithinRange(v1, v2, range));
57 
58  v2.push_back(a);
59  ASSERT_TRUE(isWithinRange(v1, v2, range));
60  ASSERT_TRUE(isWithinRange(v1, v2, -range));
61 
62  v1.push_back(b);
63  ASSERT_FALSE(isWithinRange(v1, v2, range));
64 
65  v2.push_back(b + inside_range);
66  ASSERT_TRUE(isWithinRange(v1, v2, range));
67 
68  v1.push_back(b);
69  v2.push_back(b + outside_range);
70  ASSERT_FALSE(isWithinRange(v1, v2, range));
71 
72  v1.pop_back();
73  v2.pop_back();
74 
75  v1.push_back(c);
76  v2.push_back(c + inside_range);
77  ASSERT_TRUE(isWithinRange(v1, v2, range));
78 
79  v1.push_back(c);
80  v2.push_back(c + outside_range);
81  ASSERT_FALSE(isWithinRange(v1, v2, range));
82 
83 }
84 
85 TEST(IndustrialUtilsSuite, map_within_range)
86 {
87  std::vector<double> v1;
88  std::vector<std::string> v1_keys;
89  std::vector<double> v2;
90  std::vector<std::string> v2_keys;
91  std::vector<double> v3;
92  std::vector<std::string> v3_keys;
93  const double range = 0.01;
94  const double half_range = range / 2.0;
95  const double outside_range = 1.1 * half_range;
96  const double inside_range = 0.9 * half_range;
97  const double a = 9.65;
98  const double b = 1324.239;
99  const double c = 90204987243.1433223;
100 
101 
102  // Empty vectors should return true
103  EXPECT_TRUE(isWithinRange(v1_keys, v1, v2_keys, v2, range));
104 
105 
106  v1.push_back(a);
107  v1_keys.push_back("a");
108  v1.push_back(b);
109  v1_keys.push_back("b");
110  v1.push_back(c);
111  v1_keys.push_back("c");
112 
113  ASSERT_FALSE(isWithinRange(v1_keys, v1, v2_keys, v2, range));
114  EXPECT_TRUE(isWithinRange(v1_keys, v1, v1_keys, v1, range));
115 
116 
117  v2.push_back(inside_range + c);
118  v2_keys.push_back("c");
119  v2.push_back(inside_range + b);
120  v2_keys.push_back("b");
121  v2.push_back(inside_range + a);
122  v2_keys.push_back("a");
123 
124  ASSERT_TRUE(isWithinRange(v1_keys, v1, v2_keys, v2, range));
125 
126  v3.push_back(outside_range + a);
127  v3_keys.push_back("a");
128  v3.push_back(outside_range + b);
129  v3_keys.push_back("b");
130  v3.push_back(outside_range + c);
131  v3_keys.push_back("c");
132 
133 
134  ASSERT_FALSE(isWithinRange(v1_keys, v1, v3_keys, v3, range));
135 
136 
137 }
138 
139 // Run all the tests that were declared with TEST()
140  int main(int argc, char **argv)
141  {
142  testing::InitGoogleTest(&argc, argv);
143  return RUN_ALL_TESTS();
144  }
TEST(IndustrialUtilsSuite, vector_within_range)
Definition: utest.cpp:39
int main(int argc, char **argv)
Definition: utest.cpp:140
bool isWithinRange(const std::vector< double > &lhs, const std::vector< double > &rhs, double full_range)
Checks to see if members are within the same range. Specifically, Each item in abs(lhs[i] - rhs[i]) <...
Definition: utils.cpp:45


industrial_robot_client
Author(s): Jeremy Zoss
autogenerated on Sat Sep 21 2019 03:30:13