Salome HOME
Implementation of the "0023167: [CEA 1592] JSON ParaView reader" issue.
[modules/paravis.git] / src / Plugins / JSONReader / Test / vtkJSONParserTest.cxx
1 //  Copyright (C) 2015 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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     try{
79       Parser->Parse(table);
80     } catch(vtkJSONException e) {
81       expected_exception_thrown = true;
82     }
83     Parser->Delete();
84     table->Delete();
85     if(!expected_exception_thrown) {
86       CPPUNIT_FAIL("Expected exception is not thrown !!! ");
87     }
88   }
89 }
90
91 //---------------------------------------------------
92 void vtkJSONParserTest::testParseGoodFiles() {
93   std::string dir = getGoodFilesDir();
94   if(dir.empty())
95     CPPUNIT_FAIL("Can't get examples dir !!! ");
96   std::string fn = "example";
97   
98   for(int i = 1; i <=2; i++) {
99     std::string s = dir + fn;
100     std::stringstream convert;
101     convert << i;
102     s += convert.str();
103     s += ".json";
104     vtkJSONParser* Parser = vtkJSONParser::New();
105     vtkTable* table = vtkTable::New();
106     Parser->SetFileName(s.c_str());
107     bool exception_thrown = false;
108     try{
109       Parser->Parse(table);
110     } catch(vtkJSONException e) {
111       exception_thrown = true;
112     }
113     Parser->Delete();
114     table->Delete();
115     
116     if(exception_thrown) {
117       CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
118     }
119   }
120
121   vtkJSONParser* Parser = vtkJSONParser::New();
122   vtkTable* table = vtkTable::New();
123   fn = "example";
124   std::string s = dir + fn;
125   std::stringstream convert;
126   convert << 2;
127   s += convert.str();
128   s += "_wo_metadata.json";
129   Parser->SetFileName(s.c_str());
130   bool exception_thrown = false;
131   try{
132     Parser->Parse(table);
133   } catch(vtkJSONException e) {
134     exception_thrown = true;      
135   }    
136   if(exception_thrown) {
137     CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
138   }
139
140   double v = table->GetValue(2,0).ToDouble();
141   double v1 = table->GetValue(2,1).ToDouble();
142   double v2 = table->GetValue(2,2).ToDouble();
143
144   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 3.15, table->GetValue(0,0).ToDouble(), 0.001);
145   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 0.0, table->GetValue(0,1).ToDouble(), 0.001);
146   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 43.5, table->GetValue(0,2).ToDouble(), 0.001);
147
148   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 4.156, table->GetValue(1,0).ToDouble(), 0.001);
149   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 465, table->GetValue(1,1).ToDouble(), 0.001);
150   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 137.5, table->GetValue(1,2).ToDouble(), 0.001);
151
152   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", -3.0305890e+4, table->GetValue(2,0).ToDouble(), 0.001);
153   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", 1.0305890e-1, table->GetValue(2,1).ToDouble(), 0.001);
154   CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Unexpected value : ", -3.0305890e+5, table->GetValue(2,2).ToDouble(), 0.001);
155
156   std::string s1 ();
157     
158   CPPUNIT_ASSERT_EQUAL( std::string("P[n/a]"), std::string(table->GetColumn(0)->GetName()));
159   CPPUNIT_ASSERT_EQUAL( std::string("u[n/a]"), std::string(table->GetColumn(1)->GetName()));
160   CPPUNIT_ASSERT_EQUAL( std::string("weight[n/a]"), std::string(table->GetColumn(2)->GetName()));
161   
162   Parser->Delete();
163   table->Delete();
164 }