|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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 <gtest/gtest.h> |
| 18 | + |
| 19 | +#include "eiff/LineDictionary.h" |
| 20 | + |
| 21 | +#include <string> |
| 22 | + |
| 23 | +using namespace filamat; |
| 24 | + |
| 25 | +TEST(LineDictionary, splitString) { |
| 26 | + LineDictionary dictionary; |
| 27 | + const std::string text = "first line hp_copy_123456 second line"; |
| 28 | + dictionary.addText(text); |
| 29 | + EXPECT_EQ(dictionary.size(), 3); |
| 30 | + EXPECT_EQ(dictionary[0], "first line "); |
| 31 | + EXPECT_EQ(dictionary[1], "hp_copy_123456"); |
| 32 | + EXPECT_EQ(dictionary[2], " second line"); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(LineDictionary, Empty) { |
| 36 | + LineDictionary const dictionary; |
| 37 | + EXPECT_TRUE(dictionary.empty()); |
| 38 | + EXPECT_EQ(dictionary.size(), 0); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(LineDictionary, AddTextSimple) { |
| 42 | + LineDictionary dictionary; |
| 43 | + dictionary.addText("Hello world\n"); |
| 44 | + EXPECT_FALSE(dictionary.empty()); |
| 45 | + EXPECT_EQ(dictionary.size(), 1); |
| 46 | + EXPECT_EQ(dictionary[0], "Hello world\n"); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(LineDictionary, AddTextMultipleLines) { |
| 50 | + LineDictionary dictionary; |
| 51 | + dictionary.addText("First line\nSecond line\n"); |
| 52 | + EXPECT_EQ(dictionary.size(), 2); |
| 53 | + EXPECT_EQ(dictionary[0], "First line\n"); |
| 54 | + EXPECT_EQ(dictionary[1], "Second line\n"); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(LineDictionary, AddTextDuplicateLines) { |
| 58 | + LineDictionary dictionary; |
| 59 | + dictionary.addText("Same line\nSame line\n"); |
| 60 | + EXPECT_EQ(dictionary.size(), 1); |
| 61 | + EXPECT_EQ(dictionary[0], "Same line\n"); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(LineDictionary, GetIndices) { |
| 65 | + LineDictionary dictionary; |
| 66 | + dictionary.addText("Line one\nLine two\nLine one\n"); |
| 67 | + auto const indicesOne = dictionary.getIndices("Line one\n"); |
| 68 | + ASSERT_EQ(indicesOne.size(), 1); |
| 69 | + EXPECT_EQ(indicesOne[0], 0); |
| 70 | + |
| 71 | + auto const indicesTwo = dictionary.getIndices("Line two\n"); |
| 72 | + ASSERT_EQ(indicesTwo.size(), 1); |
| 73 | + EXPECT_EQ(indicesTwo[0], 1); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(LineDictionary, SplitLogicNoPattern) { |
| 77 | + LineDictionary dictionary; |
| 78 | + dictionary.addText("A simple line with no patterns."); |
| 79 | + EXPECT_EQ(dictionary.size(), 1); |
| 80 | + EXPECT_EQ(dictionary[0], "A simple line with no patterns."); |
| 81 | +} |
| 82 | + |
| 83 | +TEST(LineDictionary, SplitLogicHpPattern) { |
| 84 | + LineDictionary dictionary; |
| 85 | + dictionary.addText("some_var = hp_copy_123;"); |
| 86 | + EXPECT_EQ(dictionary.size(), 3); |
| 87 | + EXPECT_EQ(dictionary[0], "some_var = "); |
| 88 | + EXPECT_EQ(dictionary[1], "hp_copy_123"); |
| 89 | + EXPECT_EQ(dictionary[2], ";"); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(LineDictionary, SplitLogicMpPattern) { |
| 93 | + LineDictionary dictionary; |
| 94 | + dictionary.addText("another_var = mp_copy_4567;"); |
| 95 | + EXPECT_EQ(dictionary.size(), 3); |
| 96 | + EXPECT_EQ(dictionary[0], "another_var = "); |
| 97 | + EXPECT_EQ(dictionary[1], "mp_copy_4567"); |
| 98 | + EXPECT_EQ(dictionary[2], ";"); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(LineDictionary, SplitLogicUnderscorePattern) { |
| 102 | + LineDictionary dictionary; |
| 103 | + dictionary.addText("var_1 = 0;"); |
| 104 | + EXPECT_EQ(dictionary.size(), 1); |
| 105 | + EXPECT_EQ(dictionary[0], "var_1 = 0;"); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(LineDictionary, SplitLogicMultiplePatterns) { |
| 109 | + LineDictionary dictionary; |
| 110 | + dictionary.addText("hp_copy_1 mp_copy_2 _3"); |
| 111 | + EXPECT_EQ(dictionary.size(), 4); |
| 112 | + EXPECT_EQ(dictionary[0], "hp_copy_1"); |
| 113 | + EXPECT_EQ(dictionary[1], " "); |
| 114 | + EXPECT_EQ(dictionary[2], "mp_copy_2"); |
| 115 | + EXPECT_EQ(dictionary[3], "_3"); |
| 116 | +} |
| 117 | + |
| 118 | +TEST(LineDictionary, SplitLogicInvalidPattern) { |
| 119 | + LineDictionary dictionary; |
| 120 | + dictionary.addText("hp_copy_ a_b_c"); |
| 121 | + EXPECT_EQ(dictionary.size(), 1); |
| 122 | + EXPECT_EQ(dictionary[0], "hp_copy_ a_b_c"); |
| 123 | +} |
| 124 | + |
| 125 | +TEST(LineDictionary, SplitLogicPatternFollowedByWordChar) { |
| 126 | + LineDictionary dictionary; |
| 127 | + dictionary.addText("hp_copy_99rest"); |
| 128 | + EXPECT_EQ(dictionary.size(), 1); |
| 129 | + EXPECT_EQ(dictionary[0], "hp_copy_99rest"); |
| 130 | +} |
| 131 | + |
| 132 | +TEST(LineDictionary, SplitLogicPatternPrecededByWordChar) { |
| 133 | + LineDictionary dictionary; |
| 134 | + dictionary.addText("rest_of_it_hp_copy_99"); |
| 135 | + EXPECT_EQ(dictionary.size(), 1); |
| 136 | + EXPECT_EQ(dictionary[0], "rest_of_it_hp_copy_99"); |
| 137 | +} |
| 138 | + |
| 139 | +TEST(LineDictionary, SplitLogicPatternNotFollowedByWordChar) { |
| 140 | + LineDictionary dictionary; |
| 141 | + dictionary.addText("hp_copy_99;"); |
| 142 | + EXPECT_EQ(dictionary.size(), 2); |
| 143 | + EXPECT_EQ(dictionary[0], "hp_copy_99"); |
| 144 | + EXPECT_EQ(dictionary[1], ";"); |
| 145 | +} |
| 146 | + |
| 147 | +TEST(LineDictionary, AddEmptyText) { |
| 148 | + LineDictionary dictionary; |
| 149 | + dictionary.addText(""); |
| 150 | + EXPECT_TRUE(dictionary.empty()); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(LineDictionary, GetIndicesMultiple) { |
| 154 | + LineDictionary dictionary; |
| 155 | + dictionary.addText("A _1 B _2"); |
| 156 | + auto const indices = dictionary.getIndices("A _1"); |
| 157 | + ASSERT_EQ(indices.size(), 2); |
| 158 | + EXPECT_EQ(indices[0], 0); |
| 159 | + EXPECT_EQ(indices[1], 1); |
| 160 | +} |
| 161 | + |
| 162 | +TEST(LineDictionary, GetIndicesMultiplePatternsInARow) { |
| 163 | + LineDictionary dictionary; |
| 164 | + dictionary.addText("hp_copy_1 hp_copy_2"); |
| 165 | + auto const indices = dictionary.getIndices("hp_copy_1 hp_copy_2"); |
| 166 | + ASSERT_EQ(indices.size(), 3); |
| 167 | + EXPECT_EQ(indices[0], 0); |
| 168 | + EXPECT_EQ(indices[1], 1); |
| 169 | + EXPECT_EQ(indices[2], 2); |
| 170 | +} |
| 171 | + |
| 172 | +TEST(LineDictionary, GetIndicesSamePatternMultipleTimes) { |
| 173 | + LineDictionary dictionary; |
| 174 | + dictionary.addText("hp_copy_1 hp_copy_1"); |
| 175 | + auto const indices = dictionary.getIndices("hp_copy_1 hp_copy_1"); |
| 176 | + ASSERT_EQ(indices.size(), 3); |
| 177 | + EXPECT_EQ(indices[0], 0); |
| 178 | + EXPECT_EQ(indices[1], 1); |
| 179 | + EXPECT_EQ(indices[2], 0); |
| 180 | +} |
| 181 | + |
| 182 | +TEST(LineDictionary, GetIndicesWithExistingDictionary) { |
| 183 | + LineDictionary dictionary; |
| 184 | + dictionary.addText("unrelated_string"); |
| 185 | + dictionary.addText("hp_copy_1"); |
| 186 | + dictionary.addText("another_string"); |
| 187 | + dictionary.addText(" "); |
| 188 | + auto const indices = dictionary.getIndices("hp_copy_1 hp_copy_1"); |
| 189 | + ASSERT_EQ(indices.size(), 3); |
| 190 | + EXPECT_EQ(indices[0], 1); |
| 191 | + EXPECT_EQ(indices[1], 3); |
| 192 | + EXPECT_EQ(indices[2], 1); |
| 193 | +} |
| 194 | + |
| 195 | +TEST(LineDictionary, GetIndicesWithAdjacentPatterns) { |
| 196 | + LineDictionary dictionary; |
| 197 | + dictionary.addText("hp_copy_1hp_copy_2"); |
| 198 | + auto const indices = dictionary.getIndices("hp_copy_1hp_copy_2"); |
| 199 | + ASSERT_EQ(indices.size(), 1); |
| 200 | + EXPECT_EQ(indices[0], 0); |
| 201 | +} |
| 202 | + |
| 203 | +TEST(LineDictionary, GetIndicesWithAdjacentPatternsNotInDictionary) { |
| 204 | + LineDictionary dictionary; |
| 205 | + dictionary.addText("hp_copy_1"); |
| 206 | + dictionary.addText("hp_copy_2"); |
| 207 | + auto const indices = dictionary.getIndices("hp_copy_1hp_copy_2"); |
| 208 | + ASSERT_EQ(indices.size(), 0); |
| 209 | +} |
| 210 | + |
| 211 | +TEST(LineDictionary, GetIndicesWithMixedContent) { |
| 212 | + LineDictionary dictionary; |
| 213 | + dictionary.addText("hp_copy_1"); |
| 214 | + dictionary.addText(" "); |
| 215 | + dictionary.addText("mp_copy_2"); |
| 216 | + |
| 217 | + // The query string contains patterns that are in the dictionary, |
| 218 | + // but also content that is not. |
| 219 | + auto const indices = dictionary.getIndices("prefix hp_copy_1 mp_copy_2 suffix"); |
| 220 | + |
| 221 | + // Since not all substrings of the query string are in the dictionary, |
| 222 | + // getIndices should return an empty vector. |
| 223 | + ASSERT_EQ(indices.size(), 0); |
| 224 | +} |
0 commit comments