| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <vector> | 10 #include <vector> |
| 8 | 11 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 TEST(StreamsTest, SimpleWriteRead) { | 14 TEST(StreamsTest, SimpleWriteRead) { |
| 12 const unsigned int kValue1 = 12345; | 15 const unsigned int kValue1 = 12345; |
| 13 courgette::SinkStream sink; | 16 courgette::SinkStream sink; |
| 14 | 17 |
| 15 EXPECT_TRUE(sink.WriteVarint32(kValue1)); | 18 EXPECT_TRUE(sink.WriteVarint32(kValue1)); |
| 16 | 19 |
| 17 const uint8* sink_buffer = sink.Buffer(); | 20 const uint8_t* sink_buffer = sink.Buffer(); |
| 18 size_t length = sink.Length(); | 21 size_t length = sink.Length(); |
| 19 | 22 |
| 20 courgette::SourceStream source; | 23 courgette::SourceStream source; |
| 21 source.Init(sink_buffer, length); | 24 source.Init(sink_buffer, length); |
| 22 | 25 |
| 23 unsigned int value; | 26 unsigned int value; |
| 24 bool can_read = source.ReadVarint32(&value); | 27 bool can_read = source.ReadVarint32(&value); |
| 25 EXPECT_TRUE(can_read); | 28 EXPECT_TRUE(can_read); |
| 26 EXPECT_EQ(kValue1, value); | 29 EXPECT_EQ(kValue1, value); |
| 27 EXPECT_EQ(0U, source.Remaining()); | 30 EXPECT_EQ(0U, source.Remaining()); |
| 28 } | 31 } |
| 29 | 32 |
| 30 TEST(StreamsTest, SimpleWriteRead2) { | 33 TEST(StreamsTest, SimpleWriteRead2) { |
| 31 courgette::SinkStream sink; | 34 courgette::SinkStream sink; |
| 32 | 35 |
| 33 EXPECT_TRUE(sink.Write("Hello", 5)); | 36 EXPECT_TRUE(sink.Write("Hello", 5)); |
| 34 | 37 |
| 35 const uint8* sink_buffer = sink.Buffer(); | 38 const uint8_t* sink_buffer = sink.Buffer(); |
| 36 size_t sink_length = sink.Length(); | 39 size_t sink_length = sink.Length(); |
| 37 | 40 |
| 38 courgette::SourceStream source; | 41 courgette::SourceStream source; |
| 39 source.Init(sink_buffer, sink_length); | 42 source.Init(sink_buffer, sink_length); |
| 40 | 43 |
| 41 char text[10] = {0}; | 44 char text[10] = {0}; |
| 42 bool can_read = source.Read(text, 5); | 45 bool can_read = source.Read(text, 5); |
| 43 EXPECT_TRUE(can_read); | 46 EXPECT_TRUE(can_read); |
| 44 EXPECT_EQ(0, memcmp("Hello", text, 5)); | 47 EXPECT_EQ(0, memcmp("Hello", text, 5)); |
| 45 EXPECT_EQ(0U, source.Remaining()); | 48 EXPECT_EQ(0U, source.Remaining()); |
| 46 } | 49 } |
| 47 | 50 |
| 48 TEST(StreamsTest, StreamSetWriteRead) { | 51 TEST(StreamsTest, StreamSetWriteRead) { |
| 49 courgette::SinkStreamSet out; | 52 courgette::SinkStreamSet out; |
| 50 out.Init(4); | 53 out.Init(4); |
| 51 | 54 |
| 52 const unsigned int kValue1 = 12345; | 55 const unsigned int kValue1 = 12345; |
| 53 | 56 |
| 54 EXPECT_TRUE(out.stream(3)->WriteVarint32(kValue1)); | 57 EXPECT_TRUE(out.stream(3)->WriteVarint32(kValue1)); |
| 55 | 58 |
| 56 courgette::SinkStream collected; | 59 courgette::SinkStream collected; |
| 57 | 60 |
| 58 EXPECT_TRUE(out.CopyTo(&collected)); | 61 EXPECT_TRUE(out.CopyTo(&collected)); |
| 59 | 62 |
| 60 const uint8* collected_buffer = collected.Buffer(); | 63 const uint8_t* collected_buffer = collected.Buffer(); |
| 61 size_t collected_length = collected.Length(); | 64 size_t collected_length = collected.Length(); |
| 62 | 65 |
| 63 courgette::SourceStreamSet in; | 66 courgette::SourceStreamSet in; |
| 64 bool can_init = in.Init(collected_buffer, collected_length); | 67 bool can_init = in.Init(collected_buffer, collected_length); |
| 65 EXPECT_TRUE(can_init); | 68 EXPECT_TRUE(can_init); |
| 66 | 69 |
| 67 uint32 value; | 70 uint32_t value; |
| 68 bool can_read = in.stream(3)->ReadVarint32(&value); | 71 bool can_read = in.stream(3)->ReadVarint32(&value); |
| 69 EXPECT_TRUE(can_read); | 72 EXPECT_TRUE(can_read); |
| 70 EXPECT_EQ(kValue1, value); | 73 EXPECT_EQ(kValue1, value); |
| 71 EXPECT_EQ(0U, in.stream(3)->Remaining()); | 74 EXPECT_EQ(0U, in.stream(3)->Remaining()); |
| 72 EXPECT_EQ(0U, in.stream(2)->Remaining()); | 75 EXPECT_EQ(0U, in.stream(2)->Remaining()); |
| 73 } | 76 } |
| 74 | 77 |
| 75 TEST(StreamsTest, StreamSetWriteRead2) { | 78 TEST(StreamsTest, StreamSetWriteRead2) { |
| 76 const size_t kNumberOfStreams = 4; | 79 const size_t kNumberOfStreams = 4; |
| 77 const unsigned int kEnd = ~0U; | 80 const unsigned int kEnd = ~0U; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 97 | 100 |
| 98 EXPECT_TRUE(out.CopyTo(&collected)); | 101 EXPECT_TRUE(out.CopyTo(&collected)); |
| 99 | 102 |
| 100 courgette::SourceStreamSet in; | 103 courgette::SourceStreamSet in; |
| 101 bool can_init = in.Init(collected.Buffer(), collected.Length()); | 104 bool can_init = in.Init(collected.Buffer(), collected.Length()); |
| 102 EXPECT_TRUE(can_init); | 105 EXPECT_TRUE(can_init); |
| 103 | 106 |
| 104 for (size_t i = 0; data[i] != kEnd; i += 2) { | 107 for (size_t i = 0; data[i] != kEnd; i += 2) { |
| 105 size_t id = data[i]; | 108 size_t id = data[i]; |
| 106 size_t datum = data[i + 1]; | 109 size_t datum = data[i + 1]; |
| 107 uint32 value = 77; | 110 uint32_t value = 77; |
| 108 bool can_read = in.stream(id)->ReadVarint32(&value); | 111 bool can_read = in.stream(id)->ReadVarint32(&value); |
| 109 EXPECT_TRUE(can_read); | 112 EXPECT_TRUE(can_read); |
| 110 EXPECT_EQ(datum, value); | 113 EXPECT_EQ(datum, value); |
| 111 } | 114 } |
| 112 | 115 |
| 113 for (size_t i = 0; i < kNumberOfStreams; ++i) { | 116 for (size_t i = 0; i < kNumberOfStreams; ++i) { |
| 114 EXPECT_EQ(0U, in.stream(i)->Remaining()); | 117 EXPECT_EQ(0U, in.stream(i)->Remaining()); |
| 115 } | 118 } |
| 116 } | 119 } |
| 117 | 120 |
| 118 TEST(StreamsTest, SignedVarint32) { | 121 TEST(StreamsTest, SignedVarint32) { |
| 119 courgette::SinkStream out; | 122 courgette::SinkStream out; |
| 120 | 123 |
| 121 static const int32 data[] = { | 124 static const int32_t data[] = {0, 64, 128, 8192, |
| 122 0, 64, 128, 8192, 16384, | 125 16384, 1 << 20, 1 << 21, 1 << 22, |
| 123 1 << 20, 1 << 21, 1 << 22, | 126 1 << 27, 1 << 28, 0x7fffffff, -0x7fffffff}; |
| 124 1 << 27, 1 << 28, | |
| 125 0x7fffffff, -0x7fffffff | |
| 126 }; | |
| 127 | 127 |
| 128 std::vector<int32> values; | 128 std::vector<int32_t> values; |
| 129 for (size_t i = 0; i < sizeof(data)/sizeof(data[0]); ++i) { | 129 for (size_t i = 0; i < sizeof(data)/sizeof(data[0]); ++i) { |
| 130 int32 basis = data[i]; | 130 int32_t basis = data[i]; |
| 131 for (int delta = -4; delta <= 4; ++delta) { | 131 for (int delta = -4; delta <= 4; ++delta) { |
| 132 EXPECT_TRUE(out.WriteVarint32Signed(basis + delta)); | 132 EXPECT_TRUE(out.WriteVarint32Signed(basis + delta)); |
| 133 values.push_back(basis + delta); | 133 values.push_back(basis + delta); |
| 134 EXPECT_TRUE(out.WriteVarint32Signed(-basis + delta)); | 134 EXPECT_TRUE(out.WriteVarint32Signed(-basis + delta)); |
| 135 values.push_back(-basis + delta); | 135 values.push_back(-basis + delta); |
| 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_t datum; |
| 145 bool can_read = in.ReadVarint32Signed(&datum); | 145 bool can_read = in.ReadVarint32Signed(&datum); |
| 146 EXPECT_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_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; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 181 EXPECT_TRUE(can_read_2); | 181 EXPECT_TRUE(can_read_2); |
| 182 EXPECT_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_t datum; |
| 192 EXPECT_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_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_TRUE(subset1.Empty()); | 196 EXPECT_TRUE(subset1.Empty()); |
| 197 | 197 |
| 198 EXPECT_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_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_TRUE(subset2.Empty()); | 202 EXPECT_TRUE(subset2.Empty()); |
| 203 } | 203 } |
| OLD | NEW |