• 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 "actions/zlib-utils.h"
18 
19 #include <memory>
20 
21 #include "actions/actions_model_generated.h"
22 #include "utils/zlib/zlib.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 
26 namespace libtextclassifier3 {
27 namespace {
28 
29 using testing::ElementsAre;
30 using testing::Field;
31 using testing::Pointee;
32 
TEST(ActionsZlibUtilsTest,CompressModel)33 TEST(ActionsZlibUtilsTest, CompressModel) {
34   ActionsModelT model;
35   constexpr char kTestPattern1[] = "this is a test pattern";
36   constexpr char kTestPattern2[] = "this is a second test pattern";
37   constexpr char kTestOutputPattern[] = "this is an output pattern";
38   model.rules.reset(new RulesModelT);
39   model.rules->regex_rule.emplace_back(new RulesModel_::RegexRuleT);
40   model.rules->regex_rule.back()->pattern = kTestPattern1;
41   model.rules->regex_rule.emplace_back(new RulesModel_::RegexRuleT);
42   model.rules->regex_rule.back()->pattern = kTestPattern2;
43   model.rules->regex_rule.back()->output_pattern = kTestOutputPattern;
44 
45   // Compress the model.
46   EXPECT_TRUE(CompressActionsModel(&model));
47 
48   // Sanity check that uncompressed field is removed.
49   const auto is_empty_pattern =
50       Pointee(Field(&libtextclassifier3::RulesModel_::RegexRuleT::pattern,
51                     testing::IsEmpty()));
52   EXPECT_THAT(model.rules->regex_rule,
53               ElementsAre(is_empty_pattern, is_empty_pattern));
54   // Pack and load the model.
55   flatbuffers::FlatBufferBuilder builder;
56   FinishActionsModelBuffer(builder, ActionsModel::Pack(builder, &model));
57   const ActionsModel* compressed_model = GetActionsModel(
58       reinterpret_cast<const char*>(builder.GetBufferPointer()));
59   ASSERT_TRUE(compressed_model != nullptr);
60 
61   // Decompress the fields again and check that they match the original.
62   std::unique_ptr<ZlibDecompressor> decompressor = ZlibDecompressor::Instance();
63   ASSERT_TRUE(decompressor != nullptr);
64   std::string uncompressed_pattern;
65   EXPECT_TRUE(decompressor->MaybeDecompress(
66       compressed_model->rules()->regex_rule()->Get(0)->compressed_pattern(),
67       &uncompressed_pattern));
68   EXPECT_EQ(uncompressed_pattern, kTestPattern1);
69   EXPECT_TRUE(decompressor->MaybeDecompress(
70       compressed_model->rules()->regex_rule()->Get(1)->compressed_pattern(),
71       &uncompressed_pattern));
72   EXPECT_EQ(uncompressed_pattern, kTestPattern2);
73   EXPECT_TRUE(DecompressActionsModel(&model));
74   EXPECT_EQ(model.rules->regex_rule[0]->pattern, kTestPattern1);
75   EXPECT_EQ(model.rules->regex_rule[1]->pattern, kTestPattern2);
76   EXPECT_EQ(model.rules->regex_rule[1]->output_pattern, kTestOutputPattern);
77 }
78 
79 }  // namespace
80 }  // namespace libtextclassifier3
81