OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "courgette/streams.h" | 5 #include "courgette/streams.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 TEST(StreamsTest, SimpleWriteRead) { | 11 TEST(StreamsTest, SimpleWriteRead) { |
12 const unsigned int kValue1 = 12345; | 12 const unsigned int kValue1 = 12345; |
13 courgette::SinkStream sink; | 13 courgette::SinkStream sink; |
14 | 14 |
15 sink.WriteVarint32(kValue1); | 15 sink.WriteVarint32(kValue1); |
16 | 16 |
17 const uint8* sink_buffer = sink.Buffer(); | 17 const uint8* sink_buffer = sink.Buffer(); |
18 size_t length = sink.Length(); | 18 size_t length = sink.Length(); |
19 | 19 |
20 courgette::SourceStream source; | 20 courgette::SourceStream source; |
21 source.Init(sink_buffer, length); | 21 source.Init(sink_buffer, length); |
22 | 22 |
23 unsigned int value; | 23 unsigned int value; |
24 bool can_read = source.ReadVarint32(&value); | 24 bool can_read = source.ReadVarint32(&value); |
25 EXPECT_EQ(true, can_read); | 25 EXPECT_TRUE(can_read); |
26 EXPECT_EQ(kValue1, value); | 26 EXPECT_EQ(kValue1, value); |
27 EXPECT_EQ(0U, source.Remaining()); | 27 EXPECT_EQ(0U, source.Remaining()); |
28 } | 28 } |
29 | 29 |
30 TEST(StreamsTest, SimpleWriteRead2) { | 30 TEST(StreamsTest, SimpleWriteRead2) { |
31 courgette::SinkStream sink; | 31 courgette::SinkStream sink; |
32 | 32 |
33 sink.Write("Hello", 5); | 33 sink.Write("Hello", 5); |
34 | 34 |
35 const uint8* sink_buffer = sink.Buffer(); | 35 const uint8* sink_buffer = sink.Buffer(); |
36 size_t sink_length = sink.Length(); | 36 size_t sink_length = sink.Length(); |
37 | 37 |
38 courgette::SourceStream source; | 38 courgette::SourceStream source; |
39 source.Init(sink_buffer, sink_length); | 39 source.Init(sink_buffer, sink_length); |
40 | 40 |
41 char text[10] = {0}; | 41 char text[10] = {0}; |
42 bool can_read = source.Read(text, 5); | 42 bool can_read = source.Read(text, 5); |
43 EXPECT_EQ(true, can_read); | 43 EXPECT_TRUE(can_read); |
44 EXPECT_EQ(0, memcmp("Hello", text, 5)); | 44 EXPECT_EQ(0, memcmp("Hello", text, 5)); |
45 EXPECT_EQ(0U, source.Remaining()); | 45 EXPECT_EQ(0U, source.Remaining()); |
46 } | 46 } |
47 | 47 |
48 TEST(StreamsTest, StreamSetWriteRead) { | 48 TEST(StreamsTest, StreamSetWriteRead) { |
49 courgette::SinkStreamSet out; | 49 courgette::SinkStreamSet out; |
50 out.Init(4); | 50 out.Init(4); |
51 | 51 |
52 const unsigned int kValue1 = 12345; | 52 const unsigned int kValue1 = 12345; |
53 | 53 |
54 out.stream(3)->WriteVarint32(kValue1); | 54 out.stream(3)->WriteVarint32(kValue1); |
55 | 55 |
56 courgette::SinkStream collected; | 56 courgette::SinkStream collected; |
57 | 57 |
58 out.CopyTo(&collected); | 58 out.CopyTo(&collected); |
59 | 59 |
60 const uint8* collected_buffer = collected.Buffer(); | 60 const uint8* collected_buffer = collected.Buffer(); |
61 size_t collected_length = collected.Length(); | 61 size_t collected_length = collected.Length(); |
62 | 62 |
63 courgette::SourceStreamSet in; | 63 courgette::SourceStreamSet in; |
64 bool can_init = in.Init(collected_buffer, collected_length); | 64 bool can_init = in.Init(collected_buffer, collected_length); |
65 EXPECT_EQ(true, can_init); | 65 EXPECT_TRUE(can_init); |
66 | 66 |
67 uint32 value; | 67 uint32 value; |
68 bool can_read = in.stream(3)->ReadVarint32(&value); | 68 bool can_read = in.stream(3)->ReadVarint32(&value); |
69 EXPECT_EQ(true, can_read); | 69 EXPECT_TRUE(can_read); |
70 EXPECT_EQ(kValue1, value); | 70 EXPECT_EQ(kValue1, value); |
71 EXPECT_EQ(0U, in.stream(3)->Remaining()); | 71 EXPECT_EQ(0U, in.stream(3)->Remaining()); |
72 EXPECT_EQ(0U, in.stream(2)->Remaining()); | 72 EXPECT_EQ(0U, in.stream(2)->Remaining()); |
73 } | 73 } |
74 | 74 |
75 TEST(StreamsTest, StreamSetWriteRead2) { | 75 TEST(StreamsTest, StreamSetWriteRead2) { |
76 const size_t kNumberOfStreams = 4; | 76 const size_t kNumberOfStreams = 4; |
77 const unsigned int kEnd = ~0U; | 77 const unsigned int kEnd = ~0U; |
78 | 78 |
79 courgette::SinkStreamSet out; | 79 courgette::SinkStreamSet out; |
(...skipping 12 matching lines...) Expand all Loading... |
92 size_t datum = data[i + 1]; | 92 size_t datum = data[i + 1]; |
93 out.stream(id)->WriteVarint32(datum); | 93 out.stream(id)->WriteVarint32(datum); |
94 } | 94 } |
95 | 95 |
96 courgette::SinkStream collected; | 96 courgette::SinkStream collected; |
97 | 97 |
98 out.CopyTo(&collected); | 98 out.CopyTo(&collected); |
99 | 99 |
100 courgette::SourceStreamSet in; | 100 courgette::SourceStreamSet in; |
101 bool can_init = in.Init(collected.Buffer(), collected.Length()); | 101 bool can_init = in.Init(collected.Buffer(), collected.Length()); |
102 EXPECT_EQ(true, can_init); | 102 EXPECT_TRUE(can_init); |
103 | 103 |
104 for (size_t i = 0; data[i] != kEnd; i += 2) { | 104 for (size_t i = 0; data[i] != kEnd; i += 2) { |
105 size_t id = data[i]; | 105 size_t id = data[i]; |
106 size_t datum = data[i + 1]; | 106 size_t datum = data[i + 1]; |
107 uint32 value = 77; | 107 uint32 value = 77; |
108 bool can_read = in.stream(id)->ReadVarint32(&value); | 108 bool can_read = in.stream(id)->ReadVarint32(&value); |
109 EXPECT_EQ(true, can_read); | 109 EXPECT_TRUE(can_read); |
110 EXPECT_EQ(datum, value); | 110 EXPECT_EQ(datum, value); |
111 } | 111 } |
112 | 112 |
113 for (size_t i = 0; i < kNumberOfStreams; ++i) { | 113 for (size_t i = 0; i < kNumberOfStreams; ++i) { |
114 EXPECT_EQ(0U, in.stream(i)->Remaining()); | 114 EXPECT_EQ(0U, in.stream(i)->Remaining()); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 TEST(StreamsTest, SignedVarint32) { | 118 TEST(StreamsTest, SignedVarint32) { |
119 courgette::SinkStream out; | 119 courgette::SinkStream out; |
(...skipping 16 matching lines...) Expand all Loading... |
136 } | 136 } |
137 } | 137 } |
138 | 138 |
139 courgette::SourceStream in; | 139 courgette::SourceStream in; |
140 in.Init(out); | 140 in.Init(out); |
141 | 141 |
142 for (size_t i = 0; i < values.size(); ++i) { | 142 for (size_t i = 0; i < values.size(); ++i) { |
143 int written_value = values[i]; | 143 int written_value = values[i]; |
144 int32 datum; | 144 int32 datum; |
145 bool can_read = in.ReadVarint32Signed(&datum); | 145 bool can_read = in.ReadVarint32Signed(&datum); |
146 EXPECT_EQ(true, can_read); | 146 EXPECT_TRUE(can_read); |
147 EXPECT_EQ(written_value, datum); | 147 EXPECT_EQ(written_value, datum); |
148 } | 148 } |
149 | 149 |
150 EXPECT_EQ(true, in.Empty()); | 150 EXPECT_TRUE(in.Empty()); |
151 } | 151 } |
152 | 152 |
153 TEST(StreamsTest, StreamSetReadWrite) { | 153 TEST(StreamsTest, StreamSetReadWrite) { |
154 courgette::SinkStreamSet out; | 154 courgette::SinkStreamSet out; |
155 | 155 |
156 { // Local scope for temporary stream sets. | 156 { // Local scope for temporary stream sets. |
157 courgette::SinkStreamSet subset1; | 157 courgette::SinkStreamSet subset1; |
158 subset1.stream(3)->WriteVarint32(30000); | 158 subset1.stream(3)->WriteVarint32(30000); |
159 subset1.stream(5)->WriteVarint32(50000); | 159 subset1.stream(5)->WriteVarint32(50000); |
160 out.WriteSet(&subset1); | 160 out.WriteSet(&subset1); |
161 | 161 |
162 courgette::SinkStreamSet subset2; | 162 courgette::SinkStreamSet subset2; |
163 subset2.stream(2)->WriteVarint32(20000); | 163 subset2.stream(2)->WriteVarint32(20000); |
164 subset2.stream(6)->WriteVarint32(60000); | 164 subset2.stream(6)->WriteVarint32(60000); |
165 out.WriteSet(&subset2); | 165 out.WriteSet(&subset2); |
166 } | 166 } |
167 | 167 |
168 courgette::SinkStream collected; | 168 courgette::SinkStream collected; |
169 out.CopyTo(&collected); | 169 out.CopyTo(&collected); |
170 courgette::SourceStreamSet in; | 170 courgette::SourceStreamSet in; |
171 bool can_init_in = in.Init(collected.Buffer(), collected.Length()); | 171 bool can_init_in = in.Init(collected.Buffer(), collected.Length()); |
172 EXPECT_EQ(true, can_init_in); | 172 EXPECT_TRUE(can_init_in); |
173 | 173 |
174 courgette::SourceStreamSet subset1; | 174 courgette::SourceStreamSet subset1; |
175 bool can_read_1 = in.ReadSet(&subset1); | 175 bool can_read_1 = in.ReadSet(&subset1); |
176 EXPECT_EQ(true, can_read_1); | 176 EXPECT_TRUE(can_read_1); |
177 EXPECT_FALSE(in.Empty()); | 177 EXPECT_FALSE(in.Empty()); |
178 | 178 |
179 courgette::SourceStreamSet subset2; | 179 courgette::SourceStreamSet subset2; |
180 bool can_read_2 = in.ReadSet(&subset2); | 180 bool can_read_2 = in.ReadSet(&subset2); |
181 EXPECT_EQ(true, can_read_2); | 181 EXPECT_TRUE(can_read_2); |
182 EXPECT_EQ(true, in.Empty()); | 182 EXPECT_TRUE(in.Empty()); |
183 | 183 |
184 courgette::SourceStreamSet subset3; | 184 courgette::SourceStreamSet subset3; |
185 bool can_read_3 = in.ReadSet(&subset3); | 185 bool can_read_3 = in.ReadSet(&subset3); |
186 EXPECT_FALSE(can_read_3); | 186 EXPECT_FALSE(can_read_3); |
187 | 187 |
188 EXPECT_FALSE(subset1.Empty()); | 188 EXPECT_FALSE(subset1.Empty()); |
189 EXPECT_FALSE(subset1.Empty()); | 189 EXPECT_FALSE(subset1.Empty()); |
190 | 190 |
191 uint32 datum; | 191 uint32 datum; |
192 EXPECT_EQ(true, subset1.stream(3)->ReadVarint32(&datum)); | 192 EXPECT_TRUE(subset1.stream(3)->ReadVarint32(&datum)); |
193 EXPECT_EQ(30000U, datum); | 193 EXPECT_EQ(30000U, datum); |
194 EXPECT_EQ(true, subset1.stream(5)->ReadVarint32(&datum)); | 194 EXPECT_TRUE(subset1.stream(5)->ReadVarint32(&datum)); |
195 EXPECT_EQ(50000U, datum); | 195 EXPECT_EQ(50000U, datum); |
196 EXPECT_EQ(true, subset1.Empty()); | 196 EXPECT_TRUE(subset1.Empty()); |
197 | 197 |
198 EXPECT_EQ(true, subset2.stream(2)->ReadVarint32(&datum)); | 198 EXPECT_TRUE(subset2.stream(2)->ReadVarint32(&datum)); |
199 EXPECT_EQ(20000U, datum); | 199 EXPECT_EQ(20000U, datum); |
200 EXPECT_EQ(true, subset2.stream(6)->ReadVarint32(&datum)); | 200 EXPECT_TRUE(subset2.stream(6)->ReadVarint32(&datum)); |
201 EXPECT_EQ(60000U, datum); | 201 EXPECT_EQ(60000U, datum); |
202 EXPECT_EQ(true, subset2.Empty()); | 202 EXPECT_TRUE(subset2.Empty()); |
203 } | 203 } |
OLD | NEW |