add tests!

Also, fix all the bugs I found when I wrote the tests!
This commit is contained in:
Nicole Mazzuca 2019-07-11 18:16:10 -07:00
parent 510b0c5cc0
commit a0fe40ea58
12 changed files with 290 additions and 148 deletions

View File

@ -12,8 +12,8 @@ namespace fs
using stdfs::copy_options;
using stdfs::file_status;
using stdfs::file_type;
using stdfs::perms;
using stdfs::path;
using stdfs::perms;
using stdfs::u8path;
/*
@ -26,25 +26,24 @@ namespace fs
// we want to poison ADL with these niebloids
namespace detail {
struct symlink_status_t {
namespace detail
{
struct symlink_status_t
{
file_status operator()(const path& p, std::error_code& ec) const noexcept;
file_status operator()(const path& p, vcpkg::LineInfo li) const noexcept;
};
struct is_symlink_t {
inline bool operator()(file_status s) const {
return stdfs::is_symlink(s);
}
struct is_symlink_t
{
inline bool operator()(file_status s) const { return stdfs::is_symlink(s); }
};
struct is_regular_file_t {
inline bool operator()(file_status s) const {
return stdfs::is_regular_file(s);
}
struct is_regular_file_t
{
inline bool operator()(file_status s) const { return stdfs::is_regular_file(s); }
};
struct is_directory_t {
inline bool operator()(file_status s) const {
return stdfs::is_directory(s);
}
struct is_directory_t
{
inline bool operator()(file_status s) const { return stdfs::is_directory(s); }
};
}
@ -63,10 +62,10 @@ namespace fs
We also want to poison the ADL on is_regular_file and is_directory, because
we don't want people calling these functions on paths
*/
using fs::symlink_status;
using fs::is_symlink;
using fs::is_regular_file;
using fs::is_directory;
using fs::is_regular_file;
using fs::is_symlink;
using fs::symlink_status;
namespace vcpkg::Files
{

View File

@ -188,13 +188,5 @@ namespace vcpkg::Strings
// base 64 encoding with URL and filesafe alphabet (base64url)
// based on IETF RFC 4648
// ignores padding, since one implicitly knows the length from the size of x
namespace detail {
struct b64url_encode_t {
std::string operator()(std::uint64_t x) const noexcept;
};
}
constexpr detail::b64url_encode_t b64url_encode{};
std::string b64url_encode(std::uint64_t x) noexcept;
}

View File

@ -4,78 +4,67 @@
#include <memory>
#include <queue>
namespace vcpkg {
template <class Action, class ThreadLocalData>
namespace vcpkg
{
template<class Action, class ThreadLocalData>
struct WorkQueue;
namespace detail {
namespace detail
{
// for SFINAE purposes, keep out of the class
template <class Action, class ThreadLocalData>
auto call_moved_action(
Action& action,
const WorkQueue<Action, ThreadLocalData>& work_queue,
ThreadLocalData& tld
) -> decltype(static_cast<void>(std::move(action)(tld, work_queue)))
template<class Action, class ThreadLocalData>
auto call_moved_action(Action& action,
const WorkQueue<Action, ThreadLocalData>& work_queue,
ThreadLocalData& tld) -> decltype(static_cast<void>(std::move(action)(tld, work_queue)))
{
std::move(action)(tld, work_queue);
}
template <class Action, class ThreadLocalData>
auto call_moved_action(
Action& action,
const WorkQueue<Action, ThreadLocalData>&,
ThreadLocalData& tld
) -> decltype(static_cast<void>(std::move(action)(tld)))
template<class Action, class ThreadLocalData>
auto call_moved_action(Action& action, const WorkQueue<Action, ThreadLocalData>&, ThreadLocalData& tld)
-> decltype(static_cast<void>(std::move(action)(tld)))
{
std::move(action)(tld);
}
struct immediately_run_t {};
}
constexpr detail::immediately_run_t immediately_run{};
template <class Action, class ThreadLocalData>
struct WorkQueue {
template <class F>
WorkQueue(std::size_t num_threads, LineInfo li, const F& tld_init) noexcept {
template<class Action, class ThreadLocalData>
struct WorkQueue
{
template<class F>
WorkQueue(std::uint16_t num_threads, LineInfo li, const F& tld_init) noexcept
{
m_line_info = li;
m_state = State::BeforeRun;
m_unjoined_workers = num_threads;
m_threads.reserve(num_threads);
for (std::size_t i = 0; i < num_threads; ++i) {
for (std::size_t i = 0; i < num_threads; ++i)
{
m_threads.push_back(std::thread(Worker{this, tld_init()}));
}
}
template <class F>
WorkQueue(
detail::immediately_run_t,
std::size_t num_threads,
LineInfo li,
const F& tld_init
) noexcept : WorkQueue(num_threads, li, tld_init) {
m_state = State::Running;
}
WorkQueue(WorkQueue const&) = delete;
WorkQueue(WorkQueue&&) = delete;
~WorkQueue() {
~WorkQueue()
{
auto lck = std::unique_lock<std::mutex>(m_mutex);
if (m_state == State::Running) {
if (!is_joined(m_state))
{
Checks::exit_with_message(m_line_info, "Failed to call join() on a WorkQueue that was destroyed");
}
}
// should only be called once; anything else is an error
void run(LineInfo li) {
void run(LineInfo li)
{
// this should _not_ be locked before `run()` is called; however, we
// want to terminate if someone screws up, rather than cause UB
auto lck = std::unique_lock<std::mutex>(m_mutex);
if (m_state != State::BeforeRun) {
if (m_state != State::BeforeRun)
{
Checks::exit_with_message(li, "Attempted to run() twice");
}
@ -86,18 +75,40 @@ namespace vcpkg {
// if this is called in an existing task, _will block forever_
// DO NOT DO THAT
// thread-unsafe
void join(LineInfo li) {
void join(LineInfo li)
{
{
auto lck = std::unique_lock<std::mutex>(m_mutex);
if (is_joined(m_state)) {
if (is_joined(m_state))
{
Checks::exit_with_message(li, "Attempted to call join() more than once");
} else if (m_state == State::Terminated) {
}
else if (m_state == State::Terminated)
{
m_state = State::TerminatedJoined;
} else {
}
else
{
m_state = State::Joined;
}
}
for (auto& thrd : m_threads) {
for (;;)
{
auto lck = std::unique_lock<std::mutex>(m_mutex);
if (!m_unjoined_workers)
break;
else if (!m_running_workers)
{
lck.unlock();
m_cv.notify_all();
}
}
// all threads have returned -- now, it's time to join them
for (auto& thrd : m_threads)
{
thrd.join();
}
}
@ -105,19 +116,24 @@ namespace vcpkg {
// useful in the case of errors
// doesn't stop any existing running tasks
// returns immediately, so that one can call this in a task
void terminate() const {
void terminate() const
{
{
auto lck = std::unique_lock<std::mutex>(m_mutex);
if (is_joined(m_state)) {
if (is_joined(m_state))
{
m_state = State::TerminatedJoined;
} else {
}
else
{
m_state = State::Terminated;
}
}
m_cv.notify_all();
}
void enqueue_action(Action a) const {
void enqueue_action(Action a) const
{
{
auto lck = std::unique_lock<std::mutex>(m_mutex);
m_actions.push_back(std::move(a));
@ -127,8 +143,9 @@ namespace vcpkg {
m_cv.notify_one();
}
template <class Rng>
void enqueue_all_actions_by_move(Rng&& rng) const {
template<class Rng>
void enqueue_all_actions_by_move(Rng&& rng) const
{
{
using std::begin;
using std::end;
@ -148,8 +165,9 @@ namespace vcpkg {
m_cv.notify_all();
}
template <class Rng>
void enqueue_all_actions(Rng&& rng) const {
template<class Rng>
void enqueue_all_actions(Rng&& rng) const
{
{
using std::begin;
using std::end;
@ -170,37 +188,41 @@ namespace vcpkg {
}
private:
struct Worker {
struct Worker
{
const WorkQueue* work_queue;
ThreadLocalData tld;
void operator()() {
void operator()()
{
// unlocked when waiting, or when in the action
// locked otherwise
auto lck = std::unique_lock<std::mutex>(work_queue->m_mutex);
work_queue->m_cv.wait(lck, [&] {
return work_queue->m_state != State::BeforeRun;
});
work_queue->m_cv.wait(lck, [&] { return work_queue->m_state != State::BeforeRun; });
for (;;) {
for (;;)
{
const auto state = work_queue->m_state;
if (is_terminated(state)) {
return;
if (is_terminated(state))
{
break;
}
if (work_queue->m_actions.empty()) {
if (state == State::Running || work_queue->running_workers > 0) {
--work_queue->running_workers;
if (work_queue->m_actions.empty())
{
if (state == State::Running || work_queue->m_running_workers > 1)
{
--work_queue->m_running_workers;
work_queue->m_cv.wait(lck);
++work_queue->running_workers;
++work_queue->m_running_workers;
continue;
}
// the queue isn't running, and we are the only worker
// no more work!
return;
break;
}
Action action = std::move(work_queue->m_actions.back());
@ -210,10 +232,13 @@ namespace vcpkg {
detail::call_moved_action(action, *work_queue, tld);
lck.lock();
}
--work_queue->m_unjoined_workers;
}
};
enum class State : std::int16_t {
enum class State : std::int16_t
{
// can only exist upon construction
BeforeRun = -1,
@ -223,22 +248,19 @@ namespace vcpkg {
TerminatedJoined,
};
static bool is_terminated(State st) {
return st == State::Terminated || st == State::TerminatedJoined;
}
static bool is_terminated(State st) { return st == State::Terminated || st == State::TerminatedJoined; }
static bool is_joined(State st) {
return st != State::Joined || st == State::TerminatedJoined;
}
static bool is_joined(State st) { return st == State::Joined || st == State::TerminatedJoined; }
mutable std::mutex m_mutex;
// these four are under m_mutex
mutable State m_state;
mutable std::uint16_t running_workers;
mutable std::vector<Action> m_actions;
mutable std::condition_variable m_cv;
mutable std::mutex m_mutex{};
// these are all under m_mutex
mutable State m_state = State::BeforeRun;
mutable std::uint16_t m_running_workers = 0;
mutable std::uint16_t m_unjoined_workers = 0; // num_threads
mutable std::vector<Action> m_actions{};
mutable std::condition_variable m_cv{};
std::vector<std::thread> m_threads;
std::vector<std::thread> m_threads{};
LineInfo m_line_info;
};
}

View File

@ -0,0 +1,80 @@
#include "tests.pch.h"
#include <vcpkg/base/files.h>
#include <vcpkg/base/strings.h>
#include <iostream>
#include <random>
#include <windows.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1 {
class FilesTest : public TestClass<FilesTest> {
using uid = std::uniform_int_distribution<std::uint64_t>;
std::string get_random_filename()
{
std::random_device rd;
return vcpkg::Strings::b64url_encode(uid{}(rd));
}
void create_directory_tree(
vcpkg::Files::Filesystem& fs,
std::uint64_t depth,
const fs::path& base)
{
std::random_device rd;
constexpr auto max_depth = std::uint64_t(3);
const auto width = depth ? uid{0, (max_depth - depth) * 3 / 2}(rd) : 5;
std::error_code ec;
if (width == 0) {
fs.write_contents(base, "", ec);
Assert::IsFalse(bool(ec));
return;
}
fs.create_directory(base, ec);
Assert::IsFalse(bool(ec));
for (int i = 0; i < width; ++i) {
create_directory_tree(fs, depth + 1, base / get_random_filename());
}
}
TEST_METHOD(remove_all) {
fs::path temp_dir;
{
wchar_t* tmp = static_cast<wchar_t*>(calloc(32'767, 2));
if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767)) {
Assert::Fail(L"GetEnvironmentVariable(\"TEMP\") failed");
}
temp_dir = tmp;
std::string dir_name = "vcpkg-tmp-dir-";
dir_name += get_random_filename();
temp_dir /= dir_name;
}
auto& fs = vcpkg::Files::get_real_filesystem();
std::cout << "temp dir is: " << temp_dir << '\n';
std::error_code ec;
create_directory_tree(fs, 0, temp_dir);
fs::path fp;
fs.remove_all(temp_dir, ec, fp);
Assert::IsFalse(bool(ec));
Assert::IsFalse(fs.exists(temp_dir));
}
};
}

View File

@ -0,0 +1,38 @@
#include "tests.pch.h"
#include <vcpkg/base/strings.h>
#include <cstdint>
#include <utility>
#include <vector>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1 {
class StringsTest : public TestClass<StringsTest> {
TEST_METHOD(b64url_encode)
{
using u64 = std::uint64_t;
std::vector<std::pair<std::uint64_t, std::string>> map;
map.emplace_back(0, "AAAAAAAAAAA");
map.emplace_back(1, "BAAAAAAAAAA");
map.emplace_back(u64(1) << 32, "AAAAAEAAAAA");
map.emplace_back((u64(1) << 32) + 1, "BAAAAEAAAAA");
map.emplace_back(0xE4D0'1065'D11E'0229, "pIgHRXGEQTO");
map.emplace_back(0xA626'FE45'B135'07FF, "_fQNxWk_mYK");
map.emplace_back(0xEE36'D228'0C31'D405, "FQdMMgi024O");
map.emplace_back(0x1405'64E7'FE7E'A88C, "Miqf-fOZFQB");
map.emplace_back(0xFFFF'FFFF'FFFF'FFFF, "__________P");
std::string result;
for (const auto& pr : map) {
result = vcpkg::Strings::b64url_encode(pr.first);
Assert::AreEqual(result, pr.second);
}
}
};
}

View File

@ -23,7 +23,7 @@
namespace fs::detail
{
file_status symlink_status_t::operator()(const path& p, std::error_code& ec) const noexcept
file_status symlink_status_t::operator()(const path& p, std::error_code&) const noexcept
{
#if defined(_WIN32)
/*
@ -406,6 +406,7 @@ namespace vcpkg::Files
if (!info.ec)
{
info.ec = ec;
info.failure_point = failure_point;
}
return true;

View File

@ -289,44 +289,46 @@ bool Strings::contains(StringView haystack, StringView needle)
return Strings::search(haystack, needle) != haystack.end();
}
namespace vcpkg::Strings::detail {
namespace vcpkg::Strings
{
namespace
{
template<class Integral>
std::string b64url_encode_implementation(Integral x)
{
static_assert(std::is_integral<Integral>::value, "b64url_encode must take an integer type");
using Unsigned = std::make_unsigned_t<Integral>;
auto value = static_cast<Unsigned>(x);
template <class Integral>
std::string b64url_encode_implementation(Integral x) {
static_assert(std::is_integral<Integral>::value, "b64url_encode must take an integer type");
using Unsigned = std::make_unsigned_t<Integral>;
auto value = static_cast<Unsigned>(x);
// 64 values, plus the implicit \0
constexpr static char map[65] =
/* 0123456789ABCDEF */
/*0*/ "ABCDEFGHIJKLMNOP"
/*1*/ "QRSTUVWXYZabcdef"
/*2*/ "ghijklmnopqrstuv"
/*3*/ "wxyz0123456789-_";
// 64 values, plus the implicit \0
constexpr static char map[0x41] =
/* 0123456789ABCDEF */
/*0*/ "ABCDEFGHIJKLMNOP"
/*1*/ "QRSTUVWXYZabcdef"
/*2*/ "ghijklmnopqrstuv"
/*3*/ "wxyz0123456789-_"
;
// log2(64)
constexpr static int shift = 6;
// 64 - 1
constexpr static auto mask = 63;
constexpr static int shift = 5;
constexpr static auto mask = (static_cast<Unsigned>(1) << shift) - 1;
// ceiling(bitsize(Integral) / log2(64))
constexpr static auto result_size = (sizeof(value) * 8 + shift - 1) / shift;
std::string result;
// reserve ceiling(number of bits / 3)
result.resize((sizeof(value) * 8 + 2) / 3, map[0]);
std::string result;
result.reserve(result_size);
for (char& c: result) {
if (value == 0) {
break;
for (std::size_t i = 0; i < result_size; ++i)
{
result.push_back(map[value & mask]);
value >>= shift;
}
c = map[value & mask];
value >>= shift;
return result;
}
return result;
}
std::string b64url_encode_t::operator()(std::uint64_t x) const noexcept{
return b64url_encode_implementation(x);
}
std::string b64url_encode(std::uint64_t x) noexcept { return b64url_encode_implementation(x); }
}

View File

@ -21,8 +21,8 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{34671B80-54F9-46F5-8310-AC429C11D4FB}</ProjectGuid>
<RootNamespace>vcpkg</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<PlatformToolset>v140</PlatformToolset>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

View File

@ -21,8 +21,8 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{B98C92B7-2874-4537-9D46-D14E5C237F04}</ProjectGuid>
<RootNamespace>vcpkglib</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<PlatformToolset>v140</PlatformToolset>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

View File

@ -21,8 +21,8 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{7D6FDEEB-B299-4A23-85EE-F67C4DED47BE}</ProjectGuid>
<RootNamespace>vcpkgmetricsuploader</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<PlatformToolset>v140</PlatformToolset>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

View File

@ -22,6 +22,7 @@
<ClCompile Include="..\src\tests.arguments.cpp" />
<ClCompile Include="..\src\tests.chrono.cpp" />
<ClCompile Include="..\src\tests.dependencies.cpp" />
<ClCompile Include="..\src\tests.files.cpp" />
<ClCompile Include="..\src\tests.packagespec.cpp" />
<ClCompile Include="..\src\tests.paragraph.cpp" />
<ClCompile Include="..\src\tests.pch.cpp">
@ -32,6 +33,7 @@
</ClCompile>
<ClCompile Include="..\src\tests.plan.cpp" />
<ClCompile Include="..\src\tests.statusparagraphs.cpp" />
<ClCompile Include="..\src\tests.strings.cpp" />
<ClCompile Include="..\src\tests.update.cpp" />
<ClCompile Include="..\src\tests.utils.cpp" />
</ItemGroup>
@ -48,8 +50,8 @@
<ProjectGuid>{F27B8DB0-1279-4AF8-A2E3-1D49C4F0220D}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>vcpkgtest</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<PlatformToolset>v140</PlatformToolset>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -84,7 +86,7 @@
<ImportGroup Label="Shared">
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IntDir>$(SolutionDir)msbuild.x86.debug\$(ProjectName)\</IntDir>
<OutDir>$(SolutionDir)msbuild.x86.debug\</OutDir>
</PropertyGroup>
@ -193,4 +195,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -45,6 +45,12 @@
<ClCompile Include="..\src\tests.chrono.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\tests.files.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\tests.strings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\tests.pch.h">