Plot_unit_tests.hpp 11.6 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * Plot_unit_tests.hpp
 *
 *  Created on: Jan 9, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_
#define OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_

#include "GoogleChart.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
12
#include "Plot/util.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
13 14 15

BOOST_AUTO_TEST_SUITE( plot_unit_test )

incardon's avatar
incardon committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

BOOST_AUTO_TEST_CASE( google_chart )
{
	//! [Producing an Histogram graph]

	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<size_t>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2,3,5,6});
	y.add({5,6,1,6});
	y.add({2,1,6,9});
	y.add({1,6,3,2});
	y.add({3,3,0,6});
	y.add({2,1,4,6});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.stype = std::string("bars");
	options.barWD = true;

	// it say that the colum4 must me represented with a line
	options.stypeext = std::string("{3: {type: 'line'}}");

	GoogleChart cg;
	cg.AddHistGraph(x,y,yn,options);
	cg.write("gc_out_wd.html");

	//! [Producing an Histogram graph]

	bool test = compare("gc_out_wd.html","gc_out_wd_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}


Pietro Incardona's avatar
Pietro Incardona committed
70 71
BOOST_AUTO_TEST_CASE( google_chart )
{
Pietro Incardona's avatar
Pietro Incardona committed
72 73
	//! [Producing an Histogram graph]

Pietro Incardona's avatar
Pietro Incardona committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<size_t>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2,3,5,6});
	y.add({5,6,1,6});
	y.add({2,1,6,9});
	y.add({1,6,3,2});
	y.add({3,3,0,6});
	y.add({2,1,4,6});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.stype = std::string("bars");

	// it say that the colum4 must me represented with a line
	options.stypeext = std::string("{3: {type: 'line'}}");

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
112
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
113 114
	cg.write("gc_out.html");

Pietro Incardona's avatar
Pietro Incardona committed
115 116
	//! [Producing an Histogram graph]

Pietro Incardona's avatar
Pietro Incardona committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	bool test = compare("gc_out.html","gc_out_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart2 )
{
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<float>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2.2,1.3,4.5,0.6});
	y.add({5.0,6.1,1.3,2.6});
	y.add({2.1,1.0,6.1,9.3});
	y.add({1.1,6.1,3.0,2.0});
	y.add({3.3,0.3,0.0,6.2});
	y.add({2.0,1.1,4.0,6.1});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.stype = std::string("bars");

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
158
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	cg.write("gc_out2.html");

	bool test = compare("gc_out2.html","gc_out2_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart3 )
{
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<float>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2.2,1.3,4.5,0.6});
	y.add({5.0,6.1,1.3,2.6});
	y.add({2.1,1.0,6.1,9.3});
	y.add({1.1,6.1,3.0,2.0});
	y.add({3.3,0.3,0.0,6.2});
	y.add({2.0,1.1,4.0,6.1});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
201
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	cg.write("gc_out3.html");

	bool test = compare("gc_out3.html","gc_out3_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart4 )
{
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<float>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2.2,1.3,4.5,0.6});
	y.add({5.0,6.1,1.3,2.6});
	y.add({2.1,1.0,6.1,9.3});
	y.add({1.1,6.1,3.0,2.0});
	y.add({3.3,0.3,0.0,6.2});
	y.add({2.0,1.1,4.0,6.1});

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
237
	cg.AddHistGraph(x,y,yn);
Pietro Incardona's avatar
Pietro Incardona committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	cg.write("gc_out4.html");

	bool test = compare("gc_out4.html","gc_out4_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart5 )
{
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<float>> y;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colums can have multiple data-set
	y.add({2.2,1.3,4.5,0.6});
	y.add({5.0,6.1,1.3,2.6});
	y.add({2.1,1.0,6.1,9.3});
	y.add({1.1,6.1,3.0,2.0});
	y.add({3.3,0.3,0.0,6.2});
	y.add({2.0,1.1,4.0,6.1});

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
265
	cg.AddHistGraph(x,y);
Pietro Incardona's avatar
Pietro Incardona committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	cg.write("gc_out5.html");

	bool test = compare("gc_out5.html","gc_out5_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart6 )
{
	openfpm::vector<openfpm::vector<float>> y;

	// Each colums can have multiple data-set
	y.add({2.2,1.3,4.5,0.6});
	y.add({5.0,6.1,1.3,2.6});
	y.add({2.1,1.0,6.1,9.3});
	y.add({1.1,6.1,3.0,2.0});
	y.add({3.3,0.3,0.0,6.2});
	y.add({2.0,1.1,4.0,6.1});

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
285
	cg.AddHistGraph(y);
Pietro Incardona's avatar
Pietro Incardona committed
286 287 288 289 290 291 292 293
	cg.write("gc_out6.html");

	bool test = compare("gc_out6.html","gc_out6_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart_with_inject_HTML )
{
Pietro Incardona's avatar
Pietro Incardona committed
294 295
	//! [Producing a set of histograms graphs]

Pietro Incardona's avatar
Pietro Incardona committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<size_t>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each colum can have multiple data set (in this case 4 dataset)
	// Each dataset can have a name
	yn.add("dataset1");
	yn.add("dataset2");
	yn.add("dataset3");
	yn.add("dataset4");

	// Each colums can have multiple data-set
	y.add({2,3,5,6});
	y.add({5,6,1,6});
	y.add({2,1,6,9});
	y.add({1,6,3,2});
	y.add({3,3,0,6});
	y.add({2,1,4,6});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.stype = std::string("bars");

	// it say that the colum4 must me represented with a line
	options.stypeext = std::string("{3: {type: 'line'}}");

	GoogleChart cg;
	//
	cg.addHTML("<h2>Before first graph</h2>");
Pietro Incardona's avatar
Pietro Incardona committed
336
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
337
	cg.addHTML("<h2>Before second graph</h2>");
Pietro Incardona's avatar
Pietro Incardona committed
338
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
339
	cg.addHTML("<h2>Before third graph</h2>");
Pietro Incardona's avatar
Pietro Incardona committed
340
	cg.AddHistGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
341 342 343
	cg.addHTML("<h2>At the end</h2>");
	cg.write("gc_out7.html");

Pietro Incardona's avatar
Pietro Incardona committed
344 345
	//! [Producing a set of histograms graphs]

Pietro Incardona's avatar
Pietro Incardona committed
346 347 348 349
	bool test = compare("gc_out7.html","gc_out7_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

Pietro Incardona's avatar
Pietro Incardona committed
350 351
BOOST_AUTO_TEST_CASE( google_chart_linear_plot )
{
Pietro Incardona's avatar
Pietro Incardona committed
352 353
	//! [Producing lines graph with style]

Pietro Incardona's avatar
Pietro Incardona committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<double>> y;
	openfpm::vector<std::string> yn;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Here we specify how many lines we have
	// first Line
	yn.add("line1");

	// second line + 2 intervals (Error bands)
	yn.add("line2");
	yn.add("interval");
	yn.add("interval");
	yn.add("interval");
	yn.add("interval");

	// third line + 1 interval (Error bands)
	yn.add("line3");
	yn.add("interval");
	yn.add("interval");

	// Each line can have multiple intervals or error bars
	// The first number specify the bottom line
	// The last three numbers specify the top line + error band (min, max)
	// The middle 5 line specify the middle lines + one external error band + one internal error band

	y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
	y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
	y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
	y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
	y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
	y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.lineWidth = 1.0;
	options.intervalext = std::string("{'i2': { 'color': '#4374E0', 'style':'bars', 'lineWidth':4, 'fillOpacity':1 } }");

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
403
	cg.AddLinesGraph(x,y,yn,options);
Pietro Incardona's avatar
Pietro Incardona committed
404 405
	cg.write("gc_plot_out.html");

Pietro Incardona's avatar
Pietro Incardona committed
406 407
	//! [Producing lines graph with style]

Pietro Incardona's avatar
Pietro Incardona committed
408 409 410 411 412 413
	bool test = compare("gc_plot_out.html","gc_plot_out_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

BOOST_AUTO_TEST_CASE( google_chart_linear_plot2 )
{
Pietro Incardona's avatar
Pietro Incardona committed
414 415
	//! [Producing lines]

Pietro Incardona's avatar
Pietro Incardona committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	openfpm::vector<std::string> x;
	openfpm::vector<openfpm::vector<double>> y;

	x.add("colum1");
	x.add("colum2");
	x.add("colum3");
	x.add("colum4");
	x.add("colum5");
	x.add("colum6");

	// Each line can have multiple intervals or error bars
	// The first number specify the bottom line
	// The last three numbers specify the top line + error band (min, max)
	// The middle 5 line specify the middle lines + one external error band + one internal error band

	y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
	y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
	y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
	y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
	y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
	y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});

	// Google charts options
	GCoptions options;

	options.title = std::string("Example");
	options.yAxis = std::string("Y Axis");
	options.xAxis = std::string("X Axis");
	options.lineWidth = 1.0;

	GoogleChart cg;
Pietro Incardona's avatar
Pietro Incardona committed
447
	cg.AddLinesGraph(x,y,options);
Pietro Incardona's avatar
Pietro Incardona committed
448 449
	cg.write("gc_plot2_out.html");

Pietro Incardona's avatar
Pietro Incardona committed
450 451
	//! [Producing lines]

Pietro Incardona's avatar
Pietro Incardona committed
452 453 454 455
	bool test = compare("gc_plot2_out.html","gc_plot2_out_test.html");
	BOOST_REQUIRE_EQUAL(true,test);
}

Pietro Incardona's avatar
Pietro Incardona committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
//! [Definition of a function]

double f(double x)
{
	return x*x;
}

//! [Definition of a function]

BOOST_AUTO_TEST_CASE( plot_util )
{
	//! [fill a vector]

	openfpm::vector<double> x;

	Fill1D(0.0,2.0,5,x);

	BOOST_REQUIRE_EQUAL(x.get(0),0.0);
	BOOST_REQUIRE_EQUAL(x.get(1),0.5);
	BOOST_REQUIRE_EQUAL(x.get(2),1.0);
	BOOST_REQUIRE_EQUAL(x.get(3),1.5);
	BOOST_REQUIRE_EQUAL(x.get(4),2.0);

	//! [fill a vector]

	x.clear();

	//! [fill a vector with a function]

	Fill1D(0.0,2.0,5,x,f);

	BOOST_REQUIRE_EQUAL(x.get(0),0.0);
	BOOST_REQUIRE_EQUAL(x.get(1),0.25);
	BOOST_REQUIRE_EQUAL(x.get(2),1.0);
	BOOST_REQUIRE_EQUAL(x.get(3),2.25);
	BOOST_REQUIRE_EQUAL(x.get(4),4.0);

	//! [fill a vector function]
}

Pietro Incardona's avatar
Pietro Incardona committed
496 497 498
BOOST_AUTO_TEST_SUITE_END()

#endif /* OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_ */