forked from geo-data/go-gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gdal.cpp
277 lines (251 loc) · 10.9 KB
/
test_gdal.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
265
266
267
268
269
270
271
272
273
274
275
276
277
///////////////////////////////////////////////////////////////////////////////
// $Id: test_gdal.cpp,v 1.4 2006/12/06 15:39:13 mloskot Exp $
//
// Project: C++ Test Suite for GDAL/OGR
// Purpose: Test general GDAL features.
// Author: Mateusz Loskot <[email protected]>
//
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2006, Mateusz Loskot <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
///////////////////////////////////////////////////////////////////////////////
//
// $Log: test_gdal.cpp,v $
// Revision 1.4 2006/12/06 15:39:13 mloskot
// Added file header comment and copyright note.
//
//
///////////////////////////////////////////////////////////////////////////////
#include <tut.h>
#include <gdal.h>
#include <gdal_priv.h>
#include <gdal_utils.h>
#include <string>
#include <limits>
namespace tut
{
// Common fixture with test data
struct test_gdal_data
{
// Expected number of drivers
int drv_count_;
test_gdal_data()
: drv_count_(0)
{
// Windows CE port builds with fixed number of drivers
#ifdef FRMT_aaigrid
drv_count_++;
#endif
#ifdef FRMT_dted
drv_count_++;
#endif
#ifdef FRMT_gtiff
drv_count_++;
#endif
}
};
// Register test group
typedef test_group<test_gdal_data> group;
typedef group::object object;
group test_gdal_group("GDAL");
// Test GDAL driver manager access
template<>
template<>
void object::test<1>()
{
GDALDriverManager* drv_mgr = NULL;
drv_mgr = GetGDALDriverManager();
ensure("GetGDALDriverManager() is NULL", NULL != drv_mgr);
}
// Test number of registered GDAL drivers
template<>
template<>
void object::test<2>()
{
#ifdef WIN32CE
ensure_equals("GDAL registered drivers count doesn't match",
GDALGetDriverCount(), drv_count_);
#endif
}
// Test if AAIGrid driver is registered
template<>
template<>
void object::test<3>()
{
GDALDriverH drv = GDALGetDriverByName("AAIGrid");
#ifdef FRMT_aaigrid
ensure("AAIGrid driver is not registered", NULL != drv);
#else
(void)drv;
ensure(true); // Skip
#endif
}
// Test if DTED driver is registered
template<>
template<>
void object::test<4>()
{
GDALDriverH drv = GDALGetDriverByName("DTED");
#ifdef FRMT_dted
ensure("DTED driver is not registered", NULL != drv);
#else
(void)drv;
ensure(true); // Skip
#endif
}
// Test if GeoTIFF driver is registered
template<>
template<>
void object::test<5>()
{
GDALDriverH drv = GDALGetDriverByName("GTiff");
#ifdef FRMT_gtiff
ensure("GTiff driver is not registered", NULL != drv);
#else
(void)drv;
ensure(true); // Skip
#endif
}
// Test GDALDataTypeUnion()
template<> template<> void object::test<6>()
{
for(int i=GDT_Byte;i<GDT_TypeCount;i++)
{
for(int j=GDT_Byte;j<GDT_TypeCount;j++)
{
GDALDataType eDT1 = static_cast<GDALDataType>(i);
GDALDataType eDT2 = static_cast<GDALDataType>(j);
GDALDataType eDT = GDALDataTypeUnion(eDT1,eDT2 );
ensure( eDT == GDALDataTypeUnion(eDT2,eDT1) );
ensure( GDALGetDataTypeSize(eDT) >= GDALGetDataTypeSize(eDT1) );
ensure( GDALGetDataTypeSize(eDT) >= GDALGetDataTypeSize(eDT2) );
ensure( (GDALDataTypeIsComplex(eDT) && (GDALDataTypeIsComplex(eDT1) || GDALDataTypeIsComplex(eDT2))) ||
(!(GDALDataTypeIsComplex(eDT) && !GDALDataTypeIsComplex(eDT1) && !GDALDataTypeIsComplex(eDT2))) );
}
}
}
// Test GDALAdjustValueToDataType()
template<> template<> void object::test<7>()
{
int bClamped, bRounded;
ensure( GDALAdjustValueToDataType(GDT_Byte,255.0,NULL,NULL) == 255.0);
ensure( GDALAdjustValueToDataType(GDT_Byte,255.0,&bClamped,&bRounded) == 255.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Byte,254.4,&bClamped,&bRounded) == 254.0 && !bClamped && bRounded);
ensure( GDALAdjustValueToDataType(GDT_Byte,-1,&bClamped,&bRounded) == 0.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Byte,256.0,&bClamped,&bRounded) == 255.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt16,65535.0,&bClamped,&bRounded) == 65535.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt16,65534.4,&bClamped,&bRounded) == 65534.0 && !bClamped && bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt16,-1,&bClamped,&bRounded) == 0.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt16,65536.0,&bClamped,&bRounded) == 65535.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,-32768.0,&bClamped,&bRounded) == -32768.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,32767.0,&bClamped,&bRounded) == 32767.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,-32767.4,&bClamped,&bRounded) == -32767.0 && !bClamped && bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,32766.4,&bClamped,&bRounded) == 32766.0 && !bClamped && bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,-32769.0,&bClamped,&bRounded) == -32768.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int16,32768.0,&bClamped,&bRounded) == 32767.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt32,10000000.0,&bClamped,&bRounded) == 10000000.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt32,10000000.4,&bClamped,&bRounded) == 10000000.0 && !bClamped && bRounded);
ensure( GDALAdjustValueToDataType(GDT_UInt32,-1,&bClamped,&bRounded) == 0.0 && bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int32,-10000000.0,&bClamped,&bRounded) == -10000000.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Int32,10000000.0,&bClamped,&bRounded) == 10000000.0 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Float32, 1.23,&bClamped,&bRounded) == 1.23 && !bClamped && !bRounded);
ensure( GDALAdjustValueToDataType(GDT_Float32, 1e300,&bClamped,&bRounded) == std::numeric_limits<float>::max() && bClamped && !bRounded);
}
class FakeBand: public GDALRasterBand
{
protected:
virtual CPLErr IReadBlock(int, int, void*) { return CE_None; }
virtual CPLErr IWriteBlock( int, int, void * ) { return CE_None; }
public:
FakeBand(int nXSize, int nYSize) { nBlockXSize = nXSize;
nBlockYSize = nYSize; }
};
class DatasetWithErrorInFlushCache: public GDALDataset
{
bool bHasFlushCache;
public:
DatasetWithErrorInFlushCache() : bHasFlushCache(false) { }
~DatasetWithErrorInFlushCache() { FlushCache(); }
virtual void FlushCache(void)
{
if( !bHasFlushCache)
CPLError(CE_Failure, CPLE_AppDefined, "some error");
GDALDataset::FlushCache();
bHasFlushCache = true;
}
virtual CPLErr SetProjection(const char*) { return CE_None; }
virtual CPLErr SetGeoTransform(double*) { return CE_None; }
static GDALDataset* CreateCopy(const char*, GDALDataset*,
int, char **,
GDALProgressFunc,
void *)
{
return new DatasetWithErrorInFlushCache();
}
static GDALDataset* Create(const char*, int nXSize, int nYSize, int, GDALDataType, char ** )
{
DatasetWithErrorInFlushCache* poDS = new DatasetWithErrorInFlushCache();
poDS->eAccess = GA_Update;
poDS->nRasterXSize = nXSize;
poDS->nRasterYSize = nYSize;
poDS->SetBand(1, new FakeBand(nXSize, nYSize));
return poDS;
}
};
// Test that GDALTranslate() detects error in flush cache
template<> template<> void object::test<8>()
{
GDALDriver* poDriver = new GDALDriver();
poDriver->SetDescription("DatasetWithErrorInFlushCache");
poDriver->pfnCreateCopy = DatasetWithErrorInFlushCache::CreateCopy;
GetGDALDriverManager()->RegisterDriver( poDriver );
const char* args[] = { "-of", "DatasetWithErrorInFlushCache", NULL };
GDALTranslateOptions* psOptions = GDALTranslateOptionsNew((char**)args, NULL);
GDALDatasetH hSrcDS = GDALOpen("../gcore/data/byte.tif", GA_ReadOnly);
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
GDALDatasetH hOutDS = GDALTranslate("", hSrcDS, psOptions, NULL);
CPLPopErrorHandler();
GDALClose(hSrcDS);
GDALTranslateOptionsFree(psOptions);
ensure(hOutDS == NULL);
ensure(CPLGetLastErrorType() != CE_None);
GetGDALDriverManager()->DeregisterDriver( poDriver );
delete poDriver;
}
// Test that GDALWarp() detects error in flush cache
template<> template<> void object::test<9>()
{
GDALDriver* poDriver = new GDALDriver();
poDriver->SetDescription("DatasetWithErrorInFlushCache");
poDriver->pfnCreate = DatasetWithErrorInFlushCache::Create;
GetGDALDriverManager()->RegisterDriver( poDriver );
const char* args[] = { "-of", "DatasetWithErrorInFlushCache", NULL };
GDALWarpAppOptions* psOptions = GDALWarpAppOptionsNew((char**)args, NULL);
GDALDatasetH hSrcDS = GDALOpen("../gcore/data/byte.tif", GA_ReadOnly);
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
GDALDatasetH hOutDS = GDALWarp("/", NULL, 1, &hSrcDS, psOptions, NULL);
CPLPopErrorHandler();
GDALClose(hSrcDS);
GDALWarpAppOptionsFree(psOptions);
ensure(hOutDS == NULL);
ensure(CPLGetLastErrorType() != CE_None);
GetGDALDriverManager()->DeregisterDriver( poDriver );
delete poDriver;
}
} // namespace tut