1 /*
2 XLiFE++ is an extended library of finite elements written in C++
3     Copyright (C) 2014  Lunéville, Eric; Kielbasiewicz, Nicolas; Lafranche, Yvon; Nguyen, Manh-Ha; Chambeyron, Colin
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 3 of the License, or
8     (at your option) any later version.
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13     You should have received a copy of the GNU General Public License
14     along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 /*!
18 	\file unit_Messages.cpp
19 	\author N. Kielbasiewitcz
20 	\since  8 dec 2011
21 	\date 11 may 2012
22 
23 	Low level tests of Messages class.
24 */
25 
26 #include "xlife++-libs.h"
27 #include "testUtils.hpp"
28 
29 #include <iostream>
30 #include <sstream>
31 
32 using namespace xlifepp;
33 
34 namespace unit_Messages {
35 
unit_Messages(bool check)36 String unit_Messages(bool check)
37 {
38   String rootname="unit_Messages";
39   trace_p->push(rootname);
40   std::stringstream oss;
41   oss.precision(testPrec);
42 
43   int nbErrors = 0;
44 
45   //-------------------------------------------------------------
46   // test of MsgData
47   //-------------------------------------------------------------
48 
49   std::stringstream ossMsgData;
50 
51   MsgData msgd;
52   msgd << 1 << 2.5 << "toto" << Complex(1.5, 2.5) << true;
53   msgd.push(4);
54   msgd.push("tata");
55   msgd.push(3);
56   msgd.push(Complex(-1.0, 1.0));
57   msgd.push(false);
58   msgd.push(-2.6);
59 
60   bool isInt0 = false;
61   bool isInt1 = false;
62   bool isInt2 = false;
63   bool isReal0 = false;
64   bool isReal1 = false;
65   bool isComplex0 = false;
66   bool isComplex1 = false;
67   bool isBoolean0 = false;
68   bool isBoolean1 = false;
69   bool isString0 = false;
70   bool isString1 = false;
71   bool isPrintInt = true;
72   bool isPrintReal = true;
73   bool isPrintComplex = true;
74   bool isPrintString = true;
75   bool isPrintBoolean = true;
76   bool isPushInt = true;
77   bool isPushReal = true;
78   bool isPushComplex = true;
79   bool isPushString = true;
80   bool isPushBoolean = true;
81   int nbMsgDataErrors = 0;
82 
83   if(msgd.intParameter(0) == 1) {isInt0 = true;}
84   else {nbMsgDataErrors++;}
85   if(msgd.intParameter(1) == 4) {isInt1 = true;}
86   else {nbMsgDataErrors++;}
87   if(msgd.intParameter(2) == 3) {isInt2 = true;}
88   else {nbMsgDataErrors++;}
89   isPrintInt = isPrintInt && isInt0;
90   isPushInt = isPushInt && isInt1 && isInt2;
91 
92   if(msgd.realParameter(0) == 2.5) {isReal0 = true;}
93   else {nbMsgDataErrors++;}
94   if(msgd.realParameter(1) == -2.6) {isReal1 = true;}
95   else {nbMsgDataErrors++;}
96   isPrintReal = isPrintReal && isReal0;
97   isPushReal = isPushReal && isReal1;
98 
99   if(msgd.complexParameter(0) == Complex(1.5, 2.5)) {isComplex0 = true;}
100   else {nbMsgDataErrors++;}
101   if(msgd.complexParameter(1) == Complex(-1.0, 1.0)) {isComplex1 = true;}
102   else {nbMsgDataErrors++;}
103   isPrintComplex = isPrintComplex && isComplex0;
104   isPushComplex = isPushComplex && isComplex1;
105 
106   if(msgd.stringParameter(0) == "toto") {isString0 = true;}
107   else {nbMsgDataErrors++;}
108   if(msgd.stringParameter(1) == "tata") {isString1 = true;}
109   else {nbMsgDataErrors++;}
110   isPrintString = isPrintString && isString0;
111   isPushString = isPushString && isString1;
112 
113   if(msgd.booleanParameter(0) == true) {isBoolean0 = true;}
114   else {nbMsgDataErrors++;}
115   if(msgd.booleanParameter(1) == false) {isBoolean1 = true;}
116   else {nbMsgDataErrors++;}
117   isPrintBoolean = isPrintBoolean && isBoolean0;
118   isPushBoolean = isPushBoolean && isBoolean1;
119 
120   if(!isInt0 || !isInt1 || !isInt2)
121   {
122     ossMsgData << "  - MsgData::intParameter ERROR -> (0,1=" << isInt0 << ") (1,4=" << isInt1 << ") (2,3=" << isInt2 << ")" << std::endl;
123     nbMsgDataErrors++;
124   }
125 
126   if(!isReal0 || !isReal1)
127   {
128     ossMsgData << "  - MsgData::realParameter ERROR -> (0,2.5=" << isReal0 << ") (1,-2.6=" << isReal1 << ")" << std::endl;
129     nbMsgDataErrors++;
130   }
131 
132   if(!isComplex0 || !isComplex1)
133   {
134     ossMsgData << "  - MsgData::complexParameter ERROR -> (0,1.5+2.5i=" << isComplex0 << ") (1,-1+1i=" << isComplex1 << ")" << std::endl;
135     nbMsgDataErrors++;
136   }
137 
138   if(!isString0 || !isString1)
139   {
140     ossMsgData << "  - MsgData::stringParameter ERROR -> (0,toto=" << isString0 << ") (1,tata=" << isString1 << ")" << std::endl;
141     nbMsgDataErrors++;
142   }
143 
144   if(!isBoolean0 || !isBoolean1)
145   {
146     ossMsgData << "  - MsgData::booleanParameter ERROR -> (0,true=" << isString0 << ") (1,false=" << isString1 << ")" << std::endl;
147     nbMsgDataErrors++;
148   }
149 
150   if(!isPrintInt)
151   {
152     ossMsgData << "  - MsgData friend operator<<(int) ERROR" << std::endl;
153     nbMsgDataErrors++;
154   }
155 
156   if(!isPrintReal)
157   {
158     ossMsgData << "  - MsgData friend operator<<(real) ERROR" << std::endl;
159     nbMsgDataErrors++;
160   }
161 
162   if(!isPrintComplex)
163   {
164     ossMsgData << "  - MsgData friend operator<<(complex) ERROR" << std::endl;
165     nbMsgDataErrors++;
166   }
167 
168   if(!isPrintString)
169   {
170     ossMsgData << "  - MsgData friend operator<<(string) ERROR" << std::endl;
171     nbMsgDataErrors++;
172   }
173 
174   if(!isPrintBoolean)
175   {
176     ossMsgData << "  - MsgData friend operator<<(bool) ERROR" << std::endl;
177     nbMsgDataErrors++;
178   }
179 
180   if(!isPushInt)
181   {
182     ossMsgData << "  - MsgData::push(int) ERROR" << std::endl;
183     nbMsgDataErrors++;
184   }
185 
186   if(!isPushReal)
187   {
188     ossMsgData << "  - MsgData::push(real) ERROR" << std::endl;
189     nbMsgDataErrors++;
190   }
191 
192   if(!isPushComplex)
193   {
194     ossMsgData << "  - MsgData::push(complex) ERROR" << std::endl;
195     nbMsgDataErrors++;
196   }
197 
198   if(!isPushString)
199   {
200     ossMsgData << "  - MsgData::push(string) ERROR" << std::endl;
201     nbMsgDataErrors++;
202   }
203 
204   if(!isPushBoolean)
205   {
206     ossMsgData << "  - MsgData::push(bool) ERROR" << std::endl;
207     nbMsgDataErrors++;
208   }
209 
210   oss << "---------- test of MsgData class ----------" << std::endl
211       << "Errors : " << nbMsgDataErrors << std::endl << ossMsgData.str() << std::endl;
212   nbErrors += nbMsgDataErrors;
213 
214   //-------------------------------------------------------------
215   // test of MsgFormat
216   //-------------------------------------------------------------
217 
218   std::stringstream ossMsgFormat;
219 
220   MsgFormat msgf("toto", "msg_undef", _warning, false, true);
221 
222   bool isFormat = false;
223   bool isStringId = false;
224   bool isType = false;
225   bool isStop = false;
226   bool isConsole = false;
227   int nbMsgFormatErrors = 0;
228 
229   if(msgf.format() == "toto") {isFormat = true;}
230   else {nbMsgFormatErrors++;}
231   if(msgf.stringId() == "msg_undef") {isStringId = true;}
232   else {nbMsgFormatErrors++;}
233   if(msgf.type() == _warning) {isType = true;}
234   else {nbMsgFormatErrors++;}
235   if(msgf.stop() == false) {isStop = true;}
236   else {nbMsgFormatErrors++;}
237   if(msgf.console() == true) {isConsole = true;}
238   else {nbMsgFormatErrors++;}
239 
240   if(!isFormat)
241   {
242     ossMsgFormat << "MsgFormat::format() ERROR -> toto=" << msgf.format() << std::endl;
243     nbMsgFormatErrors++;
244   }
245 
246   if(!isStringId)
247   {
248     ossMsgFormat << "MsgFormat::stringId() ERROR -> msg_undef=" << msgf.stringId() << std::endl;
249     nbMsgFormatErrors++;
250   }
251 
252   if(!isType)
253   {
254     ossMsgFormat << "MsgFormat::type() ERROR -> 2=" << msgf.type() << std::endl;
255     nbMsgFormatErrors++;
256   }
257 
258   if(!isStop)
259   {
260     ossMsgFormat << "MsgFormat::stop() ERROR -> false=" << msgf.stop() << std::endl;
261     nbMsgFormatErrors++;
262   }
263 
264   if(!isConsole)
265   {
266     ossMsgFormat << "MsgFormat::console() ERROR -> true=" << msgf.console() << std::endl;
267     nbMsgFormatErrors++;
268   }
269 
270   if(!isFormat || !isStringId || !isType || !isStop || !isConsole)
271   {
272     ossMsgFormat << "MsgFormat::MsgFormat(...) ERROR" << std::endl;
273     nbMsgFormatErrors++;
274   }
275 
276   oss << "---------- test of MsgFormat class ----------" << std::endl
277       << "Errors : " << nbMsgFormatErrors << std::endl << ossMsgFormat.str() << std::endl;
278   nbErrors += nbMsgFormatErrors;
279 
280   //-------------------------------------------------------------
281   // test of Messages
282   //-------------------------------------------------------------
283 
284   std::stringstream ossMessages;
285   bool isPrintList = true;
286   bool isFind = true;
287   bool isAppend = true;
288   int nbMessagesErrors = 0;
289 
290   const String msgType = "warning";
291   const String msgFile = inputsPathTo("userInfo_en.txt");
292   std::ofstream ofs("msgUser.txt");
293 
294   Messages* myMsg = new Messages(msgFile, ofs, msgType);
295 
296   myMsg->printList(ofs);
297 
298   std::ifstream ifs("msgUser.txt");
299   std::ifstream ifsref(msgFile.c_str());
300   String line, lineref;
301 
302   while(!ifsref.eof())
303   {
304     getline(ifsref, lineref);
305     getline(ifs, line);
306     if(line == lineref) {isPrintList = isPrintList && true;}
307   }
308 
309   if (!isPrintList)
310   {
311     ossMessages << "Messages::printList(ofstream) ERROR" << std::endl;
312     nbMessagesErrors++;
313   }
314 
315   ofs.close();
316   ofs.open("msgtest.txt");
317 
318   MsgFormat* msgf2 = myMsg->find("logon");
319   if (msgf2 != 0)
320   {
321     if (msgf2->stringId() != "logon")
322     {
323       isFind = false;
324       nbMessagesErrors++;
325     }
326     if (msgf2->format() != "Log file \"%s\" activated.")
327     {
328       isFind = false;
329       nbMessagesErrors++;
330     }
331     if (msgf2->type() != 2)
332     {
333       isFind = false;
334       nbMessagesErrors++;
335     }
336     if (msgf2->stop() != 0)
337     {
338       isFind = false;
339       nbMessagesErrors++;
340     }
341     if (msgf2->console() != 1)
342     {
343       isFind = false;
344       nbMessagesErrors++;
345     }
346   }
347   else
348   {
349     isFind = false;
350     nbMessagesErrors++;
351   }
352 
353   if (!isFind)
354   {
355     ossMessages << "Messages::find(string) ERROR" << std::endl;
356   }
357 
358   myMsg->appendFromFile(inputsPathTo("interface_en.txt"));
359   myMsg->appendFromFile(inputsPathTo("internal_en.txt"));
360   myMsg->appendFromFile(inputsPathTo("fem_en.txt"));
361   myMsg->appendFromFile(inputsPathTo("term_en.txt"));
362   myMsg->appendFromFile(inputsPathTo("mesh_en.txt"));
363   myMsg->appendFromFile(inputsPathTo("solver_en.txt"));
364 
365   msgf2 = myMsg->find("on_error");
366   if (msgf2 != 0)
367   {
368     if (msgf2->stringId() != "on_error")
369     {
370       isAppend = false;
371       nbMessagesErrors++;
372     }
373     if (msgf2->format() != ":-( %s\n:-( Error, type %s \"%s\" (%i) (see file %s for more)\n:-( in %s.\n:-( %s")
374     {
375       isAppend = false;
376       nbMessagesErrors++;
377     }
378     if (msgf2->type() != 2)
379     {
380       isAppend = false;
381       nbMessagesErrors++;
382     }
383     if (msgf2->stop() != 0)
384     {
385       isAppend = false;
386       nbMessagesErrors++;
387     }
388     if (msgf2->console() != 1)
389     {
390       isAppend = false;
391       nbMessagesErrors++;
392     }
393   }
394   else
395   {
396     isAppend = false;
397     nbMessagesErrors++;
398   }
399 
400   msgf2 = myMsg->find("invalidop");
401   if (msgf2 != 0)
402   {
403     if (msgf2->stringId() != "invalidop")
404     {
405       isAppend = false;
406       nbMessagesErrors++;
407     }
408     if (msgf2->format() != "Invalid operation : %s")
409     {
410       isAppend = false;
411       nbMessagesErrors++;
412     }
413     if (msgf2->type() != 0)
414     {
415       isAppend = false;
416       nbMessagesErrors++;
417     }
418     if (msgf2->stop() != 1)
419     {
420       isAppend = false;
421       nbMessagesErrors++;
422     }
423     if (msgf2->console() != 1)
424     {
425       isAppend = false;
426       nbMessagesErrors++;
427     }
428   }
429   else
430   {
431     isAppend = false;
432     nbMessagesErrors++;
433   }
434 
435   msgf2 = myMsg->find("failure+");
436   if (msgf2 != 0)
437   {
438     if (msgf2->stringId() != "failure+")
439     {
440       isAppend = false;
441       nbMessagesErrors++;
442     }
443     if (msgf2->format() != "%sSolver fails to converge in less than %i iterations (residue = %r, epsilon = %r)\n Previous values of residue %r, %r, %r.")
444     {
445       isAppend = false;
446       nbMessagesErrors++;
447     }
448     if (msgf2->type() != 0)
449     {
450       isAppend = false;
451       nbMessagesErrors++;
452     }
453     if (msgf2->stop() != 0)
454     {
455       isAppend = false;
456       nbMessagesErrors++;
457     }
458     if (msgf2->console() != 1)
459     {
460       isAppend = false;
461       nbMessagesErrors++;
462     }
463   }
464   else
465   {
466     isAppend = false;
467     nbMessagesErrors++;
468   }
469 
470   msgf2 = myMsg->find("bad_order");
471   if (msgf2 != 0)
472   {
473     if (msgf2->stringId() != "bad_order")
474     {
475       isAppend = false;
476       nbMessagesErrors++;
477     }
478     if (msgf2->format() != "Bad mesh element order %i in %s constructor.")
479     {
480       isAppend = false;
481       nbMessagesErrors++;
482     }
483     if (msgf2->type() != 0)
484     {
485       isAppend = false;
486       nbMessagesErrors++;
487     }
488     if (msgf2->stop() != 1)
489     {
490       isAppend = false;
491       nbMessagesErrors++;
492     }
493     if (msgf2->console() != 1)
494     {
495       isAppend = false;
496       nbMessagesErrors++;
497     }
498   }
499   else
500   {
501     isAppend = false;
502     nbMessagesErrors++;
503   }
504 
505   msgf2 = myMsg->find("msg_undef");
506   if (msgf2 != 0)
507   {
508     if (msgf2->stringId() != "msg_undef")
509     {
510       isAppend = false;
511       nbMessagesErrors++;
512     }
513     if (msgf2->format() != "Message of type \"%s\" undefined!")
514     {
515       isAppend = false;
516       nbMessagesErrors++;
517     }
518     if (msgf2->type() != 0)
519     {
520       isAppend = false;
521       nbMessagesErrors++;
522     }
523     if (msgf2->stop() != 1)
524     {
525       isAppend = false;
526       nbMessagesErrors++;
527     }
528     if (msgf2->console() != 1)
529     {
530       isAppend = false;
531       nbMessagesErrors++;
532     }
533   }
534   else
535   {
536     isAppend = false;
537     nbMessagesErrors++;
538   }
539 
540   msgf2 = myMsg->find("mat/0");
541   if (msgf2 != 0)
542   {
543     if (msgf2->stringId() != "mat/0")
544     {
545       isAppend = false;
546       nbMessagesErrors++;
547     }
548     if (msgf2->format() != "divide by 0 in matrix operation %s matrix dimension : (%i,%i)")
549     {
550       isAppend = false;
551       nbMessagesErrors++;
552     }
553     if (msgf2->type() != 0)
554     {
555       isAppend = false;
556       nbMessagesErrors++;
557     }
558     if (msgf2->stop() != 1)
559     {
560       isAppend = false;
561       nbMessagesErrors++;
562     }
563     if (msgf2->console() != 1)
564     {
565       isAppend = false;
566       nbMessagesErrors++;
567     }
568   }
569   else
570   {
571     isAppend = false;
572     nbMessagesErrors++;
573   }
574 
575   msgf2 = myMsg->find("quad_altrule");
576   if (msgf2 != 0)
577   {
578     if (msgf2->stringId() != "quad_altrule")
579     {
580       isAppend = false;
581       nbMessagesErrors++;
582     }
583     if (msgf2->format() != "No quadrature rule number %i for %s, using %s rule instead.")
584     {
585       isAppend = false;
586       nbMessagesErrors++;
587     }
588     if (msgf2->type() != 1)
589     {
590       isAppend = false;
591       nbMessagesErrors++;
592     }
593     if (msgf2->stop() != 0)
594     {
595       isAppend = false;
596       nbMessagesErrors++;
597     }
598     if (msgf2->console() != 1)
599     {
600       isAppend = false;
601       nbMessagesErrors++;
602     }
603   }
604   else
605   {
606     isAppend = false;
607     nbMessagesErrors++;
608   }
609 
610   if(!isAppend)
611   {
612     ossMessages << "Messages::appendFromFile(string) ERROR" << std::endl;
613   }
614   oss << "---------- test of Messages class ----------" << std::endl
615       << "Errors : " << nbMessagesErrors << std::endl << ossMessages.str() << std::endl;
616   nbErrors += nbMessagesErrors;
617 
618   String report = "test_Messages : ";
619   if(nbErrors > 0)
620   {
621     report += words("number of errors") + " !" + tostring(nbErrors);
622     report += "\n" + oss.str();
623   }
624   else
625   {
626     report += "OK";
627   }
628   trace_p->pop();
629   return report;
630 }
631 
632 }
633