summaryrefslogtreecommitdiffstats
path: root/courgette/streams_unittest.cc
blob: d0903c9530814c671404d1b053b1bd9c704446c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "courgette/streams.h"

#include <vector>

#include "testing/gtest/include/gtest/gtest.h"

TEST(StreamsTest, SimpleWriteRead) {
  const unsigned int kValue1 = 12345;
  courgette::SinkStream sink;

  EXPECT_TRUE(sink.WriteVarint32(kValue1));

  const uint8* sink_buffer = sink.Buffer();
  size_t length = sink.Length();

  courgette::SourceStream source;
  source.Init(sink_buffer, length);

  unsigned int value;
  bool can_read = source.ReadVarint32(&value);
  EXPECT_TRUE(can_read);
  EXPECT_EQ(kValue1, value);
  EXPECT_EQ(0U, source.Remaining());
}

TEST(StreamsTest, SimpleWriteRead2) {
  courgette::SinkStream sink;

  EXPECT_TRUE(sink.Write("Hello", 5));

  const uint8* sink_buffer = sink.Buffer();
  size_t sink_length = sink.Length();

  courgette::SourceStream source;
  source.Init(sink_buffer, sink_length);

  char text[10] = {0};
  bool can_read = source.Read(text, 5);
  EXPECT_TRUE(can_read);
  EXPECT_EQ(0, memcmp("Hello", text, 5));
  EXPECT_EQ(0U, source.Remaining());
}

TEST(StreamsTest, StreamSetWriteRead) {
  courgette::SinkStreamSet out;
  out.Init(4);

  const unsigned int kValue1 = 12345;

  EXPECT_TRUE(out.stream(3)->WriteVarint32(kValue1));

  courgette::SinkStream collected;

  EXPECT_TRUE(out.CopyTo(&collected));

  const uint8* collected_buffer = collected.Buffer();
  size_t collected_length = collected.Length();

  courgette::SourceStreamSet in;
  bool can_init = in.Init(collected_buffer, collected_length);
  EXPECT_TRUE(can_init);

  uint32 value;
  bool can_read = in.stream(3)->ReadVarint32(&value);
  EXPECT_TRUE(can_read);
  EXPECT_EQ(kValue1, value);
  EXPECT_EQ(0U, in.stream(3)->Remaining());
  EXPECT_EQ(0U, in.stream(2)->Remaining());
}

TEST(StreamsTest, StreamSetWriteRead2) {
  const size_t kNumberOfStreams = 4;
  const unsigned int kEnd = ~0U;

  courgette::SinkStreamSet out;
  out.Init(kNumberOfStreams);

  static const unsigned int data[] = {
    3, 123,  3, 1000,  0, 100, 2, 100,  0, 999999,
    0, 0,  0, 0,  1, 2,  1, 3,  1, 5,  0, 66,
    // varint32 edge case values:
    1, 127,  1, 128,  1, 129,  1, 16383,  1, 16384,
    kEnd
  };

  for (size_t i = 0;  data[i] != kEnd;  i += 2) {
    size_t id = data[i];
    size_t datum = data[i + 1];
    EXPECT_TRUE(out.stream(id)->WriteVarint32(datum));
  }

  courgette::SinkStream collected;

  EXPECT_TRUE(out.CopyTo(&collected));

  courgette::SourceStreamSet in;
  bool can_init = in.Init(collected.Buffer(), collected.Length());
  EXPECT_TRUE(can_init);

  for (size_t i = 0;  data[i] != kEnd;  i += 2) {
    size_t id = data[i];
    size_t datum = data[i + 1];
    uint32 value = 77;
    bool can_read = in.stream(id)->ReadVarint32(&value);
    EXPECT_TRUE(can_read);
    EXPECT_EQ(datum, value);
  }

  for (size_t i = 0;  i < kNumberOfStreams;  ++i) {
    EXPECT_EQ(0U, in.stream(i)->Remaining());
  }
}

TEST(StreamsTest, SignedVarint32) {
  courgette::SinkStream out;

  static const int32 data[] = {
    0, 64, 128, 8192, 16384,
    1 << 20, 1 << 21, 1 << 22,
    1 << 27, 1 << 28,
    0x7fffffff, -0x7fffffff
  };

  std::vector<int32> values;
  for (size_t i = 0;  i < sizeof(data)/sizeof(data[0]);  ++i) {
    int32 basis = data[i];
    for (int delta = -4; delta <= 4; ++delta) {
      EXPECT_TRUE(out.WriteVarint32Signed(basis + delta));
      values.push_back(basis + delta);
      EXPECT_TRUE(out.WriteVarint32Signed(-basis + delta));
      values.push_back(-basis + delta);
    }
  }

  courgette::SourceStream in;
  in.Init(out);

  for (size_t i = 0;  i < values.size();  ++i) {
    int written_value = values[i];
    int32 datum;
    bool can_read = in.ReadVarint32Signed(&datum);
    EXPECT_TRUE(can_read);
    EXPECT_EQ(written_value, datum);
  }

  EXPECT_TRUE(in.Empty());
}

TEST(StreamsTest, StreamSetReadWrite) {
  courgette::SinkStreamSet out;

  { // Local scope for temporary stream sets.
    courgette::SinkStreamSet subset1;
    EXPECT_TRUE(subset1.stream(3)->WriteVarint32(30000));
    EXPECT_TRUE(subset1.stream(5)->WriteVarint32(50000));
    EXPECT_TRUE(out.WriteSet(&subset1));

    courgette::SinkStreamSet subset2;
    EXPECT_TRUE(subset2.stream(2)->WriteVarint32(20000));
    EXPECT_TRUE(subset2.stream(6)->WriteVarint32(60000));
    EXPECT_TRUE(out.WriteSet(&subset2));
  }

  courgette::SinkStream collected;
  EXPECT_TRUE(out.CopyTo(&collected));
  courgette::SourceStreamSet in;
  bool can_init_in = in.Init(collected.Buffer(), collected.Length());
  EXPECT_TRUE(can_init_in);

  courgette::SourceStreamSet subset1;
  bool can_read_1 = in.ReadSet(&subset1);
  EXPECT_TRUE(can_read_1);
  EXPECT_FALSE(in.Empty());

  courgette::SourceStreamSet subset2;
  bool can_read_2 = in.ReadSet(&subset2);
  EXPECT_TRUE(can_read_2);
  EXPECT_TRUE(in.Empty());

  courgette::SourceStreamSet subset3;
  bool can_read_3 = in.ReadSet(&subset3);
  EXPECT_FALSE(can_read_3);

  EXPECT_FALSE(subset1.Empty());
  EXPECT_FALSE(subset1.Empty());

  uint32 datum;
  EXPECT_TRUE(subset1.stream(3)->ReadVarint32(&datum));
  EXPECT_EQ(30000U, datum);
  EXPECT_TRUE(subset1.stream(5)->ReadVarint32(&datum));
  EXPECT_EQ(50000U, datum);
  EXPECT_TRUE(subset1.Empty());

  EXPECT_TRUE(subset2.stream(2)->ReadVarint32(&datum));
  EXPECT_EQ(20000U, datum);
  EXPECT_TRUE(subset2.stream(6)->ReadVarint32(&datum));
  EXPECT_EQ(60000U, datum);
  EXPECT_TRUE(subset2.Empty());
}