1 // Copyright 2018 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 // Author: ericv@google.com (Eric Veach)
17 
18 #include "s2/encoded_string_vector.h"
19 
20 #include <vector>
21 #include <gtest/gtest.h>
22 #include "s2/third_party/absl/strings/string_view.h"
23 
24 using absl::string_view;
25 using std::vector;
26 
27 namespace s2coding {
28 
TestEncodedStringVector(const vector<string> & input,size_t expected_bytes)29 void TestEncodedStringVector(const vector<string>& input,
30                              size_t expected_bytes) {
31   Encoder encoder;
32   StringVectorEncoder::Encode(input, &encoder);
33   EXPECT_EQ(expected_bytes, encoder.length());
34   Decoder decoder(encoder.base(), encoder.length());
35   EncodedStringVector actual;
36   ASSERT_TRUE(actual.Init(&decoder));
37   vector<string_view> expected;
38   for (const auto& str : input) {
39     expected.push_back(string_view(str));
40   }
41   EXPECT_EQ(actual.Decode(), expected);
42 }
43 
TEST(EncodedStringVectorTest,Empty)44 TEST(EncodedStringVectorTest, Empty) {
45   TestEncodedStringVector({}, 1);
46 }
47 
TEST(EncodedStringVectorTest,EmptyString)48 TEST(EncodedStringVectorTest, EmptyString) {
49   TestEncodedStringVector({""}, 2);
50 }
51 
TEST(EncodedStringVectorTest,RepeatedEmptyStrings)52 TEST(EncodedStringVectorTest, RepeatedEmptyStrings) {
53   TestEncodedStringVector({"", "", ""}, 4);
54 }
55 
TEST(EncodedStringVectorTest,OneString)56 TEST(EncodedStringVectorTest, OneString) {
57   TestEncodedStringVector({"apples"}, 8);
58 }
59 
TEST(EncodedStringVectorTest,TwoStrings)60 TEST(EncodedStringVectorTest, TwoStrings) {
61   TestEncodedStringVector({"fuji", "mutsu"}, 12);
62 }
63 
TEST(EncodedStringVectorTest,TwoBigStrings)64 TEST(EncodedStringVectorTest, TwoBigStrings) {
65   TestEncodedStringVector({string(10000, 'x'), string(100000, 'y')},
66                           110007);
67 }
68 
69 }  // namespace s2coding
70