1 /* Copyright 2020 The TensorFlow Authors. 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 #include "tensorflow_lite_support/custom_ops/kernel/sentencepiece/double_array_trie.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include "tensorflow_lite_support/custom_ops/kernel/sentencepiece/double_array_trie_builder.h"
21 #include "tensorflow_lite_support/custom_ops/kernel/sentencepiece/encoder_config_generated.h"
22 
23 namespace tflite {
24 namespace ops {
25 namespace custom {
26 namespace sentencepiece {
27 
TEST(DoubleArrayTrieTest,Match)28 TEST(DoubleArrayTrieTest, Match) {
29   flatbuffers::FlatBufferBuilder builder(1024);
30   const std::vector<std::string> test_strings = {"A", "AAX", "AA", "B"};
31   const auto trie_vector = builder.CreateVector(BuildTrie(test_strings));
32   TrieBuilder trie_builder(builder);
33   trie_builder.add_nodes(trie_vector);
34   const auto pieces = trie_builder.Finish();
35   EncoderConfigBuilder ecb(builder);
36   ecb.add_pieces(pieces);
37   FinishEncoderConfigBuffer(builder, ecb.Finish());
38   const EncoderConfig* config = GetEncoderConfig(builder.GetBufferPointer());
39   DoubleArrayTrie dat(config->pieces()->nodes());
40   EXPECT_EQ(dat.LongestPrefixMatch(utils::string_view("AAL")),
41             DoubleArrayTrie::Match(2, 2));
42 
43   std::vector<DoubleArrayTrie::Match> matches;
44   dat.IteratePrefixMatches(
45       utils::string_view("AAXL"),
46       [&matches](const DoubleArrayTrie::Match& m) { matches.push_back(m); });
47   EXPECT_THAT(matches, testing::ElementsAre(DoubleArrayTrie::Match(0, 1),
48                                             DoubleArrayTrie::Match(2, 2),
49                                             DoubleArrayTrie::Match(1, 3)));
50 }
51 
TEST(DoubleArrayTrieTest,ComplexMatch)52 TEST(DoubleArrayTrieTest, ComplexMatch) {
53   flatbuffers::FlatBufferBuilder builder(1024);
54   const std::vector<std::string> test_strings = {"\xe2\x96\x81the", ",", "s",
55                                                  "\xe2\x96\x81Hello"};
56   const std::vector<int> test_ids = {0, 5, 10, 15};
57   const auto trie_vector =
58       builder.CreateVector(BuildTrie(test_strings, test_ids));
59   TrieBuilder trie_builder(builder);
60   trie_builder.add_nodes(trie_vector);
61   const auto pieces = trie_builder.Finish();
62   EncoderConfigBuilder ecb(builder);
63   ecb.add_pieces(pieces);
64   FinishEncoderConfigBuffer(builder, ecb.Finish());
65   const EncoderConfig* config = GetEncoderConfig(builder.GetBufferPointer());
66   DoubleArrayTrie dat(config->pieces()->nodes());
67 
68   std::vector<DoubleArrayTrie::Match> matches;
69   dat.IteratePrefixMatches(
70       utils::string_view("\xe2\x96\x81Hello"),
71       [&matches](const DoubleArrayTrie::Match& m) { matches.push_back(m); });
72   EXPECT_THAT(matches, testing::ElementsAre(DoubleArrayTrie::Match(15, 8)));
73 }
74 
75 }  // namespace sentencepiece
76 }  // namespace custom
77 }  // namespace ops
78 }  // namespace tflite
79