1 /*
2  * Copyright (C) 2014 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 "dictionary/structure/v4/content/terminal_position_lookup_table.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <vector>
22 
23 #include "defines.h"
24 #include "dictionary/structure/v4/ver4_dict_constants.h"
25 
26 namespace latinime {
27 namespace {
28 
TEST(TerminalPositionLookupTableTest,TestGetFromEmptyTable)29 TEST(TerminalPositionLookupTableTest, TestGetFromEmptyTable) {
30     TerminalPositionLookupTable lookupTable;
31 
32     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(0));
33     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(-1));
34     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(
35             Ver4DictConstants::NOT_A_TERMINAL_ID));
36 }
37 
TEST(TerminalPositionLookupTableTest,TestSetAndGet)38 TEST(TerminalPositionLookupTableTest, TestSetAndGet) {
39     TerminalPositionLookupTable lookupTable;
40 
41     EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(10, 100));
42     EXPECT_EQ(100, lookupTable.getTerminalPtNodePosition(10));
43     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(9));
44     EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(9, 200));
45     EXPECT_EQ(200, lookupTable.getTerminalPtNodePosition(9));
46     EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(10, 300));
47     EXPECT_EQ(300, lookupTable.getTerminalPtNodePosition(10));
48     EXPECT_FALSE(lookupTable.setTerminalPtNodePosition(-1, 400));
49     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(-1));
50     EXPECT_FALSE(lookupTable.setTerminalPtNodePosition(Ver4DictConstants::NOT_A_TERMINAL_ID, 500));
51     EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(
52             Ver4DictConstants::NOT_A_TERMINAL_ID));
53 }
54 
TEST(TerminalPositionLookupTableTest,TestGC)55 TEST(TerminalPositionLookupTableTest, TestGC) {
56     TerminalPositionLookupTable lookupTable;
57 
58     const std::vector<int> terminalIds = { 10, 20, 30 };
59     const std::vector<int> terminalPositions = { 100, 200, 300 };
60 
61     for (size_t i = 0; i < terminalIds.size(); ++i) {
62         EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(terminalIds[i], terminalPositions[i]));
63     }
64 
65     TerminalPositionLookupTable::TerminalIdMap terminalIdMap;
66     EXPECT_TRUE(lookupTable.runGCTerminalIds(&terminalIdMap));
67 
68     for (size_t i = 0; i < terminalIds.size(); ++i) {
69         EXPECT_EQ(static_cast<int>(i), terminalIdMap[terminalIds[i]])
70                 << "Terminal id (" << terminalIds[i] << ") should be changed to " << i;
71         EXPECT_EQ(terminalPositions[i], lookupTable.getTerminalPtNodePosition(i));
72     }
73 }
74 
75 }  // namespace
76 }  // namespace latinime
77