From ac6380cf20794a61467cfebe74465c97bb035ec2 Mon Sep 17 00:00:00 2001 From: Angela Acquista <157067144+AngelaAcquista@users.noreply.github.com> Date: Fri, 19 Sep 2025 18:51:05 -0400 Subject: [PATCH] Update test.cpp --- test/test.cpp | 130 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 106 insertions(+), 24 deletions(-) diff --git a/test/test.cpp b/test/test.cpp index fa5a5ac..4c9b6c5 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,40 +1,122 @@ #include #include - -// uncomment and replace the following with your own headers -// #include "AVL.h" +#include +#include +#include +#include +#include "AVL.h" using namespace std; +//UF ID: 23793962 +//Name: Angela Acquista -// the syntax for defining a test is below. It is important for the name to be unique, but you can group multiple tests with [tags]. A test can have [multiple][tags] using that syntax. -TEST_CASE("Example Test Name - Change me!", "[flag]"){ - // instantiate any class members that you need to test here - int one = 1; - - // anything that evaluates to false in a REQUIRE block will result in a failing test - REQUIRE(one == 0); // fix me! +TEST_CASE("Test 1: Incorrect Commands", "[incorrect]"){ - // all REQUIRE blocks must evaluate to true for the whole test to pass - REQUIRE(false); // also fix me! + MyAVLTree tree; + REQUIRE(tree.insert("Poo Pee", "A11y") == false); + REQUIRE(tree.insert("Doo Dee", "45679999") == false); + REQUIRE(tree.insert("Ooga Booga", "-123456") == false); + REQUIRE(tree.insert("", "87654321") == false); + REQUIRE(tree.insert("Wack Back", "1234A567") == false); } -TEST_CASE("Test 2", "[flag]"){ - // you can also use "sections" to share setup code between tests, for example: - int one = 1; +TEST_CASE("Test 2: Insert and Rotate", "[rotations]"){ + + MyAVLTree tree; + + SECTION("Left-Left Rotation Case") { + + tree.insert("third", "3"); + tree.insert("second", "2"); + tree.insert("first", "1"); + REQUIRE((tree.inorder()).size() == 3); + REQUIRE(tree.search("3") == true); + REQUIRE(tree.search("2") == true); + REQUIRE(tree.search("1") == true); + } + SECTION("Right-Right Rotation Case") { - SECTION("num is 2") { - int num = one + 1; - REQUIRE(num == 2); - }; + tree.insert("fourth", "4"); + tree.insert("fifth", "5"); + tree.insert("sixth", "6"); + REQUIRE((tree.inorder()).size() == 3); + REQUIRE(tree.search("4") == true); + REQUIRE(tree.search("5") == true); + REQUIRE(tree.search("6") == true); + } + SECTION("Left-Right Rotation Case") { - SECTION("num is 3") { - int num = one + 2; - REQUIRE(num == 3); - }; + tree.insert("ninth", "9"); + tree.insert("seventh", "7"); + tree.insert("eighth", "8"); + REQUIRE((tree.inorder()).size() == 3); + REQUIRE(tree.search("9") == true); + REQUIRE(tree.search("8") == true); + REQUIRE(tree.search("7") == true); + } + SECTION("Right-Left Rotation Case") { - // each section runs the setup code independently to ensure that they don't affect each other + tree.insert("tenth", "10"); + tree.insert("twelfth", "12"); + tree.insert("eleventh", "11"); + REQUIRE((tree.inorder()).size() == 3); + REQUIRE(tree.search("10") == true); + REQUIRE(tree.search("12") == true); + REQUIRE(tree.search("11") == true); + } } +TEST_CASE("Test 3: Insert and Delete", "[deletions]"){ + + MyAVLTree tree; + random_device rd; + mt19937 gen(rd()); + vector ids, removing; + + for(int i = 1; i <= 100; i++){ + + string id = to_string(10000000 + i); + string name = "Student" + to_string(i); + REQUIRE(tree.insert(name, id) == true); + ids.push_back(id); + } + REQUIRE(ids.size() == 100); + vector ordered = tree.inorder(); + REQUIRE(ordered.size() == 100); + + for(size_t i = 1; i < ordered.size(); i++){ + + REQUIRE(stoll(ordered.at(i)) > stoll(ordered.at(i - 1))); + } + vector remaining = ids; + + for(int i = 0; i < 10; i++){ + + uniform_int_distribution<> dis(0, remaining.size() - 1); + int rand = dis(gen); + removing.push_back(remaining.at(rand)); + remaining.erase(remaining.begin() + rand); + } + for(const auto& id : removing){ + + REQUIRE(tree.remove(id) == true); + } + vector final = tree.inorder(); + REQUIRE(final.size() == 90); + + for(size_t i = 1; i < final.size(); i++){ + + REQUIRE(stoll(final.at(i)) > stoll(final.at(i - 1))); + } + for(const auto& removed : removing){ + + REQUIRE(tree.search(removed) == false); + } + for(const auto& remains : remaining){ + + REQUIRE(tree.search(remains) == true); + } +} // you must write 5 unique, meaningful tests for credit on the testing portion of this project! // the provided test from the template is below.