]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright (c) 2019-2021 Alexander Grund |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #define BOOST_NOWIDE_TEST_NO_MAIN | |
8 | #include "file_test_helpers.hpp" | |
9 | ||
10 | #include <boost/nowide/cstdio.hpp> | |
11 | #include "test.hpp" | |
12 | #include <algorithm> | |
13 | #include <iostream> | |
14 | #include <limits> | |
15 | #include <numeric> | |
16 | #include <random> | |
17 | ||
18 | namespace boost { | |
19 | namespace nowide { | |
20 | namespace test { | |
21 | ||
22 | void create_file(const std::string& filepath, const std::string& contents, data_type type /*= data_type::text*/) | |
23 | { | |
24 | auto* f = fopen(filepath.c_str(), (type == data_type::binary) ? "wb" : "w"); | |
25 | TEST(f); | |
26 | TEST(std::fwrite(contents.data(), 1, contents.size(), f) == contents.size()); | |
27 | std::fclose(f); | |
28 | } | |
29 | ||
30 | bool file_exists(const std::string& filepath) | |
31 | { | |
32 | FILE* f = fopen(filepath.c_str(), "r"); | |
33 | if(f) | |
34 | { | |
35 | std::fclose(f); | |
36 | return true; | |
37 | } else | |
38 | return false; | |
39 | } | |
40 | ||
41 | std::string read_file(const std::string& filepath, data_type type /*= data_type::text*/) | |
42 | { | |
43 | FILE* f = fopen(filepath.c_str(), (type == data_type::binary) ? "rb" : "r"); | |
44 | TEST(f); | |
45 | std::fseek(f, 0, SEEK_END); | |
46 | const auto file_size_tmp = ftell(f); | |
47 | TEST(file_size_tmp >= 0); | |
48 | const auto file_size = static_cast<size_t>(file_size_tmp); | |
49 | std::fseek(f, 0, SEEK_SET); | |
50 | std::string content(file_size, '\0'); | |
51 | const auto read_bytes = std::fread(&content[0], 1, file_size, f); | |
52 | if(read_bytes != file_size) | |
53 | { | |
54 | TEST(std::feof(f)); | |
55 | content.resize(read_bytes); | |
56 | } | |
57 | std::fclose(f); | |
58 | return content; | |
59 | } | |
60 | ||
61 | void ensure_not_exists(const char* filepath) | |
62 | { | |
63 | if(file_exists(filepath)) | |
64 | { | |
65 | remove(filepath); | |
66 | TEST(!file_exists(filepath)); | |
67 | } | |
68 | } | |
69 | ||
70 | static std::string get_ascii_chars() | |
71 | { | |
72 | const auto first_char = char(0x20); | |
73 | const auto last_char = char(0x7E); | |
74 | std::string result(last_char - first_char + 2, '\0'); | |
75 | result[0] = '\n'; // Include newline | |
76 | std::iota(result.begin() + 1, result.end(), first_char); | |
77 | return result; | |
78 | } | |
79 | ||
80 | static std::minstd_rand make_rand_engine() | |
81 | { | |
82 | const auto seed = std::random_device{}(); | |
83 | std::cout << "RNG seed: " << seed << std::endl; | |
84 | return std::minstd_rand(seed); | |
85 | } | |
86 | ||
87 | std::string create_random_data(size_t num_chars, data_type type) | |
88 | { | |
89 | static std::minstd_rand rng = make_rand_engine(); | |
90 | std::string result(num_chars, '\0'); | |
91 | if(type == data_type::binary) | |
92 | { | |
93 | std::uniform_int_distribution<int> distr(std::numeric_limits<char>::min(), | |
94 | std::numeric_limits<char>::max()); | |
95 | std::generate(result.begin(), result.end(), [&]() { return static_cast<char>(distr(rng)); }); | |
96 | } else | |
97 | { | |
98 | static const std::string text_data = get_ascii_chars(); | |
99 | std::uniform_int_distribution<std::size_t> distr(0, text_data.size() - 1u); | |
100 | std::generate(result.begin(), result.end(), [&]() { return text_data[distr(rng)]; }); | |
101 | } | |
102 | return result; | |
103 | } | |
104 | ||
105 | } // namespace test | |
106 | } // namespace nowide | |
107 | } // namespace boost |