Debian Codegraph @

test/testUserphraseSet.cpp

raw symbols SCIP
1 /*
2 * chewing-editor: Chewing userphrase editor
3 * Copyright (C) 2014 Chewing Development Team
4
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "gtest/gtest.h"
21
22 #include "UserphraseSet.h"
23
24 #if (defined _MSC_VER) && (_MSC_VER >= 1600)
25 # pragma execution_character_set("utf-8")
26 #endif
27
28 class TestUserphraseSet : public ::testing::Test {
29 protected:
30 TestUserphraseSet() = default;
31 virtual ~TestUserphraseSet() = default;
32
33 UserphraseSet set_;
34 };
35
36 /*
37 * Use macro here because we want to know line number when it fails. Function
38 * will hide line number.
39 */
40 #define COMPARE_USERPHRASE(it, phrase, bopomofo) do { \
41 EXPECT_EQ(0, QString::compare((phrase), (it)->phrase_)); \
42 EXPECT_EQ(0, QString::compare((bopomofo), (it)->bopomofo_)); \
43 } while (0)
44
45 TEST_F(TestUserphraseSet, InsertDuplicated)
46 {
47 ASSERT_TRUE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
48 ASSERT_FALSE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
49
50 ASSERT_EQ(1, set_.size());
51
52 COMPARE_USERPHRASE(set_.begin(), "測試", "ㄘㄜˋ ㄕˋ");
53 }
54
55 TEST_F(TestUserphraseSet, IndexOperator)
56 {
57 ASSERT_TRUE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
58
59 ASSERT_EQ(1, set_.size());
60
61 EXPECT_EQ(0, QString::compare("測試", set_[0].phrase_));
62 EXPECT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", set_[0].bopomofo_));
63 }
64
65 TEST_F(TestUserphraseSet, Enumerate)
66 {
67 Userphrase data[] = {
68 Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")},
69 Userphrase{QString("一下"), QString("ㄧˊ ㄒㄧㄚˋ")},
70 };
71
72 ASSERT_TRUE(set_.insert(Userphrase{data[0]}));
73 ASSERT_TRUE(set_.insert(Userphrase{data[1]}));
74
75 ASSERT_EQ(2, set_.size());
76
77 int i = 0;
78 for (auto it = set_.begin(); it != set_.end(); ++it, ++i) {
79 COMPARE_USERPHRASE(it, data[i].phrase_, data[i].bopomofo_);
80 }
81 }
82
83 TEST_F(TestUserphraseSet, Erase)
84 {
85 ASSERT_TRUE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
86 ASSERT_TRUE(set_.insert(Userphrase{QString("一下"), QString("ㄧˊ ㄒㄧㄚˋ")}));
87
88 ASSERT_EQ(2, set_.size());
89
90 set_.erase(set_.begin());
91
92 ASSERT_EQ(1, set_.size());
93
94 COMPARE_USERPHRASE(set_.begin(), "一下", "ㄧˊ ㄒㄧㄚˋ");
95 }
96
97 TEST_F(TestUserphraseSet, SamePhraseDifferentBopomofo)
98 {
99 ASSERT_TRUE(set_.insert(Userphrase{QString("什麼"), QString("ㄕㄣˊ ㄇㄜ˙")}));
100 ASSERT_TRUE(set_.insert(Userphrase{QString("什麼"), QString("ㄕㄜˊ ㄇㄜ˙")}));
101
102 ASSERT_EQ(2, set_.size());
103
104 COMPARE_USERPHRASE(set_.begin(), "什麼", "ㄕㄣˊ ㄇㄜ˙");
105 COMPARE_USERPHRASE(set_.begin() + 1, "什麼", "ㄕㄜˊ ㄇㄜ˙");
106
107 set_.erase(set_.begin());
108
109 ASSERT_EQ(1, set_.size());
110
111 COMPARE_USERPHRASE(set_.begin(), "什麼", "ㄕㄜˊ ㄇㄜ˙");
112 }
113
114 TEST_F(TestUserphraseSet, DifferentPhraseSameBopomofo)
115 {
116 ASSERT_TRUE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
117 ASSERT_TRUE(set_.insert(Userphrase{QString("策試"), QString("ㄘㄜˋ ㄕˋ")}));
118
119 ASSERT_EQ(2, set_.size());
120
121 COMPARE_USERPHRASE(set_.begin(), "測試", "ㄘㄜˋ ㄕˋ");
122 COMPARE_USERPHRASE(set_.begin() + 1, "策試", "ㄘㄜˋ ㄕˋ");
123
124 set_.erase(set_.begin() + 1);
125
126 ASSERT_EQ(1, set_.size());
127
128 COMPARE_USERPHRASE(set_.begin(), "測試", "ㄘㄜˋ ㄕˋ");
129 }
130
131 TEST_F(TestUserphraseSet, Swap)
132 {
133 ASSERT_TRUE(set_.insert(Userphrase{QString("測試"), QString("ㄘㄜˋ ㄕˋ")}));
134 ASSERT_EQ(1, set_.size());
135
136 UserphraseSet set;
137 set.swap(set_);
138
139 EXPECT_EQ(0, set_.size());
140 EXPECT_EQ(1, set.size());
141 }