File: gdaltorture.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (273 lines) | stat: -rw-r--r-- 9,302 bytes parent folder | download
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
/******************************************************************************
 *
 * Project:  GDAL Utilities
 * Purpose:  Command line utility to torture GDAL API on datasets
 * Author:   Even Rouault, <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2008-2011, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdal.h"
#include "cpl_string.h"
#include "cpl_conv.h"

#include <cassert>

/************************************************************************/
/*                               Usage()                                */
/************************************************************************/

static void Usage()

{
    printf("Usage: gdaltorture [-r] [-u] [-rw] files*\n");
    exit(1);
}

/************************************************************************/
/*                              TortureDS()                             */
/************************************************************************/

static void TortureBand(GDALRasterBandH hBand, int /* bReadWriteOperations */,
                        int nRecurse)
{
    if (nRecurse > 5)
        return;

    GDALGetRasterDataType(hBand);
    int nBlockXSize;
    int nBlockYSize;
    GDALGetBlockSize(hBand, &nBlockXSize, &nBlockYSize);
    // GDALRasterAdviseRead
    // GDALRasterIO
    // GDALReadBlock
    // GDALWriteBlock
    const int nRasterXSize = GDALGetRasterBandXSize(hBand);
    assert(nRasterXSize >= 0);
    const int nRasterYSize = GDALGetRasterBandYSize(hBand);
    assert(nRasterYSize >= 0);
    GDALGetRasterAccess(hBand);
    GDALGetBandNumber(hBand);
    GDALGetBandDataset(hBand);
    GDALGetRasterColorInterpretation(hBand);
    // GDALSetRasterColorInterpretation
    GDALGetRasterColorTable(hBand);
    // GDALSetRasterColorTable
    GDALHasArbitraryOverviews(hBand);

    const int nOverviewCount = GDALGetOverviewCount(hBand);
    for (int iOverview = 0; iOverview < nOverviewCount; iOverview++)
    {
        GDALRasterBandH hOverviewBand = GDALGetOverview(hBand, iOverview);
        if (hOverviewBand)
            TortureBand(hOverviewBand, false, nRecurse + 1);
    }

    int bHasNoData;
    GDALGetRasterNoDataValue(hBand, &bHasNoData);
    // GDALSetRasterNoDataValue
    GDALGetRasterCategoryNames(hBand);
    // GDALSetRasterCategoryNames
    int bSuccess;
    GDALGetRasterMinimum(hBand, &bSuccess);
    GDALGetRasterMaximum(hBand, &bSuccess);
    double dfMin, dfMax, dfMean, dfStdDev;
    GDALGetRasterStatistics(hBand, TRUE, FALSE, &dfMin, &dfMax, &dfMean,
                            &dfStdDev);
    // GDALComputeRasterStatistics
    // GDALSetRasterStatistics
    GDALGetRasterUnitType(hBand);
    GDALGetRasterOffset(hBand, &bSuccess);
    // GDALSetRasterOffset
    GDALGetRasterScale(hBand, &bSuccess);
    // GDALSetRasterScale
    // double adfMinMax[2];
    // GDALComputeRasterMinMax(hBand, TRUE, adfMinMax);
    // GDALFlushRasterCache(hBand)
    // GDALGetDefaultHistogram(
    //     GDALRasterBandH hBand, double *pdfMin, double *pdfMax,
    //     int *pnBuckets, int **ppanHistogram, int bForce,
    //     GDALProgressFunc pfnProgress, void *pProgressData);
    // GDALSetDefaultHistogram(
    //     GDALRasterBandH hBand, double dfMin, double dfMax,
    //     int nBuckets, int *panHistogram);
    float afSampleBuf;
    GDALGetRandomRasterSample(hBand, 1, &afSampleBuf);
    GDALGetRasterSampleOverview(hBand, 0);  // returns a hBand
    // GDALFillRaster
    // GDALComputeBandStats
    // GDALOverviewMagnitudeCorrection
    GDALGetDefaultRAT(hBand);
    // GDALSetDefaultRAT
    // GDALAddDerivedBandPixelFunc
    GDALRasterBandH hMaskBand = GDALGetMaskBand(hBand);
    if (hMaskBand != hBand)
        TortureBand(hMaskBand, false, nRecurse + 1);
    GDALGetMaskFlags(hBand);
    // GDALCreateMaskBand
}

/************************************************************************/
/*                              TortureDS()                             */
/************************************************************************/

static void TortureDS(const char *pszTarget, bool bReadWriteOperations)
{
    // hDS = GDALOpen(pszTarget, GA_Update);
    // GDALClose(hDS);

    GDALDatasetH hDS = GDALOpen(pszTarget, GA_ReadOnly);
    if (hDS == nullptr)
        return;

    // GDALGetMetadata(GDALMajorObjectH, const char *);
    // GDALSetMetadata(GDALMajorObjectH, char **, const char *);
    // GDALGetMetadataItem(GDALMajorObjectH, const char *, const char *);
    // GDALSetMetadataItem(GDALMajorObjectH, const char *, const char *,
    //                     const char *);
    GDALGetDescription(hDS);
    // GDALSetDescription
    GDALGetDatasetDriver(hDS);
    char **papszFileList = GDALGetFileList(hDS);
    CSLDestroy(papszFileList);
    const int nXSize = GDALGetRasterXSize(hDS);
    assert(nXSize >= 0);
    const int nYSize = GDALGetRasterYSize(hDS);
    assert(nYSize >= 0);
    const int nBands = GDALGetRasterCount(hDS);
    // GDALAddBand
    // GDALDatasetRasterIO
    GDALGetProjectionRef(hDS);
    // GDALSetProjection
    double adfGeotransform[6] = {};
    GDALGetGeoTransform(hDS, adfGeotransform);
    // GDALSetGeoTransform
    GDALGetGCPCount(hDS);
    GDALGetGCPProjection(hDS);
    GDALGetGCPs(hDS);
    // GDALSetGCPs
    // GDALGetInternalHandle
    GDALReferenceDataset(hDS);
    GDALDereferenceDataset(hDS);
    // GDALBuildOverviews
    GDALGetAccess(hDS);
    // GDALFlushCache
    // GDALCreateDatasetMaskBand
    // GDALDatasetCopyWholeRaster

    for (int iBand = 0; iBand < nBands; iBand++)
    {
        GDALRasterBandH hBand = GDALGetRasterBand(hDS, iBand + 1);
        if (hBand == nullptr)
            continue;

        TortureBand(hBand, bReadWriteOperations, 0);
    }

    GDALClose(hDS);
}

/************************************************************************/
/*                       ProcessTortureTarget()                         */
/************************************************************************/

static void ProcessTortureTarget(const char *pszTarget, char **papszSiblingList,
                                 bool bRecursive, bool bReportFailures,
                                 bool bReadWriteOperations)
{
    GDALDriverH hDriver = GDALIdentifyDriver(pszTarget, papszSiblingList);

    if (hDriver != nullptr)
    {
        printf("%s: %s\n", pszTarget, GDALGetDriverShortName(hDriver));
        TortureDS(pszTarget, bReadWriteOperations);
    }
    else if (bReportFailures)
    {
        printf("%s: unrecognized\n", pszTarget);
    }

    if (!bRecursive || hDriver != nullptr)
        return;

    VSIStatBufL sStatBuf;
    if (VSIStatL(pszTarget, &sStatBuf) != 0 || !VSI_ISDIR(sStatBuf.st_mode))
        return;

    papszSiblingList = VSIReadDir(pszTarget);
    for (int i = 0; papszSiblingList && papszSiblingList[i]; i++)
    {
        if (EQUAL(papszSiblingList[i], "..") || EQUAL(papszSiblingList[i], "."))
            continue;

        const CPLString osSubTarget =
            CPLFormFilenameSafe(pszTarget, papszSiblingList[i], nullptr);

        ProcessTortureTarget(osSubTarget, papszSiblingList, bRecursive,
                             bReportFailures, bReadWriteOperations);
    }
    CSLDestroy(papszSiblingList);
}

/************************************************************************/
/*                                main()                                */
/************************************************************************/

int main(int argc, char **argv)
{
    GDALAllRegister();

    argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
    if (argc < 1)
        exit(-argc);

    if (argc < 2)
        Usage();

    /* -------------------------------------------------------------------- */
    /*      Scan for command line switches                                   */
    /* -------------------------------------------------------------------- */
    char **papszArgv = argv + 1;
    argc--;

    bool bRecursive = false;
    bool bReportFailures = false;
    bool bReadWriteOperations = false;

    while (argc > 0 && papszArgv[0][0] == '-')
    {
        if (EQUAL(papszArgv[0], "-r"))
            bRecursive = true;
        else if (EQUAL(papszArgv[0], "-u"))
            bReportFailures = true;
        else if (EQUAL(papszArgv[0], "-rw"))
            bReadWriteOperations = true;
        else
            Usage();

        papszArgv++;
        argc--;
    }

    /* -------------------------------------------------------------------- */
    /*      Process given files.                                            */
    /* -------------------------------------------------------------------- */
    while (argc > 0)
    {
        ProcessTortureTarget(papszArgv[0], nullptr, bRecursive, bReportFailures,
                             bReadWriteOperations);
        argc--;
        papszArgv++;
    }

    /* -------------------------------------------------------------------- */
    /*      Cleanup                                                         */
    /* -------------------------------------------------------------------- */
    CSLDestroy(argv);
    GDALDestroyDriverManager();

    return 0;
}