aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorBorja Lorente Escobar2016-03-02 17:07:50 +0100
committerBorja Lorente2016-05-16 22:01:21 +0200
commita19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd (patch)
tree09d1e0bd96a74f17b8928676b233a52fa9831f46 /test
parentf397a7313810f2538a7474aa1f7040252617a2ba (diff)
downloadscummvm-rg350-a19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd.tar.gz
scummvm-rg350-a19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd.tar.bz2
scummvm-rg350-a19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd.zip
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()
Diffstat (limited to 'test')
-rw-r--r--test/common/algorithm.h56
-rw-r--r--test/common/str.h74
2 files changed, 130 insertions, 0 deletions
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<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) {}
@@ -97,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/str.h b/test/common/str.h
index 3ab5d828c1..461b26088a 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -419,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");
+ }
};