File: mglTesting.cpp

package info (click to toggle)
3depict 0.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,908 kB
  • sloc: cpp: 72,771; sh: 6,497; xml: 1,576; python: 326; makefile: 194; ansic: 186
file content (183 lines) | stat: -rw-r--r-- 3,863 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
70
71
72
73
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
112
113
114
115
116
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
158
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
/*
 *	mglTesting.cpp - unit testing implementation for mgl code
 *	Copyright (C) 2018, D Haley 

 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.

 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef DEBUG
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <limits>

#include "config.h"

#include "common/assertion.h"

#include <mgl2/canvas_wnd.h>

#include "common/basics.h"
#include "common/stringFuncs.h"


//Create a fixed type of mathgl graph, then compare it to
// a reference image (if possible)
bool mglTest()
{

	//something is wrong with mathgl's FP handling
	bool fpeTrapped=getTrapfpe();
	if(fpeTrapped)
		trapfpe(false);

	unsigned int w=1024,h=768;
	mglGraph *grS;
	grS = new mglGraph(w,h);

	//Create some fake data
	mglData someDataX,someDataY;
	float *bufferX,*bufferY;
	bufferX=new float[100];
	bufferY=new float[100];
	for(unsigned int ui=0; ui<100; ui++)
	{
		bufferX[ui]=ui;
		bufferY[ui]=ui;
	}
	someDataX.Set(bufferX, 100);
	someDataY.Set(bufferY, 100);

	//Set up the plot area
	grS->SetRanges(0,100);
	grS->SetOrigin(mglPoint(0,0));
	grS->Label('x',"axis one");
	grS->Label('y',"axis two",0);
	grS->SetCut(true);
	//set up the axes a little
	mglCanvas *canvas = dynamic_cast<mglCanvas*>(grS->Self());
	canvas->AdjustTicks("x");
	canvas->SetTickTempl('x',"%g");
	canvas->Axis("xy");

	grS->Plot(someDataX,someDataY,"r");

	std::string s,t;
	genRandomFilename(t);
	s=t+".svg";
	
	pushLocale("C",LC_NUMERIC);
	grS->WriteSVG(s.c_str());
	popLocale();

	//Check that the SVG was written
	{
	std::ifstream f(s.c_str());
	if(!f)
	{
		WARN(false,"MGL Did not generate SVG");
		delete[] bufferX;
		delete[] bufferY;
		delete grS;
		return false;
	}
	}

	//Check that mathgl was OK with this
	if(grS->GetWarn())
	{
		WARN(false,"MGL functions returned an error");
		std::cerr << "warncode :" << grS->Self()->GetWarn() << " message:" << grS->Message()<< std::endl;
		delete[] bufferX;
		delete[] bufferY;
		delete grS;

		return false;
	}


	//Try writing a PNG
	s=t+".png";
	grS->WritePNG(s.c_str());
	{
	std::ifstream f(s.c_str());
	if(!f)
	{
		WARN(false,"MGL Did not generate PNG");
		delete[] bufferX;
		delete[] bufferY;
		delete grS;
		return false;
	}
	}

	delete[] bufferX;
	delete[] bufferY;

	//Check that the PNG write was OK
	if(grS->GetWarn())
	{
		WARN(false,"MGL functions returned an error");
		std::cerr << "warncode :" << grS->Self()->GetWarn() << " message:" << grS->Message()<< std::endl;
		delete grS;
		
		return false;
	}

	delete grS;


	//TODO: write non-hack image comparison function
	{
/*
	std::string call="/usr/bin/python ../extras/image-compare-hist.py ";
	call+=s;
	call += " ../test/ref-images/plot-ref.png";
	if(!system(call.c_str()))
	{
		const char *FILE_COMPARE_OUT="img-compare-result-arkd.txt";
		std::ifstream f(FILE_COMPARE_OUT);
		if(f)
		{
			float answer=std::numeric_limits<float>::max();
			std::string strAnswer;
			 strAnswer << f ;

			stream_cast(f,strAnswer);


			//As an example, an "OK" image gave 177, a broken image 13000
			const float THRESHOLD=2000;
			TEST(answer < THRESHOLD,"Image comparison failed")
				
		}
		rmFile(FILE_COMPARE_OUT);
	}
	else
	{
		WARN(false,"Unable to execute rather hacky image comparison code");
	}
*/
	}


	rmFile(s);
	rmFile(t+".svg");

	if(fpeTrapped)
		trapfpe(true);
	return true;
}

#endif