1 // Copyright (C) 2015-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "vtkJSONParserTest.hxx"
22 #include <vtkJSONParser.h>
25 #include <vtkVariant.h>
26 #include <vtkAbstractArray.h>
32 //---------------------------------------------------
33 std::string vtkJSONParserTest::getGoodFilesDir() {
34 char* c = getenv("PARAVIS_SRC_DIR");
35 std::string s = std::string(c == 0 ? "" : c);
45 //---------------------------------------------------
46 std::string vtkJSONParserTest::getBadFilesDir() {
47 char* c = getenv("PARAVIS_SRC_DIR");
48 std::string s = std::string(c == 0 ? "" : c);
60 //---------------------------------------------------
61 void vtkJSONParserTest::testParseBadFiles() {
62 std::string dir = getBadFilesDir();
64 CPPUNIT_FAIL("Can't get examples dir !!! ");
65 std::string fn = "bad_ex";
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;
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);
81 // } catch(vtkJSONException e) {
82 // expected_exception_thrown = true;
86 // if(!expected_exception_thrown) {
87 // CPPUNIT_FAIL("Expected exception is not thrown !!! ");
92 //---------------------------------------------------
93 void vtkJSONParserTest::testParseGoodFiles() {
94 std::string dir = getGoodFilesDir();
96 CPPUNIT_FAIL("Can't get examples dir !!! ");
97 std::string fn = "example";
99 for(int i = 1; i <=2; i++) {
100 std::string s = dir + fn;
101 std::stringstream convert;
105 vtkJSONParser* Parser = vtkJSONParser::New();
106 vtkTable* table = vtkTable::New();
107 Parser->SetFileName(s.c_str());
108 // bool exception_thrown = false;
110 Parser->Parse(table);
111 // } catch(vtkJSONException e) {
112 // exception_thrown = true;
117 // if(exception_thrown) {
118 // CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
122 vtkJSONParser* Parser = vtkJSONParser::New();
123 vtkTable* table = vtkTable::New();
125 std::string s = dir + fn;
126 std::stringstream convert;
129 s += "_wo_metadata.json";
130 Parser->SetFileName(s.c_str());
131 bool exception_thrown = false;
133 Parser->Parse(table);
134 // } catch(vtkJSONException e) {
135 // exception_thrown = true;
137 // if(exception_thrown) {
138 // CPPUNIT_FAIL("Unexpected exception has been thrown !!! ");
141 double v = table->GetValue(2,0).ToDouble();
142 double v1 = table->GetValue(2,1).ToDouble();
143 double v2 = table->GetValue(2,2).ToDouble();
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);
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);
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);
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()));