1 | #include <sstream> |
2 | |
3 | #include <IO/LimitReadBuffer.h> |
4 | #include <IO/copyData.h> |
5 | #include <IO/WriteBufferFromString.h> |
6 | #include <IO/ReadHelpers.h> |
7 | |
8 | |
9 | namespace DB |
10 | { |
11 | namespace ErrorCodes |
12 | { |
13 | extern const int LOGICAL_ERROR; |
14 | } |
15 | } |
16 | |
17 | |
18 | int main(int, char **) |
19 | try |
20 | { |
21 | using namespace DB; |
22 | |
23 | std::stringstream s; |
24 | |
25 | { |
26 | std::string src = "1" ; |
27 | |
28 | std::string dst; |
29 | |
30 | ReadBuffer in(src.data(), src.size(), 0); |
31 | |
32 | LimitReadBuffer limit_in(in, 1, false); |
33 | |
34 | { |
35 | WriteBufferFromString out(dst); |
36 | |
37 | copyData(limit_in, out); |
38 | } |
39 | |
40 | if (limit_in.count() != 1) |
41 | { |
42 | s << "Failed!, incorrect count(): " << limit_in.count(); |
43 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
44 | } |
45 | |
46 | if (in.count() != limit_in.count()) |
47 | { |
48 | s << "Failed!, incorrect underlying buffer's count(): " << in.count(); |
49 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
50 | } |
51 | if (src != dst) |
52 | { |
53 | s << "Failed!, incorrect destination value, read: " << dst << ", expected: " << src; |
54 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
55 | } |
56 | } |
57 | { |
58 | std::string src = "abc" ; |
59 | ReadBuffer in(src.data(), src.size(), 0); |
60 | |
61 | std::string dst; |
62 | |
63 | { |
64 | WriteBufferFromString out(dst); |
65 | |
66 | char x; |
67 | readChar(x, in); |
68 | |
69 | LimitReadBuffer limit_in(in, 1, false); |
70 | |
71 | copyData(limit_in, out); |
72 | |
73 | |
74 | if (in.count() != 2) |
75 | { |
76 | s << "Failed!, Incorrect underlying buffer's count: " << in.count() << ", expected: " << 2; |
77 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
78 | } |
79 | |
80 | if (limit_in.count() != 1) |
81 | { |
82 | s << "Failed!, Incorrect count: " << limit_in.count() << ", expected: " << 1; |
83 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
84 | } |
85 | } |
86 | |
87 | if (dst != "b" ) |
88 | { |
89 | s << "Failed!, Incorrect destination value: " << dst << ", expected 'b'" ; |
90 | throw Exception(dst, ErrorCodes::LOGICAL_ERROR); |
91 | } |
92 | |
93 | char y; |
94 | readChar(y, in); |
95 | if (y != 'c') |
96 | { |
97 | s << "Failed!, Read incorrect value from underlying buffer: " << y << ", expected 'c'" ; |
98 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
99 | } |
100 | while (!in.eof()) |
101 | in.ignore(); |
102 | if (in.count() != 3) |
103 | { |
104 | s << "Failed!, Incorrect final count from underlying buffer: " << in.count() << ", expected: 3" ; |
105 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
106 | } |
107 | } |
108 | |
109 | { |
110 | std::string src = "abc" ; |
111 | ReadBuffer in(src.data(), src.size(), 0); |
112 | |
113 | { |
114 | LimitReadBuffer limit_in(in, 1, false); |
115 | |
116 | char x; |
117 | readChar(x, limit_in); |
118 | |
119 | if (limit_in.count() != 1) |
120 | { |
121 | s << "Failed!, Incorrect count: " << limit_in.count() << ", expected: " << 1; |
122 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
123 | } |
124 | } |
125 | |
126 | if (in.count() != 1) |
127 | { |
128 | s << "Failed!, Incorrect final count from underlying buffer: " << in.count() << ", expected: 1" ; |
129 | throw Exception(s.str(), ErrorCodes::LOGICAL_ERROR); |
130 | } |
131 | } |
132 | |
133 | return 0; |
134 | } |
135 | catch (const DB::Exception & e) |
136 | { |
137 | std::cerr << e.what() << ", " << e.displayText() << std::endl; |
138 | return 1; |
139 | } |
140 | |