stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
stream.cpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#include "pch.hpp"
7
8using namespace std;
9using namespace stdex;
10using namespace stdex::stream;
11#ifdef _WIN32
12using namespace Microsoft::VisualStudio::CppUnitTestFramework;
13#endif
14
15namespace UnitTests
16{
17 TEST_CLASS(stream)
18 {
19 public:
20 TEST_METHOD(async)
21 {
22 constexpr uint32_t total = 1000;
23 memory_file source(mul(total, sizeof(uint32_t)));
24 {
25 async_writer<70> writer(source);
26 for (uint32_t i = 0; i < total; ++i) {
27 Assert::IsTrue(writer.ok());
28 writer << i;
29 }
30 }
31 Assert::AreEqual<stdex::stream::fpos_t>(0, source.seekbeg(0));
32 {
33 async_reader<50> reader(source);
34 uint32_t x;
35 for (uint32_t i = 0; i < total; ++i) {
36 reader >> x;
37 Assert::IsTrue(reader.ok());
38 Assert::AreEqual(i, x);
39 }
40 reader >> x;
41 Assert::IsFalse(reader.ok());
42 }
43 }
44
45 TEST_METHOD(replicator)
46 {
47 constexpr uint32_t total = 1000;
48
49 memory_file f1(mul(total, sizeof(uint32_t)));
50
51 sstring filename2, filename3;
52 filename2 = filename3 = temp_path();
53 filename2 += _T("stdex-stream-replicator-2.tmp");
54 file f2(
55 filename2.c_str(),
56 mode_for_reading | mode_for_writing | mode_create | mode_binary);
57
58 filename3 += _T("stdex-stream-replicator-3.tmp");
59 cached_file f3(
60 filename3.c_str(),
61 mode_for_reading | mode_for_writing | mode_create | mode_binary,
62 128);
63
64 {
66 buffer f2_buf(f2, 0, 32);
67 writer.push_back(&f1);
68 writer.push_back(&f2_buf);
69 writer.push_back(&f3);
70 for (uint32_t i = 0; i < total; ++i) {
71 Assert::IsTrue(writer.ok());
72 writer << i;
73 }
74 }
75
76 f1.seekbeg(0);
77 f2.seekbeg(0);
78 f3.seekbeg(0);
79 {
80 buffer f2_buf(f2, 64, 0);
81 uint32_t x;
82 for (uint32_t i = 0; i < total; ++i) {
83 f1 >> x;
84 Assert::IsTrue(f1.ok());
85 Assert::AreEqual(i, x);
86 f2_buf >> x;
87 Assert::IsTrue(f2_buf.ok());
88 Assert::AreEqual(i, x);
89 f3 >> x;
90 Assert::IsTrue(f3.ok());
91 Assert::AreEqual(i, x);
92 }
93 f1 >> x;
94 Assert::IsFalse(f1.ok());
95 f2_buf >> x;
96 Assert::IsFalse(f2_buf.ok());
97 f3 >> x;
98 Assert::IsFalse(f3.ok());
99 }
100
101 f2.close();
102 std::filesystem::remove(filename2);
103 f3.close();
104 std::filesystem::remove(filename3);
105 }
106
107 TEST_METHOD(open_close)
108 {
109 cached_file dat(invalid_handle, state_t::fail, 4096);
110 const sstring filepath = temp_path();
111 constexpr uint32_t count = 3;
112 sstring filename[count];
113 stdex::stream::fpos_t start[count];
114 for (uint32_t i = 0; i < count; ++i) {
115 filename[i] = filepath + sprintf(_T("stdex-stream-open_close%u.tmp"), NULL, i);
116 dat.open(filename[i].c_str(), mode_for_reading | mode_for_writing | share_none | mode_preserve_existing | mode_binary);
117 Assert::IsTrue(dat.ok());
118 start[i] = dat.tell();
119 Assert::AreNotEqual(fpos_max, start[i]);
120 for (uint32_t j = 0; j < 31 + 11 * i; ++j) {
121 dat << j * count + i;
122 Assert::IsTrue(dat.ok());
123 }
124 dat.close();
125 }
126 for (uint32_t i = 0; i < count; ++i) {
127 dat.open(filename[i].c_str(), mode_for_reading | mode_open_existing | share_none | mode_binary);
128 Assert::IsTrue(dat.ok());
129 for (;;) {
130 uint32_t x;
131 dat >> x;
132 if (!dat.ok())
133 break;
134 Assert::AreEqual(i, x % count);
135 }
136 }
137 dat.close();
138 for (uint32_t i = 0; i < count; ++i)
139 std::filesystem::remove(filename[i]);
140 }
141
142 TEST_METHOD(file_stat)
143 {
144 sstring path(temp_path());
145 Assert::IsTrue(stdex::stream::file::exists(path));
146 Assert::IsFalse(stdex::stream::file::readonly(path));
147 }
148
149 protected:
150 static sstring temp_path()
151 {
152#ifdef _WIN32
153 TCHAR temp_path[MAX_PATH];
154 Assert::IsTrue(ExpandEnvironmentStrings(_T("%TEMP%\\"), temp_path, _countof(temp_path)) < MAX_PATH);
155 return temp_path;
156#else
157 return "/tmp/";
158#endif
159 }
160 };
161}
Provides read-ahead stream capability.
Definition stream.hpp:1246
Provides write-back stream capability.
Definition stream.hpp:1313
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:175
Buffered read/write stream.
Definition stream.hpp:1384
Cached file-system file.
Definition stream.hpp:3099
File-system file.
Definition stream.hpp:2691
static bool readonly(const stdex::schar_t *filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3069
static bool exists(const stdex::schar_t *filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3041
In-memory file.
Definition stream.hpp:3183
Replicates writing of the same data to multiple streams.
Definition stream.hpp:1077
void push_back(basic *source)
Adds stream on the list.
Definition stream.hpp:1096