Debian Codegraph @

sw/qa/writerfilter/misc/misc.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <tuple>
11 #include <vector>
12
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
17
18 #include <sal/types.h>
19
20 #include <rtl/ustring.hxx>
21 #include <dmapper/ConversionHelper.hxx>
22 #include <dmapper/DomainMapperFactory.hxx>
23
24 namespace
25 {
26 class WriterfilterMiscTest : public ::CppUnit::TestFixture
27 {
28 public:
29 void testTwipConversions();
30 void testFieldParameters();
31
32 CPPUNIT_TEST_SUITE(WriterfilterMiscTest);
33 CPPUNIT_TEST(testTwipConversions);
34 CPPUNIT_TEST(testFieldParameters);
35 CPPUNIT_TEST_SUITE_END();
36 };
37
38 void WriterfilterMiscTest::testTwipConversions()
39 {
40 using writerfilter::dmapper::ConversionHelper::convertTwipToMm100_Limited;
41 using writerfilter::dmapper::ConversionHelper::convertTwipToMm100_LimitedUnsigned;
42
43 CPPUNIT_ASSERT_EQUAL(sal_Int32(-2), convertTwipToMm100_Limited(-1));
44 CPPUNIT_ASSERT_EQUAL(sal_Int32(-17639), convertTwipToMm100_Limited(-10000));
45 CPPUNIT_ASSERT_EQUAL(sal_Int32(-70556), convertTwipToMm100_Limited(-40000));
46 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), convertTwipToMm100_Limited(1));
47 CPPUNIT_ASSERT_EQUAL(sal_Int32(17639), convertTwipToMm100_Limited(10000));
48 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), convertTwipToMm100_Limited(40000));
49
50 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), convertTwipToMm100_LimitedUnsigned(-1));
51 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), convertTwipToMm100_LimitedUnsigned(-10000));
52 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), convertTwipToMm100_LimitedUnsigned(-40000));
53 CPPUNIT_ASSERT_EQUAL(sal_uInt32(2), convertTwipToMm100_LimitedUnsigned(1));
54 CPPUNIT_ASSERT_EQUAL(sal_uInt32(17639), convertTwipToMm100_LimitedUnsigned(10000));
55 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), convertTwipToMm100_LimitedUnsigned(40000));
56 }
57
58 void WriterfilterMiscTest::testFieldParameters()
59 {
60 using writerfilter::dmapper::splitFieldCommand;
61 std::tuple<OUString, std::vector<OUString>, std::vector<OUString>> result;
62
63 result = splitFieldCommand(u"PAGEREF last_page");
64 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
65 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
66 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
67 CPPUNIT_ASSERT(std::get<2>(result).empty());
68
69 result = splitFieldCommand(u" PAGEREF last_page ");
70 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
71 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
72 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
73
74 result = splitFieldCommand(u"pageref last_page");
75 CPPUNIT_ASSERT(std::get<2>(result).empty());
76 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
77 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
78 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
79 CPPUNIT_ASSERT(std::get<2>(result).empty());
80
81 result = splitFieldCommand(u"pageref \"last_page\"");
82 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
83 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
84 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
85 CPPUNIT_ASSERT(std::get<2>(result).empty());
86
87 result = splitFieldCommand(u"\"PAGEREF\" \"last_page\" \"\" ");
88 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
89 CPPUNIT_ASSERT_EQUAL(size_t(2), std::get<1>(result).size());
90 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
91 CPPUNIT_ASSERT_EQUAL(OUString(), std::get<1>(result)[1]);
92 CPPUNIT_ASSERT(std::get<2>(result).empty());
93
94 result = splitFieldCommand(u"\"PAGEREF\"\"last_page\" ");
95 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
96 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
97 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
98 CPPUNIT_ASSERT(std::get<2>(result).empty());
99
100 result = splitFieldCommand(u"PAGEREF\"last_page\" ");
101 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
102 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
103 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
104 CPPUNIT_ASSERT(std::get<2>(result).empty());
105
106 result = splitFieldCommand(u"\"PAGEREF\"last_page \"\"");
107 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
108 CPPUNIT_ASSERT_EQUAL(size_t(2), std::get<1>(result).size());
109 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
110 CPPUNIT_ASSERT_EQUAL(OUString(), std::get<1>(result)[1]);
111 CPPUNIT_ASSERT(std::get<2>(result).empty());
112
113 result = splitFieldCommand(u"\"PAGEREF\"last_page \"\"");
114 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
115 CPPUNIT_ASSERT_EQUAL(size_t(2), std::get<1>(result).size());
116 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
117 CPPUNIT_ASSERT_EQUAL(OUString(), std::get<1>(result)[1]);
118 CPPUNIT_ASSERT(std::get<2>(result).empty());
119
120 result = splitFieldCommand(u"pageref \"last\\\\pa\\\"ge\"");
121 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
122 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
123 CPPUNIT_ASSERT_EQUAL(u"last\\pa\"ge"_ustr, std::get<1>(result)[0]);
124 CPPUNIT_ASSERT(std::get<2>(result).empty());
125
126 result = splitFieldCommand(u"PAGEREF\"last_page\"\\*");
127 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
128 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
129 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
130 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<2>(result).size());
131 CPPUNIT_ASSERT_EQUAL(u"\\*"_ustr, std::get<2>(result)[0]);
132
133 result = splitFieldCommand(u"PAGEREF last_page \\b foobar ");
134 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
135 CPPUNIT_ASSERT_EQUAL(size_t(1), std::get<1>(result).size());
136 CPPUNIT_ASSERT_EQUAL(u"last_page"_ustr, std::get<1>(result)[0]);
137 CPPUNIT_ASSERT_EQUAL(size_t(2), std::get<2>(result).size());
138 CPPUNIT_ASSERT_EQUAL(u"\\B"_ustr, std::get<2>(result)[0]);
139 CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, std::get<2>(result)[1]);
140
141 result = splitFieldCommand(u"PAGEREF\\bfoobar\\A\"\"");
142 CPPUNIT_ASSERT_EQUAL(u"PAGEREF"_ustr, std::get<0>(result));
143 CPPUNIT_ASSERT(std::get<1>(result).empty());
144 CPPUNIT_ASSERT_EQUAL(size_t(4), std::get<2>(result).size());
145 CPPUNIT_ASSERT_EQUAL(u"\\B"_ustr, std::get<2>(result)[0]);
146 CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, std::get<2>(result)[1]);
147 CPPUNIT_ASSERT_EQUAL(u"\\A"_ustr, std::get<2>(result)[2]);
148 CPPUNIT_ASSERT_EQUAL(OUString(), std::get<2>(result)[3]);
149
150 for (auto prefix : { "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":",
151 ";", "<", ">", "?", "@", "[", "]", "^", "_", "`", "{", "|", "}", "~" })
152 {
153 OUString test(OUString::createFromAscii(prefix) + "PAGE");
154 result = splitFieldCommand(Concat2View(test + " "));
155 CPPUNIT_ASSERT_EQUAL(test, std::get<0>(result));
156 }
157 result = splitFieldCommand(u"\\PAGE ");
158 CPPUNIT_ASSERT_EQUAL(u"PAGE"_ustr, std::get<0>(result));
159 result = splitFieldCommand(u"\\ PAGE ");
160 CPPUNIT_ASSERT_EQUAL(u"\\ "_ustr, std::get<0>(result));
161 CPPUNIT_ASSERT_EQUAL(u"PAGE"_ustr, std::get<1>(result)[0]);
162 result = splitFieldCommand(u"\\\\PAGE ");
163 CPPUNIT_ASSERT_EQUAL(u"\\PAGE"_ustr, std::get<0>(result));
164 result = splitFieldCommand(u"\"PAGE\" ");
165 CPPUNIT_ASSERT_EQUAL(u"PAGE"_ustr, std::get<0>(result));
166 result = splitFieldCommand(u"\"PAGE ");
167 CPPUNIT_ASSERT_EQUAL(u"PAGE "_ustr, std::get<0>(result));
168 }
169
170 CPPUNIT_TEST_SUITE_REGISTRATION(WriterfilterMiscTest);
171 }
172
173 CPPUNIT_PLUGIN_IMPLEMENT();
174
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */