From 190a609b33e2162de1efa4f2a76b588bab177479 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 6 Mar 2023 03:28:39 +0700 Subject: [PATCH] Add tests for new collections' `Append()` methods --- .../Data/Collections/Tests/TEST_ArrayList.uc | 52 +++++++++++- .../Data/Collections/Tests/TEST_HashTable.uc | 82 +++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/sources/Data/Collections/Tests/TEST_ArrayList.uc b/sources/Data/Collections/Tests/TEST_ArrayList.uc index 9834b5e..06423ae 100644 --- a/sources/Data/Collections/Tests/TEST_ArrayList.uc +++ b/sources/Data/Collections/Tests/TEST_ArrayList.uc @@ -31,6 +31,7 @@ protected static function TESTS() Test_Find(); Test_ReferenceManagementGet(); Test_Take(); + Test_Append(); } protected static function Test_GetSet() @@ -289,7 +290,7 @@ protected static function Test_Take() local array allocatedItems; array = NewMockArray(20, allocatedItems); - Context("Testing how well `ArrayList`'s `TakeItem()` command"); + Context("Testing how well `ArrayList`'s `TakeItem()` command."); Issue("`TakeItem()` return wrongs item."); for (i = 0; i < allocatedItems.length; i += 1) { @@ -312,6 +313,55 @@ protected static function Test_Take() } } +protected static function Test_Append() +{ + local int i; + local ArrayList main, additive, empty; + + main = __().collections.EmptyArrayList(); + additive = __().collections.EmptyArrayList(); + empty = __().collections.EmptyArrayList(); + // Ref counter = 2, from creation and adding to collection + main.AddItem(__().box.int(76)).AddItem(__().text.FromString("yoyoyo")); + main.AddItem(none).AddItem(__().ref.float(34.3)); + additive.AddItem(none).AddItem(__().ref.bool(true)); + + Context("Testing appending `ArrayList`'s."); + Issue("`Append(none)` changes caller `ArrayList`."); + main.Append(none); + TEST_ExpectTrue(__().json.Print(main).ToString() + == "[76,\"yoyoyo\",null,34.3]"); + + Issue("`Append()` doesn't properly work on empty `ArrayList`"); + // main ref = 3, +1 from copying + // additive ref = 2 (still) + empty.Append(main); + TEST_ExpectTrue(__().json.Print(main).ToString() + == "[76,\"yoyoyo\",null,34.3]"); + + Issue("`Append()` doesn't properly append `ArrayList`s."); + // main ref = 3 + // additive ref = 3, +1 from copying + main.Append(additive); + TEST_ExpectTrue(__().json.Print(main).ToString() + == "[76,\"yoyoyo\",null,34.3,null,true]"); + + Issue("`Append()` changes appended `ArrayList`"); + TEST_ExpectTrue(__().json.Print(additive).ToString() == "[null,true]"); + + Issue("`Append()` incorrectly changes reference count of stored objects."); + // Ref counter = 3, but will be visible as 4 from getters + for (i = 0; i < main.GetLength(); i += 1) + { + if (i == 2 || i == 4) { + TEST_ExpectNone(main.GetItem(i)); + } + else { + TEST_ExpectTrue(main.GetItem(i)._getRefCount() == 4); + } + } +} + defaultproperties { caseGroup = "Collections" diff --git a/sources/Data/Collections/Tests/TEST_HashTable.uc b/sources/Data/Collections/Tests/TEST_HashTable.uc index 66b461e..cfd02df 100644 --- a/sources/Data/Collections/Tests/TEST_HashTable.uc +++ b/sources/Data/Collections/Tests/TEST_HashTable.uc @@ -37,6 +37,7 @@ protected static function TESTS() Test_ReferenceManagement(); Test_Take(); Test_LargeArray(); + Test_Append(); } protected static function AcediaObject NewKey(int value) @@ -570,6 +571,87 @@ protected static function Test_LargeArray() } } +protected static function Test_Append() +{ + local HashTable main, additive, empty; + + main = __().collections.EmptyHashTable(); + additive = __().collections.EmptyHashTable(); + empty = __().collections.EmptyHashTable(); + // Ref count in main = 2, creation and copy into collection + main.SetItem(P("A"), __().text.FromString("value of A")); + main.SetItem(P("B"), __().text.FromString("value of B")); + main.SetItem(P("C"), __().text.FromString("value of C")); + main.SetItem(P("D"), none); + additive.SetItem(P("C"), __().text.FromString("other value of C!")); + additive.SetItem(P("D"), __().text.FromString("value of D")); + additive.SetItem(P("E"), __().text.FromString("value of E")); + + Context("Testing appending `HashTable`'s."); + SubTest_EmptyCopies(main, additive, empty); + SubTest_ProperCopies(main, additive, empty); +} + +protected static function SubTest_EmptyCopies( + HashTable main, + HashTable additive, + HashTable empty) +{ + Issue("`Append(none)` changes caller `HashTable`."); + main.Append(none); + TEST_ExpectTrue(main.GetLength() == 4); + TEST_ExpectTrue(main.GetString(P("A")) == "value of A"); + TEST_ExpectTrue(main.GetString(P("B")) == "value of B"); + TEST_ExpectTrue(main.GetString(P("C")) == "value of C"); + TEST_ExpectNone(main.GetItem(P("D"))); + + Issue("`Append()` for empty argument changes caller `HashTable`."); + main.Append(empty); + TEST_ExpectTrue(main.GetLength() == 4); + TEST_ExpectTrue(main.GetString(P("A")) == "value of A"); + TEST_ExpectTrue(main.GetString(P("B")) == "value of B"); + TEST_ExpectTrue(main.GetString(P("C")) == "value of C"); + TEST_ExpectNone(main.GetItem(P("D"))); + + Issue("`Append()` doesn't properly work on empty `HashTable`"); + // Ref count in main = 3, +1 for appending + empty.Append(main); + TEST_ExpectTrue(empty.GetLength() == 4); + TEST_ExpectTrue(empty.GetString(P("A")) == "value of A"); + TEST_ExpectTrue(empty.GetString(P("B")) == "value of B"); + TEST_ExpectTrue(empty.GetString(P("C")) == "value of C"); + TEST_ExpectNone(empty.GetItem(P("D"))); +} + +protected static function SubTest_ProperCopies( + HashTable main, + HashTable additive, + HashTable empty) +{ + Issue("`Append()` doesn't properly append `HashTable`s."); + main.Append(additive); + TEST_ExpectTrue(main.GetLength() == 5); + TEST_ExpectTrue(main.GetString(P("A")) == "value of A"); + TEST_ExpectTrue(main.GetString(P("B")) == "value of B"); + TEST_ExpectTrue(main.GetString(P("C")) == "value of C"); + TEST_ExpectNone(main.GetItem(P("D"))); + TEST_ExpectTrue(main.GetString(P("E")) == "value of E"); + + Issue("`Append()` changes appended `HashTable`"); + TEST_ExpectTrue(additive.GetLength() == 3); + TEST_ExpectTrue(additive.GetString(P("C")) == "other value of C!"); + TEST_ExpectTrue(additive.GetString(P("D")) == "value of D"); + TEST_ExpectTrue(additive.GetString(P("E")) == "value of E"); + + Issue("`Append()` incorrectly changes reference counts of items inside" + @ "`HashTable`"); + // Ref count in main = 3, so 4 after getter + TEST_ExpectTrue(main.GetItem(P("A"))._getRefCount() == 4); + TEST_ExpectTrue(main.GetItem(P("B"))._getRefCount() == 4); + TEST_ExpectTrue(main.GetItem(P("C"))._getRefCount() == 4); + TEST_ExpectTrue(main.GetItem(P("E"))._getRefCount() == 4); +} + defaultproperties { caseGroup = "Collections"