summaryrefslogtreecommitdiff
path: root/dviware/dvi2bitmap/test/t7.cc
blob: c853786f9c6bbdc58d3046eaef8491fe314789af (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Testing DviFile

// This tests only a small subset of the things that could be tested here.

#include <config.h>

#include <DviFile.h>

#include <iostream>
#include <cstdlib>

#if HAVE_CSTD_INCLUDE
#include <cmath>
#else
#include <math.h>
#endif

using STD::cerr;
using STD::endl;
using STD::fabs;

// could do with boosting accuracy, but that would involve being
// cleverer in convertUnits
#define EQTOL(a,b) (fabs((a)/(b)-1)<2e-5)

int testConvertFromScaledPoints(void)
{
    struct {
	int testvalue;
	double expected;
	DviFile::DviUnits unit;
	const char* unitname;
    } testset[] = {
	{ 12345, 12345, DviFile::unit_sp, "sp", },
	{ 65536, 1, DviFile::unit_pt, "pt", },
	{ 65536*12, 1, DviFile::unit_pc, "pc", },
	{ 65536*7227, 7200, DviFile::unit_bp, "bp", },
	{ 65536*7227, 100, DviFile::unit_in, "in", },
	{ 65536*7227, 2540, DviFile::unit_mm, "mm", },
	{ 65536*7227, 254, DviFile::unit_cm, "cm", },
	{ 65536*1238, 1157, DviFile::unit_dd, "dd", },
	{ 65536*1238*12, 1157, DviFile::unit_cc, "cc", },
    };
    int ntests = sizeof(testset)/sizeof(testset[0]);

    int nerrors = 0;

    for (int i=0; i<ntests; i++) {
	double tvalue = DviFile::convertFromScaledPoints(testset[i].testvalue,
							 testset[i].unit);
 
	if (! EQTOL(tvalue, testset[i].expected)) {
	    cerr << "Converting: " << testset[i].testvalue
		 << "sp = " << tvalue << testset[i].unitname
		 << ", expected "
		 << testset[i].expected << testset[i].unitname
		 << endl;
	    nerrors++;
	}
    }

    return nerrors;
}

int testConvertToScaledPoints(void)
{
    struct {
	int testvalue;
	double expected;
	DviFile::DviUnits unit;
	const char* unitname;
    } testset[] = {
	// Same tests as above
	{ 12345, 12345, DviFile::unit_sp, "sp", },
	{ 1, 65536, DviFile::unit_pt, "pt", },
	{ 1, 65536*12, DviFile::unit_pc, "pc", },
	{ 7200, 65536*7227, DviFile::unit_bp, "bp", },
	{ 100, 65536*7227, DviFile::unit_in, "in", },
	{ 2540, 65536*7227, DviFile::unit_mm, "mm", },
	{ 254, 65536*7227, DviFile::unit_cm, "cm", },
	{ 1157, 65536*1238, DviFile::unit_dd, "dd", },
	{ 1157, 65536*1238*12, DviFile::unit_cc, "cc", },
    };
    int ntests = sizeof(testset)/sizeof(testset[0]);

    int nerrors = 0;

    for (int i=0; i<ntests; i++) {
	double tvalue = DviFile::convertToScaledPoints(testset[i].testvalue,
						       testset[i].unit);
 
	if (! EQTOL(tvalue, testset[i].expected)) {
	    cerr << "Converting: "
		 << testset[i].testvalue << testset[i].unitname
		 << " = "
		 << tvalue
		 << "sp, expected "
		 << testset[i].expected << "sp"
		 << " (diff=" << tvalue-testset[i].expected << ")"
		 << endl;
	    nerrors++;
	}
    }

    return nerrors;
}

int testConvertUnits(void)
{
#define CONV(f,fu, t,tu)						\
    { f, t, DviFile::unit_ ## fu, #fu, DviFile::unit_ ## tu, #tu, }

    struct {
	int testvalue;
	double expected;
	DviFile::DviUnits from_unit;
	const char* from_unitname;
	DviFile::DviUnits to_unit;
	const char* to_unitname;
    } testset[] = {
	CONV(12345, sp, 12345, sp),
	CONV(1, pt, 65536, sp),
	CONV(1, in, 72.27, pt),
	CONV(10, in, 254, mm),
	CONV(254, mm, 10, in),
    };
#undef CONV
    int ntests = sizeof(testset)/sizeof(testset[0]);

    int nerrors = 0;

    for (int i=0; i<ntests; i++) {
	double tvalue = DviFile::convertUnits(testset[i].testvalue,
					      testset[i].from_unit,
					      testset[i].to_unit);
 
	if (! EQTOL(tvalue,testset[i].expected)) {
	    cerr << "Converting: "
		 << testset[i].testvalue << testset[i].from_unitname
		 << " = "
		 << tvalue << testset[i].to_unitname
		 << ", expected "
		 << testset[i].expected << testset[i].to_unitname
		 << " (diff=" << tvalue-testset[i].expected << ")"
		 << endl;
	    nerrors++;
	}
    }

    return nerrors;
}

int testDviFile()
{
    string fn = "t5.dvi";
    DviFile *dvif = new DviFile(fn);
    DviFilePreamble *p = dynamic_cast<DviFilePreamble*>(dvif->getEvent());
    
    if (! p) {
	cerr << "Eeek!  First event from DVI file is not DviFilePreamble"
	     << endl;
	return 1;		// JUMP OUT
    }
    
    int nerrors = 0;

    struct {
	double testvalue;	// in points
	double expected_sp;
	double expected_px;
    } testset[] = {
	{ 72.27, 65536*72.27, 110 },
    };
    int ntests = sizeof(testset)/sizeof(testset[0]);

    for (int i=0; i<ntests; i++) {
	double tvalue_sp = DviFile::convertUnits(testset[i].testvalue,
						 DviFile::unit_pt,
						 DviFile::unit_sp,
						 dvif);
	double tvalue_px = DviFile::convertUnits(testset[i].testvalue,
						 DviFile::unit_pt,
						 DviFile::unit_pixels,
						 dvif);
	if (! (EQTOL(tvalue_sp,testset[i].expected_sp)
	       && EQTOL(tvalue_px,testset[i].expected_px))) {
	    cerr << "Converting "
		 << testset[i].testvalue << "pt: got "
		 << tvalue_sp << "sp," << tvalue_px << "px, expected "
		 << testset[i].expected_sp << "sp,"
		 << testset[i].expected_px << "px (diff="
		 << tvalue_sp - testset[i].expected_sp
		 << ","
		 << tvalue_px - testset[i].expected_px
		 << ")" << endl;
	    nerrors++;
	}
    }
    return nerrors;
}


int main(int argc, char** argv)
{
    int nerrors = 0;

    try {
	nerrors += testConvertFromScaledPoints();
	nerrors += testConvertToScaledPoints();
	nerrors += testConvertUnits();
	nerrors += testDviFile();
    } catch (DviError &e) {
	cerr << "Test threw DviError: " << e.problem() << endl;
    }

    STD::exit(nerrors);
}