1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
//
// Copyleft RIME Developers
// License: GPLv3
//
// 2011-05-17 Zou xu <zouivex@gmail.com>
//
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <rime/dict/prism.h>
using namespace rime;
class RimePrismTest : public ::testing::Test {
protected:
RimePrismTest() {}
virtual void SetUp() {
prism_.reset(new Prism("prism_test.bin"));
prism_->Remove();
std::set<std::string> keyset;
keyset.insert("google"); // 4
keyset.insert("good"); // 2
keyset.insert("goodbye"); // 3
keyset.insert("microsoft");
keyset.insert("macrosoft");
keyset.insert("adobe"); // 0 == id
keyset.insert("yahoo");
keyset.insert("baidu"); // 1
prism_->Build(keyset);
}
virtual void TearDown() {
}
scoped_ptr<Prism> prism_;
};
TEST_F(RimePrismTest, SaveAndLoad) {
EXPECT_TRUE(prism_->Save());
Prism test(prism_->file_name());
EXPECT_TRUE(test.Load());
EXPECT_EQ(prism_->array_size(), test.array_size());
}
TEST_F(RimePrismTest, HasKey) {
EXPECT_TRUE(prism_->HasKey("google"));
EXPECT_FALSE(prism_->HasKey("googlesoft"));
EXPECT_TRUE(prism_->HasKey("microsoft"));
EXPECT_FALSE(prism_->HasKey("peoplesoft"));
}
TEST_F(RimePrismTest, GetValue) {
int value = -1;
EXPECT_TRUE(prism_->GetValue("adobe", &value));
EXPECT_EQ(value, 0);
value = -1;
EXPECT_TRUE(prism_->GetValue("baidu", &value));
EXPECT_EQ(value, 1);
}
TEST_F(RimePrismTest, CommonPrefixMatch) {
std::vector<Prism::Match> result;
prism_->CommonPrefixSearch("goodbye", &result);
//result is good and goodbye.
ASSERT_EQ(result.size(), 2);
EXPECT_EQ(result[0].value, 2); // good
EXPECT_EQ(result[0].length, 4); // good
EXPECT_EQ(result[1].value, 3); // goodbye
EXPECT_EQ(result[1].length, 7); // goodbye
}
TEST_F(RimePrismTest, ExpandSearch) {
std::vector<Prism::Match> result;
prism_->ExpandSearch("goo", &result, 10);
//result is good, google and goodbye (ordered by length asc).
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0].value, 2); // good
EXPECT_EQ(result[0].length, 4); // good
EXPECT_EQ(result[1].value, 4); // google
EXPECT_EQ(result[1].length, 6); // google
EXPECT_EQ(result[2].value, 3); // goodbye
EXPECT_EQ(result[2].length, 7); // goodbye
}
|