diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-24 21:33:44 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-27 00:11:41 +0300 |
commit | a864099ba77157090c4cd12817245122c163ec24 (patch) | |
tree | f15b1f2801219fa9af6111fa28591858d87711ec /test/unit_tests/shared/fixed_size.hpp | |
parent | Process: add termination methods (diff) | |
download | winapi-common-a864099ba77157090c4cd12817245122c163ec24.tar.gz winapi-common-a864099ba77157090c4cd12817245122c163ec24.zip |
rework Process API & tests
* Add separate classes ProcessParameters & ShellParameters for
Process::create() and Process::shell() methods.
* Add a separate "worker" executable. It's used in tests via a fairly
complicated scheme, receiving orders to execute via a shared memory
region.
* Add tests that utilize the Console API, reading console window's
screen buffer directly, making for more reliable tests & broader
coverage.
Diffstat (limited to '')
-rw-r--r-- | test/unit_tests/shared/fixed_size.hpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/unit_tests/shared/fixed_size.hpp b/test/unit_tests/shared/fixed_size.hpp new file mode 100644 index 0000000..e4b11c7 --- /dev/null +++ b/test/unit_tests/shared/fixed_size.hpp @@ -0,0 +1,79 @@ +// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com> +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include <array> +#include <cstddef> +#include <cstring> +#include <string> +#include <vector> + +namespace fixed_size { + +/* + * These are fixed-size classes, to be used as part of an interprocess shared + * memory region. + */ + +// 120 characters is an arbitrary limit, strings are cut to this many +// characters for storage. +template <std::size_t Length = 120> +class String : public std::array<char, Length> { +public: + static String convert(const std::string& src) { + String dest; + std::size_t nch = dest.size() - 1; + if (src.size() < nch) { + nch = src.size(); + } + std::memcpy(dest.data(), src.c_str(), nch); + dest[nch] = '\0'; + return dest; + } + + std::string extract() const { + // Lines are null-terminated, and don't store their lenghts, so... + return data(); + } + +private: +}; + +// 5 lines to store is also arbitrary, set it higher if needed. +template <std::size_t Length = 5, std::size_t StringLength = 120> +struct StringList : public std::array<String<StringLength>, Length> { + static StringList convert(const std::vector<std::string>& src) { + // If src.size() > Length, only the last Length lines from the source + // list are stored. + + StringList dest; + std::memset(&dest, 0, sizeof(dest)); + + std::size_t src_offset = 0; + if (src.size() > dest.size()) { + src_offset = src.size() - dest.size(); + } + + for (std::size_t i = 0, j = src_offset; i < dest.size() && j < src.size(); + ++i, ++j, ++dest.numof_lines) { + dest[i] = dest[i].convert(src[j]); + } + + return dest; + } + + std::vector<std::string> extract() const { + std::vector<std::string> dest; + for (std::size_t i = 0; i < numof_lines; ++i) { + dest.emplace_back(at(i).extract()); + } + return dest; + } + + std::size_t numof_lines; +}; + +} // namespace fixed_size |