From 5ad9cd1a1a18bc97ff0bc651f9704e8fa0c7eedb Mon Sep 17 00:00:00 2001 From: Bertrand Augereau Date: Tue, 10 Nov 2015 20:30:51 +0100 Subject: COMMON: More sort unit tests --- test/common/algorithm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/common/algorithm.h') diff --git a/test/common/algorithm.h b/test/common/algorithm.h index 6eecae3635..ccf6469f67 100644 --- a/test/common/algorithm.h +++ b/test/common/algorithm.h @@ -46,11 +46,29 @@ public: } void test_pod_sort() { + { + int dummy; + Common::sort(&dummy, &dummy); + TS_ASSERT_EQUALS(checkSort(&dummy, &dummy, Common::Less()), true); + } + { + int array[] = { 12 }; + Common::sort(array, ARRAYEND(array)); + TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less()), true); + + // already sorted + Common::sort(array, ARRAYEND(array)); + TS_ASSERT_EQUALS(checkSort(array, ARRAYEND(array), Common::Less()), 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()), 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()), true); -- cgit v1.2.3 From a19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd Mon Sep 17 00:00:00 2001 From: Borja Lorente Escobar Date: Wed, 2 Mar 2016 17:07:50 +0100 Subject: COMMON: Add replace functions to Common and String. COMMON: Add replacement to common/algorithm.h COMMON: Intermediate commit to show doubts. COMMON: Basic String::replace() methods implemented. COMMON: Fix typo in the algorithm.h documentation. COMMON: Fix documentation of String::replace() COMMON: Fix formatting issues in method signatures. COMMON: Add assert and reformat loops in str and algorithm. COMMON: Fix typo in comment. COMMON: Fix style in string test cases. COMMON: Add Doxygen documentation to algorithm and String. COMMON: Add Doxygen documentation to algorithm and String. COMMON: Add Doxygen documentation to algorithm. COMMON: Fix style in algorithm comments. COMMON: Add Doxygen comments to String. COMMON: Add Doxygen comments to algorithm test function. COMMON: Add String support for substring replace. COMMON: Fix string replace to comply with STL COMMON: Fix documentation on string replace COMMON: Fix style in string replace COMMON: Fix unwanted reference problem in String::replace(). COMMON: Fix indentation in comments for replace COMMON: Fix indentation in replace COMMON: Fix comments in String::replace to match implementation. COMMON: Remove assert to allow for not-null-terminated character arrays COMMON: Add new test for String::replace COMMON: Fix broken comments on String::replace COMMON: Fix sharing bug on ensureCapacity COMMON: Remove superfluous call to makeUnique() --- test/common/algorithm.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test/common/algorithm.h') diff --git a/test/common/algorithm.h b/test/common/algorithm.h index ccf6469f67..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 + 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) {} @@ -97,4 +125,32 @@ public: Common::sort(list.begin(), list.end()); TS_ASSERT_EQUALS(checkSort(list.begin(), list.end(), Common::Less()), 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 original; + Common::List 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); + } }; -- cgit v1.2.3