• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(AnnotatorZlibUtilsTest,CompressModel)28 TEST(AnnotatorZlibUtilsTest, 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   model.intent_options.reset(new IntentFactoryModelT);
47   model.intent_options->generator.emplace_back(
48       new IntentFactoryModel_::IntentGeneratorT);
49   const std::string intent_generator1 = "lua generator 1";
50   model.intent_options->generator.back()->lua_template_generator =
51       std::vector<uint8_t>(intent_generator1.begin(), intent_generator1.end());
52   model.intent_options->generator.emplace_back(
53       new IntentFactoryModel_::IntentGeneratorT);
54   const std::string intent_generator2 = "lua generator 2";
55   model.intent_options->generator.back()->lua_template_generator =
56       std::vector<uint8_t>(intent_generator2.begin(), intent_generator2.end());
57 
58   // Compress the model.
59   EXPECT_TRUE(CompressModel(&model));
60 
61   // Pack and load the model.
62   flatbuffers::FlatBufferBuilder builder;
63   builder.Finish(Model::Pack(builder, &model));
64   const Model* compressed_model =
65       GetModel(reinterpret_cast<const char*>(builder.GetBufferPointer()));
66   ASSERT_TRUE(compressed_model != nullptr);
67 
68   // Decompress the fields again and check that they match the original.
69   std::unique_ptr<ZlibDecompressor> decompressor = ZlibDecompressor::Instance();
70   ASSERT_TRUE(decompressor != nullptr);
71   std::string uncompressed_pattern;
72   EXPECT_TRUE(decompressor->MaybeDecompress(
73       compressed_model->regex_model()->patterns()->Get(0)->compressed_pattern(),
74       &uncompressed_pattern));
75   EXPECT_EQ(uncompressed_pattern, "this is a test pattern");
76   EXPECT_TRUE(decompressor->MaybeDecompress(
77       compressed_model->regex_model()->patterns()->Get(1)->compressed_pattern(),
78       &uncompressed_pattern));
79   EXPECT_EQ(uncompressed_pattern, "this is a second test pattern");
80   EXPECT_TRUE(decompressor->MaybeDecompress(compressed_model->datetime_model()
81                                                 ->patterns()
82                                                 ->Get(0)
83                                                 ->regexes()
84                                                 ->Get(0)
85                                                 ->compressed_pattern(),
86                                             &uncompressed_pattern));
87   EXPECT_EQ(uncompressed_pattern, "an example datetime pattern");
88   EXPECT_TRUE(decompressor->MaybeDecompress(compressed_model->datetime_model()
89                                                 ->extractors()
90                                                 ->Get(0)
91                                                 ->compressed_pattern(),
92                                             &uncompressed_pattern));
93   EXPECT_EQ(uncompressed_pattern, "an example datetime extractor");
94 
95   EXPECT_TRUE(DecompressModel(&model));
96   EXPECT_EQ(model.regex_model->patterns[0]->pattern, "this is a test pattern");
97   EXPECT_EQ(model.regex_model->patterns[1]->pattern,
98             "this is a second test pattern");
99   EXPECT_EQ(model.datetime_model->patterns[0]->regexes[0]->pattern,
100             "an example datetime pattern");
101   EXPECT_EQ(model.datetime_model->extractors[0]->pattern,
102             "an example datetime extractor");
103   EXPECT_EQ(
104       model.intent_options->generator[0]->lua_template_generator,
105       std::vector<uint8_t>(intent_generator1.begin(), intent_generator1.end()));
106   EXPECT_EQ(
107       model.intent_options->generator[1]->lua_template_generator,
108       std::vector<uint8_t>(intent_generator2.begin(), intent_generator2.end()));
109 }
110 
111 }  // namespace libtextclassifier3
112