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;
9#ifdef _WIN32
10using namespace Microsoft::VisualStudio::CppUnitTestFramework;
11#endif
12
13namespace UnitTests
14{
15 TEST_CLASS(stream)
16 {
17 public:
18 TEST_METHOD(async)
19 {
20 constexpr uint32_t total = 1000;
21 stdex::stream::memory_file source(stdex::mul(total, sizeof(uint32_t)));
22 {
24 for (uint32_t i = 0; i < total; ++i) {
25 Assert::IsTrue(writer.ok());
26 writer << i;
27 }
28 }
29 Assert::AreEqual<stdex::stream::fpos_t>(0, source.seekbeg(0));
30 {
32 uint32_t x;
33 for (uint32_t i = 0; i < total; ++i) {
34 reader >> x;
35 Assert::IsTrue(reader.ok());
36 Assert::AreEqual(i, x);
37 }
38 reader >> x;
39 Assert::IsFalse(reader.ok());
40 }
41 }
42
43 TEST_METHOD(replicator)
44 {
45 constexpr uint32_t total = 1000;
46
47 stdex::stream::memory_file f1(stdex::mul(total, sizeof(uint32_t)));
48
49 stdex::sstring filename2, filename3;
50 filename2 = filename3 = temp_path();
51 filename2 += _T("stdex-stream-replicator-2.tmp");
53 filename2.c_str(),
54 stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary);
55
56 filename3 += _T("stdex-stream-replicator-3.tmp");
58 filename3.c_str(),
59 stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary,
60 128);
61
62 {
64 stdex::stream::buffer f2_buf(f2, 0, 32);
65 writer.push_back(&f1);
66 writer.push_back(&f2_buf);
67 writer.push_back(&f3);
68 for (uint32_t i = 0; i < total; ++i) {
69 Assert::IsTrue(writer.ok());
70 writer << i;
71 }
72 }
73
74 f1.seekbeg(0);
75 f2.seekbeg(0);
76 f3.seekbeg(0);
77 {
78 stdex::stream::buffer f2_buf(f2, 64, 0);
79 uint32_t x;
80 for (uint32_t i = 0; i < total; ++i) {
81 f1 >> x;
82 Assert::IsTrue(f1.ok());
83 Assert::AreEqual(i, x);
84 f2_buf >> x;
85 Assert::IsTrue(f2_buf.ok());
86 Assert::AreEqual(i, x);
87 f3 >> x;
88 Assert::IsTrue(f3.ok());
89 Assert::AreEqual(i, x);
90 }
91 f1 >> x;
92 Assert::IsFalse(f1.ok());
93 f2_buf >> x;
94 Assert::IsFalse(f2_buf.ok());
95 f3 >> x;
96 Assert::IsFalse(f3.ok());
97 }
98
99 f2.close();
100 std::filesystem::remove(filename2);
101 f3.close();
102 std::filesystem::remove(filename3);
103 }
104
105 TEST_METHOD(open_close)
106 {
107 stdex::stream::cached_file dat(stdex::invalid_handle, stdex::stream::state_t::fail, 4096);
108 const stdex::sstring filepath = temp_path();
109 constexpr uint32_t count = 3;
110 stdex::sstring filename[count];
111 stdex::stream::fpos_t start[count];
112 for (uint32_t i = 0; i < count; ++i) {
113 filename[i] = filepath + stdex::sprintf(_T("stdex-stream-open_close%u.tmp"), NULL, i);
114 dat.open(filename[i].c_str(), stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::share_none | stdex::stream::mode_preserve_existing | stdex::stream::mode_binary);
115 Assert::IsTrue(dat.ok());
116 start[i] = dat.tell();
117 Assert::AreNotEqual(stdex::stream::fpos_max, start[i]);
118 for (uint32_t j = 0; j < 31 + 11 * i; ++j) {
119 dat << j * count + i;
120 Assert::IsTrue(dat.ok());
121 }
122 dat.close();
123 }
124 for (uint32_t i = 0; i < count; ++i) {
125 dat.open(filename[i].c_str(), stdex::stream::mode_for_reading | stdex::stream::mode_open_existing | stdex::stream::share_none | stdex::stream::mode_binary);
126 Assert::IsTrue(dat.ok());
127 for (;;) {
128 uint32_t x;
129 dat >> x;
130 if (!dat.ok())
131 break;
132 Assert::AreEqual(i, x % count);
133 }
134 }
135 dat.close();
136 for (uint32_t i = 0; i < count; ++i)
137 std::filesystem::remove(filename[i]);
138 }
139
140 TEST_METHOD(file_stat)
141 {
142 stdex::sstring path(temp_path());
143 Assert::IsTrue(stdex::stream::file::exists(path));
144 Assert::IsFalse(stdex::stream::file::readonly(path));
145 }
146
147 protected:
148 static stdex::sstring temp_path()
149 {
150#ifdef _WIN32
151 TCHAR temp_path[MAX_PATH];
152 Assert::IsTrue(ExpandEnvironmentStrings(_T("%TEMP%\\"), temp_path, _countof(temp_path)) < MAX_PATH);
153 return temp_path;
154#else
155 return "/tmp/";
156#endif
157 }
158 };
159}
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