• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2017 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 "trie_builder.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace android {
22 namespace properties {
23 
TEST(propertyinfoserializer,BuildTrie_Simple)24 TEST(propertyinfoserializer, BuildTrie_Simple) {
25   auto trie_builder = TrieBuilder("default", "default_type");
26 
27   // Add test data to tree
28   auto error = std::string();
29   EXPECT_TRUE(trie_builder.AddToTrie("test.", "1st", "1st_type", false, &error));
30   EXPECT_TRUE(trie_builder.AddToTrie("test.test", "2nd", "2nd_type", false, &error));
31   EXPECT_TRUE(trie_builder.AddToTrie("test.test1", "3rd", "3rd_type", true, &error));
32   EXPECT_TRUE(trie_builder.AddToTrie("test.test2", "3rd", "3rd_type", true, &error));
33   EXPECT_TRUE(trie_builder.AddToTrie("test.test3", "3rd", "3rd_type", true, &error));
34   EXPECT_TRUE(trie_builder.AddToTrie("this.is.a.long.string", "4th", "4th_type", true, &error));
35 
36   ASSERT_EQ(5U, trie_builder.contexts().size());
37   ASSERT_EQ(5U, trie_builder.types().size());
38 
39   auto& builder_root = trie_builder.builder_root();
40 
41   // Check the root node
42   EXPECT_EQ("root", builder_root.name());
43   ASSERT_NE(nullptr, builder_root.context());
44   EXPECT_EQ("default", *builder_root.context());
45   ASSERT_NE(nullptr, builder_root.type());
46   EXPECT_EQ("default_type", *builder_root.type());
47 
48   EXPECT_EQ(0U, builder_root.prefixes().size());
49   EXPECT_EQ(0U, builder_root.exact_matches().size());
50 
51   ASSERT_EQ(2U, builder_root.children().size());
52 
53   // Check the 'test.' node
54   auto* test_node = builder_root.FindChild("test");
55   EXPECT_EQ("test", test_node->name());
56   ASSERT_NE(nullptr, test_node->context());
57   EXPECT_EQ("1st", *test_node->context());
58   ASSERT_NE(nullptr, test_node->type());
59   EXPECT_EQ("1st_type", *test_node->type());
60 
61   EXPECT_EQ(0U, test_node->children().size());
62   EXPECT_EQ(1U, test_node->prefixes().size());
63   {
64     auto& property_entry = test_node->prefixes()[0];
65     EXPECT_EQ("test", property_entry.name);
66     ASSERT_NE(nullptr, property_entry.context);
67     EXPECT_EQ("2nd", *property_entry.context);
68     ASSERT_NE(nullptr, property_entry.type);
69     EXPECT_EQ("2nd_type", *property_entry.type);
70   }
71   EXPECT_EQ(3U, test_node->exact_matches().size());
72   EXPECT_EQ("test1", test_node->exact_matches()[0].name);
73   EXPECT_EQ("test2", test_node->exact_matches()[1].name);
74   EXPECT_EQ("test3", test_node->exact_matches()[2].name);
75 
76   ASSERT_NE(nullptr, test_node->exact_matches()[0].context);
77   ASSERT_NE(nullptr, test_node->exact_matches()[1].context);
78   ASSERT_NE(nullptr, test_node->exact_matches()[2].context);
79   EXPECT_EQ("3rd", *test_node->exact_matches()[0].context);
80   EXPECT_EQ("3rd", *test_node->exact_matches()[1].context);
81   EXPECT_EQ("3rd", *test_node->exact_matches()[2].context);
82 
83   ASSERT_NE(nullptr, test_node->exact_matches()[0].type);
84   ASSERT_NE(nullptr, test_node->exact_matches()[1].type);
85   ASSERT_NE(nullptr, test_node->exact_matches()[2].type);
86   EXPECT_EQ("3rd_type", *test_node->exact_matches()[0].type);
87   EXPECT_EQ("3rd_type", *test_node->exact_matches()[1].type);
88   EXPECT_EQ("3rd_type", *test_node->exact_matches()[2].type);
89 
90   // Check the long string node
91   auto expect_empty_one_child = [](auto* node) {
92     ASSERT_NE(nullptr, node);
93     EXPECT_EQ(nullptr, node->context());
94     EXPECT_EQ(nullptr, node->type());
95     EXPECT_EQ(0U, node->prefixes().size());
96     EXPECT_EQ(0U, node->exact_matches().size());
97     EXPECT_EQ(1U, node->children().size());
98   };
99 
100   // Start with 'this'
101   auto* long_string_node = builder_root.FindChild("this");
102   expect_empty_one_child(long_string_node);
103 
104   // Move to 'is'
105   long_string_node = long_string_node->FindChild("is");
106   expect_empty_one_child(long_string_node);
107 
108   // Move to 'a'
109   long_string_node = long_string_node->FindChild("a");
110   expect_empty_one_child(long_string_node);
111 
112   // Move to 'long'
113   long_string_node = long_string_node->FindChild("long");
114   EXPECT_EQ(0U, long_string_node->prefixes().size());
115   EXPECT_EQ(1U, long_string_node->exact_matches().size());
116   EXPECT_EQ(0U, long_string_node->children().size());
117 
118   {
119     auto& property_entry = long_string_node->exact_matches()[0];
120     EXPECT_EQ("string", property_entry.name);
121     ASSERT_NE(nullptr, property_entry.context);
122     EXPECT_EQ("4th", *property_entry.context);
123     ASSERT_NE(nullptr, property_entry.type);
124     EXPECT_EQ("4th_type", *property_entry.type);
125   }
126 }
127 
128 }  // namespace properties
129 }  // namespace android
130