testDiscreteValues.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  * testDiscreteValues.cpp
14  *
15  * @date Jan, 2022
16  * @author Frank Dellaert
17  */
18 
20 #include <gtsam/base/Testable.h>
23 
24 using namespace std;
25 using namespace gtsam;
26 
27 static const DiscreteValues kExample{{12, 1}, {5, 0}};
28 
29 /* ************************************************************************* */
30 // Check insert
31 TEST(DiscreteValues, Insert) {
32  EXPECT(assert_equal({{12, 1}, {5, 0}, {13, 2}},
33  DiscreteValues(kExample).insert({{13, 2}})));
34 }
35 
36 /* ************************************************************************* */
37 // Check update.
38 TEST(DiscreteValues, Update) {
39  EXPECT(assert_equal({{12, 2}, {5, 0}},
40  DiscreteValues(kExample).update({{12, 2}})));
41 }
42 
43 /* ************************************************************************* */
44 // Test DiscreteValues::filter
45 TEST(DiscreteValues, Filter) {
46  DiscreteValues values = {{12, 1}, {5, 0}, {13, 2}};
47  DiscreteKeys keys = {{12, 0}, {13, 0}, {99, 0}}; // 99 is missing in values
48 
49  EXPECT(assert_equal(DiscreteValues({{12, 1}, {13, 2}}), values.filter(keys)));
50 }
51 
52 /* ************************************************************************* */
53 // Test DiscreteValues::missingKeys
54 TEST(DiscreteValues, MissingKeys) {
55  DiscreteValues values = {{12, 1}, {5, 0}};
56  DiscreteKeys keys = {{12, 0}, {5, 0}, {99, 0}, {42, 0}}; // 99 and 42 are missing
57 
58  EXPECT(assert_equal(DiscreteKeys({{99, 0}, {42, 0}}), values.missingKeys(keys)));
59 }
60 
61 /* ************************************************************************* */
62 // Check markdown representation with a value formatter.
63 TEST(DiscreteValues, markdownWithValueFormatter) {
64  string expected =
65  "|Variable|value|\n"
66  "|:-:|:-:|\n"
67  "|B|-|\n"
68  "|A|One|\n";
69  auto keyFormatter = [](Key key) { return key == 12 ? "A" : "B"; };
70  DiscreteValues::Names names{{12, {"Zero", "One", "Two"}}, {5, {"-", "+"}}};
71  string actual = kExample.markdown(keyFormatter, names);
72  EXPECT(actual == expected);
73 }
74 
75 /* ************************************************************************* */
76 // Check html representation with a value formatter.
77 TEST(DiscreteValues, htmlWithValueFormatter) {
78  string expected =
79  "<div>\n"
80  "<table class='DiscreteValues'>\n"
81  " <thead>\n"
82  " <tr><th>Variable</th><th>value</th></tr>\n"
83  " </thead>\n"
84  " <tbody>\n"
85  " <tr><th>B</th><td>-</td></tr>\n"
86  " <tr><th>A</th><td>One</td></tr>\n"
87  " </tbody>\n"
88  "</table>\n"
89  "</div>";
90  auto keyFormatter = [](Key key) { return key == 12 ? "A" : "B"; };
91  DiscreteValues::Names names{{12, {"Zero", "One", "Two"}}, {5, {"-", "+"}}};
92  string actual = kExample.html(keyFormatter, names);
93  EXPECT(actual == expected);
94 }
95 
96 /* ************************************************************************* */
97 int main() {
98  TestResult tr;
99  return TestRegistry::runAllTests(tr);
100 }
101 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::DiscreteValues::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a markdown table.
Definition: DiscreteValues.cpp:106
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::DiscreteValues::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a html table.
Definition: DiscreteValues.cpp:123
main
int main()
Definition: testDiscreteValues.cpp:97
gtsam::DiscreteValues::update
DiscreteValues & update(const DiscreteValues &values)
Update values with corresponding keys from another DiscreteValues object.
Definition: DiscreteValues.cpp:82
Signature.h
signatures for conditional densities
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:916
TestResult
Definition: TestResult.h:26
key
const gtsam::Symbol key('X', 0)
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam
traits
Definition: SFMdata.h:40
DiscreteValues.h
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
std
Definition: BFloat16.h:88
gtsam::DiscreteValues::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: DiscreteValues.h:71
TEST
TEST(DiscreteValues, Insert)
Definition: testDiscreteValues.cpp:31
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
gtsam::DiscreteValues::Names
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition: DiscreteValues.h:158
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
kExample
static const DiscreteValues kExample
Definition: testDiscreteValues.cpp:27


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:06:33