Moved unit tests close to the sources
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
add_library(lmsutils SHARED
|
||||
impl/ChildProcess.cpp
|
||||
impl/ChildProcessManager.cpp
|
||||
@@ -37,3 +36,6 @@ target_link_libraries(lmsutils PUBLIC
|
||||
|
||||
install(TARGETS lmsutils DESTINATION lib)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
include(GoogleTest)
|
||||
|
||||
add_executable(test-utils
|
||||
String.cpp
|
||||
RecursiveSharedMutex.cpp
|
||||
Utils.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test-utils PRIVATE
|
||||
lmsutils
|
||||
Threads::Threads
|
||||
GTest::GTest
|
||||
)
|
||||
|
||||
gtest_discover_tests(test-utils)
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "utils/RecursiveSharedMutex.hpp"
|
||||
|
||||
TEST(RecursiveSharedMutex, SingleThreaded)
|
||||
{
|
||||
{
|
||||
RecursiveSharedMutex mutex;
|
||||
|
||||
{
|
||||
std::unique_lock lock {mutex};
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock {mutex};
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock lock1 {mutex};
|
||||
std::unique_lock lock2 {mutex};
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock1 {mutex};
|
||||
std::shared_lock lock2 {mutex};
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock lock1 {mutex};
|
||||
std::shared_lock lock2 {mutex};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RecursiveSharedMutex, MultiThreaded)
|
||||
{
|
||||
{
|
||||
constexpr std::size_t nbThreads {10};
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
RecursiveSharedMutex mutex;
|
||||
std::atomic<std::size_t> nbUnique {};
|
||||
std::atomic<std::size_t> nbShared {};
|
||||
for (std::size_t i {}; i < nbThreads; ++i)
|
||||
{
|
||||
threads.emplace_back([&]
|
||||
{
|
||||
{
|
||||
std::unique_lock lock {mutex};
|
||||
|
||||
std::shared_lock lock2 {mutex};
|
||||
|
||||
assert(nbUnique == 0);
|
||||
assert(nbShared == 0);
|
||||
nbUnique++;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
assert(nbUnique == 1);
|
||||
assert(nbShared == 0);
|
||||
nbUnique--;
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock {mutex};
|
||||
std::shared_lock lock2 {mutex};
|
||||
|
||||
assert(nbUnique == 0);
|
||||
nbShared++;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(15));
|
||||
|
||||
assert(nbShared > 0);
|
||||
assert(nbShared <= nbThreads);
|
||||
assert(nbUnique == 0);
|
||||
nbShared--;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (std::thread& t : threads)
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "utils/String.hpp"
|
||||
|
||||
TEST(StringUtils, splitString)
|
||||
{
|
||||
{
|
||||
const std::string test{"a"};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, "")};
|
||||
ASSERT_EQ(strings.size(), 1);
|
||||
EXPECT_EQ(strings.front() , "a");
|
||||
}
|
||||
|
||||
{
|
||||
const std::string test{"a b"};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, "|")};
|
||||
ASSERT_EQ(strings.size(), 1);
|
||||
EXPECT_EQ(strings.front(), "a b");
|
||||
}
|
||||
|
||||
{
|
||||
const std::string test{" a"};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, " ")};
|
||||
ASSERT_EQ(strings.size(), 1);
|
||||
EXPECT_EQ(strings.front(), "a");
|
||||
}
|
||||
|
||||
{
|
||||
const std::string test{"a "};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, " ")};
|
||||
ASSERT_EQ(strings.size(), 1);
|
||||
EXPECT_EQ(strings.front(), "a");
|
||||
}
|
||||
|
||||
{
|
||||
const std::string test{"a b"};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, " ")};
|
||||
ASSERT_EQ(strings.size(), 2);
|
||||
EXPECT_EQ(strings.front(), "a");
|
||||
EXPECT_EQ(strings.back(), "b");
|
||||
}
|
||||
|
||||
{
|
||||
const std::string test{"a b,c|defgh "};
|
||||
|
||||
const std::vector<std::string_view> strings {StringUtils::splitString(test, " ,|")};
|
||||
ASSERT_EQ(strings.size(), 4);
|
||||
EXPECT_EQ(strings[0], "a");
|
||||
EXPECT_EQ(strings[1], "b");
|
||||
EXPECT_EQ(strings[2], "c");
|
||||
EXPECT_EQ(strings[3], "defgh");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StringUtils, escapeString)
|
||||
{
|
||||
EXPECT_EQ(StringUtils::escapeString("", "*", ' '), "");
|
||||
EXPECT_EQ(StringUtils::escapeString("", "", ' '), "");
|
||||
EXPECT_EQ(StringUtils::escapeString("a", "", ' '), "a");
|
||||
EXPECT_EQ(StringUtils::escapeString("*", "*", '_'), "_*");
|
||||
EXPECT_EQ(StringUtils::escapeString("*a*", "*", '_'), "_*a_*");
|
||||
EXPECT_EQ(StringUtils::escapeString("*a|", "*|", '_'), "_*a_|");
|
||||
EXPECT_EQ(StringUtils::escapeString("**||", "*|", '_'), "_*_*_|_|");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user