representation_util.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2023, Toyota 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
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Open Source Robotics Foundation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 #ifndef FCL_SHAPE_REPRESENTATION_UTIL_H
38 #define FCL_SHAPE_REPRESENTATION_UTIL_H
39 
40 #include <regex>
41 
42 #include <gtest/gtest.h>
43 
44 namespace fcl {
45 namespace detail {
46 
47 // For the representation() method we just need to show that it outputs
48 // a string that can be used to instantiate the shape. It is sufficient to show
49 // that it outputs *a* valid format (and not necessarily the original).
50 //
51 // It may be that formatting standards change, it's alright to reformulate
52 // the code captured here or the representation() method implementation to keep
53 // this test passing.
54 //
55 // To that end, we'll use a code string that gets converted into a string and
56 // gets compiled and confirm that it is equal to the string given by
57 // representation() (modulo some whitespace artifacts).
58 #define INSTANTIATE_AND_SAVE_STRING(code) \
59  const std::string code_string(#code); \
60  const auto shape = code;
61 
62 // Given the shape and a string comprising the code that constructs it, confirms
63 // two things:
64 //
65 // 1. With "sufficient" precision, the representation() of the shape matches
66 // the given code string.
67 // 2. With "insufficient" precision, they don't match.
68 //
69 // Note: The parameters for the shape should have *at least* two digits of
70 // precision for this test to work (e.g., prefer 1.5 to 1). But within that
71 // requirement, things will work best if the values are perfectly represented
72 // by floating point values.
73 template <typename Shape>
74 ::testing::AssertionResult ValidateRepresentation(
75  const Shape& shape, const std::string& code_string) {
76  bool success = true;
77  ::testing::AssertionResult failure = ::testing::AssertionFailure();
78  // Normalize whitespace; newlines and blocks of spaces become single spaces.
79  const std::regex whitespace_re("[\\s\\n]+");
80  const std::string expected =
81  std::regex_replace(code_string, whitespace_re, " ");
82 
83  // Confirm precision matters. We get an exact match with sufficient precision.
84  // Insufficient precision causes round-off and the strings don't match.
85  {
86  const std::string tested =
87  std::regex_replace(shape.representation(20), whitespace_re, " ");
88  if (tested != expected) {
89  success = false;
90  failure
91  << "Representation string mismatch with sufficient precision:"
92  << "\n expected: " << expected
93  << "\n tested: " << tested;
94  }
95  }
96  {
97  const std::string tested =
98  std::regex_replace(shape.representation(1), whitespace_re, " ");
99  if (tested == expected) {
100  success = false;
101  failure
102  << "Representation string still matches with insufficient precision:"
103  << "\n expected: " << expected
104  << "\n tested: " << tested;
105  }
106  }
107  if (success) {
108  return ::testing::AssertionSuccess();
109  }
110  return failure;
111 }
112 
113 } // namespace detail
114 } // namespace fcl
115 
116 #endif // FCL_SHAPE_REPRESENTATION_UTIL_H
fcl::detail::ValidateRepresentation
::testing::AssertionResult ValidateRepresentation(const Shape &shape, const std::string &code_string)
Definition: representation_util.h:74
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:48