Skip to content

Commit

Permalink
fixed formatting :(
Browse files Browse the repository at this point in the history
  • Loading branch information
ricemaking committed Nov 16, 2024
1 parent 439f74e commit ca0169d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 76 deletions.
9 changes: 3 additions & 6 deletions tests/util/CoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ TEST_CASE("Test almostEqual", "[util][core]") {
TEST_CASE("Test Key Set", "[util][core]") {
SECTION("Extracting keys from a given map") {
std::unordered_map<std::string, int> inputtedMap = {
{"key1", 1},
{"key2", 2},
{"key3", 3}
};
{"key1", 1}, {"key2", 2}, {"key3", 3}};

auto keys = keySet(inputtedMap);

Expand All @@ -45,7 +42,7 @@ TEST_CASE("Test To String", "[util][core]") {
}

SECTION("Convert floats to strings") {
REQUIRE(to_string(3.14).substr(0,4) == "3.14");
REQUIRE(to_string(3.14).substr(0, 4) == "3.14");
REQUIRE(to_string(1e-6) == "0.000001");
}

Expand All @@ -71,7 +68,7 @@ TEST_CASE("Test Freeze String", "[util][core]") {

TEST_CASE("Test Pair To Tuple", "[util][core]") {
SECTION("Converting pair of integers to tuple") {
std::pair<int, int> pair = {0,1};
std::pair<int, int> pair = {0, 1};
auto tuple = pairToTuple(pair);
REQUIRE(std::get<0>(tuple) == 0);
REQUIRE(std::get<1>(tuple) == 1);
Expand Down
139 changes: 69 additions & 70 deletions tests/util/ThreadingTest.cpp
Original file line number Diff line number Diff line change
@@ -1,102 +1,101 @@
#include "../../src/utils/threading.h"

#include <catch2/catch.hpp>

#include <chrono>
#include <thread>

#include <catch2/catch.hpp>

using namespace util;

using namespace std::chrono_literals;

TEST_CASE("Test Wait", "[util][threading]") {
SECTION("Wait until latch unlocks when countdown reaches zero") {
latch l(3);
std::thread worker([&]() {
l.count_down();
l.count_down();
l.count_down();
});

l.wait();
worker.join();

REQUIRE(true); // if thread isn't blocked, will be reached
}
SECTION("Wait until latch unlocks when countdown reaches zero") {
latch l(3);
std::thread worker([&]() {
l.count_down();
l.count_down();
l.count_down();
});

l.wait();

worker.join();

REQUIRE(true); // if thread isn't blocked, will be reached
}
}

TEST_CASE("Test Wait For", "[util][threading]") {
SECTION("Wait for latch unlocks within timeout") {
latch l(1);
std::thread worker([&](){
std::this_thread::sleep_for(50ms);
l.count_down();
});

REQUIRE(l.wait_for(100ms));
SECTION("Wait for latch unlocks within timeout") {
latch l(1);
std::thread worker([&]() {
std::this_thread::sleep_for(50ms);
l.count_down();
});

worker.join();
}
REQUIRE(l.wait_for(100ms));

SECTION("Wait for latch times out when not unlocked") {
latch l(1);
worker.join();
}

REQUIRE_FALSE(l.wait_for(50ms));
}
SECTION("Wait for latch times out when not unlocked") {
latch l(1);

REQUIRE_FALSE(l.wait_for(50ms));
}
}

TEST_CASE("Test Wait Until", "[util][threading]") {
SECTION("Wait until latch unlocks before timeout") {
latch l(1);
std::thread worker([&]() {
std::this_thread::sleep_for(50ms);
l.count_down();
});

auto timeout = std::chrono::steady_clock::now() + 100ms;
REQUIRE(l.wait_until(timeout));

worker.join();
}

SECTION("Wait until latch times out") {
latch l(1);
auto timeout = std::chrono::steady_clock::now() + 50ms;
REQUIRE_FALSE(l.wait_until(timeout));
}
SECTION("Wait until latch unlocks before timeout") {
latch l(1);
std::thread worker([&]() {
std::this_thread::sleep_for(50ms);
l.count_down();
});

auto timeout = std::chrono::steady_clock::now() + 100ms;
REQUIRE(l.wait_until(timeout));

worker.join();
}

SECTION("Wait until latch times out") {
latch l(1);
auto timeout = std::chrono::steady_clock::now() + 50ms;
REQUIRE_FALSE(l.wait_until(timeout));
}
}

TEST_CASE("Test Count Down", "[util][threading]") {
SECTION("Latch unlocks after enough countdowns") {
latch l(3);
SECTION("Latch unlocks after enough countdowns") {
latch l(3);

REQUIRE_FALSE(l.wait_for(10ms));
REQUIRE_FALSE(l.wait_for(10ms));

l.count_down();
l.count_down();
REQUIRE_FALSE(l.wait_for(10ms));
l.count_down();
l.count_down();
REQUIRE_FALSE(l.wait_for(10ms));

l.count_down();
REQUIRE(l.wait_for(10ms));
}
l.count_down();
REQUIRE(l.wait_for(10ms));
}

SECTION("Latch remains locked if not counted down enough") {
latch l(3);
SECTION("Latch remains locked if not counted down enough") {
latch l(3);

l.count_down();
l.count_down();
REQUIRE_FALSE(l.wait_for(10ms));
}
l.count_down();
l.count_down();
REQUIRE_FALSE(l.wait_for(10ms));
}

SECTION("Latch can count down by more than 1") {
latch l(3);
SECTION("Latch can count down by more than 1") {
latch l(3);

l.count_down(2);
REQUIRE_FALSE(l.wait_for(10ms));
l.count_down(2);
REQUIRE_FALSE(l.wait_for(10ms));

l.count_down(1);
REQUIRE(l.wait_for(10ms));
}
l.count_down(1);
REQUIRE(l.wait_for(10ms));
}
}

0 comments on commit ca0169d

Please sign in to comment.