From 16c2500a9993878aead5a1c33762d71edbe44ee9 Mon Sep 17 00:00:00 2001 From: Richard Roberts Date: Fri, 23 Sep 2011 15:34:57 +0000 Subject: [PATCH] Bug fix and unit tests for Permutation, documentation fixes --- gtsam/inference/Permutation.cpp | 4 +- gtsam/inference/Permutation.h | 24 +++--- gtsam/inference/tests/testPermutation.cpp | 100 ++++++++++++++++++++++ 3 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 gtsam/inference/tests/testPermutation.cpp diff --git a/gtsam/inference/Permutation.cpp b/gtsam/inference/Permutation.cpp index 781ebb30a..f162df712 100644 --- a/gtsam/inference/Permutation.cpp +++ b/gtsam/inference/Permutation.cpp @@ -45,7 +45,7 @@ Permutation Permutation::PullToFront(const vector& toFront, size_t size, // Put the pulled variables at the front of the permutation and set up the // pulled flags. - size_t toFrontUniqueSize; + size_t toFrontUniqueSize = 0; for(Index j=0; j permuted(permutation, container); - * permuted[index1]; - * permuted[index2]; - * which is equivalent to: - * container[permutation[index1]]; - * container[permutation[index2]]; - * but more concise. - */ - - /** - * Permute the permutation, p1.permute(p2)[i] is equivalent to p2[p1[i]]. + * Permute the permutation, p1.permute(p2)[i] is equivalent to p1[p2[i]]. */ Permutation::shared_ptr permute(const Permutation& permutation) const; @@ -154,7 +142,15 @@ protected: /** - * Definition of Permuted class, see above comment for details. + * Syntactic sugar for accessing another container through a permutation. + * Allows the syntax: + * Permuted permuted(permutation, container); + * permuted[index1]; + * permuted[index2]; + * which is equivalent to: + * container[permutation[index1]]; + * container[permutation[index2]]; + * but more concise. */ template class Permuted { diff --git a/gtsam/inference/tests/testPermutation.cpp b/gtsam/inference/tests/testPermutation.cpp new file mode 100644 index 000000000..a6c5116b5 --- /dev/null +++ b/gtsam/inference/tests/testPermutation.cpp @@ -0,0 +1,100 @@ +/* + * testPermutation.cpp + * + * Created on: Sep 22, 2011 + * Author: richard + */ + +#include + +#include +#include + +#include + +using namespace gtsam; +using namespace std; + +TEST(Permutation, Identity) { + Permutation expected(5); + expected[0] = 0; + expected[1] = 1; + expected[2] = 2; + expected[3] = 3; + expected[4] = 4; + + Permutation actual = Permutation::Identity(5); + + EXPECT(assert_equal(expected, actual)); +} + +TEST(Permutation, PullToFront) { + Permutation expected(5); + expected[0] = 4; + expected[1] = 0; + expected[2] = 2; + expected[3] = 1; + expected[4] = 3; + + std::vector toFront; + toFront.push_back(4); + toFront.push_back(0); + toFront.push_back(2); + Permutation actual = Permutation::PullToFront(toFront, 5); + + EXPECT(assert_equal(expected, actual)); +} + +TEST(Permutation, PushToBack) { + Permutation expected(5); + expected[0] = 1; + expected[1] = 3; + expected[2] = 4; + expected[3] = 0; + expected[4] = 2; + + std::vector toBack; + toBack.push_back(4); + toBack.push_back(0); + toBack.push_back(2); + Permutation actual = Permutation::PushToBack(toBack, 5); + + EXPECT(assert_equal(expected, actual)); +} + +TEST(Permutation, compose) { + Permutation p1(5); + p1[0] = 1; + p1[1] = 2; + p1[2] = 3; + p1[3] = 4; + p1[4] = 0; + + Permutation p2(5); + p2[0] = 1; + p2[1] = 2; + p2[2] = 4; + p2[3] = 3; + p2[4] = 0; + + Permutation expected(5); + expected[0] = 2; + expected[1] = 4; + expected[2] = 3; + expected[3] = 0; + expected[4] = 1; + + Permutation actual = *p2.permute(p1); + + EXPECT(assert_equal(expected, actual)); + LONGS_EQUAL(p2[p1[0]], actual[0]); + LONGS_EQUAL(p2[p1[1]], actual[1]); + LONGS_EQUAL(p2[p1[2]], actual[2]); + LONGS_EQUAL(p2[p1[3]], actual[3]); + LONGS_EQUAL(p2[p1[4]], actual[4]); +} + +/* ************************************************************************* */ +int main() { TestResult tr; return TestRegistry::runAllTests(tr); } + +