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);
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;
117  EXPECT("special" == ss2.str());
118 
119  // use key_formatter with a function object.
120  stringstream ss3;
122  EXPECT("c3" == ss3.str());
123 }
124 
125 /* ************************************************************************* */
126 int main() {
127  TestResult tr;
128  return TestRegistry::runAllTests(tr);
129 }
130 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::SymbolGenerator
Definition: inference/Symbol.h:178
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
keyMyFormatter
string keyMyFormatter(Key key)
Definition: testKey.cpp:101
TestHarness.h
z1
Point2 z1
Definition: testTriangulationFactor.cpp:45
KeyTestValue< 4 >
Key KeyTestValue< 4 >()
Definition: testKey.cpp:75
gtsam::symbol_shorthand::A
Key A(std::uint64_t j)
Definition: inference/Symbol.h:148
TEST
TEST(Key, KeySymbolConversion)
Definition: testKey.cpp:32
TestableAssertions.h
Provides additional testing facilities for common data structures.
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
ss
static std::stringstream ss
Definition: testBTree.cpp:31
Key.h
align_3::a1
Point2 a1
Definition: testPose2.cpp:769
gtsam::symbol_shorthand::X
Key X(std::uint64_t j)
Definition: inference/Symbol.h:171
KeyTestValue
Key KeyTestValue()
x1
Pose3 x1
Definition: testPose3.cpp:663
gtsam::symbol
Key symbol(unsigned char c, std::uint64_t j)
Definition: inference/Symbol.h:139
Symbol.h
gtsam::Symbol::key
Key key() const
Definition: Symbol.cpp:40
TestResult
Definition: TestResult.h:26
str
Definition: pytypes.h:1524
key
const gtsam::Symbol key('X', 0)
main
int main()
Definition: testKey.cpp:126
gtsam::StreamedKey
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:64
gtsam
traits
Definition: chartTesting.h:28
std
Definition: BFloat16.h:88
gtsam::key_formatter
Definition: Key.h:78
KeyTestValue< 8 >
Key KeyTestValue< 8 >()
Definition: testKey.cpp:70
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
aKey
Key aKey
Definition: testKey.cpp:29
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
Z
#define Z
Definition: icosphere.cpp:21
gtsam::symbol_shorthand::V
Key V(std::uint64_t j)
Definition: inference/Symbol.h:169
v1
Vector v1
Definition: testSerializationBase.cpp:38
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:06:07