Browse Source

Add tests for new collections' `Append()` methods

core_refactor
Anton Tarasenko 2 years ago
parent
commit
190a609b33
  1. 52
      sources/Data/Collections/Tests/TEST_ArrayList.uc
  2. 82
      sources/Data/Collections/Tests/TEST_HashTable.uc

52
sources/Data/Collections/Tests/TEST_ArrayList.uc

@ -31,6 +31,7 @@ protected static function TESTS()
Test_Find(); Test_Find();
Test_ReferenceManagementGet(); Test_ReferenceManagementGet();
Test_Take(); Test_Take();
Test_Append();
} }
protected static function Test_GetSet() protected static function Test_GetSet()
@ -289,7 +290,7 @@ protected static function Test_Take()
local array<MockItem> allocatedItems; local array<MockItem> allocatedItems;
array = NewMockArray(20, 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."); Issue("`TakeItem()` return wrongs item.");
for (i = 0; i < allocatedItems.length; i += 1) 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 defaultproperties
{ {
caseGroup = "Collections" caseGroup = "Collections"

82
sources/Data/Collections/Tests/TEST_HashTable.uc

@ -37,6 +37,7 @@ protected static function TESTS()
Test_ReferenceManagement(); Test_ReferenceManagement();
Test_Take(); Test_Take();
Test_LargeArray(); Test_LargeArray();
Test_Append();
} }
protected static function AcediaObject NewKey(int value) 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 defaultproperties
{ {
caseGroup = "Collections" caseGroup = "Collections"

Loading…
Cancel
Save