Skip to content

Commit

Permalink
added more functions to arraylist and more tests while developing the…
Browse files Browse the repository at this point in the history
… proper test for FacePolygon::operator== (#30)
  • Loading branch information
lene committed Mar 28, 2014
1 parent 6eaff81 commit bfbeee9
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Displayable/Object/FacePolygon.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ FacePolygon<D, N_vertex>::index(unsigned i) const {
template <unsigned D, unsigned N_vertex>
bool
FacePolygon<D, N_vertex>::operator==(const FacePolygon<D, N_vertex> &other) const {
if (!isPermutation(
if (!isCircularPermutation(
ArrayList<N_vertex, unsigned>(_indices),
ArrayList<N_vertex, unsigned>(other._indices))) {
return false;
Expand Down
20 changes: 20 additions & 0 deletions src/VecMath/ArrayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ template <unsigned size, typename T> class ArrayList {
ArrayList<size-1, T> minusElement(unsigned i) const;

bool contains(const T &x) const;

bool operator==(const ArrayList<size, T> &other) const;

ArrayList<size, T> &shift(int amount = 1);
ArrayList<size, T> &reverse();

/// String representation.
std::string toString() const;
Expand Down Expand Up @@ -94,6 +99,8 @@ template <typename T> class ArrayList<1, T> {
ArrayList<0, T> minusElement(unsigned) const;

bool contains (const T &x) const;

bool operator==(const ArrayList<1, T> &other) const;

/// Empty String.
std::string toString() const;
Expand Down Expand Up @@ -121,11 +128,24 @@ template <typename T> class ArrayList<0, T> {

};

template<unsigned size, typename T>
std::ostream &operator<<(std::ostream &o, const ArrayList<size, T> &list) {
o << list.toString();
return o;
}

template<unsigned size, typename T>
bool isPermutation(const ArrayList<size, T> &list1, const ArrayList<size, T> &list2);
template<typename T>
bool isPermutation(const ArrayList<1, T> &list1, const ArrayList<1, T> &list2);

template<unsigned size, typename T>
bool isCircularPermutation(const ArrayList<size, T> &list1, const ArrayList<size, T> &list2);
template<typename T>
bool isCircularPermutation(const ArrayList<2, T> &list1, const ArrayList<2, T> &list2);
template<typename T>
bool isCircularPermutation(const ArrayList<1, T> &list1, const ArrayList<1, T> &list2);

template<typename T>
ArrayList<1, T> makeArrayList(const T &x0);
template<typename T>
Expand Down
55 changes: 54 additions & 1 deletion src/VecMath/ArrayList.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ template <unsigned size, typename T>
T &ArrayList<size, T>::operator[](unsigned i) {
assert(i < size);
if (i == 0) return head();
ArrayList<size-1, T> _tail = tail();
ArrayList<size-1, T> &_tail = tail();
return _tail.operator[](i-1);
}

Expand All @@ -98,6 +98,30 @@ bool ArrayList<size, T>::contains (const T &x) const {
return tail().contains(x);
}

template <unsigned size, typename T>
bool ArrayList<size, T>::operator==(const ArrayList<size, T> &other) const {
return head() == other.head() && tail() == other.tail();
}

template <unsigned size, typename T>
ArrayList<size, T> &ArrayList<size, T>::shift(int amount) {
assert(amount == 1);
T temp = operator[](0);
for (unsigned i = 1; i < size; ++i) {
operator[](i-1) = operator[](i);
}
operator[](size-1) = temp;
return *this;
}

template <unsigned size, typename T>
ArrayList<size, T> &ArrayList<size, T>::reverse() {
for (unsigned i = 0; i < size/2; ++i) {
std::swap(operator[](i), operator[](size-1-i));
}
return *this;
}

template <unsigned size, typename T>
std::string ArrayList<size, T>::toString() const {
std::ostringstream o;
Expand Down Expand Up @@ -158,6 +182,11 @@ bool ArrayList<1, T>::contains (const T &x) const {
return head() == x;
}

template <typename T>
bool ArrayList<1, T>::operator==(const ArrayList<1, T> &other) const {
return head() == other.head();
}

template <typename T>
std::string ArrayList<1, T>::toString() const {
std::ostringstream o;
Expand Down Expand Up @@ -194,6 +223,30 @@ bool isPermutation(const ArrayList<1, T> &list1, const ArrayList<1, T> &list2) {
return list1.head() == list2.head();
}

template<unsigned size, typename T>
bool isCircularPermutation(const ArrayList<size, T> &list1, const ArrayList<size, T> &list2) {
ArrayList<size, T> shifted = list2;
ArrayList<size, T> reversed = list2;
reversed.reverse();
for (unsigned shift = 0; shift < size-1; ++shift) {
if (list1 == shifted) return true;
if (list1 == reversed) return true;
shifted.shift();
reversed.shift();
}
return false;
}

template<typename T>
bool isCircularPermutation(const ArrayList<2, T> &list1, const ArrayList<2, T> &list2) {
return isPermutation(list1, list2);
}

template<typename T>
bool isCircularPermutation(const ArrayList<1, T> &list1, const ArrayList<1, T> &list2) {
return list1.head() == list2.head();
}

////////////////////////////////////////////////////////////////////////////////

template<typename T>
Expand Down
35 changes: 31 additions & 4 deletions tests/Test_ArrayList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void Test_ArrayList::minusElement3() {

}

void Test_ArrayList::test_isPermutation2() {
void Test_ArrayList::isPermutation2() {

ArrayList<2, int> original = makeArrayList(0, 1);

Expand All @@ -111,7 +111,7 @@ void Test_ArrayList::test_isPermutation2() {
QVERIFY(!isPermutation(original, makeArrayList(1, 1)));
}

void Test_ArrayList::test_isPermutation3() {
void Test_ArrayList::isPermutation3() {

ArrayList<3, int> original2 = makeArrayList(0, 1, 2);

Expand All @@ -127,7 +127,7 @@ void Test_ArrayList::test_isPermutation3() {
QVERIFY(!isPermutation(original2, makeArrayList(0, 1, 3)));
}

void Test_ArrayList::test_isPermutation5() {
void Test_ArrayList::isPermutation5() {

ArrayList<5, int> original3 = makeArrayList(0, 1, 2, 3, 4);

Expand All @@ -137,7 +137,7 @@ void Test_ArrayList::test_isPermutation5() {
QVERIFY(!isPermutation(original3, makeArrayList(5, 3, 2, 1, 0)));
}

void Test_ArrayList::test_isPermutation10() {
void Test_ArrayList::isPermutation10() {

ArrayList<10, int> original4 = makeArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

Expand Down Expand Up @@ -179,3 +179,30 @@ void Test_ArrayList::test_isPermutation10() {
}
}
#endif

void Test_ArrayList::shift() {
test(makeArrayList(1,2,3).shift() == makeArrayList(2, 3, 1), std::string("shift?"));
}

void Test_ArrayList::reverse() {
test(makeArrayList(1,2,3).reverse() == makeArrayList(3, 2, 1), std::string("reverse (odd number of elements)"));
test(makeArrayList(1,2,3,4).reverse() == makeArrayList(4, 3, 2, 1), std::string("reverse (even number of elements)"));
}


void Test_ArrayList::circularPermutation() {
test(isCircularPermutation(makeArrayList(1), makeArrayList(1)), std::string("one element, equal"));
test(!isCircularPermutation(makeArrayList(1), makeArrayList(2)), std::string("one element, not equal"));
test(isCircularPermutation(makeArrayList(1,2), makeArrayList(1,2)), std::string("two elements, equal"));
test(isCircularPermutation(makeArrayList(1,2), makeArrayList(2,1)), std::string("two elements, swapped"));
test(!isCircularPermutation(makeArrayList(1,2), makeArrayList(1,3)), std::string("two elements, not equal"));
test(isCircularPermutation(makeArrayList(1,2,3), makeArrayList(1,2,3)), std::string("three elements, equal"));
test(isCircularPermutation(makeArrayList(1,2,3), makeArrayList(3,1,2)), std::string("three elements, shifted"));
test(isCircularPermutation(makeArrayList(1,2,3), makeArrayList(3,2,1)), std::string("three elements, reversed"));
test(!isCircularPermutation(makeArrayList(1,2,3), makeArrayList(4,2,1)), std::string("three elements, not equal"));
test(isCircularPermutation(makeArrayList(1,2,3,4), makeArrayList(1,2,3,4)), std::string("four elements, equal"));
test(isCircularPermutation(makeArrayList(1,2,3,4), makeArrayList(4,1,2,3)), std::string("four elements, shifted"));
test(isCircularPermutation(makeArrayList(1,2,3,4), makeArrayList(4,3,2,1)), std::string("four elements, reversed"));
test(!isCircularPermutation(makeArrayList(1,2,3,4), makeArrayList(1,2,4,3)), std::string("four elements, garbled"));

}
11 changes: 7 additions & 4 deletions tests/Test_ArrayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ private slots:
void makeListRuns();
void minusElement2();
void minusElement3();
void test_isPermutation2();
void test_isPermutation3();
void test_isPermutation5();
void test_isPermutation10();
void isPermutation2();
void isPermutation3();
void isPermutation5();
void isPermutation10();
void shift();
void reverse();
void circularPermutation();

# ifdef TEST_ACCESS_OPERATOR
void accessOperator2();
Expand Down
1 change: 1 addition & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ int main(int argc, char **argv) {
runner.run(test.second);
}
# else
runner.run(new Test_ArrayList);
runner.run(new Test_FacePolygon);
# endif
} else {
Expand Down

0 comments on commit bfbeee9

Please sign in to comment.