testKey.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * @file testKey.cpp
14  * @author Alex Cunningham
15  */
16 
17 #include <gtsam/inference/Key.h>
18 #include <gtsam/inference/Symbol.h>
19 #include <gtsam/base/Testable.h>
21 
23 
24 #include <boost/assign/std/list.hpp> // for operator +=
25 
26 #include <sstream>
27 
28 using namespace boost::assign;
29 using namespace std;
30 using namespace gtsam;
31 
33 
34 /* ************************************************************************* */
35 TEST(Key, KeySymbolConversion) {
36  Symbol original('j', 4);
37  Key key(original);
38  EXPECT(assert_equal(key, original.key()))
39  Symbol actual(key);
40  EXPECT(assert_equal(original, actual))
41 }
42 
43 /* ************************************************************************* */
45  const auto x1 = gtsam::symbol_shorthand::X(1);
46  const auto v1 = gtsam::symbol_shorthand::V(1);
47  const auto a1 = gtsam::symbol_shorthand::A(1);
48 
49  const auto Z = gtsam::SymbolGenerator('x');
50  const auto DZ = gtsam::SymbolGenerator('v');
51  const auto DDZ = gtsam::SymbolGenerator('a');
52 
53  const auto z1 = Z(1);
54  const auto dz1 = DZ(1);
55  const auto ddz1 = DDZ(1);
56 
58  EXPECT(assert_equal(v1, dz1));
59  EXPECT(assert_equal(a1, ddz1));
60 }
61 
62 /* ************************************************************************* */
63 TEST(Key, SymbolGeneratorConstexpr) {
64  constexpr auto Z = gtsam::SymbolGenerator('x');
65  EXPECT(assert_equal(Z.chr(), 'x'));
66 }
67 
68 /* ************************************************************************* */
69 template<int KeySize>
71 
72 template <>
74  return 0x6100000000000005;
75 }
76 
77 template <>
79  return 0x61000005;
80 }
81 
82 /* ************************************************************************* */
83 TEST(Key, KeySymbolEncoding) {
84 
85  // Test encoding of Symbol <-> size_t <-> string
86  Symbol symbol(0x61, 5);
87  Key key = KeyTestValue<sizeof(Key)>();
88  string str = "a5";
89 
90  EXPECT_LONGS_EQUAL((long)key, (long)(Key)symbol);
92  EXPECT(assert_equal(symbol, Symbol(key)));
93 }
94 
95 /* ************************************************************************* */
96 TEST(Key, ChrTest) {
97  Symbol key('c', 3);
98  EXPECT(Symbol::ChrTest('c')(key));
99  EXPECT(!Symbol::ChrTest('d')(key));
100 }
101 
102 /* ************************************************************************* */
103 // A custom (nonsensical) formatter.
105  return "special";
106 }
107 
108 TEST(Key, Formatting) {
109  Symbol key('c', 3);
110  EXPECT("c3" == DefaultKeyFormatter(key));
111 
112  // Try streaming keys, should be default-formatted.
113  stringstream ss;
114  ss << StreamedKey(key);
115  EXPECT("c3" == ss.str());
116 
117  // use key_formatter with a function pointer
118  stringstream ss2;
119  ss2 << key_formatter(keyMyFormatter) << StreamedKey(key);
120  EXPECT("special" == ss2.str());
121 
122  // use key_formatter with a function object.
123  stringstream ss3;
124  ss3 << key_formatter(DefaultKeyFormatter) << StreamedKey(key);
125  EXPECT("c3" == ss3.str());
126 }
127 
128 /* ************************************************************************* */
129 int main() {
130  TestResult tr;
131  return TestRegistry::runAllTests(tr);
132 }
133 /* ************************************************************************* */
Provides additional testing facilities for common data structures.
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
Vector v1
string keyMyFormatter(Key key)
Definition: testKey.cpp:104
Definition: Half.h:150
Key X(std::uint64_t j)
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
int main()
Definition: testKey.cpp:129
#define Z
Definition: icosphere.cpp:21
TEST(Key, KeySymbolConversion)
Definition: testKey.cpp:35
Definition: pytypes.h:928
#define EXPECT(condition)
Definition: Test.h:151
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:58
Key symbol(unsigned char c, std::uint64_t j)
Key key() const
Definition: Symbol.cpp:41
static std::stringstream ss
Definition: testBTree.cpp:33
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
Point2 z1
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:155
Pose3 x1
Definition: testPose3.cpp:588
Key V(std::uint64_t j)
Key KeyTestValue()
Key aKey
Definition: testKey.cpp:32
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
Key KeyTestValue< 8 >()
Definition: testKey.cpp:73
Key KeyTestValue< 4 >()
Definition: testKey.cpp:78
Key A(std::uint64_t j)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:47:43