Salome HOME
50fde39b7e703b0e8f59d39d4a90aae5f2814425
[modules/paravis.git] / src / Plugins / JSONReader / Test / vtkJSONParserTest.cxx
1 // Copyright (C) 2015-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20
21 #include "vtkJSONParserTest.hxx"
22 #include <vtkJSONParser.h>
23
24 #include <vtkTable.h>
25 #include <vtkVariant.h>
26 #include <vtkAbstractArray.h>
27
28 #include <iostream>
29 #include <sstream>
30 #include <stdlib.h>
31
32 //---------------------------------------------------
33 std::string vtkJSONParserTest::getGoodFilesDir() {
34   char* c =  getenv("PARAVIS_SRC_DIR");
35   std::string s = std::string(c == 0 ? "" : c);
36   if( !s.empty() ) {
37     s+="/src";
38     s+="/Plugins";
39     s+="/JSONReader";
40     s+="/Examples/";
41   }
42   return s;
43 }
44
45 //---------------------------------------------------
46 std::string vtkJSONParserTest::getBadFilesDir() {
47   char* c =  getenv("PARAVIS_SRC_DIR");
48   std::string s = std::string(c == 0 ? "" : c);
49   if( !s.empty() ) {
50     s+="/src";
51     s+="/Plugins";
52     s+="/JSONReader";
53     s+="/Test";
54     s+="/BadFiles";
55     s+="/";
56   }  
57   return s;
58 }
59
60 //---------------------------------------------------
61 void vtkJSONParserTest::testParseBadFiles() {
62   std::string dir = getBadFilesDir();
63   if(dir.empty())
64     CPPUNIT_FAIL("Can't get examples dir !!! ");
65   std::string fn = "bad_ex";
66   
67   // 11 existing files, bad_ex12.json doesn't exists, check exception
68   for(int i = 1; i <=12; i++) {
69     std::string s = dir + fn;
70     std::stringstream convert;
71     convert << i;
72     s += convert.str();
73     s += ".json";
74     vtkJSONParser* Parser = vtkJSONParser::New();
75     vtkTable* table = vtkTable::New();
76     Parser->SetFileName(s.c_str());
77     bool expected_exception_thrown = false;
78     CPPUNIT_ASSERT_THROW(Parser->Parse(table), vtkJSONException);
79 //    try{
80 //      ;
81 //    } catch(vtkJSONException e) {
82 //      expected_exception_thrown = true;
83 //    }
84     Parser->Delete();
85     table->Delete();
86 //    if(!expected_exception_thrown) {
87 //      CPPUNIT_FAIL("Expected exception is not thrown !!! ");
88 //    }
89   }
90 }
91
92 //---------------------------------------------------
93 void vtkJSONParserTest::testParseGoodFiles() {
94   std::string dir = getGoodFilesDir();
95   if(dir.empty())
96     CPPUNIT_FAIL("Can't get examples dir !!! ");
97   std::string fn = "example";
98   
99   for(int i = 1; i <=2; i++) {
100     std::string s = dir + fn;
101     std::stringstream convert;
102     convert << i;
103     s += convert.str();
104     s += ".json";
105     vtkJSONParser* Parser = vtkJSONParser::New();
106     vtkTable* table = vtkTable::New();
107     Parser->SetFileName(s.c_str());
108 //    bool exception_thrown = false;
109 //    try{
110       Parser->Parse(table);
111 //    } catch(vtkJSONException e) {
112 //      exception_thrown = true;
113 //    }
114     Parser->Delete();
115     table->Delete();
116     
117 //    if(exception_thrown) {
118 //      CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
119 //    }
120   }
121
122   vtkJSONParser* Parser = vtkJSONParser::New();
123   vtkTable* table = vtkTable::New();
124   fn = "example";
125   std::string s = dir + fn;
126   std::stringstream convert;
127   convert << 2;
128   s += convert.str();
129   s += "_wo_metadata.json";
130   Parser->SetFileName(s.c_str());
131   bool exception_thrown = false;
132 //  try{
133     Parser->Parse(table);
134 //  } catch(vtkJSONException e) {
135 //    exception_thrown = true;
136 //  }
137 //  if(exception_thrown) {
138 //    CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
139 //  }
140
141   double v = table->GetValue(2,0).ToDouble();
142   double v1 = table->GetValue(2,1).ToDouble();
143   double v2 = table->GetValue(2,2).ToDouble();
144
145   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 3.15, table->GetValue(0,0).ToDouble(), 0.001);
146   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 0.0, table->GetValue(0,1).ToDouble(), 0.001);
147   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 43.5, table->GetValue(0,2).ToDouble(), 0.001);
148
149   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 4.156, table->GetValue(1,0).ToDouble(), 0.001);
150   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 465, table->GetValue(1,1).ToDouble(), 0.001);
151   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 137.5, table->GetValue(1,2).ToDouble(), 0.001);
152
153   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", -3.0305890e+4, table->GetValue(2,0).ToDouble(), 0.001);
154   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 1.0305890e-1, table->GetValue(2,1).ToDouble(), 0.001);
155   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", -3.0305890e+5, table->GetValue(2,2).ToDouble(), 0.001);
156
157   std::string s1 ();
158     
159   CPPUNIT_ASSERT_EQUAL( std::string("P[n/a]"), std::string(table->GetColumn(0)->GetName()));
160   CPPUNIT_ASSERT_EQUAL( std::string("u[n/a]"), std::string(table->GetColumn(1)->GetName()));
161   CPPUNIT_ASSERT_EQUAL( std::string("weight[n/a]"), std::string(table->GetColumn(2)->GetName()));
162   
163   Parser->Delete();
164   table->Delete();
165 }