Line data Source code
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 <sal/config.h>
11 : #include <test/bootstrapfixture.hxx>
12 :
13 : #include <vcl/svapp.hxx>
14 : #include <smdll.hxx>
15 :
16 : #include <document.hxx>
17 : #include <node.hxx>
18 : #include <visitors.hxx>
19 : #include <cursor.hxx>
20 :
21 : typedef tools::SvRef<SmDocShell> SmDocShellRef;
22 :
23 : using namespace ::com::sun::star;
24 :
25 : namespace {
26 :
27 51 : class Test : public test::BootstrapFixture {
28 :
29 : public:
30 : // init
31 : virtual void setUp() SAL_OVERRIDE;
32 : virtual void tearDown() SAL_OVERRIDE;
33 :
34 : // tests
35 : void SimpleUnaryOp();
36 : void SimpleBinaryOp();
37 : void SimpleRelationalOp();
38 : void SimpleSetOp();
39 : void SimpleFunctions();
40 : void SimpleOperators();
41 : void SimpleAttributes();
42 : void SimpleMisc();
43 : void SimpleBrackets();
44 : void SimpleFormats();
45 : void SimpleGreekChars();
46 : void SimpleSpecialChars();
47 : void testBinomInBinHor();
48 : void testBinVerInUnary();
49 : void testBinHorInSubSup();
50 : void testUnaryInMixedNumberAsNumerator();
51 : void testMiscEquivalent();
52 :
53 2 : CPPUNIT_TEST_SUITE(Test);
54 1 : CPPUNIT_TEST(SimpleUnaryOp);
55 1 : CPPUNIT_TEST(SimpleBinaryOp);
56 1 : CPPUNIT_TEST(SimpleRelationalOp);
57 1 : CPPUNIT_TEST(SimpleSetOp);
58 1 : CPPUNIT_TEST(SimpleFunctions);
59 1 : CPPUNIT_TEST(SimpleOperators);
60 1 : CPPUNIT_TEST(SimpleAttributes);
61 1 : CPPUNIT_TEST(SimpleMisc);
62 1 : CPPUNIT_TEST(SimpleBrackets);
63 1 : CPPUNIT_TEST(SimpleFormats);
64 1 : CPPUNIT_TEST(SimpleGreekChars);
65 1 : CPPUNIT_TEST(SimpleSpecialChars);
66 1 : CPPUNIT_TEST(testBinomInBinHor);
67 1 : CPPUNIT_TEST(testBinVerInUnary);
68 1 : CPPUNIT_TEST(testBinHorInSubSup);
69 1 : CPPUNIT_TEST(testUnaryInMixedNumberAsNumerator);
70 1 : CPPUNIT_TEST(testMiscEquivalent);
71 2 : CPPUNIT_TEST_SUITE_END();
72 :
73 : private:
74 : uno::Reference< uno::XComponentContext > m_context;
75 : SmDocShellRef xDocShRef;
76 : void parseandparseagain(const char *input, const char *test_name);
77 : void ParseAndCheck(const char *input, const char *expected, const char *test_name);
78 : void ParseAndCompare(const char *formula1, const char *formula2, const char *test_name);
79 : };
80 :
81 17 : void Test::setUp()
82 : {
83 17 : BootstrapFixture::setUp();
84 :
85 17 : SmGlobals::ensure();
86 :
87 17 : xDocShRef = new SmDocShell(SFXOBJECTSHELL_STD_NORMAL);
88 17 : }
89 :
90 17 : void Test::tearDown()
91 : {
92 17 : xDocShRef.Clear();
93 17 : BootstrapFixture::tearDown();
94 17 : }
95 :
96 : /*
97 : * Most of the formula commands in this file came from:
98 : * http://wiki.openoffice.org/wiki/Template:Math_commands_reference
99 : * which was licensed with a
100 : * Creative Common Attribution 3.0 license and written by:
101 : * Jeanweber, Weegreenblobbie, Jdpipe, TJFrazier, Ysangkok, B michaelsen, Spellbreaker
102 : */
103 :
104 1 : void Test::SimpleUnaryOp()
105 : {
106 1 : parseandparseagain("+1", "Positive (plus)");
107 1 : parseandparseagain("-2", "Negative (minus)");
108 1 : parseandparseagain("+-3", "Plus/minus");
109 1 : parseandparseagain("-+4", "Minus/plus");
110 1 : parseandparseagain("neg a", "Boolean 'not'");
111 1 : parseandparseagain("fact a", "Factorial");
112 1 : parseandparseagain(" - { 1 over 2 } ", "BinVer in Unary 1");
113 1 : ParseAndCheck(" - { 1 over 2 } ", " - { 1 over 2 } ", "BinVer in Unary 1");
114 1 : parseandparseagain(" { - { 1 over 2 } } ", "BinVer in Unary 2");
115 1 : parseandparseagain(" - 1 over 2 ", "Unary in BinVer as numerator 1");
116 1 : parseandparseagain(" { - 1 } over 2 ", "Unary in BinVer as numerator 2");
117 1 : parseandparseagain(" 1 over - 2 ", "Unary in BinVer as denominator 1");
118 1 : parseandparseagain(" 1 over { - 2 } ", "Unary in BinVer as denominator 2");
119 1 : parseandparseagain(" 2 { - 1 over 2 } ", "Mixed number with Unary in denominator 1");
120 1 : parseandparseagain(" 2 { - 1 } over 2 ", "Mixed number with Unary in denominator 2");
121 1 : parseandparseagain(" - 1 + 2 ", "Unary in BinHor");
122 1 : }
123 :
124 1 : void Test::SimpleBinaryOp()
125 : {
126 1 : parseandparseagain("a + b", "Addition");
127 1 : parseandparseagain("a cdot b", "Dot product");
128 1 : parseandparseagain("a times b", "Cross product");
129 1 : parseandparseagain("a * b", "Multiplication (asterisk)");
130 1 : parseandparseagain("a and b", "Boolean 'and'");
131 1 : parseandparseagain("a - b", "Subtraction");
132 1 : parseandparseagain("a over b", "Division (as a fraction)");
133 1 : parseandparseagain("a div b", "Division (as an operator)");
134 1 : parseandparseagain("a / b", "Division (with a slash)");
135 1 : parseandparseagain("a or b", "Boolean 'or'");
136 1 : parseandparseagain("a circ b", "Concatenation");
137 1 : }
138 :
139 1 : void Test::SimpleRelationalOp()
140 : {
141 1 : parseandparseagain("a = b", "Is equal");
142 1 : parseandparseagain("a <> b", "Is not equal");
143 1 : parseandparseagain("a approx 2", "Approximately");
144 1 : parseandparseagain("a divides b", "Divides");
145 1 : parseandparseagain("a ndivides b", "Does not divide");
146 1 : parseandparseagain("a < 2", "Less than");
147 1 : parseandparseagain("a > 2", "Greater than");
148 1 : parseandparseagain("a simeq b", "Similar to or equal");
149 1 : parseandparseagain("a parallel b", "Parallel");
150 1 : parseandparseagain("a ortho b", "Orthogonal to");
151 1 : parseandparseagain("a leslant b", "Less than or equal to");
152 1 : parseandparseagain("a geslant b", "Greater than or equal to");
153 1 : parseandparseagain("a sim b", "Similar to");
154 1 : parseandparseagain("a equiv b", "Congruent");
155 1 : parseandparseagain("a <= b", "Less than or equal to");
156 1 : parseandparseagain("a >= b", "Greater than or equal to");
157 1 : parseandparseagain("a prop b", "Proportional");
158 1 : parseandparseagain("a toward b", "Toward");
159 1 : parseandparseagain("a dlarrow b", "Arrow left");
160 1 : parseandparseagain("a dlrarrow b", "Double arrow left and right");
161 1 : parseandparseagain("drarrow b", "Arrow right");
162 1 : }
163 :
164 1 : void Test::SimpleSetOp()
165 : {
166 1 : parseandparseagain("a in B", "Is in");
167 1 : parseandparseagain("a notin B", "Is not in");
168 1 : parseandparseagain("A owns b", "Owns");
169 1 : parseandparseagain("emptyset", "Empty set");
170 1 : parseandparseagain("A intersection B", "Intersection");
171 1 : parseandparseagain("A union B", "Union");
172 1 : parseandparseagain("A setminus B", "Difference");
173 1 : parseandparseagain("A slash B", "Quotient");
174 1 : parseandparseagain("aleph", "Aleph");
175 1 : parseandparseagain("A subset B", "Subset");
176 1 : parseandparseagain("A subseteq B", "Subset or equal to");
177 1 : parseandparseagain("A supset B", "Superset");
178 1 : parseandparseagain("A supseteq B", "Superset or equal to");
179 1 : parseandparseagain("A nsubset B", "Not subset");
180 1 : parseandparseagain("A nsubseteq B", "Not subset or equal");
181 1 : parseandparseagain("A nsupset B", "Not superset");
182 1 : parseandparseagain("A nsupseteq B", "Not superset or equal");
183 1 : parseandparseagain("setN", "Set of natural numbers");
184 1 : parseandparseagain("setZ", "Set of integers");
185 1 : parseandparseagain("setQ", "Set of rational numbers");
186 1 : parseandparseagain("setR", "Set of real numbers");
187 1 : parseandparseagain("setC", "Set of complex numbers");
188 1 : }
189 :
190 1 : void Test::SimpleFunctions()
191 : {
192 1 : parseandparseagain("func e^{a}", "Exponential");
193 1 : parseandparseagain("ln(a)", "Natural logarithm");
194 1 : parseandparseagain("exp(a)", "Exponential function");
195 1 : parseandparseagain("log(a)", "Logarithm");
196 1 : parseandparseagain("a^{b}", "Power");
197 1 : parseandparseagain("sin(a)", "Sine");
198 1 : parseandparseagain("cos(a)", "Cosine");
199 1 : parseandparseagain("tan(a)", "Tangent");
200 1 : parseandparseagain("cot(a)", "Cotangent");
201 1 : parseandparseagain("sqrt{a}", "Square root");
202 1 : parseandparseagain("arcsin(a)", "Arcsine");
203 1 : parseandparseagain("arccos(a)", "Arccosine");
204 1 : parseandparseagain("arctan(a)", "Arctangent");
205 1 : parseandparseagain("arccot(a)", "Arc cotangent");
206 1 : parseandparseagain("nroot{a}{b}", "nth root");
207 1 : parseandparseagain("sinh(a)", "Hyperbolic sine");
208 1 : parseandparseagain("cosh(a)", "Hyperbolic cosine");
209 1 : parseandparseagain("tanh(a)", "Hyperbolic tangent");
210 1 : parseandparseagain("coth(a)", "Hyperbolic cotangent");
211 1 : parseandparseagain("abs{a}", "Absolute value");
212 1 : parseandparseagain("arsinh(a)", "Arc hyperbolic sine");
213 1 : parseandparseagain("arcosh(a)", "Arc hyperbolic cosine");
214 1 : parseandparseagain("artanh(a)", "Arc hyperbolic tangent");
215 1 : parseandparseagain("arcoth(a)", "Arc hyperbolic cotangent");
216 1 : }
217 :
218 1 : void Test::SimpleOperators()
219 : {
220 1 : parseandparseagain("lim{a}", "Limit");
221 1 : parseandparseagain("sum{a}", "Sum");
222 1 : parseandparseagain("prod{a}", "Product");
223 1 : parseandparseagain("coprod{a}", "Coproduct");
224 : //FIXME parseandparseagain("int from {r_0} to {r_t} a", "Upper and lower bounds shown with integral (from & to)");
225 : //FIXME ParseAndCheck("int csup {r_0} csub {r_t} a", "int csup { r rsub 0 } csub { r rsub t } a ", "Upper and lower bounds shown with integral (csub & csup)");
226 : //FIXME ParseAndCheck("sum csup { size 8 { x - 1 } } csub { size 8 a } b ", "sum csup { size 8 { x - 1 } } csub { size 8 a } b ", "Sum with sized upper and lower bounds");
227 1 : parseandparseagain("int{a}", "Integral");
228 1 : parseandparseagain("iint{a}", "Double integral");
229 1 : parseandparseagain("iiint{a}", "Triple integral");
230 1 : parseandparseagain("sum from{3}b", "Lower bound shown with summation symbol");
231 1 : parseandparseagain("lint a", "Contour integral");
232 1 : parseandparseagain("llint a", "Double curved integral");
233 1 : parseandparseagain("lllint a", "Triple curved integral");
234 1 : parseandparseagain("prod from {i=1} to {n} {(i+1)}", "Product with range");
235 1 : }
236 :
237 1 : void Test::SimpleAttributes()
238 : {
239 1 : parseandparseagain("acute a", "Acute accent");
240 1 : parseandparseagain("grave a", "Grave accent");
241 1 : parseandparseagain("check a", "Reverse circumflex");
242 1 : parseandparseagain("breve a", "Breve");
243 1 : parseandparseagain("circle a", "Circle");
244 1 : parseandparseagain("vec a", "Vector arrow");
245 1 : parseandparseagain("tilde a", "Tilde");
246 1 : parseandparseagain("hat a", "Circumflex");
247 1 : parseandparseagain("bar a", "Line above");
248 1 : parseandparseagain("dot a", "Dot");
249 1 : parseandparseagain("widevec abc", "Wide vector arrow");
250 1 : parseandparseagain("widetilde abc", "Wide tilde");
251 1 : parseandparseagain("widehat abc", "Wide circumflex");
252 1 : parseandparseagain("ddot a", "Double dot");
253 1 : parseandparseagain("overline abc", "Line over");
254 1 : parseandparseagain("underline abc", "Line under");
255 1 : parseandparseagain("overstrike abc", "Line through");
256 1 : parseandparseagain("dddot a", "Triple dot");
257 1 : parseandparseagain("phantom a", "Transparent (useful to get a placeholder of a given size)");
258 1 : parseandparseagain("bold a", "Bold font");
259 1 : parseandparseagain("ital a", "Italic font");
260 1 : parseandparseagain("nitalic a", "Roman (non-italic) font 1");
261 1 : parseandparseagain("\"a\"", "Roman (non-italic) font 2");
262 1 : parseandparseagain("size 16 qv", "Resize font");
263 1 : parseandparseagain("font sans qv", "Sans serif font");
264 1 : parseandparseagain("font serif qv", "Serif font");
265 1 : parseandparseagain("font fixed qv", "Fixed font");
266 1 : parseandparseagain("color cyan qv", "Cyan color");
267 1 : parseandparseagain("color yellow qv", "Yellow color");
268 1 : parseandparseagain("color white qv", "White color");
269 1 : parseandparseagain("color green qv", "Green color");
270 1 : parseandparseagain("color blue qv", "Blue color");
271 1 : parseandparseagain("color red qv", "Red color");
272 1 : parseandparseagain("color green X qv", "Green color changes back");
273 1 : parseandparseagain("color green {X qv}", "Green color, more than one item");
274 1 : }
275 :
276 1 : void Test::SimpleMisc()
277 : {
278 1 : parseandparseagain("infinity", "Infinity");
279 1 : parseandparseagain("partial", "Partial");
280 1 : parseandparseagain("nabla", "Nabla");
281 1 : parseandparseagain("exists", "There exists");
282 1 : parseandparseagain("notexists", "There not exists");
283 1 : parseandparseagain("forall", "For all");
284 1 : parseandparseagain("hbar", "H bar");
285 1 : parseandparseagain("lambdabar", "Lambda bar");
286 1 : parseandparseagain("re", "Real part");
287 1 : parseandparseagain("im", "Imaginary part");
288 1 : parseandparseagain("wp", "Weierstrass p");
289 1 : parseandparseagain("leftarrow", "Left arrow");
290 1 : parseandparseagain("rightarrow", "Right arrow");
291 1 : parseandparseagain("uparrow", "Up arrow");
292 1 : parseandparseagain("downarrow", "Down arrow");
293 1 : parseandparseagain("dotslow", "Dots at bottom");
294 1 : parseandparseagain("dotsaxis", "Dots at middle");
295 1 : parseandparseagain("dotsvert", "Dots vertical");
296 1 : parseandparseagain("dotsup", "Dots diagonal upward");
297 1 : parseandparseagain("dotsdown", "Dots diagonal downward");
298 1 : }
299 :
300 1 : void Test::SimpleBrackets()
301 : {
302 1 : parseandparseagain("(a)", "Round Brackets");
303 1 : parseandparseagain("[b]", "Square Brackets");
304 1 : parseandparseagain("ldbracket c rdbracket", "Double Square Brackets");
305 1 : parseandparseagain("lline a rline", "Single line or absolute");
306 1 : parseandparseagain("abs a", "Single line or absolute 2");
307 1 : parseandparseagain("ldline a rdline", "Double line");
308 1 : parseandparseagain("lbrace w rbrace", "Braces");
309 1 : parseandparseagain("left lbrace stack{0, n <> 0 # 1, n = 1} right none", "Single left brace");
310 1 : parseandparseagain("langle d rangle", "Angle Brackets");
311 1 : parseandparseagain("langle a mline b rangle", "Operator Brackets");
312 1 : parseandparseagain("{a}", "Group brackets (used for program control)");
313 1 : parseandparseagain("left ( stack{a # b # z} right )", "Round brackets scalable");
314 1 : parseandparseagain("left [ stack{x # y} right ]", "Square brackets scalable");
315 1 : parseandparseagain("left ldbracket c right rdbracket", "Double square brackets scalable");
316 1 : parseandparseagain("left lline a right rline", "Line scalable");
317 1 : parseandparseagain("left ldline d right rdline", "Double line scalable");
318 1 : parseandparseagain("left lbrace e right rbrace", "Brace scalable");
319 1 : parseandparseagain("left langle f right rangle", "Angle bracket scalable");
320 1 : parseandparseagain("left langle g mline h right rangle", "Operator brackets scalable");
321 1 : parseandparseagain("{a} overbrace b", "Over brace scalable");
322 1 : parseandparseagain("{b} underbrace a", "Under brace scalable");
323 1 : }
324 :
325 1 : void Test::SimpleFormats()
326 : {
327 1 : parseandparseagain("a lsup{b}", "Left superscript");
328 1 : parseandparseagain("a csup{b}", "Center superscript");
329 1 : parseandparseagain("a^{b}", "Right superscript");
330 1 : parseandparseagain("a lsub{b}", "Left subscript");
331 1 : parseandparseagain("a csub{b}", "Center subscript");
332 1 : parseandparseagain("a_{b}", "Right subscript");
333 1 : parseandparseagain("stack { Hello world # alignl (a) }", "Align character to left");
334 1 : parseandparseagain("stack{Hello world # alignc(a)}", "Align character to center");
335 1 : parseandparseagain("stack { Hello world # alignr(a)}", "Align character to right");
336 1 : parseandparseagain("binom{a}{b}", "Vertical stack of 2");
337 1 : parseandparseagain("stack{a # b # z}", "Vertical stack, more than 2");
338 1 : parseandparseagain("matrix{a # b ## c # d}", "Matrix");
339 1 : parseandparseagain("matrix{a # \"=\" # alignl{b} ## {} # \"=\" # alignl{c+1}}", "Equations aligned at '=' (using 'matrix') ");
340 1 : parseandparseagain("stack{alignl{a} = b # alignl{phantom{a} = c+1}}", "Equations aligned at '=' (using 'phantom') ");
341 1 : parseandparseagain("asldkfjo newline sadkfj", "New line");
342 1 : parseandparseagain("stuff `stuff", "Small gap (grave)");
343 1 : parseandparseagain("stuff~stuff", "Large gap (tilde)");
344 1 : }
345 :
346 1 : void Test::SimpleGreekChars()
347 : {
348 1 : parseandparseagain("%ALPHA", "Capital alpha");
349 1 : parseandparseagain("%BETA", "Capital beta");
350 1 : parseandparseagain("%CHI", "Capital chi");
351 1 : parseandparseagain("%DELTA", "Capital delta");
352 1 : parseandparseagain("%EPSILON", "Capital epsilon");
353 1 : parseandparseagain("%ETA", "Capital eta");
354 1 : parseandparseagain("%GAMMA", "Capital gamma");
355 1 : parseandparseagain("%IOTA", "Capital iota");
356 1 : parseandparseagain("%LAMBDA", "Capital lambda");
357 1 : parseandparseagain("%MU", "Capital mu");
358 1 : parseandparseagain("%NU", "Capital nu");
359 1 : parseandparseagain("%OMEGA", "Capital omega");
360 1 : parseandparseagain("%OMICRON", "Capital omicron");
361 1 : parseandparseagain("%PHI", "Capital phi");
362 1 : parseandparseagain("%PI", "Capital pi");
363 1 : parseandparseagain("%PSI", "Capital psi");
364 1 : parseandparseagain("%RHO", "Capital rho");
365 1 : parseandparseagain("%SIGMA", "Capital sigma");
366 1 : parseandparseagain("%TAU", "Capital tau");
367 1 : parseandparseagain("%THETA", "Capital theta");
368 1 : parseandparseagain("%UPSILON", "Capital upsilon");
369 1 : parseandparseagain("%XI", "Capital xi");
370 1 : parseandparseagain("%ZETA", "Capital zeta");
371 1 : parseandparseagain("%alpha", "lowercase alpha");
372 1 : parseandparseagain("%beta", "lowercase beta");
373 1 : parseandparseagain("%chi", "lowercase chi");
374 1 : parseandparseagain("%delta", "lowercase delta");
375 1 : parseandparseagain("%epsilon", "lowercase epsilon");
376 1 : parseandparseagain("%eta", "lowercase eta");
377 1 : parseandparseagain("%gamma", "lowercase gamma");
378 1 : parseandparseagain("%iota", "lowercase iota");
379 1 : parseandparseagain("%kappa", "lowercase kappa");
380 1 : parseandparseagain("%lambda", "lowercase lambda");
381 1 : parseandparseagain("%mu", "lowercase mu");
382 1 : parseandparseagain("%nu", "lowercase nu");
383 1 : parseandparseagain("%omega", "lowercase omega");
384 1 : parseandparseagain("%omicron", "lowercase omicron");
385 1 : parseandparseagain("%phi", "lowercase phi");
386 1 : parseandparseagain("%pi", "lowercase pi");
387 1 : parseandparseagain("%psi", "lowercase psi");
388 1 : parseandparseagain("%rho", "lowercase rho");
389 1 : parseandparseagain("%sigma", "lowercase sigma");
390 1 : parseandparseagain("%tau", "lowercase tau");
391 1 : parseandparseagain("%theta", "lowercase theta");
392 1 : parseandparseagain("%upsilon", "lowercase upsilon");
393 1 : parseandparseagain("%varepsilon", "Varepsilon");
394 1 : parseandparseagain("%varphi", "Varphi");
395 1 : parseandparseagain("%varpi", "Varpi");
396 1 : parseandparseagain("%varrho", "Varrho");
397 1 : parseandparseagain("%varsigma", "Varsigma");
398 1 : parseandparseagain("%vartheta", "Vartheta");
399 1 : parseandparseagain("%xi", "lowercase xi");
400 1 : parseandparseagain("%zeta", "lowercase zeta");
401 1 : }
402 :
403 1 : void Test::SimpleSpecialChars()
404 : {
405 1 : parseandparseagain("%and", "And");
406 1 : parseandparseagain("%angle", "Angle");
407 1 : parseandparseagain("%element", "Element");
408 1 : parseandparseagain("%identical", "Identical");
409 1 : parseandparseagain("%infinite", "Infinite");
410 1 : parseandparseagain("%noelement", "No element");
411 1 : parseandparseagain("%notequal", "Not equal");
412 1 : parseandparseagain("%or", "Or");
413 1 : parseandparseagain("%perthousand", "Per thousand");
414 1 : parseandparseagain("%strictlygreaterthan", "Strictly greater than");
415 1 : parseandparseagain("%strictlylessthan", "Strictly less than");
416 1 : parseandparseagain("%tendto", "Tend to");
417 1 : }
418 :
419 : /* This test takes a formula command, parses it, converts the node to text,
420 : * parses it again, converts it to text again, and compares the values.
421 : * Doing this doesn't prove that it is correct, but it should prove that the
422 : * meaning of the original command is not being changed.
423 : */
424 263 : void Test::parseandparseagain(const char *formula, const char *test_name)
425 : {
426 526 : OUString output1, output2;
427 : SmNode *pNode1, *pNode2;
428 :
429 : // parse 1
430 526 : OUString input = OUString::createFromAscii(formula);
431 263 : pNode1 = SmParser().ParseExpression(input);
432 263 : pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef);
433 263 : SmNodeToTextVisitor(pNode1, output1);
434 :
435 : // parse 2
436 263 : pNode2 = SmParser().ParseExpression(output1);
437 263 : pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef);
438 263 : SmNodeToTextVisitor(pNode2, output2);
439 :
440 : // compare
441 526 : CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name,
442 : output1,
443 263 : output2);
444 :
445 263 : delete pNode1;
446 526 : delete pNode2;
447 263 : }
448 :
449 1 : void Test::ParseAndCheck(const char *formula, const char * expected, const char *test_name)
450 : {
451 1 : OUString sOutput;
452 : SmNode *pNode;
453 :
454 : // parse
455 2 : OUString sInput = OUString::createFromAscii(formula);
456 1 : pNode = SmParser().ParseExpression(sInput);
457 1 : pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef);
458 1 : SmNodeToTextVisitor(pNode, sOutput);
459 :
460 : // compare
461 2 : OUString sExpected = OUString::createFromAscii(expected);
462 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name,
463 : sExpected,
464 1 : sOutput);
465 :
466 2 : delete pNode;
467 1 : }
468 :
469 : // Parse two formula commands and verify that they give the same output
470 6 : void Test::ParseAndCompare(const char *formula1, const char *formula2, const char *test_name)
471 : {
472 12 : OUString sOutput1, sOutput2;
473 : SmNode *pNode1, *pNode2;
474 :
475 : // parse formula1
476 12 : OUString sInput1 = OUString::createFromAscii(formula1);
477 6 : pNode1 = SmParser().ParseExpression(sInput1);
478 6 : pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef);
479 6 : SmNodeToTextVisitor(pNode1, sOutput1);
480 :
481 : // parse formula2
482 12 : OUString sInput2 = OUString::createFromAscii(formula2);
483 6 : pNode2 = SmParser().ParseExpression(sInput2);
484 6 : pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef);
485 6 : SmNodeToTextVisitor(pNode2, sOutput2);
486 :
487 6 : CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name, sOutput1, sOutput2);
488 :
489 6 : delete pNode1;
490 12 : delete pNode2;
491 6 : }
492 :
493 1 : void Test::testBinomInBinHor()
494 : {
495 2 : OUString sInput, sExpected;
496 : SmNode* pTree;
497 :
498 : // set up a binom (table) node
499 1 : sInput += "binom a b + c";
500 1 : pTree = SmParser().Parse(sInput);
501 1 : pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
502 :
503 2 : SmCursor aCursor(pTree, xDocShRef);
504 2 : VirtualDevice aOutputDevice;
505 :
506 : // move forward (more than) enough places to be at the end
507 : int i;
508 9 : for (i = 0; i < 8; ++i)
509 8 : aCursor.Move(&aOutputDevice, MoveRight);
510 :
511 : // tack +d on the end, which will put the binom into an SmBinHorNode
512 1 : aCursor.InsertElement(PlusElement);
513 1 : aCursor.InsertText("d");
514 :
515 1 : sExpected += " { { binom a b + c } + d } ";
516 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Binom Node in BinHor Node", sExpected, xDocShRef->GetText());
517 :
518 2 : delete pTree;
519 1 : }
520 :
521 1 : void Test::testBinVerInUnary()
522 : {
523 2 : OUString sInput, sExpected;
524 : SmNode* pTree;
525 :
526 : // set up a unary operator with operand
527 1 : sInput += "- 1";
528 1 : pTree = SmParser().Parse(sInput);
529 1 : pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
530 :
531 2 : SmCursor aCursor(pTree, xDocShRef);
532 2 : VirtualDevice aOutputDevice;
533 :
534 : // move forward (more than) enough places to be at the end
535 : int i;
536 4 : for (i = 0; i < 3; ++i)
537 3 : aCursor.Move(&aOutputDevice, MoveRight);
538 :
539 : // select the operand
540 1 : aCursor.Move(&aOutputDevice, MoveLeft, false);
541 : // set up a fraction
542 1 : aCursor.InsertFraction();
543 1 : aCursor.Move(&aOutputDevice, MoveDown);
544 1 : aCursor.InsertText("2");
545 :
546 1 : sExpected += " - { 1 over 2 } ";
547 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Binary Vertical in Unary Operator", sExpected, xDocShRef->GetText());
548 :
549 2 : delete pTree;
550 1 : }
551 :
552 1 : void Test::testBinHorInSubSup()
553 : {
554 1 : OUString sInput;
555 :
556 : // set up a blank formula
557 1 : SmNode* pTree = SmParser().Parse(sInput);
558 1 : pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
559 :
560 2 : SmCursor aCursor(pTree, xDocShRef);
561 2 : VirtualDevice aOutputDevice;
562 :
563 : // Insert an RSup expression with a BinHor for the exponent
564 1 : aCursor.InsertText("a");
565 1 : aCursor.InsertSubSup(RSUP);
566 1 : aCursor.InsertText("b");
567 1 : aCursor.InsertElement(PlusElement);
568 1 : aCursor.InsertText("c");
569 :
570 : // Move to the end and add d to the expression
571 1 : aCursor.Move(&aOutputDevice, MoveRight);
572 1 : aCursor.InsertElement(PlusElement);
573 1 : aCursor.InsertText("d");
574 :
575 : //FIXME OUString sExpected = " { a rsup { b + c } + d } ";
576 : //FIXME CPPUNIT_ASSERT_EQUAL_MESSAGE("BinHor in SubSup", sExpected, xDocShRef->GetText());
577 :
578 2 : delete pTree;
579 1 : }
580 :
581 1 : void Test::testUnaryInMixedNumberAsNumerator()
582 : {
583 : // set up a unary operator
584 1 : OUString sInput = "- 1";
585 1 : SmNode* pTree = SmParser().Parse(sInput);
586 1 : pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
587 :
588 2 : SmCursor aCursor(pTree, xDocShRef);
589 2 : VirtualDevice aOutputDevice;
590 :
591 : // move forward (more than) enough places to be at the end
592 4 : for (size_t i = 0; i < 3; ++i)
593 3 : aCursor.Move(&aOutputDevice, MoveRight);
594 :
595 : // Select the whole Unary Horizontal Node
596 1 : aCursor.Move(&aOutputDevice, MoveLeft, false);
597 1 : aCursor.Move(&aOutputDevice, MoveLeft, false);
598 :
599 : // Set up a fraction
600 1 : aCursor.InsertFraction();
601 1 : aCursor.Move(&aOutputDevice, MoveDown);
602 1 : aCursor.InsertText("2");
603 :
604 : // Move left and turn this into a mixed number
605 : // (bad form, but this could happen right?)
606 1 : aCursor.Move(&aOutputDevice, MoveLeft);
607 1 : aCursor.Move(&aOutputDevice, MoveLeft);
608 1 : aCursor.InsertText("2");
609 :
610 : // move forward (more than) enough places to be at the end
611 9 : for (size_t i = 0; i < 8; ++i)
612 8 : aCursor.Move(&aOutputDevice, MoveRight);
613 :
614 : // add 4 to the end
615 1 : aCursor.InsertElement(PlusElement);
616 1 : aCursor.InsertText("4");
617 :
618 2 : OUString sExpected = " { 2 { - 1 over 2 } + 4 } ";
619 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Unary in mixed number as Numerator", sExpected, xDocShRef->GetText());
620 :
621 2 : delete pTree;
622 1 : }
623 :
624 1 : void Test::testMiscEquivalent()
625 : {
626 : // fdo#55853
627 1 : ParseAndCompare("2x", "2 x", "Number times variable");
628 1 : ParseAndCompare("3x^2", "3 x^2", "Number times power");
629 :
630 : // i#11752 and fdo#55853
631 1 : ParseAndCompare("x_2n", "x_{2 n}", "Number times variable in subscript");
632 1 : ParseAndCompare("x^2n", "x^{2 n}", "Number times variable in supscript");
633 :
634 : // fdo#66081
635 1 : ParseAndCompare("{x}", "x", "Variable in brace");
636 1 : ParseAndCompare("{{x+{{y}}}}", "x+y", "Nested braces");
637 1 : }
638 :
639 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
640 :
641 3 : }
642 :
643 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|