diff options
Diffstat (limited to 'test/unit/testSparseVector.cxx')
-rw-r--r-- | test/unit/testSparseVector.cxx | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/test/unit/testSparseVector.cxx b/test/unit/testSparseVector.cxx index 16498397a..c4c3a6ab1 100644 --- a/test/unit/testSparseVector.cxx +++ b/test/unit/testSparseVector.cxx @@ -11,6 +11,7 @@ #include "Platform.h" #include "Position.h" +#include "UniqueString.h" #include "SplitVector.h" #include "Partitioning.h" #include "SparseVector.h" @@ -21,11 +22,11 @@ // Helper to produce a string representation of a SparseVector<const char *> // to simplify checks. -static std::string Representation(const SparseVector<const char *> &st) { +static std::string Representation(const SparseVector<UniqueString> &st) { std::string ret; for (int i = 0;i < st.Length();i++) { - const char *value = st.ValueAt(i); - if (value) + const char *value = st.ValueAt(i).get(); + if (value && *value) ret += value; else ret += "-"; @@ -35,7 +36,7 @@ static std::string Representation(const SparseVector<const char *> &st) { TEST_CASE("SparseVector") { - SparseVector<const char *> st; + SparseVector<UniqueString> st; SECTION("IsEmptyInitially") { REQUIRE(1 == st.Elements()); @@ -46,24 +47,26 @@ TEST_CASE("SparseVector") { SECTION("InsertSpace") { st.InsertSpace(0, 5); REQUIRE(1 == st.Elements()); - REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(0)); - REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(1)); - REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(4)); + REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(0).get()); + REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(1).get()); + REQUIRE(static_cast<const char *>(nullptr) == st.ValueAt(4).get()); st.Check(); } SECTION("InsertValue") { st.InsertSpace(0, 5); - st.SetValueAt(3, "3"); + st.SetValueAt(3, UniqueStringCopy("3")); REQUIRE(2 == st.Elements()); REQUIRE("---3-" == Representation(st)); st.Check(); } - SECTION("InsertAndDeleteValue") { + SECTION("InsertAndChangeAndDeleteValue") { st.InsertSpace(0, 5); REQUIRE(5 == st.Length()); - st.SetValueAt(3, "3"); + st.SetValueAt(3, UniqueStringCopy("3")); + REQUIRE(2 == st.Elements()); + st.SetValueAt(3, UniqueStringCopy("4")); REQUIRE(2 == st.Elements()); st.DeletePosition(3); REQUIRE(1 == st.Elements()); @@ -75,13 +78,13 @@ TEST_CASE("SparseVector") { SECTION("InsertAndDeleteAtStart") { REQUIRE(1 == st.Elements()); st.InsertSpace(0, 5); - st.SetValueAt(0, "3"); + st.SetValueAt(0, UniqueStringCopy("3")); REQUIRE(1 == st.Elements()); REQUIRE("3----" == Representation(st)); st.DeletePosition(0); REQUIRE(1 == st.Elements()); REQUIRE("----" == Representation(st)); - st.SetValueAt(0, "4"); + st.SetValueAt(0, UniqueStringCopy("4")); REQUIRE(1 == st.Elements()); REQUIRE("4---" == Representation(st)); st.DeletePosition(0); @@ -93,7 +96,7 @@ TEST_CASE("SparseVector") { SECTION("InsertStringAtStartThenInsertSpaceAtStart") { REQUIRE(1 == st.Elements()); st.InsertSpace(0, 5); - st.SetValueAt(0, "3"); + st.SetValueAt(0, UniqueStringCopy("3")); REQUIRE(1 == st.Elements()); REQUIRE("3----" == Representation(st)); st.InsertSpace(0, 1); @@ -102,10 +105,42 @@ TEST_CASE("SparseVector") { st.Check(); } + SECTION("InsertSpaceAfterStart") { + REQUIRE(1 == st.Elements()); + st.InsertSpace(0, 5); + st.SetValueAt(1, UniqueStringCopy("1")); + REQUIRE(2 == st.Elements()); + REQUIRE("-1---" == Representation(st)); + st.InsertSpace(1, 1); + REQUIRE(2 == st.Elements()); + REQUIRE("--1---" == Representation(st)); + st.Check(); + } + + SECTION("InsertStringAt1ThenInsertLettersAt1") { + REQUIRE(1 == st.Elements()); + st.InsertSpace(0, 5); + st.SetValueAt(1, UniqueStringCopy("9")); + REQUIRE(2 == st.Elements()); + REQUIRE("-9---" == Representation(st)); + st.InsertSpace(0, 1); + REQUIRE(2 == st.Elements()); + REQUIRE("--9---" == Representation(st)); + // Initial st has allocation of 9 values so this should cause reallocation + const std::string letters("ABCDEFGHIJKLMNOP"); // 16 letters + for (const char letter : letters) { + const char sLetter[] = { letter, 0 }; + st.InsertSpace(0, 1); + st.SetValueAt(1, UniqueStringCopy(sLetter)); + } + REQUIRE("-PONMLKJIHGFEDCBA-9---" == Representation(st)); + st.Check(); + } + SECTION("InsertAndDeleteAtEnd") { REQUIRE(1 == st.Elements()); st.InsertSpace(0, 5); - st.SetValueAt(4, "5"); + st.SetValueAt(4, UniqueStringCopy("5")); REQUIRE(2 == st.Elements()); REQUIRE("----5" == Representation(st)); st.DeletePosition(4); @@ -117,7 +152,7 @@ TEST_CASE("SparseVector") { SECTION("SetNULL") { REQUIRE(1 == st.Elements()); st.InsertSpace(0, 5); - st.SetValueAt(4, "5"); + st.SetValueAt(4, UniqueStringCopy("5")); REQUIRE(2 == st.Elements()); REQUIRE("----5" == Representation(st)); st.SetValueAt(4, nullptr); @@ -129,10 +164,10 @@ TEST_CASE("SparseVector") { SECTION("DeleteAll") { REQUIRE(1 == st.Elements()); st.InsertSpace(0, 10); - st.SetValueAt(9, "9"); - st.SetValueAt(7, "7"); - st.SetValueAt(4, "4"); - st.SetValueAt(3, "3"); + st.SetValueAt(9, UniqueStringCopy("9")); + st.SetValueAt(7, UniqueStringCopy("7")); + st.SetValueAt(4, UniqueStringCopy("4")); + st.SetValueAt(3, UniqueStringCopy("3")); REQUIRE(5 == st.Elements()); REQUIRE("---34--7-9" == Representation(st)); st.Check(); @@ -178,7 +213,7 @@ TEST_CASE("SparseTextString") { SECTION("InsertAndDeleteValue") { st.InsertSpace(0, 5); REQUIRE(5 == st.Length()); - st.SetValueAt(3, "3"); + st.SetValueAt(3, std::string("3")); REQUIRE(2 == st.Elements()); REQUIRE("" == st.ValueAt(0)); REQUIRE("" == st.ValueAt(2)); |