|
14 | 14 | // class basic_spanstream |
15 | 15 | // : public basic_iostream<charT, traits> { |
16 | 16 |
|
17 | | -// Test stream operations inherited from `basic_istream` and `basic_ostream` |
| 17 | +// Test stream operations inherited from `basic_istream` |
18 | 18 |
|
19 | 19 | #include <cassert> |
20 | 20 | #include <span> |
21 | 21 | #include <spanstream> |
22 | | -#include <string> |
23 | 22 | #include <string_view> |
24 | 23 |
|
25 | 24 | #include "constexpr_char_traits.h" |
|
29 | 28 | #include "../helper_macros.h" |
30 | 29 | #include "../helper_types.h" |
31 | 30 |
|
| 31 | +#include <print> // REMOVE ME |
| 32 | +#include <iostream> // REMOVE ME |
| 33 | + |
| 34 | +template <typename CharT, typename TraitsT> |
| 35 | +void test_ispanstream(std::basic_ispanstream<CharT, TraitsT>& spSt, std::size_t size) { |
| 36 | + assert(spSt); |
| 37 | + assert(!spSt.bad()); |
| 38 | + assert(!spSt.fail()); |
| 39 | + assert(spSt.good()); |
| 40 | + assert(spSt.span().size() == size); |
| 41 | + |
| 42 | + // Read from stream |
| 43 | + std::basic_string<CharT, TraitsT> str1; |
| 44 | + spSt >> str1; |
| 45 | + int i1; |
| 46 | + spSt >> i1; |
| 47 | + std::basic_string<CharT, TraitsT> str2; |
| 48 | + spSt >> str2; |
| 49 | + int i2; |
| 50 | + spSt >> i2; |
| 51 | + std::basic_string<CharT, TraitsT> str3; |
| 52 | + spSt >> str3; |
| 53 | + int i3; |
| 54 | + spSt >> i3; |
| 55 | + |
| 56 | + assert(str1 == CS("zmt")); |
| 57 | + assert(i1 == 94); |
| 58 | + assert(str2 == CS("hkt")); |
| 59 | + assert(i2 == 82); |
| 60 | + assert(str3 == CS("pir")); |
| 61 | + assert(i3 == 43); |
| 62 | + |
| 63 | + assert(spSt); |
| 64 | + assert(!spSt.bad()); |
| 65 | + assert(!spSt.fail()); |
| 66 | + assert(spSt.good()); |
| 67 | + |
| 68 | + spSt.clear(); |
| 69 | + |
| 70 | + assert(spSt); |
| 71 | + assert(!spSt.bad()); |
| 72 | + assert(!spSt.fail()); |
| 73 | + assert(spSt.good()); |
| 74 | +} |
| 75 | + |
32 | 76 | template <typename CharT, typename TraitsT = std::char_traits<CharT>> |
33 | 77 | void test() { |
34 | 78 | using SpStream = std::basic_ispanstream<CharT, TraitsT>; |
35 | 79 |
|
36 | 80 | constexpr std::basic_string_view<CharT, TraitsT> sv{SV("zmt 94 hkt 82 pir 43vr")}; |
37 | | - constexpr auto arrSize{30UZ}; |
38 | | - assert(sv.size() < arrSize); |
| 81 | + assert(sv.size() < 30UZ); |
39 | 82 |
|
40 | 83 | // Create a std::span test value |
41 | | - CharT arr[arrSize]{}; |
| 84 | + CharT arr[30UZ]{}; |
42 | 85 | initialize_array_from_string_view(arr, sv); |
43 | 86 |
|
44 | 87 | std::span<CharT> sp{arr}; |
45 | 88 |
|
46 | 89 | // Create a "Read Only Sequence" test value |
47 | | - CharT rosArr[arrSize]{}; |
| 90 | + CharT rosArr[30UZ]{}; |
48 | 91 | initialize_array_from_string_view(rosArr, sv); |
49 | 92 |
|
50 | | - ReadOnlySpan<CharT, arrSize> ros{rosArr}; |
51 | | - assert(ros.size() == arrSize); |
| 93 | + ReadOnlySpan<CharT, 30UZ> ros{rosArr}; |
| 94 | + assert(ros.size() == 30UZ); |
52 | 95 |
|
53 | | - // `std::span` + Mode: default (`in`) |
| 96 | + // std::span` + Mode: default (`in`) |
54 | 97 | { |
55 | 98 | SpStream spSt(sp); |
56 | | - |
57 | | - assert(spSt); |
58 | | - assert(!spSt.bad()); |
59 | | - assert(!spSt.fail()); |
60 | | - assert(spSt.good()); |
61 | | - assert(spSt.span().size() == arrSize); |
62 | | - |
63 | | - // Read from stream |
64 | | - std::basic_string<CharT, TraitsT> str1; |
65 | | - spSt >> str1; |
66 | | - int i1; |
67 | | - spSt >> i1; |
68 | | - std::basic_string<CharT, TraitsT> str2; |
69 | | - spSt >> str2; |
70 | | - int i2; |
71 | | - spSt >> i2; |
72 | | - std::basic_string<CharT, TraitsT> str3; |
73 | | - spSt >> str3; |
74 | | - int i3; |
75 | | - spSt >> i3; |
76 | | - |
77 | | - assert(str1 == CS("zmt")); |
78 | | - assert(i1 == 94); |
79 | | - assert(str2 == CS("hkt")); |
80 | | - assert(i2 == 82); |
81 | | - assert(str3 == CS("pir")); |
82 | | - assert(i3 == 43); |
83 | | - |
84 | | - assert(spSt); |
85 | | - assert(!spSt.bad()); |
86 | | - assert(!spSt.fail()); |
87 | | - assert(spSt.good()); |
88 | | - |
89 | | - spSt.clear(); |
90 | | - |
91 | | - assert(spSt); |
92 | | - assert(!spSt.bad()); |
93 | | - assert(!spSt.fail()); |
94 | | - assert(spSt.good()); |
| 99 | + test_ispanstream(spSt, 30UZ); |
95 | 100 | } |
96 | | - // `ReadOnlySpan` + Mode: default (`in`) |
| 101 | + // std::span` + Mode: explicit `in` |
97 | 102 | { |
98 | | - SpStream spSt(sp); |
99 | | - |
100 | | - assert(spSt); |
101 | | - assert(!spSt.bad()); |
102 | | - assert(!spSt.fail()); |
103 | | - assert(spSt.good()); |
104 | | - assert(spSt.span().size() == arrSize); |
105 | | - |
106 | | - // Read from stream |
107 | | - std::basic_string<CharT, TraitsT> str1; |
108 | | - spSt >> str1; |
109 | | - int i1; |
110 | | - spSt >> i1; |
111 | | - std::basic_string<CharT, TraitsT> str2; |
112 | | - spSt >> str2; |
113 | | - int i2; |
114 | | - spSt >> i2; |
115 | | - std::basic_string<CharT, TraitsT> str3; |
116 | | - spSt >> str3; |
117 | | - int i3; |
118 | | - spSt >> i3; |
119 | | - |
120 | | - assert(str1 == CS("zmt")); |
121 | | - assert(i1 == 94); |
122 | | - assert(str2 == CS("hkt")); |
123 | | - assert(i2 == 82); |
124 | | - assert(str3 == CS("pir")); |
125 | | - assert(i3 == 43); |
126 | | - |
127 | | - assert(spSt); |
128 | | - assert(!spSt.bad()); |
129 | | - assert(!spSt.fail()); |
130 | | - assert(spSt.good()); |
131 | | - |
132 | | - spSt.clear(); |
133 | | - |
134 | | - assert(spSt); |
135 | | - assert(!spSt.bad()); |
136 | | - assert(!spSt.fail()); |
137 | | - assert(spSt.good()); |
| 103 | + SpStream spSt(sp, std::ios_base::in); |
| 104 | + test_ispanstream(spSt, 30UZ); |
138 | 105 | } |
139 | 106 |
|
140 | | - // `std::span` + Mode: `ate` |
| 107 | + // `ReadOnlySpan` + Mode: default (`in`) |
141 | 108 | { |
142 | | - SpStream spSt(sp, std::ios_base::ate); |
143 | | - |
144 | | - assert(spSt); |
145 | | - assert(!spSt.bad()); |
146 | | - assert(!spSt.fail()); |
147 | | - assert(spSt.good()); |
148 | | - assert(spSt.span().size() == arrSize); |
149 | | - |
150 | | - // Read from stream |
151 | | - std::basic_string<CharT, TraitsT> str1; |
152 | | - spSt >> str1; |
153 | | - int i1; |
154 | | - spSt >> i1; |
155 | | - std::basic_string<CharT, TraitsT> str2; |
156 | | - spSt >> str2; |
157 | | - int i2; |
158 | | - spSt >> i2; |
159 | | - std::basic_string<CharT, TraitsT> str3; |
160 | | - spSt >> str3; |
161 | | - int i3; |
162 | | - spSt >> i3; |
163 | | - |
164 | | - assert(str1 == CS("zmt")); |
165 | | - assert(i1 == 94); |
166 | | - assert(str2 == CS("hkt")); |
167 | | - assert(i2 == 82); |
168 | | - assert(str3 == CS("pir")); |
169 | | - assert(i3 == 43); |
170 | | - |
171 | | - assert(spSt); |
172 | | - assert(!spSt.bad()); |
173 | | - assert(!spSt.fail()); |
174 | | - assert(spSt.good()); |
175 | | - |
176 | | - spSt.clear(); |
177 | | - |
178 | | - assert(spSt); |
179 | | - assert(!spSt.bad()); |
180 | | - assert(!spSt.fail()); |
181 | | - assert(spSt.good()); |
| 109 | + SpStream spSt(ros); |
| 110 | + test_ispanstream(spSt, 30UZ); |
182 | 111 | } |
183 | | - // `ReadOnlySpan` + Mode: `ate` |
| 112 | + |
184 | 113 | { |
185 | 114 | SpStream spSt(sp, std::ios_base::ate); |
186 | | - |
187 | | - assert(spSt); |
188 | | - assert(!spSt.bad()); |
189 | | - assert(!spSt.fail()); |
190 | | - assert(spSt.good()); |
191 | | - assert(spSt.span().size() == arrSize); |
192 | | - |
193 | | - // Read from stream |
194 | | - std::basic_string<CharT, TraitsT> str1; |
195 | | - spSt >> str1; |
196 | | - int i1; |
197 | | - spSt >> i1; |
198 | | - std::basic_string<CharT, TraitsT> str2; |
199 | | - spSt >> str2; |
200 | | - int i2; |
201 | | - spSt >> i2; |
202 | | - std::basic_string<CharT, TraitsT> str3; |
203 | | - spSt >> str3; |
204 | | - int i3; |
205 | | - spSt >> i3; |
206 | | - |
207 | | - assert(str1 == CS("zmt")); |
208 | | - assert(i1 == 94); |
209 | | - assert(str2 == CS("hkt")); |
210 | | - assert(i2 == 82); |
211 | | - assert(str3 == CS("pir")); |
212 | | - assert(i3 == 43); |
213 | | - |
214 | | - assert(spSt); |
215 | | - assert(!spSt.bad()); |
216 | | - assert(!spSt.fail()); |
217 | | - assert(spSt.good()); |
218 | | - |
219 | | - spSt.clear(); |
220 | | - |
221 | | - assert(spSt); |
222 | | - assert(!spSt.bad()); |
223 | | - assert(!spSt.fail()); |
224 | | - assert(spSt.good()); |
| 115 | + std::println(stderr, "spSt.span().size() = {}", spSt.span().size()); |
| 116 | + // std::println(stderr, "spSt.tellg() = {}", spSt.tellg()); |
| 117 | + std::cerr << "spSt.tellg() = " << spSt.tellg() << std::endl; |
| 118 | + assert(false); |
225 | 119 | } |
226 | 120 | } |
227 | 121 |
|
228 | 122 | int main(int, char**) { |
229 | 123 | test<char>(); |
230 | 124 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
231 | | - test<wchar_t>(); |
| 125 | + // test<wchar_t>(); |
232 | 126 | #endif |
233 | 127 |
|
234 | 128 | return 0; |
|
0 commit comments