Bug fix and unit tests for Permutation, documentation fixes

release/4.3a0
Richard Roberts 2011-09-23 15:34:57 +00:00
parent 63ca74e492
commit 16c2500a99
3 changed files with 112 additions and 16 deletions

View File

@ -45,7 +45,7 @@ Permutation Permutation::PullToFront(const vector<Index>& 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<toFront.size(); ++j) {
if(!pulled[toFront[j]]) {
ret[j] = toFront[j];
@ -148,7 +148,7 @@ Permutation::shared_ptr Permutation::inverse() const {
/* ************************************************************************* */
void Permutation::print(const std::string& str) const {
std::cout << str;
std::cout << str << " ";
BOOST_FOREACH(Index s, rangeIndices_) { std::cout << s << " "; }
std::cout << std::endl;
}

View File

@ -116,19 +116,7 @@ public:
bool equals(const Permutation& rhs, double tol=0.0) const { return rangeIndices_ == rhs.rangeIndices_; }
/**
* Syntactic sugar for accessing another container through a permutation.
* Allows the syntax:
* Permuted<Container> 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<Container> permuted(permutation, container);
* permuted[index1];
* permuted[index2];
* which is equivalent to:
* container[permutation[index1]];
* container[permutation[index2]];
* but more concise.
*/
template<typename CONTAINER, typename VALUETYPE = typename CONTAINER::value_reference_type>
class Permuted {

View File

@ -0,0 +1,100 @@
/*
* testPermutation.cpp
*
* Created on: Sep 22, 2011
* Author: richard
*/
#include <CppUnitLite/TestHarness.h>
#include <gtsam/base/Testable.h>
#include <gtsam/inference/Permutation.h>
#include <vector>
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<Index> 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<Index> 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); }