Home | History | Annotate | Download | only in offdevice_intermediate_dict
      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 "offdevice_intermediate_dict/offdevice_intermediate_dict.h"
     18 
     19 #include <gtest/gtest.h>
     20 
     21 #include <vector>
     22 
     23 #include "suggest/core/dictionary/property/word_property.h"
     24 #include "utils/int_array_view.h"
     25 
     26 namespace latinime {
     27 namespace dicttoolkit {
     28 namespace {
     29 
     30 const std::vector<int> getCodePointVector(const char *str) {
     31     std::vector<int> codePoints;
     32     while (*str) {
     33         codePoints.push_back(*str);
     34         ++str;
     35     }
     36     return codePoints;
     37 }
     38 
     39 const WordProperty getDummpWordProperty(const std::vector<int> &&codePoints) {
     40     return WordProperty(std::move(codePoints), UnigramProperty(), std::vector<NgramProperty>());
     41 }
     42 
     43 TEST(OffdeviceIntermediateDictTest, TestAddWordProperties) {
     44     OffdeviceIntermediateDict dict = OffdeviceIntermediateDict(
     45             OffdeviceIntermediateDictHeader(OffdeviceIntermediateDictHeader::AttributeMap()));
     46     EXPECT_EQ(nullptr, dict.getWordProperty(CodePointArrayView()));
     47 
     48     const WordProperty wordProperty0 = getDummpWordProperty(getCodePointVector("abcd"));
     49     EXPECT_TRUE(dict.addWord(wordProperty0));
     50     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty0.getCodePoints()));
     51 
     52     const WordProperty wordProperty1 = getDummpWordProperty(getCodePointVector("efgh"));
     53     EXPECT_TRUE(dict.addWord(wordProperty1));
     54     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty1.getCodePoints()));
     55 
     56     const WordProperty wordProperty2 = getDummpWordProperty(getCodePointVector("ab"));
     57     EXPECT_TRUE(dict.addWord(wordProperty2));
     58     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty2.getCodePoints()));
     59 
     60     const WordProperty wordProperty3 = getDummpWordProperty(getCodePointVector("abcdefg"));
     61     EXPECT_TRUE(dict.addWord(wordProperty3));
     62     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty3.getCodePoints()));
     63 
     64     const WordProperty wordProperty4 = getDummpWordProperty(getCodePointVector("efef"));
     65     EXPECT_TRUE(dict.addWord(wordProperty4));
     66     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty4.getCodePoints()));
     67 
     68     const WordProperty wordProperty5 = getDummpWordProperty(getCodePointVector("ef"));
     69     EXPECT_TRUE(dict.addWord(wordProperty5));
     70     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty5.getCodePoints()));
     71 
     72     const WordProperty wordProperty6 = getDummpWordProperty(getCodePointVector("abcd"));
     73     EXPECT_FALSE(dict.addWord(wordProperty6)) << "Adding the same word multiple times should fail.";
     74 
     75     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty0.getCodePoints()));
     76     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty1.getCodePoints()));
     77     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty2.getCodePoints()));
     78     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty3.getCodePoints()));
     79     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty4.getCodePoints()));
     80     EXPECT_NE(nullptr, dict.getWordProperty(wordProperty5.getCodePoints()));
     81 }
     82 
     83 } // namespace
     84 } // namespace dicttoolkit
     85 } // namespace latinime
     86