Debian Codegraph @

src/SimpleConverterTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2015-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <thread>
20 #include <string_view>
21
22 #include "ConfigTestBase.hpp"
23 #include "SimpleConverter.hpp"
24 #include "TestUtilsUTF8.hpp"
25 #include "opencc.h"
26
27 namespace opencc {
28
29 class SimpleConverterTest : public ConfigTestBase {
30 protected:
31 SimpleConverterTest() {}
32
33 void TestConverter(const std::string& config) const {
34 const SimpleConverter converter(config);
35 const std::string converted = converter.Convert(
36 std::string_view(utf8("燕燕于飞差池其羽之子于归远送于野")));
37 EXPECT_EQ(utf8("燕燕于飛差池其羽之子于歸遠送於野"), converted);
38 }
39 };
40
41 TEST_F(SimpleConverterTest, Convert) { TestConverter(CONFIG_TEST_JSON_PATH); }
42
43 TEST_F(SimpleConverterTest, Multithreading) {
44 const auto& routine = [this](const std::string& config) {
45 TestConverter(config);
46 };
47 std::thread thread1(routine, CONFIG_TEST_JSON_PATH);
48 std::thread thread2(routine, CONFIG_TEST_JSON_PATH);
49 routine(CONFIG_TEST_JSON_PATH);
50 thread1.join();
51 thread2.join();
52 }
53
54 TEST_F(SimpleConverterTest, CInterface) {
55 const std::string& text = utf8("燕燕于飞差池其羽之子于归远送于野");
56 const std::string& expected = utf8("燕燕于飛差池其羽之子于歸遠送於野");
57 {
58 opencc_t od = opencc_open(CONFIG_TEST_JSON_PATH.c_str());
59 char* converted = opencc_convert_utf8(od, text.c_str(), (size_t)-1);
60 EXPECT_EQ(expected, converted);
61 opencc_convert_utf8_free(converted);
62 EXPECT_EQ(0, opencc_close(od));
63 }
64 {
65 char output[1024];
66 opencc_t od = opencc_open(CONFIG_TEST_JSON_PATH.c_str());
67 size_t length =
68 opencc_convert_utf8_to_buffer(od, text.c_str(), (size_t)-1, output);
69 EXPECT_EQ(expected.length(), length);
70 EXPECT_EQ(expected, output);
71 EXPECT_EQ(0, opencc_close(od));
72 }
73 {
74 std::string path = "/opencc/no/such/file/or/directory";
75 opencc_t od = opencc_open(path.c_str());
76 EXPECT_EQ(reinterpret_cast<opencc_t>(-1), od);
77 EXPECT_EQ(path + " not found or not accessible.", opencc_error());
78 }
79 }
80
81 } // namespace opencc