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 <sstream>
25 
26 using namespace std;
27 using namespace gtsam;
28 
30 
31 /* ************************************************************************* */
32 TEST(Key, KeySymbolConversion) {
33  Symbol original('j', 4);
34  Key key(original);
35  EXPECT(assert_equal(key, original.key()))
36  Symbol actual(key);
37  EXPECT(assert_equal(original, actual))
38 }
39 
40 /* ************************************************************************* */
42  const auto x1 = gtsam::symbol_shorthand::X(1);
43  const auto v1 = gtsam::symbol_shorthand::V(1);
44  const auto a1 = gtsam::symbol_shorthand::A(1);
45 
46  const auto Z = gtsam::SymbolGenerator('x');
47  const auto DZ = gtsam::SymbolGenerator('v');
48  const auto DDZ = gtsam::SymbolGenerator('a');
49 
50  const auto z1 = Z(1);
51  const auto dz1 = DZ(1);
52  const auto ddz1 = DDZ(1);
53 
55  EXPECT(assert_equal(v1, dz1));
56  EXPECT(assert_equal(a1, ddz1));
57 }
58 
59 /* ************************************************************************* */
60 TEST(Key, SymbolGeneratorConstexpr) {
61  constexpr auto Z = gtsam::SymbolGenerator('x');
62  EXPECT(assert_equal(Z.chr(), 'x'));
63 }
64 
65 /* ************************************************************************* */
66 template<int KeySize>
68 
69 template <>
71  return 0x6100000000000005;
72 }
73 
74 template <>
76  return 0x61000005;
77 }
78 
79 /* ************************************************************************* */
80 TEST(Key, KeySymbolEncoding) {
81 
82  // Test encoding of Symbol <-> size_t <-> string
83  Symbol symbol(0x61, 5);
84  Key key = KeyTestValue<sizeof(Key)>();
85  string str = "a5";
86 
87  EXPECT_LONGS_EQUAL((long)key, (long)(Key)symbol);
89  EXPECT(assert_equal(symbol, Symbol(key)));
90 }
91 
92 /* ************************************************************************* */
93 TEST(Key, ChrTest) {
94  Symbol key('c', 3);
95  EXPECT(Symbol::ChrTest('c')(key));
96  EXPECT(!Symbol::ChrTest('d')(key));
97 }
98 
99 /* ************************************************************************* */
100 // A custom (nonsensical) formatter.
102  return "special";
103 }
104 
105 TEST(Key, Formatting) {
106  Symbol key('c', 3);
107  EXPECT("c3" == DefaultKeyFormatter(key));
108 
109  // Try streaming keys, should be default-formatted.
110  stringstream ss;
111  ss << StreamedKey(key);
112  EXPECT("c3" == ss.str());
113 
114  // use key_formatter with a function pointer
115  stringstream ss2;
116  ss2 << key_formatter(keyMyFormatter) << StreamedKey(key);
117  EXPECT("special" == ss2.str());
118 
119  // use key_formatter with a function object.
120  stringstream ss3;
121  ss3 << key_formatter(DefaultKeyFormatter) << StreamedKey(key);
122  EXPECT("c3" == ss3.str());
123 }
124 
125 /* ************************************************************************* */
126 int main() {
127  TestResult tr;
128  return TestRegistry::runAllTests(tr);
129 }
130 /* ************************************************************************* */
Point2 a1
Definition: testPose2.cpp:769
const gtsam::Symbol key('X', 0)
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:101
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Definition: BFloat16.h:88
Key X(std::uint64_t j)
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
int main()
Definition: testKey.cpp:126
#define Z
Definition: icosphere.cpp:21
TEST(Key, KeySymbolConversion)
Definition: testKey.cpp:32
Definition: pytypes.h:1403
#define EXPECT(condition)
Definition: Test.h:150
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:58
Key key() const
Definition: Symbol.cpp:40
Key symbol(unsigned char c, std::uint64_t j)
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
Pose3 x1
Definition: testPose3.cpp:663
Key V(std::uint64_t j)
Key KeyTestValue()
Key aKey
Definition: testKey.cpp:29
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Key KeyTestValue< 8 >()
Definition: testKey.cpp:70
Key KeyTestValue< 4 >()
Definition: testKey.cpp:75
Key A(std::uint64_t j)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:33