File indexing completed on 2024-05-19 04:34:08
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 ***************************************************************************/ 0011 0012 #include "testlabelparser.h" 0013 0014 #include <QtTest> 0015 0016 #include <datacollection.h> 0017 #include <objectstore.h> 0018 0019 static Kst::ObjectStore _store; 0020 0021 void TestLabelParser::cleanupTestCase() { 0022 _store.clear(); 0023 } 0024 0025 0026 void TestLabelParser::dumpAttributes(Label::Chunk *l, QString indent) { 0027 printf("%sb:%d i:%d u:%d c:%s\n", indent.toLatin1().data(), l->attributes.bold, l->attributes.italic, l->attributes.underline, l->attributes.color.name().toLatin1().data()); 0028 } 0029 0030 0031 void TestLabelParser::dumpTree(Label::Chunk *l, QString indent) { 0032 while (l) { 0033 printf("%sNode: %p %s[%s]%s\n", indent.toLatin1().data(), l, l->vOffset == Label::Chunk::None ? "" : (l->vOffset == Label::Chunk::Up ? "U " : "D "), l->text.toLatin1().data(), l->group ? " [G]" : ""); 0034 printf("%sPrev: %p Next: %p Up: %p Down: %p\n", indent.toLatin1().data(), l->prev, l->next, l->up, l->down); 0035 dumpAttributes(l, indent); 0036 if (l->group) { 0037 dumpTree(l->group, indent + " "); 0038 } 0039 if (l->up) { 0040 dumpTree(l->up, indent + " "); 0041 } 0042 if (l->down) { 0043 dumpTree(l->down, indent + " "); 0044 } 0045 l = l->next; 0046 } 0047 } 0048 0049 0050 void TestLabelParser::dumpTree(Label::Parsed *l) { 0051 dumpTree(l->chunk); 0052 } 0053 0054 0055 0056 void TestLabelParser::testLabelParser() { 0057 Label::Parsed *parsed = Label::parse(""); 0058 QVERIFY(parsed != 0L); 0059 QVERIFY(parsed->chunk->prev == 0L); 0060 QVERIFY(parsed->chunk->next == 0L); 0061 QVERIFY(parsed->chunk->text == QString::null); 0062 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0063 delete parsed; 0064 0065 parsed = Label::parse("a"); 0066 QVERIFY(parsed != 0L); 0067 QVERIFY(parsed->chunk->prev == 0L); 0068 QVERIFY(parsed->chunk->next == 0L); 0069 QCOMPARE(parsed->chunk->text, QString("a")); 0070 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0071 delete parsed; 0072 0073 parsed = Label::parse("\\]"); 0074 QVERIFY(parsed != 0L); 0075 QVERIFY(parsed->chunk->prev == 0L); 0076 QVERIFY(parsed->chunk->next == 0L); 0077 QCOMPARE(parsed->chunk->text, QString("]")); 0078 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0079 delete parsed; 0080 0081 parsed = Label::parse("\\\\"); 0082 QVERIFY(parsed != 0L); 0083 QVERIFY(parsed->chunk->prev == 0L); 0084 QVERIFY(parsed->chunk->next == 0L); 0085 QCOMPARE(parsed->chunk->text, QString("\\")); 0086 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0087 delete parsed; 0088 0089 parsed = Label::parse("\\tau"); 0090 QVERIFY(parsed != 0L); 0091 QVERIFY(parsed->chunk->prev == 0L); 0092 QVERIFY(parsed->chunk->next == 0L); 0093 QCOMPARE(parsed->chunk->text, QString(QChar(0x3A4 + 0x20))); 0094 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0095 delete parsed; 0096 0097 parsed = Label::parse("\\t"); 0098 QVERIFY(parsed != 0L); 0099 QCOMPARE(parsed->chunk->text, QString("")); 0100 QCOMPARE(parsed->chunk->tab, true); 0101 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0102 delete parsed; 0103 0104 parsed = Label::parse("\\n"); 0105 QVERIFY(parsed != 0L); 0106 QCOMPARE(parsed->chunk->text, QString("")); 0107 QCOMPARE(parsed->chunk->linebreak, true); 0108 QCOMPARE(parsed->chunk->vOffset, Label::Chunk::None); 0109 delete parsed; 0110 0111 parsed = Label::parse("\\tau\\Theta"); 0112 QVERIFY(parsed != 0L); 0113 QVERIFY(parsed->chunk->next == 0L); 0114 QVERIFY(parsed->chunk->text[0] == QChar(0x3A4 + 0x20)); 0115 QVERIFY(parsed->chunk->text[1] == QChar(0x398)); 0116 QCOMPARE(parsed->chunk->text.length(), 2); 0117 delete parsed; 0118 0119 Label::Chunk *c = 0L; 0120 0121 parsed = Label::parse("\\taufoo bar\\n\\Theta"); 0122 QVERIFY(parsed != 0L); 0123 c = parsed->chunk; 0124 QVERIFY(c->next != 0L); 0125 QCOMPARE(c->text, QString(QChar(0x3A4 + 0x20)) + "foo bar"); 0126 c = c->next; 0127 QVERIFY(c->prev != 0L); 0128 QVERIFY(c->next != 0L); 0129 QCOMPARE(c->text, QString("")); 0130 QCOMPARE(c->linebreak, true); 0131 c = c->next; 0132 QVERIFY(c->prev != 0L); 0133 QVERIFY(c->next == 0L); 0134 QCOMPARE(c->text, QString(QChar(0x398))); 0135 delete parsed; 0136 0137 parsed = Label::parse("x^y^z"); 0138 QVERIFY(parsed == 0L); 0139 0140 parsed = Label::parse("x_y_z"); 0141 QVERIFY(parsed == 0L); 0142 0143 parsed = Label::parse("x^y_z"); 0144 QVERIFY(parsed != 0L); 0145 c = parsed->chunk; 0146 QVERIFY(c->prev == 0L); 0147 QVERIFY(c->next == 0L); 0148 QVERIFY(c->up != 0L); 0149 QVERIFY(c->down != 0L); 0150 QCOMPARE(c->text, QString("x")); 0151 QCOMPARE(c->vOffset, Label::Chunk::None); 0152 c = c->up; 0153 QVERIFY(c->prev != 0L); 0154 QVERIFY(c->next == 0L); 0155 QVERIFY(c->up == 0L); 0156 QVERIFY(c->down == 0L); 0157 QCOMPARE(c->text, QString("y")); 0158 QCOMPARE(c->vOffset, Label::Chunk::Up); 0159 c = c->prev->down; 0160 QVERIFY(c->prev != 0L); 0161 QVERIFY(c->next == 0L); 0162 QVERIFY(c->up == 0L); 0163 QVERIFY(c->down == 0L); 0164 QCOMPARE(c->text, QString("z")); 0165 QCOMPARE(c->vOffset, Label::Chunk::Down); 0166 delete parsed; 0167 0168 parsed = Label::parse("x^{}"); 0169 QVERIFY(parsed != 0L); 0170 c = parsed->chunk; 0171 QVERIFY(c->prev == 0L); 0172 QVERIFY(c->next == 0L); 0173 QVERIFY(c->up != 0L); 0174 QVERIFY(c->down == 0L); 0175 QCOMPARE(c->text, QString("x")); 0176 QVERIFY(c->group == 0L); 0177 QCOMPARE(c->vOffset, Label::Chunk::None); 0178 c = c->up; 0179 QVERIFY(c->prev != 0L); 0180 QVERIFY(c->next == 0L); 0181 QVERIFY(c->up == 0L); 0182 QVERIFY(c->down == 0L); 0183 QVERIFY(c->group != 0L); 0184 QCOMPARE(c->vOffset, Label::Chunk::Up); 0185 c = c->group; 0186 QCOMPARE(c->text, QString("")); 0187 QVERIFY(c->prev != 0L); 0188 QVERIFY(c->next == 0L); 0189 QVERIFY(c->up == 0L); 0190 QVERIFY(c->down == 0L); 0191 QVERIFY(c->group == 0L); 0192 QCOMPARE(c->vOffset, Label::Chunk::None); 0193 delete parsed; 0194 0195 parsed = Label::parse("x^{y+1}_{z-1}"); 0196 QVERIFY(parsed != 0L); 0197 c = parsed->chunk; 0198 QVERIFY(c->prev == 0L); 0199 QVERIFY(c->next == 0L); 0200 QVERIFY(c->up != 0L); 0201 QVERIFY(c->down != 0L); 0202 QCOMPARE(c->text, QString("x")); 0203 QVERIFY(c->group == 0L); 0204 QCOMPARE(c->vOffset, Label::Chunk::None); 0205 c = c->up; 0206 QVERIFY(c->prev != 0L); 0207 QVERIFY(c->next == 0L); 0208 QVERIFY(c->up == 0L); 0209 QVERIFY(c->down == 0L); 0210 QVERIFY(c->group != 0L); 0211 QCOMPARE(c->vOffset, Label::Chunk::Up); 0212 c = c->group; 0213 QCOMPARE(c->text, QString("y+1")); 0214 QVERIFY(c->group == 0L); 0215 QCOMPARE(c->vOffset, Label::Chunk::None); 0216 c = c->prev->prev->down; 0217 QVERIFY(c->prev != 0L); 0218 QVERIFY(c->next == 0L); 0219 QVERIFY(c->up == 0L); 0220 QVERIFY(c->down == 0L); 0221 QVERIFY(c->group != 0L); 0222 QCOMPARE(c->vOffset, Label::Chunk::Down); 0223 c = c->group; 0224 QCOMPARE(c->text, QString("z-1")); 0225 QVERIFY(c->group == 0L); 0226 QCOMPARE(c->vOffset, Label::Chunk::None); 0227 delete parsed; 0228 0229 parsed = Label::parse("x\\^y"); 0230 QVERIFY(parsed != 0L); 0231 c = parsed->chunk; 0232 QVERIFY(c->prev == 0L); 0233 QVERIFY(c->next == 0L); 0234 QVERIFY(c->up == 0L); 0235 QVERIFY(c->down == 0L); 0236 QCOMPARE(c->text, QString("x^y")); 0237 QCOMPARE(c->vOffset, Label::Chunk::None); 0238 delete parsed; 0239 0240 parsed = Label::parse("x^{y^{q+1} + 1}_{z-1}"); 0241 QVERIFY(parsed != 0L); 0242 c = parsed->chunk; 0243 QVERIFY(c->prev == 0L); 0244 QVERIFY(c->next == 0L); 0245 QVERIFY(c->up != 0L); 0246 QVERIFY(c->down != 0L); 0247 QCOMPARE(c->text, QString("x")); 0248 QCOMPARE(c->vOffset, Label::Chunk::None); 0249 c = c->up; 0250 QVERIFY(c->prev != 0L); 0251 QVERIFY(c->next == 0L); 0252 QVERIFY(c->up == 0L); 0253 QVERIFY(c->down == 0L); 0254 QVERIFY(c->group != 0L); 0255 QCOMPARE(c->vOffset, Label::Chunk::Up); 0256 c = c->group; 0257 QCOMPARE(c->text, QString("y")); 0258 QVERIFY(c->prev != 0L); 0259 QVERIFY(c->next != 0L); 0260 QVERIFY(c->up != 0L); 0261 QVERIFY(c->down == 0L); 0262 QVERIFY(c->group == 0L); 0263 QCOMPARE(c->vOffset, Label::Chunk::None); 0264 c = c->up; 0265 QVERIFY(c->prev != 0L); 0266 QVERIFY(c->next == 0L); 0267 QVERIFY(c->up == 0L); 0268 QVERIFY(c->down == 0L); 0269 QVERIFY(c->group != 0L); 0270 QCOMPARE(c->vOffset, Label::Chunk::Up); 0271 c = c->group; 0272 QVERIFY(c->prev != 0L); 0273 QVERIFY(c->next == 0L); 0274 QVERIFY(c->up == 0L); 0275 QVERIFY(c->down == 0L); 0276 QCOMPARE(c->text, QString("q+1")); 0277 QCOMPARE(c->vOffset, Label::Chunk::None); 0278 c = c->prev->prev->next; 0279 QVERIFY(c->prev != 0L); 0280 QVERIFY(c->next == 0L); 0281 QVERIFY(c->up == 0L); 0282 QVERIFY(c->down == 0L); 0283 QCOMPARE(c->text, QString(" + 1")); 0284 QCOMPARE(c->vOffset, Label::Chunk::None); 0285 c = c->prev->prev->prev->down; 0286 QVERIFY(c->prev != 0L); 0287 QVERIFY(c->next == 0L); 0288 QVERIFY(c->up == 0L); 0289 QVERIFY(c->down == 0L); 0290 QVERIFY(c->group); 0291 QCOMPARE(c->vOffset, Label::Chunk::Down); 0292 c = c->group; 0293 QVERIFY(c->prev != 0L); 0294 QVERIFY(c->next == 0L); 0295 QVERIFY(c->up == 0L); 0296 QVERIFY(c->down == 0L); 0297 QCOMPARE(c->text, QString("z-1")); 0298 QCOMPARE(c->vOffset, Label::Chunk::None); 0299 delete parsed; 0300 0301 parsed = Label::parse("{2*2}"); 0302 QVERIFY(parsed != 0L); 0303 c = parsed->chunk; 0304 QVERIFY(c->next == 0L); 0305 QVERIFY(c->group != 0L); 0306 QCOMPARE(c->vOffset, Label::Chunk::None); 0307 c = c->group; 0308 QCOMPARE(c->text, QString("2*2")); 0309 QCOMPARE(c->vOffset, Label::Chunk::None); 0310 delete parsed; 0311 0312 parsed = Label::parse("x^100"); 0313 QVERIFY(parsed != 0L); 0314 c = parsed->chunk; 0315 QVERIFY(c->next != 0L); 0316 QVERIFY(c->up != 0L); 0317 QCOMPARE(c->text, QString("x")); 0318 QCOMPARE(c->vOffset, Label::Chunk::None); 0319 c = c->next; 0320 QVERIFY(c->next == 0L); 0321 QCOMPARE(c->text, QString("00")); 0322 QCOMPARE(c->vOffset, Label::Chunk::None); 0323 c = c->prev->up; 0324 QVERIFY(c->next == 0L); 0325 QCOMPARE(c->text, QString("1")); 0326 QCOMPARE(c->vOffset, Label::Chunk::Up); 0327 delete parsed; 0328 0329 parsed = Label::parse("x^100*200"); 0330 QVERIFY(parsed != 0L); 0331 c = parsed->chunk; 0332 QVERIFY(c->next != 0L); 0333 QVERIFY(c->up != 0L); 0334 QCOMPARE(c->text, QString("x")); 0335 QCOMPARE(c->vOffset, Label::Chunk::None); 0336 c = c->next; 0337 QVERIFY(c->next == 0L); 0338 QCOMPARE(c->text, QString("00*200")); 0339 QCOMPARE(c->vOffset, Label::Chunk::None); 0340 c = c->prev->up; 0341 QVERIFY(c->next == 0L); 0342 QCOMPARE(c->text, QString("1")); 0343 QCOMPARE(c->vOffset, Label::Chunk::Up); 0344 delete parsed; 0345 0346 parsed = Label::parse("[ a ^label ]"); 0347 QVERIFY(parsed != 0L); 0348 c = parsed->chunk; 0349 QVERIFY(c->next == 0L); 0350 QVERIFY(c->up == 0L); 0351 QVERIFY(c->down == 0L); 0352 QCOMPARE(c->text,QString( "a ^label")); 0353 QVERIFY(c->scalar); 0354 delete parsed; 0355 0356 parsed = Label::parse("[vector[2]]"); 0357 QVERIFY(parsed != 0L); 0358 c = parsed->chunk; 0359 QVERIFY(c->next == 0L); 0360 QVERIFY(c->up == 0L); 0361 QVERIFY(c->down == 0L); 0362 QCOMPARE(c->text, QString("vector")); 0363 QCOMPARE(c->expression, QString("2")); 0364 QVERIFY(c->vector); 0365 delete parsed; 0366 0367 parsed = Label::parse("[a][b]"); 0368 QVERIFY(parsed != 0L); 0369 c = parsed->chunk; 0370 QVERIFY(c->next != 0L); 0371 QVERIFY(c->up == 0L); 0372 QVERIFY(c->down == 0L); 0373 QCOMPARE(c->text, QString("a")); 0374 QVERIFY(c->scalar); 0375 c = c->next; 0376 QVERIFY(c->next == 0L); 0377 QVERIFY(c->up == 0L); 0378 QVERIFY(c->down == 0L); 0379 QCOMPARE(c->text, QString("b")); 0380 QVERIFY(c->scalar); 0381 delete parsed; 0382 0383 parsed = Label::parse("[a]*[b]"); 0384 QVERIFY(parsed != 0L); 0385 c = parsed->chunk; 0386 QVERIFY(c->next != 0L); 0387 QVERIFY(c->up == 0L); 0388 QVERIFY(c->down == 0L); 0389 QCOMPARE(c->text, QString("a")); 0390 QVERIFY(c->scalar); 0391 c = c->next; 0392 QVERIFY(c->next != 0L); 0393 QVERIFY(c->up == 0L); 0394 QVERIFY(c->down == 0L); 0395 QCOMPARE(c->text, QString("*")); 0396 QVERIFY(!c->scalar); 0397 c = c->next; 0398 QVERIFY(c->next == 0L); 0399 QVERIFY(c->up == 0L); 0400 QVERIFY(c->down == 0L); 0401 QCOMPARE(c->text, QString("b")); 0402 QVERIFY(c->scalar); 0403 delete parsed; 0404 0405 parsed = Label::parse("[x]^[a]_[b][c]"); 0406 QVERIFY(parsed != 0L); 0407 c = parsed->chunk; 0408 QVERIFY(c->next != 0L); 0409 QVERIFY(c->up != 0L); 0410 QVERIFY(c->down != 0L); 0411 QCOMPARE(c->text, QString("x")); 0412 QVERIFY(c->scalar); 0413 c = c->up; 0414 QVERIFY(c->next == 0L); 0415 QVERIFY(c->up == 0L); 0416 QVERIFY(c->down == 0L); 0417 QCOMPARE(c->text, QString("a")); 0418 QVERIFY(c->scalar); 0419 c = c->prev->down; 0420 QVERIFY(c->next == 0L); 0421 QVERIFY(c->up == 0L); 0422 QVERIFY(c->down == 0L); 0423 QCOMPARE(c->text, QString("b")); 0424 QVERIFY(c->scalar); 0425 c = c->prev->next; 0426 QVERIFY(c->next == 0L); 0427 QVERIFY(c->up == 0L); 0428 QVERIFY(c->down == 0L); 0429 QCOMPARE(c->text, QString("c")); 0430 QVERIFY(c->scalar); 0431 delete parsed; 0432 0433 parsed = Label::parse("Ends in a \\"); 0434 delete parsed; 0435 0436 parsed = Label::parse("_"); 0437 delete parsed; 0438 0439 parsed = Label::parse("A weird { case }"); 0440 delete parsed; 0441 0442 parsed = Label::parse("x^y^foo{ case }"); 0443 delete parsed; 0444 0445 parsed = Label::parse("x^5\\"); 0446 delete parsed; 0447 0448 parsed = Label::parse("}"); 0449 delete parsed; 0450 0451 parsed = Label::parse("Average period (ADU)"); 0452 delete parsed; 0453 0454 parsed = Label::parse("PSD (ADU/{Hz^{1/2}})"); 0455 delete parsed; 0456 0457 parsed = Label::parse("\\Sigma^{(x+5)^2}_{5+1} + \\Pi^{\\Sigma^{i-j}_{i+j}}_{x = 0} + 5"); 0458 QVERIFY(parsed != 0L); 0459 // more to test here 0460 delete parsed; 0461 0462 parsed = Label::parse("A test \\textbf{bold \\textit{italicbold}} \\textit{italic}\n42^{\\textbf{42}^{42}} 42^\\textbf{42^{42}} 42^{42}\n\\underline{a whole\\tregion underlined}"); 0463 //dumpTree(parsed); 0464 // FIXME 0465 delete parsed; 0466 0467 parsed = Label::parse("\\textbf{\\textit{italicbold}bold}"); 0468 // FIXME 0469 delete parsed; 0470 0471 parsed = Label::parse("\\textcolor{red}{red}black"); 0472 // FIXME 0473 delete parsed; 0474 0475 parsed = Label::parse("\\textbf{bold}unbold"); 0476 // FIXME 0477 delete parsed; 0478 0479 // more to test... 0480 } 0481 0482 0483 #ifdef KST_USE_QTEST_MAIN 0484 QTEST_MAIN(TestLabelParser) 0485 #endif 0486 0487 // vim: ts=2 sw=2 et