|
15 | 15 |
|
16 | 16 | // streamsize xsgetn(char_type*, streamsize) override; |
17 | 17 |
|
18 | | -// This isn't a required override by the standard, but most implementations override it, since it allows for |
19 | | -// significantly improved performance in some cases. All of this code is required to work, so this isn't a libc++ |
20 | | -// extension |
| 18 | +// This isn't a required override by the standard, but most implementations |
| 19 | +// override it, since it allows for significantly improved performance in some |
| 20 | +// cases. All of this code is required to work, so this isn't a libc++ extension |
21 | 21 |
|
22 | 22 | #include <cassert> |
23 | 23 | #include <fstream> |
| 24 | +#include <vector> |
24 | 25 |
|
25 | 26 | #include "test_macros.h" |
26 | 27 |
|
27 | 28 | int main(int, char**) { |
28 | | - { |
29 | | - char buffer[10]; |
30 | | - std::ifstream fs("xsgetn.test.dat"); |
31 | | - std::filebuf* fb = fs.rdbuf(); |
32 | | - fb->pubsetbuf(buffer, 10); |
| 29 | + std::vector<char> stream_buffer(10); |
| 30 | + std::ifstream fs("xsgetn.test.dat"); |
| 31 | + std::filebuf* fb = fs.rdbuf(); |
| 32 | + fb->pubsetbuf(stream_buffer.data(), 10); |
33 | 33 |
|
34 | | - // Ensure that the buffer is set up |
35 | | - assert(fb->sgetc() == 't'); |
| 34 | + // Ensure that the buffer is set up |
| 35 | + assert(fb->sgetc() == 't'); |
36 | 36 |
|
37 | | - std::string str(5, '\0'); |
| 37 | + std::vector<char> test_buffer(5); |
| 38 | + test_buffer[0] = '\0'; |
38 | 39 |
|
39 | | - { // Check that a read smaller than the buffer works fine |
40 | | - assert(fb->sgetn(str.data(), 5) == 5); |
41 | | - assert(str == "this "); |
42 | | - } |
43 | | - { // Check that reading up to the buffer end works fine |
44 | | - assert(fb->sgetn(str.data(), 5) == 5); |
45 | | - assert(str == "is so"); |
46 | | - } |
47 | | - { // Check that reading from an empty buffer, but more than the buffer can hold works fine |
48 | | - str.resize(12); |
49 | | - assert(fb->sgetn(str.data(), 12) == 12); |
50 | | - assert(str == "me random da"); |
51 | | - } |
52 | | - { // Check that reading from a non-empty buffer, and more than the buffer can hold works fine |
53 | | - // Fill the buffer up |
54 | | - str.resize(2); |
55 | | - assert(fb->sgetn(str.data(), 2) == 2); |
56 | | - assert(str == "ta"); |
| 40 | + { // Check that a read smaller than the buffer works fine |
| 41 | + assert(fb->sgetn(test_buffer.data(), 5) == 5); |
| 42 | + assert(std::string(test_buffer.data()) == "this "); |
| 43 | + } |
| 44 | + { // Check that reading up to the buffer end works fine |
| 45 | + assert(fb->sgetn(test_buffer.data(), 5) == 5); |
| 46 | + assert(std::string(test_buffer.data(), 5) == "is so"); |
| 47 | + } |
| 48 | + { // Check that reading from an empty buffer, but more than the buffer can |
| 49 | + // hold works fine |
| 50 | + test_buffer.resize(12); |
| 51 | + assert(fb->sgetn(test_buffer.data(), 12) == 12); |
| 52 | + assert(std::string(test_buffer.data(), 12) == "me random da"); |
| 53 | + } |
| 54 | + { // Check that reading from a non-empty buffer, and more than the buffer can |
| 55 | + // hold works fine Fill the buffer up |
| 56 | + test_buffer.resize(2); |
| 57 | + assert(fb->sgetn(test_buffer.data(), 2) == 2); |
| 58 | + assert(std::string(test_buffer.data(), 2) == "ta"); |
57 | 59 |
|
58 | | - // Do the actual check |
59 | | - str.resize(12); |
60 | | - assert(fb->sgetn(str.data(), 12) == 12); |
61 | | - assert(str == " to be able "); |
62 | | - } |
63 | | - { // Check that trying to read more than the file size works fine |
64 | | - str.resize(30); |
65 | | - assert(fb->sgetn(str.data(), 30) == 24); |
66 | | - str.resize(24); |
67 | | - assert(str == "to test buffer behaviour"); |
68 | | - } |
| 60 | + // Do the actual check |
| 61 | + test_buffer.resize(12); |
| 62 | + assert(fb->sgetn(test_buffer.data(), 12) == 12); |
| 63 | + assert(std::string(test_buffer.data(), 12) == " to be able "); |
| 64 | + } |
| 65 | + { // Check that trying to read more than the file size works fine |
| 66 | + test_buffer.resize(30); |
| 67 | + assert(fb->sgetn(test_buffer.data(), 30) == 24); |
| 68 | + test_buffer.resize(24); |
| 69 | + assert(std::string(test_buffer.data(), 24) == "to test buffer behaviour"); |
69 | 70 | } |
70 | 71 |
|
71 | 72 | return 0; |
|
0 commit comments