diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/common/algorithm.h | 74 | ||||
-rw-r--r-- | test/common/array.h | 82 | ||||
-rw-r--r-- | test/common/rational.h | 11 | ||||
-rw-r--r-- | test/common/str.h | 77 | ||||
-rw-r--r-- | test/module.mk | 1 |
5 files changed, 245 insertions, 0 deletions
diff --git a/test/common/algorithm.h b/test/common/algorithm.h index 6eecae3635..eeed59d821 100644 --- a/test/common/algorithm.h +++ b/test/common/algorithm.h @@ -25,6 +25,34 @@ class AlgorithmTestSuite : public CxxTest::TestSuite { return true; } + /** + * Auxiliary function to check the equality of two generic collections (A and B), from one_first to one_last. + * + * @note: It assumes that other has at least (one_last - one-first) lenght, starting from other_first. + * + * @param one_first: The first element of the first collection to be compared. + * @param one_last: The last element of the first collection to be compared. + * @param other_first: The first element of the collection to be compared. + * @return true if, for each index i in [one_first, one_last), A[i] == B[i], false otherwise. + */ + template<typename It> + bool checkEqual(It one_first, It one_last, It other_first) { + if (one_first == one_last) + return true; + + // Check whether two containers have the same items in the same order, + // starting from some iterators one_first and other_first + // + // It iterates through the containers, comparing the elements one by one. + // If it finds a discrepancy, it returns false. Otherwise, it returns true. + + for (; one_first != one_last; ++one_first, ++other_first) + if (*one_first != *other_first) + return false; + + return true; + } + struct Item { int value; Item(int v) : value(v) {} @@ -47,10 +75,28 @@ public: void test_pod_sort() { { + int dummy; + Common::sort(&dummy, &dummy); + TS_ASSERT_EQUALS(checkSort(&dummy, &dummy, Common::Less<int>()), true); + } + { + int array[] = { 12 }; + Common::sort(array, ARRAYEND(array)); + TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less<int>()), true); + + // already sorted + Common::sort(array, ARRAYEND(array)); + TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less<int>()), true); + } + { int array[] = { 63, 11, 31, 72, 1, 48, 32, 69, 38, 31 }; Common::sort(array, ARRAYEND(array)); TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less<int>()), true); + int sortedArray[] = { 1, 11, 31, 31, 32, 38, 48, 63, 69, 72 }; + for (size_t i = 0; i < 10; ++i) + TS_ASSERT_EQUALS(array[i], sortedArray[i]); + // already sorted Common::sort(array, ARRAYEND(array)); TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less<int>()), true); @@ -79,4 +125,32 @@ public: Common::sort(list.begin(), list.end()); TS_ASSERT_EQUALS(checkSort(list.begin(), list.end(), Common::Less<Item>()), true); } + + void test_string_replace() { + + Common::String original = "Hello World"; + Common::String expected = "Hells Wsrld"; + + Common::replace(original.begin(), original.end(), 'o', 's'); + + TS_ASSERT_EQUALS(original, expected); + } + + void test_container_replace() { + + Common::List<int> original; + Common::List<int> expected; + for (int i = 0; i < 6; ++i) { + original.push_back(i); + if (i == 3) { + expected.push_back(5); + } else { + expected.push_back(i); + } + } + + Common::replace(original.begin(), original.end(), 3, 5); + + TS_ASSERT_EQUALS(checkEqual(original.begin(), original.end(), expected.begin()), true); + } }; diff --git a/test/common/array.h b/test/common/array.h index f0027ec201..2dc67837db 100644 --- a/test/common/array.h +++ b/test/common/array.h @@ -44,6 +44,48 @@ class ArrayTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS(iter, array.end()); } + void test_erase_iterator() { + Common::Array<int> array; + Common::Array<int>::iterator iter; + + // Fill the array with some random data + array.push_back(17); + array.push_back(33); + array.push_back(-11); + + iter = array.begin(); + ++iter; + + iter = array.erase(iter); + TS_ASSERT_DIFFERS(iter, array.end()); + TS_ASSERT_EQUALS(*iter, -11); + TS_ASSERT_EQUALS(array.size(), (unsigned int)2); + TS_ASSERT_EQUALS(array[0], 17); + TS_ASSERT_EQUALS(array[1], -11); + } + + void test_insert_iterator() { + Common::Array<int> array; + Common::Array<int>::iterator iter; + + // Fill the array with some random data + array.push_back(17); + array.push_back(33); + array.push_back(-11); + + iter = array.begin(); + ++iter; + + array.insert(iter, 99); + + TS_ASSERT_EQUALS(*iter, 99); + TS_ASSERT_EQUALS(array.size(), (unsigned int)4); + TS_ASSERT_EQUALS(array[0], 17); + TS_ASSERT_EQUALS(array[1], 99); + TS_ASSERT_EQUALS(array[2], 33); + TS_ASSERT_EQUALS(array[3], -11); + } + void test_direct_access() { Common::Array<int> array; @@ -312,3 +354,43 @@ class ArrayTestSuite : public CxxTest::TestSuite } }; + +struct ListElement { + int value; + + ListElement(int v) : value(v) {} +}; + +static int compareInts(const void *a, const void *b) { + return ((ListElement *)a)->value - ((ListElement *)b)->value; +} + +class SortedArrayTestSuite : public CxxTest::TestSuite { +public: + void test_insert() { + Common::SortedArray<ListElement *> container(compareInts); + Common::SortedArray<ListElement *>::iterator iter; + + // Fill the container with some random data + container.insert(new ListElement(1)); + container.insert(new ListElement(7)); + container.insert(new ListElement(8)); + container.insert(new ListElement(3)); + container.insert(new ListElement(5)); + container.insert(new ListElement(4)); + container.insert(new ListElement(9)); + container.insert(new ListElement(2)); + container.insert(new ListElement(6)); + + // Verify contents are correct + iter = container.begin(); + + for (int i = 1; i < 10; i++) { + TS_ASSERT_EQUALS((*iter)->value, i); + ++iter; + } + + TS_ASSERT_EQUALS(iter, container.end()); + } + +}; diff --git a/test/common/rational.h b/test/common/rational.h index 46dfc278c7..23d0c10acd 100644 --- a/test/common/rational.h +++ b/test/common/rational.h @@ -130,4 +130,15 @@ public: TS_ASSERT_EQUALS(r1 / 2, Common::Rational(1, 4)); TS_ASSERT_EQUALS(2 / r1, Common::Rational(4, 1)); } + + void test_isOne() { + Common::Rational r0(5, 5); + Common::Rational r1(1, 2); + Common::Rational r2(2, 1); + Common::Rational r3(1, 1); + TS_ASSERT(r0.isOne()); + TS_ASSERT(!r1.isOne()); + TS_ASSERT(!r2.isOne()); + TS_ASSERT(r3.isOne()); + } }; diff --git a/test/common/str.h b/test/common/str.h index adc6a099e4..461b26088a 100644 --- a/test/common/str.h +++ b/test/common/str.h @@ -332,6 +332,9 @@ class StringTestSuite : public CxxTest::TestSuite TS_ASSERT(!Common::matchString("monkey.s99", "monkey.s*1")); TS_ASSERT(Common::matchString("monkey.s101", "monkey.s*1")); + TS_ASSERT(Common::matchString("monkey.s01", "monkey.s##")); + TS_ASSERT(!Common::matchString("monkey.s01", "monkey.###")); + TS_ASSERT(!Common::String("").matchString("*_")); TS_ASSERT(Common::String("a").matchString("a***")); } @@ -416,4 +419,78 @@ class StringTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "ABCde", 4), 0); TS_ASSERT_LESS_THAN(scumm_strnicmp("abCd", "ABCde", 5), 0); } + + void test_replace() { + // Tests created with the results of the STL std::string class + + // -------------------------- + // Tests without displacement + // -------------------------- + Common::String testString = Common::String("This is the original string."); + + // Positions and sizes as parameters, string as replacement + testString.replace(12, 8, Common::String("newnewne")); + TS_ASSERT_EQUALS(testString, Common::String("This is the newnewne string.")); + + // The same but with char* + testString.replace(0, 4, "That"); + TS_ASSERT_EQUALS(testString, Common::String("That is the newnewne string.")); + + // Using iterators (also a terribly useless program as a test). + testString.replace(testString.begin(), testString.end(), "That is the supernew string."); + TS_ASSERT_EQUALS(testString, Common::String("That is the supernew string.")); + + // With sub strings of character arrays. + testString.replace(21, 6, "That phrase is new.", 5, 6); + TS_ASSERT_EQUALS(testString, Common::String("That is the supernew phrase.")); + + // Now with substrings. + testString.replace(12, 2, Common::String("That hy is new."), 5, 2); + TS_ASSERT_EQUALS(testString, Common::String("That is the hypernew phrase.")); + + // -------------------------- + // Tests with displacement + // -------------------------- + testString = Common::String("Hello World"); + + // Positions and sizes as parameters, string as replacement + testString.replace(6, 5, Common::String("friends")); + TS_ASSERT_EQUALS(testString, Common::String("Hello friends")); + + // The same but with char* + testString.replace(0, 5, "Good"); + TS_ASSERT_EQUALS(testString, Common::String("Good friends")); + + // Using iterators (also a terribly useless program as a test) + testString.replace(testString.begin() + 4, testString.begin() + 5, " coffee "); + TS_ASSERT_EQUALS(testString, Common::String("Good coffee friends")); + + // With sub strings of character arrays + testString.replace(4, 0, "Lorem ipsum expresso dolor sit amet", 11, 9); + TS_ASSERT_EQUALS(testString, Common::String("Good expresso coffee friends")); + + // Now with substrings + testString.replace(5, 9, Common::String("Displaced ristretto string"), 10, 10); + TS_ASSERT_EQUALS(testString, Common::String("Good ristretto coffee friends")); + + // ----------------------- + // Deep copy compliance + // ----------------------- + + // Makes a deep copy without changing the length of the original + Common::String s1 = "TestTestTestTestTestTestTestTestTestTestTest"; + Common::String s2(s1); + TS_ASSERT_EQUALS(s1, "TestTestTestTestTestTestTestTestTestTestTest"); + TS_ASSERT_EQUALS(s2, "TestTestTestTestTestTestTestTestTestTestTest"); + s1.replace(0, 4, "TEST"); + TS_ASSERT_EQUALS(s1, "TESTTestTestTestTestTestTestTestTestTestTest"); + TS_ASSERT_EQUALS(s2, "TestTestTestTestTestTestTestTestTestTestTest"); + + // Makes a deep copy when we shorten the string + Common::String s3 = "TestTestTestTestTestTestTestTestTestTestTest"; + Common::String s4(s3); + s3.replace(0, 32, ""); + TS_ASSERT_EQUALS(s3, "TestTestTest"); + TS_ASSERT_EQUALS(s4, "TestTestTestTestTestTestTestTestTestTestTest"); + } }; diff --git a/test/module.mk b/test/module.mk index 11ee6bd200..e591854ace 100644 --- a/test/module.mk +++ b/test/module.mk @@ -26,6 +26,7 @@ endif #TEST_LDFLAGS += -L/usr/X11R6/lib -lX11 +test: CXXFLAGS += -DCOMPILING_TESTS=1 test: test/runner ./test/runner test/runner: test/runner.cpp $(TEST_LIBS) |