1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "annotator/zlib-utils.h"
18
19 #include <memory>
20
21 #include "annotator/model_generated.h"
22 #include "utils/zlib/zlib.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25
26 namespace libtextclassifier3 {
27
TEST(ZlibUtilsTest,CompressModel)28 TEST(ZlibUtilsTest, CompressModel) {
29 ModelT model;
30 model.regex_model.reset(new RegexModelT);
31 model.regex_model->patterns.emplace_back(new RegexModel_::PatternT);
32 model.regex_model->patterns.back()->pattern = "this is a test pattern";
33 model.regex_model->patterns.emplace_back(new RegexModel_::PatternT);
34 model.regex_model->patterns.back()->pattern = "this is a second test pattern";
35
36 model.datetime_model.reset(new DatetimeModelT);
37 model.datetime_model->patterns.emplace_back(new DatetimeModelPatternT);
38 model.datetime_model->patterns.back()->regexes.emplace_back(
39 new DatetimeModelPattern_::RegexT);
40 model.datetime_model->patterns.back()->regexes.back()->pattern =
41 "an example datetime pattern";
42 model.datetime_model->extractors.emplace_back(new DatetimeModelExtractorT);
43 model.datetime_model->extractors.back()->pattern =
44 "an example datetime extractor";
45
46 // Compress the model.
47 EXPECT_TRUE(CompressModel(&model));
48
49 // Sanity check that uncompressed field is removed.
50 EXPECT_TRUE(model.regex_model->patterns[0]->pattern.empty());
51 EXPECT_TRUE(model.regex_model->patterns[1]->pattern.empty());
52 EXPECT_TRUE(model.datetime_model->patterns[0]->regexes[0]->pattern.empty());
53 EXPECT_TRUE(model.datetime_model->extractors[0]->pattern.empty());
54
55 // Pack and load the model.
56 flatbuffers::FlatBufferBuilder builder;
57 builder.Finish(Model::Pack(builder, &model));
58 const Model* compressed_model =
59 GetModel(reinterpret_cast<const char*>(builder.GetBufferPointer()));
60 ASSERT_TRUE(compressed_model != nullptr);
61
62 // Decompress the fields again and check that they match the original.
63 std::unique_ptr<ZlibDecompressor> decompressor = ZlibDecompressor::Instance();
64 ASSERT_TRUE(decompressor != nullptr);
65 std::string uncompressed_pattern;
66 EXPECT_TRUE(decompressor->MaybeDecompress(
67 compressed_model->regex_model()->patterns()->Get(0)->compressed_pattern(),
68 &uncompressed_pattern));
69 EXPECT_EQ(uncompressed_pattern, "this is a test pattern");
70 EXPECT_TRUE(decompressor->MaybeDecompress(
71 compressed_model->regex_model()->patterns()->Get(1)->compressed_pattern(),
72 &uncompressed_pattern));
73 EXPECT_EQ(uncompressed_pattern, "this is a second test pattern");
74 EXPECT_TRUE(decompressor->MaybeDecompress(compressed_model->datetime_model()
75 ->patterns()
76 ->Get(0)
77 ->regexes()
78 ->Get(0)
79 ->compressed_pattern(),
80 &uncompressed_pattern));
81 EXPECT_EQ(uncompressed_pattern, "an example datetime pattern");
82 EXPECT_TRUE(decompressor->MaybeDecompress(compressed_model->datetime_model()
83 ->extractors()
84 ->Get(0)
85 ->compressed_pattern(),
86 &uncompressed_pattern));
87 EXPECT_EQ(uncompressed_pattern, "an example datetime extractor");
88
89 EXPECT_TRUE(DecompressModel(&model));
90 EXPECT_EQ(model.regex_model->patterns[0]->pattern, "this is a test pattern");
91 EXPECT_EQ(model.regex_model->patterns[1]->pattern,
92 "this is a second test pattern");
93 EXPECT_EQ(model.datetime_model->patterns[0]->regexes[0]->pattern,
94 "an example datetime pattern");
95 EXPECT_EQ(model.datetime_model->extractors[0]->pattern,
96 "an example datetime extractor");
97 }
98
99 } // namespace libtextclassifier3
100