• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 15885686134

25 Jun 2025 07:44PM UTC coverage: 71.084%. Remained the same
15885686134

push

github

rouault
gdal_priv.h: fix C++11 compatibility

573814 of 807237 relevant lines covered (71.08%)

250621.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.97
/autotest/cpp/testcopywords.cpp
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Core
4
 * Purpose:  Test GDALCopyWords().
5
 * Author:   Even Rouault, <even dot rouault at spatialys.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12

13
#include "cpl_conv.h"
14
#include "cpl_float.h"
15
#include "gdal.h"
16

17
#include <cmath>
18
#include <cstdint>
19
#include <iostream>
20
#include <limits>
21
#include <type_traits>
22

23
#include "gtest_include.h"
24

25
namespace
26
{
27

28
// ---------------------------------------------------------------------------
29

30
template <class OutType, class CT1, class CT2>
31
void AssertRes(GDALDataType intype, CT1 inval, GDALDataType outtype,
59,991✔
32
               CT2 expected_outval, OutType outval, int numLine)
33
{
34
    if (static_cast<double>(expected_outval) == static_cast<double>(outval) ||
62,729✔
35
        (std::isnan(static_cast<double>(expected_outval)) &&
2,738✔
36
         std::isnan(static_cast<double>(outval))))
201✔
37
    {
38
        // ok
39
    }
40
    else
41
    {
42
        EXPECT_NEAR((double)outval, (double)expected_outval, 1.0)
2,336✔
43
            << "Test failed at line " << numLine
×
44
            << " (intype=" << GDALGetDataTypeName(intype)
×
45
            << ",inval=" << (double)inval
×
46
            << ",outtype=" << GDALGetDataTypeName(outtype) << ",got "
×
47
            << (double)outval << " expected  " << expected_outval;
×
48
    }
49
}
59,991✔
50

51
#define MY_EXPECT(intype, inval, outtype, expected_outval, outval)             \
52
    AssertRes(intype, inval, outtype, expected_outval, outval, numLine)
53

54
class TestCopyWords : public ::testing::Test
55
{
56
  protected:
57
    void SetUp() override
138✔
58
    {
59
        pIn = (GByte *)malloc(2048);
138✔
60
        pOut = (GByte *)malloc(2048);
138✔
61
    }
138✔
62

63
    void TearDown() override
138✔
64
    {
65

66
        free(pIn);
138✔
67
        free(pOut);
138✔
68
    }
138✔
69

70
    GByte *pIn;
71
    GByte *pOut;
72

73
    template <class InType, class OutType, class ConstantType>
74
    void Test(GDALDataType intype, ConstantType inval, ConstantType invali,
1,024✔
75
              GDALDataType outtype, ConstantType outval, ConstantType outvali,
76
              int numLine)
77
    {
78
        memset(pIn, 0xff, 1024);
1,024✔
79
        memset(pOut, 0xff, 1024);
1,024✔
80

81
        *(InType *)(pIn) = (InType)inval;
1,024✔
82
        *(InType *)(pIn + 32) = (InType)inval;
1,024✔
83
        if (GDALDataTypeIsComplex(intype))
1,024✔
84
        {
85
            ((InType *)(pIn))[1] = (InType)invali;
250✔
86
            ((InType *)(pIn + 32))[1] = (InType)invali;
250✔
87
        }
88

89
        /* Test positive offsets */
90
        GDALCopyWords(pIn, intype, 32, pOut, outtype, 32, 2);
1,024✔
91

92
        /* Test negative offsets */
93
        GDALCopyWords(pIn + 32, intype, -32, pOut + 1024 - 16, outtype, -32, 2);
1,024✔
94

95
        MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut));
1,024✔
96
        MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut + 32));
1,024✔
97
        MY_EXPECT(intype, inval, outtype, outval,
1,024✔
98
                  *(OutType *)(pOut + 1024 - 16));
99
        MY_EXPECT(intype, inval, outtype, outval,
1,024✔
100
                  *(OutType *)(pOut + 1024 - 16 - 32));
101

102
        if (GDALDataTypeIsComplex(outtype))
1,024✔
103
        {
104
            MY_EXPECT(intype, invali, outtype, outvali, ((OutType *)(pOut))[1]);
304✔
105
            MY_EXPECT(intype, invali, outtype, outvali,
304✔
106
                      ((OutType *)(pOut + 32))[1]);
107

108
            MY_EXPECT(intype, invali, outtype, outvali,
304✔
109
                      ((OutType *)(pOut + 1024 - 16))[1]);
110
            MY_EXPECT(intype, invali, outtype, outvali,
304✔
111
                      ((OutType *)(pOut + 1024 - 16 - 32))[1]);
112
        }
113
        else
114
        {
115
            constexpr int N = 32 + 31;
720✔
116
            for (int i = 0; i < N; ++i)
46,080✔
117
            {
118
                *(InType *)(pIn + i * GDALGetDataTypeSizeBytes(intype)) =
45,360✔
119
                    (InType)inval;
8,694✔
120
            }
121

122
            /* Test packed offsets */
123
            GDALCopyWords(pIn, intype, GDALGetDataTypeSizeBytes(intype), pOut,
720✔
124
                          outtype, GDALGetDataTypeSizeBytes(outtype), N);
125

126
            for (int i = 0; i < N; ++i)
46,080✔
127
            {
128
                MY_EXPECT(
45,360✔
129
                    intype, inval, outtype, outval,
130
                    *(OutType *)(pOut + i * GDALGetDataTypeSizeBytes(outtype)));
131
            }
132
        }
133
    }
1,024✔
134

135
    template <class InType, class ConstantType>
136
    void FromR_2(GDALDataType intype, ConstantType inval, ConstantType invali,
1,024✔
137
                 GDALDataType outtype, ConstantType outval,
138
                 ConstantType outvali, int numLine)
139
    {
140
        if (outtype == GDT_Byte)
1,024✔
141
            Test<InType, GByte, ConstantType>(intype, inval, invali, outtype,
77✔
142
                                              outval, outvali, numLine);
143
        else if (outtype == GDT_Int8)
947✔
144
            Test<InType, GInt8, ConstantType>(intype, inval, invali, outtype,
61✔
145
                                              outval, outvali, numLine);
146
        else if (outtype == GDT_Int16)
886✔
147
            Test<InType, GInt16, ConstantType>(intype, inval, invali, outtype,
92✔
148
                                               outval, outvali, numLine);
149
        else if (outtype == GDT_UInt16)
794✔
150
            Test<InType, GUInt16, ConstantType>(intype, inval, invali, outtype,
71✔
151
                                                outval, outvali, numLine);
152
        else if (outtype == GDT_Int32)
723✔
153
            Test<InType, GInt32, ConstantType>(intype, inval, invali, outtype,
87✔
154
                                               outval, outvali, numLine);
155
        else if (outtype == GDT_UInt32)
636✔
156
            Test<InType, GUInt32, ConstantType>(intype, inval, invali, outtype,
66✔
157
                                                outval, outvali, numLine);
158
        else if (outtype == GDT_Int64)
570✔
159
            Test<InType, std::int64_t, ConstantType>(
71✔
160
                intype, inval, invali, outtype, outval, outvali, numLine);
161
        else if (outtype == GDT_UInt64)
499✔
162
            Test<InType, std::uint64_t, ConstantType>(
50✔
163
                intype, inval, invali, outtype, outval, outvali, numLine);
164
        else if (outtype == GDT_Float16)
449✔
165
            Test<InType, GFloat16, ConstantType>(intype, inval, invali, outtype,
34✔
166
                                                 outval, outvali, numLine);
167
        else if (outtype == GDT_Float32)
415✔
168
            Test<InType, float, ConstantType>(intype, inval, invali, outtype,
57✔
169
                                              outval, outvali, numLine);
170
        else if (outtype == GDT_Float64)
358✔
171
            Test<InType, double, ConstantType>(intype, inval, invali, outtype,
54✔
172
                                               outval, outvali, numLine);
173
        else if (outtype == GDT_CInt16)
304✔
174
            Test<InType, GInt16, ConstantType>(intype, inval, invali, outtype,
87✔
175
                                               outval, outvali, numLine);
176
        else if (outtype == GDT_CInt32)
217✔
177
            Test<InType, GInt32, ConstantType>(intype, inval, invali, outtype,
87✔
178
                                               outval, outvali, numLine);
179
        else if (outtype == GDT_CFloat16)
130✔
180
            Test<InType, GFloat16, ConstantType>(intype, inval, invali, outtype,
28✔
181
                                                 outval, outvali, numLine);
182
        else if (outtype == GDT_CFloat32)
102✔
183
            Test<InType, float, ConstantType>(intype, inval, invali, outtype,
51✔
184
                                              outval, outvali, numLine);
185
        else if (outtype == GDT_CFloat64)
51✔
186
            Test<InType, double, ConstantType>(intype, inval, invali, outtype,
51✔
187
                                               outval, outvali, numLine);
188
    }
1,024✔
189

190
    template <class ConstantType>
191
    void FromR(GDALDataType intype, ConstantType inval, ConstantType invali,
1,024✔
192
               GDALDataType outtype, ConstantType outval, ConstantType outvali,
193
               int numLine)
194
    {
195
        if (intype == GDT_Byte)
1,024✔
196
            FromR_2<GByte, ConstantType>(intype, inval, invali, outtype, outval,
47✔
197
                                         outvali, numLine);
198
        else if (intype == GDT_Int8)
977✔
199
            FromR_2<GInt8, ConstantType>(intype, inval, invali, outtype, outval,
46✔
200
                                         outvali, numLine);
201
        else if (intype == GDT_Int16)
931✔
202
            FromR_2<GInt16, ConstantType>(intype, inval, invali, outtype,
42✔
203
                                          outval, outvali, numLine);
204
        else if (intype == GDT_UInt16)
889✔
205
            FromR_2<GUInt16, ConstantType>(intype, inval, invali, outtype,
45✔
206
                                           outval, outvali, numLine);
207
        else if (intype == GDT_Int32)
844✔
208
            FromR_2<GInt32, ConstantType>(intype, inval, invali, outtype,
45✔
209
                                          outval, outvali, numLine);
210
        else if (intype == GDT_UInt32)
799✔
211
            FromR_2<GUInt32, ConstantType>(intype, inval, invali, outtype,
45✔
212
                                           outval, outvali, numLine);
213
        else if (intype == GDT_Int64)
754✔
214
            FromR_2<std::int64_t, ConstantType>(intype, inval, invali, outtype,
39✔
215
                                                outval, outvali, numLine);
216
        else if (intype == GDT_UInt64)
715✔
217
            FromR_2<std::uint64_t, ConstantType>(intype, inval, invali, outtype,
45✔
218
                                                 outval, outvali, numLine);
219
        else if (intype == GDT_Float16)
670✔
220
            FromR_2<GFloat16, ConstantType>(intype, inval, invali, outtype,
135✔
221
                                            outval, outvali, numLine);
222
        else if (intype == GDT_Float32)
535✔
223
            FromR_2<float, ConstantType>(intype, inval, invali, outtype, outval,
141✔
224
                                         outvali, numLine);
225
        else if (intype == GDT_Float64)
394✔
226
            FromR_2<double, ConstantType>(intype, inval, invali, outtype,
144✔
227
                                          outval, outvali, numLine);
228
        else if (intype == GDT_CInt16)
250✔
229
            FromR_2<GInt16, ConstantType>(intype, inval, invali, outtype,
38✔
230
                                          outval, outvali, numLine);
231
        else if (intype == GDT_CInt32)
212✔
232
            FromR_2<GInt32, ConstantType>(intype, inval, invali, outtype,
38✔
233
                                          outval, outvali, numLine);
234
        else if (intype == GDT_CFloat16)
174✔
235
            FromR_2<GFloat16, ConstantType>(intype, inval, invali, outtype,
58✔
236
                                            outval, outvali, numLine);
237
        else if (intype == GDT_CFloat32)
116✔
238
            FromR_2<float, ConstantType>(intype, inval, invali, outtype, outval,
58✔
239
                                         outvali, numLine);
240
        else if (intype == GDT_CFloat64)
58✔
241
            FromR_2<double, ConstantType>(intype, inval, invali, outtype,
58✔
242
                                          outval, outvali, numLine);
243
    }
1,024✔
244
};
245

246
#define FROM_R(intype, inval, outtype, outval)                                 \
247
    FromR<GIntBig>(intype, inval, 0, outtype, outval, 0, __LINE__)
248
#define FROM_R_F(intype, inval, outtype, outval)                               \
249
    FromR<double>(intype, inval, 0, outtype, outval, 0, __LINE__)
250

251
#define FROM_C(intype, inval, invali, outtype, outval, outvali)                \
252
    FromR<GIntBig>(intype, inval, invali, outtype, outval, outvali, __LINE__)
253
#define FROM_C_F(intype, inval, invali, outtype, outval, outvali)              \
254
    FromR<double>(intype, inval, invali, outtype, outval, outvali, __LINE__)
255

256
#define IS_UNSIGNED(x)                                                         \
257
    (x == GDT_Byte || x == GDT_UInt16 || x == GDT_UInt32 || x == GDT_UInt64)
258
#define IS_FLOAT(x)                                                            \
259
    (x == GDT_Float16 || x == GDT_Float32 || x == GDT_Float64 ||               \
260
     x == GDT_CFloat16 || x == GDT_CFloat32 || x == GDT_CFloat64)
261

262
#define CST_3000000000 (((GIntBig)3000) * 1000 * 1000)
263
#define CST_5000000000 (((GIntBig)5000) * 1000 * 1000)
264

265
TEST_F(TestCopyWords, GDT_Byte)
4✔
266
{
267
    /* GDT_Byte */
268
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
269
         outtype = (GDALDataType)(outtype + 1))
16✔
270
    {
271
        FROM_R(GDT_Byte, 0, outtype, 0);
16✔
272
        FROM_R(GDT_Byte, 127, outtype, 127);
16✔
273
        if (outtype != GDT_Int8)
16✔
274
            FROM_R(GDT_Byte, 255, outtype, 255);
15✔
275
    }
276

277
    for (int i = 0; i < 17; i++)
18✔
278
    {
279
        pIn[i] = (GByte)i;
17✔
280
    }
281

282
    memset(pOut, 0xff, 128);
1✔
283
    GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Int32, 4, 17);
1✔
284
    for (int i = 0; i < 17; i++)
18✔
285
    {
286
        AssertRes(GDT_Byte, i, GDT_Int32, i, ((int *)pOut)[i], __LINE__);
17✔
287
    }
288

289
    memset(pOut, 0xff, 128);
1✔
290
    GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Float32, 4, 17);
1✔
291
    for (int i = 0; i < 17; i++)
18✔
292
    {
293
        AssertRes(GDT_Byte, i, GDT_Float32, i, ((float *)pOut)[i], __LINE__);
17✔
294
    }
295
}
1✔
296

297
TEST_F(TestCopyWords, GDT_Int8)
4✔
298
{
299
    /* GDT_Int8 */
300
    FROM_R(GDT_Int8, -128, GDT_Byte, 0);    /* clamp */
1✔
301
    FROM_R(GDT_Int8, -128, GDT_Int8, -128); /* clamp */
1✔
302
    FROM_R(GDT_Int8, -128, GDT_Int16, -128);
1✔
303
    FROM_R(GDT_Int8, -128, GDT_UInt16, 0); /* clamp */
1✔
304
    FROM_R(GDT_Int8, -128, GDT_Int32, -128);
1✔
305
    FROM_R(GDT_Int8, -128, GDT_UInt32, 0); /* clamp */
1✔
306
    FROM_R(GDT_Int8, -128, GDT_Int64, -128);
1✔
307
    FROM_R(GDT_Int8, -128, GDT_UInt64, 0); /* clamp */
1✔
308
    FROM_R(GDT_Int8, -128, GDT_Float16, -128);
1✔
309
    FROM_R(GDT_Int8, -128, GDT_Float32, -128);
1✔
310
    FROM_R(GDT_Int8, -128, GDT_Float64, -128);
1✔
311
    FROM_R(GDT_Int8, -128, GDT_CInt16, -128);
1✔
312
    FROM_R(GDT_Int8, -128, GDT_CInt32, -128);
1✔
313
    FROM_R(GDT_Int8, -128, GDT_CFloat16, -128);
1✔
314
    FROM_R(GDT_Int8, -128, GDT_CFloat32, -128);
1✔
315
    FROM_R(GDT_Int8, -128, GDT_CFloat64, -128);
1✔
316
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
317
         outtype = (GDALDataType)(outtype + 1))
16✔
318
    {
319
        FROM_R(GDT_Int8, 127, outtype, 127);
16✔
320
    }
321

322
    FROM_R(GDT_Int8, 127, GDT_Byte, 127);
1✔
323
    FROM_R(GDT_Int8, 127, GDT_Int8, 127);
1✔
324
    FROM_R(GDT_Int8, 127, GDT_Int16, 127);
1✔
325
    FROM_R(GDT_Int8, 127, GDT_UInt16, 127);
1✔
326
    FROM_R(GDT_Int8, 127, GDT_Int32, 127);
1✔
327
    FROM_R(GDT_Int8, 127, GDT_UInt32, 127);
1✔
328
    FROM_R(GDT_Int8, 127, GDT_Int64, 127);
1✔
329
    FROM_R(GDT_Int8, 127, GDT_UInt64, 127);
1✔
330
    FROM_R(GDT_Int8, 127, GDT_Float32, 127);
1✔
331
    FROM_R(GDT_Int8, 127, GDT_Float64, 127);
1✔
332
    FROM_R(GDT_Int8, 127, GDT_CInt16, 127);
1✔
333
    FROM_R(GDT_Int8, 127, GDT_CInt32, 127);
1✔
334
    FROM_R(GDT_Int8, 127, GDT_CFloat32, 127);
1✔
335
    FROM_R(GDT_Int8, 127, GDT_CFloat64, 127);
1✔
336
}
1✔
337

338
TEST_F(TestCopyWords, GDT_Int16)
4✔
339
{
340
    /* GDT_Int16 */
341
    FROM_R(GDT_Int16, -32000, GDT_Byte, 0); /* clamp */
1✔
342
    FROM_R(GDT_Int16, -32000, GDT_Int16, -32000);
1✔
343
    FROM_R(GDT_Int16, -32000, GDT_UInt16, 0); /* clamp */
1✔
344
    FROM_R(GDT_Int16, -32000, GDT_Int32, -32000);
1✔
345
    FROM_R(GDT_Int16, -32000, GDT_UInt32, 0); /* clamp */
1✔
346
    FROM_R(GDT_Int16, -32000, GDT_Int64, -32000);
1✔
347
    FROM_R(GDT_Int16, -32000, GDT_UInt64, 0); /* clamp */
1✔
348
    FROM_R(GDT_Int16, -32000, GDT_Float32, -32000);
1✔
349
    FROM_R(GDT_Int16, -32000, GDT_Float64, -32000);
1✔
350
    FROM_R(GDT_Int16, -32000, GDT_CInt16, -32000);
1✔
351
    FROM_R(GDT_Int16, -32000, GDT_CInt32, -32000);
1✔
352
    FROM_R(GDT_Int16, -32000, GDT_CFloat32, -32000);
1✔
353
    FROM_R(GDT_Int16, -32000, GDT_CFloat64, -32000);
1✔
354
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
355
         outtype = (GDALDataType)(outtype + 1))
16✔
356
    {
357
        FROM_R(GDT_Int16, 127, outtype, 127);
16✔
358
    }
359

360
    FROM_R(GDT_Int16, 32000, GDT_Byte, 255); /* clamp */
1✔
361
    FROM_R(GDT_Int16, 32000, GDT_Int16, 32000);
1✔
362
    FROM_R(GDT_Int16, 32000, GDT_UInt16, 32000);
1✔
363
    FROM_R(GDT_Int16, 32000, GDT_Int32, 32000);
1✔
364
    FROM_R(GDT_Int16, 32000, GDT_UInt32, 32000);
1✔
365
    FROM_R(GDT_Int16, 32000, GDT_Int64, 32000);
1✔
366
    FROM_R(GDT_Int16, 32000, GDT_UInt64, 32000);
1✔
367
    FROM_R(GDT_Int16, 32000, GDT_Float32, 32000);
1✔
368
    FROM_R(GDT_Int16, 32000, GDT_Float64, 32000);
1✔
369
    FROM_R(GDT_Int16, 32000, GDT_CInt16, 32000);
1✔
370
    FROM_R(GDT_Int16, 32000, GDT_CInt32, 32000);
1✔
371
    FROM_R(GDT_Int16, 32000, GDT_CFloat32, 32000);
1✔
372
    FROM_R(GDT_Int16, 32000, GDT_CFloat64, 32000);
1✔
373
}
1✔
374

375
TEST_F(TestCopyWords, GDT_UInt16)
4✔
376
{
377
    /* GDT_UInt16 */
378
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
379
         outtype = (GDALDataType)(outtype + 1))
16✔
380
    {
381
        FROM_R(GDT_UInt16, 0, outtype, 0);
16✔
382
        FROM_R(GDT_UInt16, 127, outtype, 127);
16✔
383
    }
384

385
    FROM_R(GDT_UInt16, 65000, GDT_Byte, 255);    /* clamp */
1✔
386
    FROM_R(GDT_UInt16, 65000, GDT_Int16, 32767); /* clamp */
1✔
387
    FROM_R(GDT_UInt16, 65000, GDT_UInt16, 65000);
1✔
388
    FROM_R(GDT_UInt16, 65000, GDT_Int32, 65000);
1✔
389
    FROM_R(GDT_UInt16, 65000, GDT_UInt32, 65000);
1✔
390
    FROM_R(GDT_UInt16, 65000, GDT_Int64, 65000);
1✔
391
    FROM_R(GDT_UInt16, 65000, GDT_UInt64, 65000);
1✔
392
    FROM_R(GDT_UInt16, 65000, GDT_Float32, 65000);
1✔
393
    FROM_R(GDT_UInt16, 65000, GDT_Float64, 65000);
1✔
394
    FROM_R(GDT_UInt16, 65000, GDT_CInt16, 32767); /* clamp */
1✔
395
    FROM_R(GDT_UInt16, 65000, GDT_CInt32, 65000);
1✔
396
    FROM_R(GDT_UInt16, 65000, GDT_CFloat32, 65000);
1✔
397
    FROM_R(GDT_UInt16, 65000, GDT_CFloat64, 65000);
1✔
398
}
1✔
399

400
TEST_F(TestCopyWords, GDT_Int32)
4✔
401
{
402
    /* GDT_Int32 */
403
    FROM_R(GDT_Int32, -33000, GDT_Byte, 0);       /* clamp */
1✔
404
    FROM_R(GDT_Int32, -33000, GDT_Int16, -32768); /* clamp */
1✔
405
    FROM_R(GDT_Int32, -33000, GDT_UInt16, 0);     /* clamp */
1✔
406
    FROM_R(GDT_Int32, -33000, GDT_Int32, -33000);
1✔
407
    FROM_R(GDT_Int32, -33000, GDT_UInt32, 0); /* clamp */
1✔
408
    FROM_R(GDT_Int32, -33000, GDT_Int64, -33000);
1✔
409
    FROM_R(GDT_Int32, -33000, GDT_UInt64, 0); /* clamp */
1✔
410
    FROM_R(GDT_Int32, -33000, GDT_Float32, -33000);
1✔
411
    FROM_R(GDT_Int32, -33000, GDT_Float64, -33000);
1✔
412
    FROM_R(GDT_Int32, -33000, GDT_CInt16, -32768); /* clamp */
1✔
413
    FROM_R(GDT_Int32, -33000, GDT_CInt32, -33000);
1✔
414
    FROM_R(GDT_Int32, -33000, GDT_CFloat32, -33000);
1✔
415
    FROM_R(GDT_Int32, -33000, GDT_CFloat64, -33000);
1✔
416
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
417
         outtype = (GDALDataType)(outtype + 1))
16✔
418
    {
419
        FROM_R(GDT_Int32, 127, outtype, 127);
16✔
420
    }
421

422
    FROM_R(GDT_Int32, 67000, GDT_Byte, 255);     /* clamp */
1✔
423
    FROM_R(GDT_Int32, 67000, GDT_Int16, 32767);  /* clamp */
1✔
424
    FROM_R(GDT_Int32, 67000, GDT_UInt16, 65535); /* clamp */
1✔
425
    FROM_R(GDT_Int32, 67000, GDT_Int32, 67000);
1✔
426
    FROM_R(GDT_Int32, 67000, GDT_UInt32, 67000);
1✔
427
    FROM_R(GDT_Int32, 67000, GDT_Int64, 67000);
1✔
428
    FROM_R(GDT_Int32, 67000, GDT_UInt64, 67000);
1✔
429
    FROM_R(GDT_Int32, 67000, GDT_Float32, 67000);
1✔
430
    FROM_R(GDT_Int32, 67000, GDT_Float64, 67000);
1✔
431
    FROM_R(GDT_Int32, 67000, GDT_CInt16, 32767); /* clamp */
1✔
432
    FROM_R(GDT_Int32, 67000, GDT_CInt32, 67000);
1✔
433
    FROM_R(GDT_Int32, 67000, GDT_CFloat32, 67000);
1✔
434
    FROM_R(GDT_Int32, 67000, GDT_CFloat64, 67000);
1✔
435
}
1✔
436

437
TEST_F(TestCopyWords, GDT_UInt32)
4✔
438
{
439
    /* GDT_UInt32 */
440
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
441
         outtype = (GDALDataType)(outtype + 1))
16✔
442
    {
443
        FROM_R(GDT_UInt32, 0, outtype, 0);
16✔
444
        FROM_R(GDT_UInt32, 127, outtype, 127);
16✔
445
    }
446

447
    FROM_R(GDT_UInt32, 3000000000U, GDT_Byte, 255);         /* clamp */
1✔
448
    FROM_R(GDT_UInt32, 3000000000U, GDT_Int16, 32767);      /* clamp */
1✔
449
    FROM_R(GDT_UInt32, 3000000000U, GDT_UInt16, 65535);     /* clamp */
1✔
450
    FROM_R(GDT_UInt32, 3000000000U, GDT_Int32, 2147483647); /* clamp */
1✔
451
    FROM_R(GDT_UInt32, 3000000000U, GDT_UInt32, 3000000000U);
1✔
452
    FROM_R(GDT_UInt32, 3000000000U, GDT_Int64, 3000000000U);
1✔
453
    FROM_R(GDT_UInt32, 3000000000U, GDT_UInt64, 3000000000U);
1✔
454
    FROM_R(GDT_UInt32, 3000000000U, GDT_Float32, 3000000000U);
1✔
455
    FROM_R(GDT_UInt32, 3000000000U, GDT_Float64, 3000000000U);
1✔
456
    FROM_R(GDT_UInt32, 3000000000U, GDT_CInt16, 32767);      /* clamp */
1✔
457
    FROM_R(GDT_UInt32, 3000000000U, GDT_CInt32, 2147483647); /* clamp */
1✔
458
    FROM_R(GDT_UInt32, 3000000000U, GDT_CFloat32, 3000000000U);
1✔
459
    FROM_R(GDT_UInt32, 3000000000U, GDT_CFloat64, 3000000000U);
1✔
460
}
1✔
461

462
TEST_F(TestCopyWords, check_GDT_Int64)
4✔
463
{
464
    /* GDT_Int64 */
465
    FROM_R(GDT_Int64, -33000, GDT_Byte, 0);       /* clamp */
1✔
466
    FROM_R(GDT_Int64, -33000, GDT_Int16, -32768); /* clamp */
1✔
467
    FROM_R(GDT_Int64, -33000, GDT_UInt16, 0);     /* clamp */
1✔
468
    FROM_R(GDT_Int64, -33000, GDT_Int32, -33000);
1✔
469
    FROM_R(GDT_Int64, -33000, GDT_UInt32, 0); /* clamp */
1✔
470
    FROM_R(GDT_Int64, -33000, GDT_Int64, -33000);
1✔
471
    FROM_R(GDT_Int64, -33000, GDT_UInt64, 0); /* clamp */
1✔
472
    FROM_R(GDT_Int64, -33000, GDT_Float32, -33000);
1✔
473
    FROM_R(GDT_Int64, -33000, GDT_Float64, -33000);
1✔
474
    FROM_R(GDT_Int64, -33000, GDT_CInt16, -32768); /* clamp */
1✔
475
    FROM_R(GDT_Int64, -33000, GDT_CInt32, -33000);
1✔
476
    FROM_R(GDT_Int64, -33000, GDT_CFloat32, -33000);
1✔
477
    FROM_R(GDT_Int64, -33000, GDT_CFloat64, -33000);
1✔
478
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
479
         outtype = (GDALDataType)(outtype + 1))
16✔
480
    {
481
        FROM_R(GDT_Int64, 127, outtype, 127);
16✔
482
    }
483

484
    FROM_R(GDT_Int64, 67000, GDT_Byte, 255);     /* clamp */
1✔
485
    FROM_R(GDT_Int64, 67000, GDT_Int16, 32767);  /* clamp */
1✔
486
    FROM_R(GDT_Int32, 67000, GDT_UInt16, 65535); /* clamp */
1✔
487
    FROM_R(GDT_Int32, 67000, GDT_Int32, 67000);
1✔
488
    FROM_R(GDT_Int64, 67000, GDT_UInt32, 67000);
1✔
489
    FROM_R(GDT_Int32, 67000, GDT_Int64, 67000);
1✔
490
    FROM_R(GDT_Int64, 67000, GDT_UInt64, 67000);
1✔
491
    FROM_R(GDT_Int64, 67000, GDT_Float32, 67000);
1✔
492
    FROM_R(GDT_Int64, 67000, GDT_Float64, 67000);
1✔
493
    FROM_R(GDT_Int64, 67000, GDT_CInt16, 32767); /* clamp */
1✔
494
    FROM_R(GDT_Int64, 67000, GDT_CInt32, 67000);
1✔
495
    FROM_R(GDT_Int64, 67000, GDT_CFloat32, 67000);
1✔
496
    FROM_R(GDT_Int64, 67000, GDT_CFloat64, 67000);
1✔
497
}
1✔
498

499
TEST_F(TestCopyWords, GDT_UInt64)
4✔
500
{
501
    /* GDT_UInt64 */
502
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
503
         outtype = (GDALDataType)(outtype + 1))
16✔
504
    {
505
        FROM_R(GDT_UInt64, 0, outtype, 0);
16✔
506
        FROM_R(GDT_UInt64, 127, outtype, 127);
16✔
507
    }
508

509
    std::uint64_t nVal = static_cast<std::uint64_t>(3000000000) * 1000;
1✔
510
    FROM_R(GDT_UInt64, nVal, GDT_Byte, 255);           /* clamp */
1✔
511
    FROM_R(GDT_UInt64, nVal, GDT_Int16, 32767);        /* clamp */
1✔
512
    FROM_R(GDT_UInt64, nVal, GDT_UInt16, 65535);       /* clamp */
1✔
513
    FROM_R(GDT_UInt64, nVal, GDT_Int32, 2147483647);   /* clamp */
1✔
514
    FROM_R(GDT_UInt64, nVal, GDT_UInt32, 4294967295U); /* clamp */
1✔
515
    FROM_R(GDT_UInt64, nVal, GDT_Int64, nVal);
1✔
516
    FROM_R(GDT_UInt64, nVal, GDT_UInt64, nVal);
1✔
517
    FROM_R(GDT_UInt64, nVal, GDT_Float32,
1✔
518
           static_cast<uint64_t>(static_cast<float>(nVal)));
519
    FROM_R(GDT_UInt64, nVal, GDT_Float64, nVal);
1✔
520
    FROM_R(GDT_UInt64, nVal, GDT_CInt16, 32767);      /* clamp */
1✔
521
    FROM_R(GDT_UInt64, nVal, GDT_CInt32, 2147483647); /* clamp */
1✔
522
    FROM_R(GDT_UInt64, nVal, GDT_CFloat32,
1✔
523
           static_cast<uint64_t>(static_cast<float>(nVal)));
524
    FROM_R(GDT_UInt64, nVal, GDT_CFloat64, nVal);
1✔
525
}
1✔
526

527
TEST_F(TestCopyWords, GDT_Float64)
4✔
528
{
529
    FROM_R_F(GDT_Float64, std::numeric_limits<double>::max(), GDT_Float32,
1✔
530
             std::numeric_limits<double>::infinity());
531
    FROM_R_F(GDT_Float64, -std::numeric_limits<double>::max(), GDT_Float32,
1✔
532
             -std::numeric_limits<double>::infinity());
533
    FROM_R_F(GDT_Float64, std::numeric_limits<double>::quiet_NaN(), GDT_Float32,
1✔
534
             std::numeric_limits<double>::quiet_NaN());
535
}
1✔
536

537
TEST_F(TestCopyWords, GDT_Float16only)
4✔
538
{
539
    GDALDataType intype = GDT_Float16;
1✔
540
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
541
         outtype = (GDALDataType)(outtype + 1))
16✔
542
    {
543
        if (IS_FLOAT(outtype))
16✔
544
        {
545
            FROM_R_F(intype, 127.1, outtype, 127.1);
6✔
546
            FROM_R_F(intype, -127.1, outtype, -127.1);
6✔
547
        }
548
        else
549
        {
550
            FROM_R_F(intype, 125.1, outtype, 125);
10✔
551
            FROM_R_F(intype, 125.9, outtype, 126);
10✔
552

553
            FROM_R_F(intype, 0.4, outtype, 0);
10✔
554
            FROM_R_F(intype, 0.5, outtype,
10✔
555
                     1); /* We could argue how to do this rounding */
556
            FROM_R_F(intype, 0.6, outtype, 1);
10✔
557
            FROM_R_F(intype, 126.5, outtype,
10✔
558
                     127); /* We could argue how to do this rounding */
559

560
            if (!IS_UNSIGNED(outtype))
10✔
561
            {
562
                FROM_R_F(intype, -125.9, outtype, -126);
6✔
563
                FROM_R_F(intype, -127.1, outtype, -127);
6✔
564

565
                FROM_R_F(intype, -0.4, outtype, 0);
6✔
566
                FROM_R_F(intype, -0.5, outtype,
6✔
567
                         -1); /* We could argue how to do this rounding */
568
                FROM_R_F(intype, -0.6, outtype, -1);
6✔
569
                FROM_R_F(intype, -127.5, outtype,
6✔
570
                         -128); /* We could argue how to do this rounding */
571
            }
572
        }
573
    }
574
    FROM_R(intype, -30000, GDT_Byte, 0);
1✔
575
    FROM_R(intype, -32768, GDT_Byte, 0);
1✔
576
    FROM_R(intype, -1, GDT_Byte, 0);
1✔
577
    FROM_R(intype, 256, GDT_Byte, 255);
1✔
578
    FROM_R(intype, 30000, GDT_Byte, 255);
1✔
579
    FROM_R(intype, -330000, GDT_Int16, -32768);
1✔
580
    FROM_R(intype, -33000, GDT_Int16, -32768);
1✔
581
    FROM_R(intype, 33000, GDT_Int16, 32767);
1✔
582
    FROM_R(intype, -33000, GDT_UInt16, 0);
1✔
583
    FROM_R(intype, -1, GDT_UInt16, 0);
1✔
584
    FROM_R(intype, 60000, GDT_UInt16, 60000);
1✔
585
    FROM_R(intype, -33000, GDT_Int32, -32992);
1✔
586
    FROM_R(intype, 33000, GDT_Int32, 32992);
1✔
587
    FROM_R(intype, -1, GDT_UInt32, 0);
1✔
588
    FROM_R(intype, 60000, GDT_UInt32, 60000U);
1✔
589
    FROM_R(intype, 33000, GDT_Float32, 32992);
1✔
590
    FROM_R(intype, -33000, GDT_Float32, -32992);
1✔
591
    FROM_R(intype, 33000, GDT_Float64, 32992);
1✔
592
    FROM_R(intype, -33000, GDT_Float64, -32992);
1✔
593
    FROM_R(intype, -33000, GDT_CInt16, -32768);
1✔
594
    FROM_R(intype, 33000, GDT_CInt16, 32767);
1✔
595
    FROM_R(intype, -33000, GDT_CInt32, -32992);
1✔
596
    FROM_R(intype, 33000, GDT_CInt32, 32992);
1✔
597
    FROM_R(intype, 33000, GDT_CFloat32, 32992);
1✔
598
    FROM_R(intype, -33000, GDT_CFloat32, -32992);
1✔
599
    FROM_R(intype, 33000, GDT_CFloat64, 32992);
1✔
600
    FROM_R(intype, -33000, GDT_CFloat64, -32992);
1✔
601

602
    FROM_R_F(GDT_Float32, std::numeric_limits<float>::max(), GDT_Float16,
1✔
603
             std::numeric_limits<double>::infinity());
604
    FROM_R_F(GDT_Float32, -std::numeric_limits<float>::max(), GDT_Float16,
1✔
605
             -std::numeric_limits<double>::infinity());
606
    FROM_R_F(GDT_Float32, std::numeric_limits<float>::quiet_NaN(), GDT_Float16,
1✔
607
             std::numeric_limits<double>::quiet_NaN());
608

609
    FROM_R_F(GDT_Float64, std::numeric_limits<double>::max(), GDT_Float16,
1✔
610
             std::numeric_limits<double>::infinity());
611
    FROM_R_F(GDT_Float64, -std::numeric_limits<double>::max(), GDT_Float16,
1✔
612
             -std::numeric_limits<double>::infinity());
613
    FROM_R_F(GDT_Float64, std::numeric_limits<double>::quiet_NaN(), GDT_Float16,
1✔
614
             std::numeric_limits<double>::quiet_NaN());
615

616
    // Float16 to Int64
617
    {
618
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::quiet_NaN();
1✔
619
        int64_t out_value = 0;
1✔
620
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_Int64, 0, 1);
1✔
621
        EXPECT_EQ(out_value, 0);
1✔
622
    }
623

624
    {
625
        GFloat16 in_value = -cpl::NumericLimits<GFloat16>::infinity();
1✔
626
        int64_t out_value = 0;
1✔
627
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_Int64, 0, 1);
1✔
628
        EXPECT_EQ(out_value, INT64_MIN);
1✔
629
    }
630

631
    {
632
        GFloat16 in_value = -cpl::NumericLimits<GFloat16>::max();
1✔
633
        int64_t out_value = 0;
1✔
634
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_Int64, 0, 1);
1✔
635
        EXPECT_EQ(out_value, -65504);
1✔
636
    }
637

638
    {
639
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::max();
1✔
640
        int64_t out_value = 0;
1✔
641
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_Int64, 0, 1);
1✔
642
        EXPECT_EQ(out_value, 65504);
1✔
643
    }
644

645
    {
646
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::infinity();
1✔
647
        int64_t out_value = 0;
1✔
648
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_Int64, 0, 1);
1✔
649
        EXPECT_EQ(out_value, INT64_MAX);
1✔
650
    }
651

652
    // Float16 to UInt64
653
    {
654
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::quiet_NaN();
1✔
655
        uint64_t out_value = 0;
1✔
656
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_UInt64, 0, 1);
1✔
657
        EXPECT_EQ(out_value, 0);
1✔
658
    }
659

660
    {
661
        GFloat16 in_value = -cpl::NumericLimits<GFloat16>::infinity();
1✔
662
        uint64_t out_value = 0;
1✔
663
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_UInt64, 0, 1);
1✔
664
        EXPECT_EQ(out_value, 0);
1✔
665
    }
666

667
    {
668
        GFloat16 in_value = -cpl::NumericLimits<GFloat16>::max();
1✔
669
        uint64_t out_value = 0;
1✔
670
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_UInt64, 0, 1);
1✔
671
        EXPECT_EQ(out_value, 0);
1✔
672
    }
673

674
    {
675
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::max();
1✔
676
        uint64_t out_value = 0;
1✔
677
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_UInt64, 0, 1);
1✔
678
        EXPECT_EQ(out_value, 65504);
1✔
679
    }
680

681
    {
682
        GFloat16 in_value = cpl::NumericLimits<GFloat16>::infinity();
1✔
683
        uint64_t out_value = 0;
1✔
684
        GDALCopyWords(&in_value, GDT_Float16, 0, &out_value, GDT_UInt64, 0, 1);
1✔
685
        EXPECT_EQ(out_value, UINT64_MAX);
1✔
686
    }
687
}
1✔
688

689
TEST_F(TestCopyWords, GDT_Float32and64)
4✔
690
{
691
    /* GDT_Float32 and GDT_Float64 */
692
    for (int i = 0; i < 2; i++)
3✔
693
    {
694
        GDALDataType intype = (i == 0) ? GDT_Float32 : GDT_Float64;
2✔
695
        for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
34✔
696
             outtype = (GDALDataType)(outtype + 1))
32✔
697
        {
698
            if (IS_FLOAT(outtype))
32✔
699
            {
700
                FROM_R_F(intype, 127.1, outtype, 127.1);
12✔
701
                FROM_R_F(intype, -127.1, outtype, -127.1);
12✔
702
            }
703
            else
704
            {
705
                FROM_R_F(intype, 125.1, outtype, 125);
20✔
706
                FROM_R_F(intype, 125.9, outtype, 126);
20✔
707

708
                FROM_R_F(intype, 0.4, outtype, 0);
20✔
709
                FROM_R_F(intype, 0.5, outtype,
20✔
710
                         1); /* We could argue how to do this rounding */
711
                FROM_R_F(intype, 0.6, outtype, 1);
20✔
712
                FROM_R_F(intype, 126.5, outtype,
20✔
713
                         127); /* We could argue how to do this rounding */
714

715
                if (!IS_UNSIGNED(outtype))
20✔
716
                {
717
                    FROM_R_F(intype, -125.9, outtype, -126);
12✔
718
                    FROM_R_F(intype, -127.1, outtype, -127);
12✔
719

720
                    FROM_R_F(intype, -0.4, outtype, 0);
12✔
721
                    FROM_R_F(intype, -0.5, outtype,
12✔
722
                             -1); /* We could argue how to do this rounding */
723
                    FROM_R_F(intype, -0.6, outtype, -1);
12✔
724
                    FROM_R_F(intype, -127.5, outtype,
12✔
725
                             -128); /* We could argue how to do this rounding */
726
                }
727
            }
728
        }
729
        FROM_R(intype, -CST_3000000000, GDT_Byte, 0);
2✔
730
        FROM_R(intype, -32768, GDT_Byte, 0);
2✔
731
        FROM_R(intype, -1, GDT_Byte, 0);
2✔
732
        FROM_R(intype, 256, GDT_Byte, 255);
2✔
733
        FROM_R(intype, 65536, GDT_Byte, 255);
2✔
734
        FROM_R(intype, CST_3000000000, GDT_Byte, 255);
2✔
735
        FROM_R(intype, -CST_3000000000, GDT_Int16, -32768);
2✔
736
        FROM_R(intype, -33000, GDT_Int16, -32768);
2✔
737
        FROM_R(intype, 33000, GDT_Int16, 32767);
2✔
738
        FROM_R(intype, CST_3000000000, GDT_Int16, 32767);
2✔
739
        FROM_R(intype, -CST_3000000000, GDT_UInt16, 0);
2✔
740
        FROM_R(intype, -1, GDT_UInt16, 0);
2✔
741
        FROM_R(intype, 66000, GDT_UInt16, 65535);
2✔
742
        FROM_R(intype, CST_3000000000, GDT_UInt16, 65535);
2✔
743
        FROM_R(intype, -CST_3000000000, GDT_Int32, INT_MIN);
2✔
744
        FROM_R(intype, CST_3000000000, GDT_Int32, 2147483647);
2✔
745
        FROM_R(intype, -1, GDT_UInt32, 0);
2✔
746
        FROM_R(intype, CST_5000000000, GDT_UInt32, 4294967295UL);
2✔
747
        FROM_R(intype, CST_5000000000, GDT_Float32, CST_5000000000);
2✔
748
        FROM_R(intype, -CST_5000000000, GDT_Float32, -CST_5000000000);
2✔
749
        FROM_R(intype, CST_5000000000, GDT_Float64, CST_5000000000);
2✔
750
        FROM_R(intype, -CST_5000000000, GDT_Float64, -CST_5000000000);
2✔
751
        FROM_R(intype, -33000, GDT_CInt16, -32768);
2✔
752
        FROM_R(intype, 33000, GDT_CInt16, 32767);
2✔
753
        FROM_R(intype, -CST_3000000000, GDT_CInt32, INT_MIN);
2✔
754
        FROM_R(intype, CST_3000000000, GDT_CInt32, 2147483647);
2✔
755
        FROM_R(intype, CST_5000000000, GDT_CFloat32, CST_5000000000);
2✔
756
        FROM_R(intype, -CST_5000000000, GDT_CFloat32, -CST_5000000000);
2✔
757
        FROM_R(intype, CST_5000000000, GDT_CFloat64, CST_5000000000);
2✔
758
        FROM_R(intype, -CST_5000000000, GDT_CFloat64, -CST_5000000000);
2✔
759
    }
760

761
    // Float32 to Int64
762
    {
763
        float in_value = cpl::NumericLimits<float>::quiet_NaN();
1✔
764
        int64_t out_value = 0;
1✔
765
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_Int64, 0, 1);
1✔
766
        EXPECT_EQ(out_value, 0);
1✔
767
    }
768

769
    {
770
        float in_value = -cpl::NumericLimits<float>::infinity();
1✔
771
        int64_t out_value = 0;
1✔
772
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_Int64, 0, 1);
1✔
773
        EXPECT_EQ(out_value, INT64_MIN);
1✔
774
    }
775

776
    {
777
        float in_value = -cpl::NumericLimits<float>::max();
1✔
778
        int64_t out_value = 0;
1✔
779
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_Int64, 0, 1);
1✔
780
        EXPECT_EQ(out_value, INT64_MIN);
1✔
781
    }
782

783
    {
784
        float in_value = cpl::NumericLimits<float>::max();
1✔
785
        int64_t out_value = 0;
1✔
786
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_Int64, 0, 1);
1✔
787
        EXPECT_EQ(out_value, INT64_MAX);
1✔
788
    }
789

790
    {
791
        float in_value = cpl::NumericLimits<float>::infinity();
1✔
792
        int64_t out_value = 0;
1✔
793
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_Int64, 0, 1);
1✔
794
        EXPECT_EQ(out_value, INT64_MAX);
1✔
795
    }
796

797
    // Float64 to Int64
798
    {
799
        double in_value = cpl::NumericLimits<double>::quiet_NaN();
1✔
800
        int64_t out_value = 0;
1✔
801
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_Int64, 0, 1);
1✔
802
        EXPECT_EQ(out_value, 0);
1✔
803
    }
804

805
    {
806
        double in_value = -cpl::NumericLimits<double>::infinity();
1✔
807
        int64_t out_value = 0;
1✔
808
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_Int64, 0, 1);
1✔
809
        EXPECT_EQ(out_value, INT64_MIN);
1✔
810
    }
811

812
    {
813
        double in_value = -cpl::NumericLimits<double>::max();
1✔
814
        int64_t out_value = 0;
1✔
815
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_Int64, 0, 1);
1✔
816
        EXPECT_EQ(out_value, INT64_MIN);
1✔
817
    }
818

819
    {
820
        double in_value = cpl::NumericLimits<double>::max();
1✔
821
        int64_t out_value = 0;
1✔
822
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_Int64, 0, 1);
1✔
823
        EXPECT_EQ(out_value, INT64_MAX);
1✔
824
    }
825

826
    {
827
        double in_value = cpl::NumericLimits<double>::infinity();
1✔
828
        int64_t out_value = 0;
1✔
829
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_Int64, 0, 1);
1✔
830
        EXPECT_EQ(out_value, INT64_MAX);
1✔
831
    }
832

833
    // Float32 to UInt64
834
    {
835
        float in_value = cpl::NumericLimits<float>::quiet_NaN();
1✔
836
        uint64_t out_value = 0;
1✔
837
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_UInt64, 0, 1);
1✔
838
        EXPECT_EQ(out_value, 0);
1✔
839
    }
840

841
    {
842
        float in_value = -cpl::NumericLimits<float>::infinity();
1✔
843
        uint64_t out_value = 0;
1✔
844
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_UInt64, 0, 1);
1✔
845
        EXPECT_EQ(out_value, 0);
1✔
846
    }
847

848
    {
849
        float in_value = -cpl::NumericLimits<float>::max();
1✔
850
        uint64_t out_value = 0;
1✔
851
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_UInt64, 0, 1);
1✔
852
        EXPECT_EQ(out_value, 0);
1✔
853
    }
854

855
    {
856
        float in_value = cpl::NumericLimits<float>::max();
1✔
857
        uint64_t out_value = 0;
1✔
858
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_UInt64, 0, 1);
1✔
859
        EXPECT_EQ(out_value, UINT64_MAX);
1✔
860
    }
861

862
    {
863
        float in_value = cpl::NumericLimits<float>::infinity();
1✔
864
        uint64_t out_value = 0;
1✔
865
        GDALCopyWords(&in_value, GDT_Float32, 0, &out_value, GDT_UInt64, 0, 1);
1✔
866
        EXPECT_EQ(out_value, UINT64_MAX);
1✔
867
    }
868

869
    // Float64 to UInt64
870
    {
871
        double in_value = -cpl::NumericLimits<double>::quiet_NaN();
1✔
872
        uint64_t out_value = 0;
1✔
873
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_UInt64, 0, 1);
1✔
874
        EXPECT_EQ(out_value, 0);
1✔
875
    }
876

877
    {
878
        double in_value = -cpl::NumericLimits<double>::infinity();
1✔
879
        uint64_t out_value = 0;
1✔
880
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_UInt64, 0, 1);
1✔
881
        EXPECT_EQ(out_value, 0);
1✔
882
    }
883

884
    {
885
        double in_value = -cpl::NumericLimits<double>::max();
1✔
886
        uint64_t out_value = 0;
1✔
887
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_UInt64, 0, 1);
1✔
888
        EXPECT_EQ(out_value, 0);
1✔
889
    }
890

891
    {
892
        double in_value = cpl::NumericLimits<double>::max();
1✔
893
        uint64_t out_value = 0;
1✔
894
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_UInt64, 0, 1);
1✔
895
        EXPECT_EQ(out_value, UINT64_MAX);
1✔
896
    }
897

898
    {
899
        double in_value = cpl::NumericLimits<double>::infinity();
1✔
900
        uint64_t out_value = 0;
1✔
901
        GDALCopyWords(&in_value, GDT_Float64, 0, &out_value, GDT_UInt64, 0, 1);
1✔
902
        EXPECT_EQ(out_value, UINT64_MAX);
1✔
903
    }
904
}
1✔
905

906
TEST_F(TestCopyWords, GDT_CInt16)
4✔
907
{
908
    /* GDT_CInt16 */
909
    FROM_C(GDT_CInt16, -32000, -32500, GDT_Byte, 0, 0); /* clamp */
1✔
910
    FROM_C(GDT_CInt16, -32000, -32500, GDT_Int16, -32000, 0);
1✔
911
    FROM_C(GDT_CInt16, -32000, -32500, GDT_UInt16, 0, 0); /* clamp */
1✔
912
    FROM_C(GDT_CInt16, -32000, -32500, GDT_Int32, -32000, 0);
1✔
913
    FROM_C(GDT_CInt16, -32000, -32500, GDT_UInt32, 0, 0); /* clamp */
1✔
914
    FROM_C(GDT_CInt16, -32000, -32500, GDT_Float32, -32000, 0);
1✔
915
    FROM_C(GDT_CInt16, -32000, -32500, GDT_Float64, -32000, 0);
1✔
916
    FROM_C(GDT_CInt16, -32000, -32500, GDT_CInt16, -32000, -32500);
1✔
917
    FROM_C(GDT_CInt16, -32000, -32500, GDT_CInt32, -32000, -32500);
1✔
918
    FROM_C(GDT_CInt16, -32000, -32500, GDT_CFloat32, -32000, -32500);
1✔
919
    FROM_C(GDT_CInt16, -32000, -32500, GDT_CFloat64, -32000, -32500);
1✔
920
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
921
         outtype = (GDALDataType)(outtype + 1))
16✔
922
    {
923
        FROM_C(GDT_CInt16, 127, 128, outtype, 127, 128);
16✔
924
    }
925

926
    FROM_C(GDT_CInt16, 32000, 32500, GDT_Byte, 255, 0); /* clamp */
1✔
927
    FROM_C(GDT_CInt16, 32000, 32500, GDT_Int16, 32000, 0);
1✔
928
    FROM_C(GDT_CInt16, 32000, 32500, GDT_UInt16, 32000, 0);
1✔
929
    FROM_C(GDT_CInt16, 32000, 32500, GDT_Int32, 32000, 0);
1✔
930
    FROM_C(GDT_CInt16, 32000, 32500, GDT_UInt32, 32000, 0);
1✔
931
    FROM_C(GDT_CInt16, 32000, 32500, GDT_Float32, 32000, 0);
1✔
932
    FROM_C(GDT_CInt16, 32000, 32500, GDT_Float64, 32000, 0);
1✔
933
    FROM_C(GDT_CInt16, 32000, 32500, GDT_CInt16, 32000, 32500);
1✔
934
    FROM_C(GDT_CInt16, 32000, 32500, GDT_CInt32, 32000, 32500);
1✔
935
    FROM_C(GDT_CInt16, 32000, 32500, GDT_CFloat32, 32000, 32500);
1✔
936
    FROM_C(GDT_CInt16, 32000, 32500, GDT_CFloat64, 32000, 32500);
1✔
937
}
1✔
938

939
TEST_F(TestCopyWords, GDT_CInt32)
4✔
940
{
941
    /* GDT_CInt32 */
942
    FROM_C(GDT_CInt32, -33000, -33500, GDT_Byte, 0, 0);       /* clamp */
1✔
943
    FROM_C(GDT_CInt32, -33000, -33500, GDT_Int16, -32768, 0); /* clamp */
1✔
944
    FROM_C(GDT_CInt32, -33000, -33500, GDT_UInt16, 0, 0);     /* clamp */
1✔
945
    FROM_C(GDT_CInt32, -33000, -33500, GDT_Int32, -33000, 0);
1✔
946
    FROM_C(GDT_CInt32, -33000, -33500, GDT_UInt32, 0, 0); /* clamp */
1✔
947
    FROM_C(GDT_CInt32, -33000, -33500, GDT_Float32, -33000, 0);
1✔
948
    FROM_C(GDT_CInt32, -33000, -33500, GDT_Float64, -33000, 0);
1✔
949
    FROM_C(GDT_CInt32, -33000, -33500, GDT_CInt16, -32768, -32768); /* clamp */
1✔
950
    FROM_C(GDT_CInt32, -33000, -33500, GDT_CInt32, -33000, -33500);
1✔
951
    FROM_C(GDT_CInt32, -33000, -33500, GDT_CFloat32, -33000, -33500);
1✔
952
    FROM_C(GDT_CInt32, -33000, -33500, GDT_CFloat64, -33000, -33500);
1✔
953
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
954
         outtype = (GDALDataType)(outtype + 1))
16✔
955
    {
956
        FROM_C(GDT_CInt32, 127, 128, outtype, 127, 128);
16✔
957
    }
958

959
    FROM_C(GDT_CInt32, 67000, 67500, GDT_Byte, 255, 0);     /* clamp */
1✔
960
    FROM_C(GDT_CInt32, 67000, 67500, GDT_Int16, 32767, 0);  /* clamp */
1✔
961
    FROM_C(GDT_CInt32, 67000, 67500, GDT_UInt16, 65535, 0); /* clamp */
1✔
962
    FROM_C(GDT_CInt32, 67000, 67500, GDT_Int32, 67000, 0);
1✔
963
    FROM_C(GDT_CInt32, 67000, 67500, GDT_UInt32, 67000, 0);
1✔
964
    FROM_C(GDT_CInt32, 67000, 67500, GDT_Float32, 67000, 0);
1✔
965
    FROM_C(GDT_CInt32, 67000, 67500, GDT_Float64, 67000, 0);
1✔
966
    FROM_C(GDT_CInt32, 67000, 67500, GDT_CInt16, 32767, 32767); /* clamp */
1✔
967
    FROM_C(GDT_CInt32, 67000, 67500, GDT_CInt32, 67000, 67500);
1✔
968
    FROM_C(GDT_CInt32, 67000, 67500, GDT_CFloat32, 67000, 67500);
1✔
969
    FROM_C(GDT_CInt32, 67000, 67500, GDT_CFloat64, 67000, 67500);
1✔
970
}
1✔
971

972
TEST_F(TestCopyWords, GDT_CFloat32and64)
4✔
973
{
974
    /* GDT_CFloat32 and GDT_CFloat64 */
975
    for (int i = 0; i < 2; i++)
3✔
976
    {
977
        GDALDataType intype = (i == 0) ? GDT_CFloat32 : GDT_CFloat64;
2✔
978
        for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
34✔
979
             outtype = (GDALDataType)(outtype + 1))
32✔
980
        {
981
            if (IS_FLOAT(outtype))
32✔
982
            {
983
                FROM_C_F(intype, 127.1, 127.9, outtype, 127.1, 127.9);
12✔
984
                FROM_C_F(intype, -127.1, -127.9, outtype, -127.1, -127.9);
12✔
985
            }
986
            else
987
            {
988
                FROM_C_F(intype, 126.1, 150.9, outtype, 126, 151);
20✔
989
                FROM_C_F(intype, 126.9, 150.1, outtype, 127, 150);
20✔
990
                if (!IS_UNSIGNED(outtype))
20✔
991
                {
992
                    FROM_C_F(intype, -125.9, -127.1, outtype, -126, -127);
12✔
993
                }
994
            }
995
        }
996
        FROM_C(intype, -1, 256, GDT_Byte, 0, 0);
2✔
997
        FROM_C(intype, 256, -1, GDT_Byte, 255, 0);
2✔
998
        FROM_C(intype, -33000, 33000, GDT_Int16, -32768, 0);
2✔
999
        FROM_C(intype, 33000, -33000, GDT_Int16, 32767, 0);
2✔
1000
        FROM_C(intype, -1, 66000, GDT_UInt16, 0, 0);
2✔
1001
        FROM_C(intype, 66000, -1, GDT_UInt16, 65535, 0);
2✔
1002
        FROM_C(intype, -CST_3000000000, -CST_3000000000, GDT_Int32, INT_MIN, 0);
2✔
1003
        FROM_C(intype, CST_3000000000, CST_3000000000, GDT_Int32, 2147483647,
2✔
1004
               0);
1005
        FROM_C(intype, -1, CST_5000000000, GDT_UInt32, 0, 0);
2✔
1006
        FROM_C(intype, CST_5000000000, -1, GDT_UInt32, 4294967295UL, 0);
2✔
1007
        FROM_C(intype, CST_5000000000, -1, GDT_Float32, CST_5000000000, 0);
2✔
1008
        FROM_C(intype, CST_5000000000, -1, GDT_Float64, CST_5000000000, 0);
2✔
1009
        FROM_C(intype, -CST_5000000000, -1, GDT_Float32, -CST_5000000000, 0);
2✔
1010
        FROM_C(intype, -CST_5000000000, -1, GDT_Float64, -CST_5000000000, 0);
2✔
1011
        FROM_C(intype, -33000, 33000, GDT_CInt16, -32768, 32767);
2✔
1012
        FROM_C(intype, 33000, -33000, GDT_CInt16, 32767, -32768);
2✔
1013
        FROM_C(intype, -CST_3000000000, -CST_3000000000, GDT_CInt32, INT_MIN,
2✔
1014
               INT_MIN);
1015
        FROM_C(intype, CST_3000000000, CST_3000000000, GDT_CInt32, 2147483647,
2✔
1016
               2147483647);
1017
        FROM_C(intype, CST_5000000000, -CST_5000000000, GDT_CFloat32,
2✔
1018
               CST_5000000000, -CST_5000000000);
1019
        FROM_C(intype, CST_5000000000, -CST_5000000000, GDT_CFloat64,
2✔
1020
               CST_5000000000, -CST_5000000000);
1021
    }
1022
}
1✔
1023

1024
TEST_F(TestCopyWords, GDT_CFloat16only)
4✔
1025
{
1026
    GDALDataType intype = GDT_CFloat16;
1✔
1027
    for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
17✔
1028
         outtype = (GDALDataType)(outtype + 1))
16✔
1029
    {
1030
        if (IS_FLOAT(outtype))
16✔
1031
        {
1032
            FROM_C_F(intype, 127.1, 127.9, outtype, 127.1, 127.9);
6✔
1033
            FROM_C_F(intype, -127.1, -127.9, outtype, -127.1, -127.9);
6✔
1034
        }
1035
        else
1036
        {
1037
            FROM_C_F(intype, 126.1, 150.9, outtype, 126, 151);
10✔
1038
            FROM_C_F(intype, 126.9, 150.1, outtype, 127, 150);
10✔
1039
            if (!IS_UNSIGNED(outtype))
10✔
1040
            {
1041
                FROM_C_F(intype, -125.9, -127.1, outtype, -126, -127);
6✔
1042
            }
1043
        }
1044
    }
1045
    FROM_C(intype, -1, 256, GDT_Byte, 0, 0);
1✔
1046
    FROM_C(intype, 256, -1, GDT_Byte, 255, 0);
1✔
1047
    FROM_C(intype, -33000, 33000, GDT_Int16, -32768, 0);
1✔
1048
    FROM_C(intype, 33000, -33000, GDT_Int16, 32767, 0);
1✔
1049
    FROM_C(intype, -1, 66000, GDT_UInt16, 0, 0);
1✔
1050
    FROM_C(intype, 66000, -1, GDT_UInt16, 65535, 0);
1✔
1051
    FROM_C(intype, -33000, -33000, GDT_Int32, -32992, 0);
1✔
1052
    FROM_C(intype, 33000, 33000, GDT_Int32, 32992, 0);
1✔
1053
    FROM_C(intype, -1, 33000, GDT_UInt32, 0, 0);
1✔
1054
    FROM_C(intype, 33000, -1, GDT_UInt32, 32992, 0);
1✔
1055
    FROM_C(intype, 33000, -1, GDT_Float32, 32992, 0);
1✔
1056
    FROM_C(intype, 33000, -1, GDT_Float64, 32992, 0);
1✔
1057
    FROM_C(intype, -33000, -1, GDT_Float32, -32992, 0);
1✔
1058
    FROM_C(intype, -33000, -1, GDT_Float64, -32992, 0);
1✔
1059
    FROM_C(intype, -33000, 33000, GDT_CInt16, -32768, 32767);
1✔
1060
    FROM_C(intype, 33000, -33000, GDT_CInt16, 32767, -32768);
1✔
1061
    FROM_C(intype, -33000, -33000, GDT_CInt32, -32992, -32992);
1✔
1062
    FROM_C(intype, 33000, 33000, GDT_CInt32, 32992, 32992);
1✔
1063
    FROM_C(intype, 33000, -33000, GDT_CFloat32, 32992, -32992);
1✔
1064
    FROM_C(intype, 33000, -33000, GDT_CFloat64, 32992, -32992);
1✔
1065
}
1✔
1066

1067
template <class Tin, class Tout>
1068
void CheckPackedGeneric(GDALDataType eIn, GDALDataType eOut)
121✔
1069
{
1070
    const int N = 64 + 7;
121✔
1071
    Tin arrayIn[N];
792✔
1072
    Tout arrayOut[N];
792✔
1073
    for (int i = 0; i < N; i++)
8,712✔
1074
    {
1075
        if constexpr (!std::is_integral_v<Tin> && std::is_integral_v<Tout>)
1076
        {
1077
            // Test correct rounding
1078
            if (i == 0 && std::is_unsigned_v<Tout>)
852✔
1079
                arrayIn[i] = cpl::NumericLimits<Tin>::quiet_NaN();
12✔
1080
            else if ((i % 2) != 0)
1,692✔
1081
                arrayIn[i] = static_cast<Tin>(i + 0.4);
840✔
1082
            else
1083
                arrayIn[i] = static_cast<Tin>(i + 0.6);
852✔
1084
        }
1085
        else
1086
        {
1087
            arrayIn[i] = static_cast<Tin>(i + 1);
6,887✔
1088
        }
1089
        arrayOut[i] = 0;
8,591✔
1090
    }
1091
    GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
121✔
1092
                  GDALGetDataTypeSizeBytes(eOut), N);
1093
    int numLine = 0;
121✔
1094
    for (int i = 0; i < N; i++)
8,712✔
1095
    {
1096
        if constexpr (!std::is_integral_v<Tin> && std::is_integral_v<Tout>)
1097
        {
1098
            if (i == 0 && std::is_unsigned_v<Tout>)
852✔
1099
            {
1100
                MY_EXPECT(eIn, cpl::NumericLimits<Tin>::quiet_NaN(), eOut, 0,
12✔
1101
                          arrayOut[i]);
1102
            }
1103
            else if ((i % 2) != 0)
1,692✔
1104
            {
1105
                MY_EXPECT(eIn, i + 0.4, eOut, i, arrayOut[i]);
840✔
1106
            }
1107
            else
1108
            {
1109
                MY_EXPECT(eIn, i + 0.6, eOut, i + 1, arrayOut[i]);
852✔
1110
            }
1111
        }
1112
        else
1113
        {
1114
            MY_EXPECT(eIn, i + 1, eOut, i + 1, arrayOut[i]);
6,887✔
1115
        }
1116
    }
1117
}
121✔
1118

1119
template <class Tin, class Tout>
1120
void CheckPacked(GDALDataType eIn, GDALDataType eOut)
119✔
1121
{
1122
    CheckPackedGeneric<Tin, Tout>(eIn, eOut);
119✔
1123
}
119✔
1124

1125
template <>
1126
void CheckPacked<GUInt16, GByte>(GDALDataType eIn, GDALDataType eOut)
1✔
1127
{
1128
    CheckPackedGeneric<GUInt16, GByte>(eIn, eOut);
1✔
1129

1130
    const int N = 64 + 7;
1✔
1131
    GUInt16 arrayIn[N] = {0};
1✔
1132
    GByte arrayOut[N] = {0};
1✔
1133
    for (int i = 0; i < N; i++)
72✔
1134
    {
1135
        arrayIn[i] = (i % 6) == 0   ? 254
130✔
1136
                     : (i % 6) == 1 ? 255
59✔
1137
                     : (i % 4) == 2 ? 256
47✔
1138
                     : (i % 6) == 3 ? 32767
35✔
1139
                     : (i % 6) == 4 ? 32768
23✔
1140
                                    : 65535;
1141
    }
1142
    GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
1✔
1143
                  GDALGetDataTypeSizeBytes(eOut), N);
1144
    int numLine = 0;
1✔
1145
    for (int i = 0; i < N; i++)
72✔
1146
    {
1147
        MY_EXPECT(eIn, (int)arrayIn[i], eOut, (i % 6) == 0 ? 254 : 255,
71✔
1148
                  arrayOut[i]);
1149
    }
1150
}
1✔
1151

1152
template <>
1153
void CheckPacked<GUInt16, GInt16>(GDALDataType eIn, GDALDataType eOut)
1✔
1154
{
1155
    CheckPackedGeneric<GUInt16, GInt16>(eIn, eOut);
1✔
1156

1157
    const int N = 64 + 7;
1✔
1158
    GUInt16 arrayIn[N] = {0};
1✔
1159
    GInt16 arrayOut[N] = {0};
1✔
1160
    for (int i = 0; i < N; i++)
72✔
1161
    {
1162
        arrayIn[i] = 32766 + (i % 4);
71✔
1163
    }
1164
    GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
1✔
1165
                  GDALGetDataTypeSizeBytes(eOut), N);
1166
    int numLine = 0;
1✔
1167
    for (int i = 0; i < N; i++)
72✔
1168
    {
1169
        MY_EXPECT(eIn, (int)arrayIn[i], eOut, (i % 4) == 0 ? 32766 : 32767,
71✔
1170
                  arrayOut[i]);
1171
    }
1172
}
1✔
1173

1174
template <class Tin> void CheckPacked(GDALDataType eIn, GDALDataType eOut)
121✔
1175
{
1176
    switch (eOut)
121✔
1177
    {
1178
        case GDT_Byte:
11✔
1179
            CheckPacked<Tin, GByte>(eIn, eOut);
11✔
1180
            break;
11✔
1181
        case GDT_Int8:
11✔
1182
            CheckPacked<Tin, GInt8>(eIn, eOut);
11✔
1183
            break;
11✔
1184
        case GDT_UInt16:
11✔
1185
            CheckPacked<Tin, GUInt16>(eIn, eOut);
11✔
1186
            break;
11✔
1187
        case GDT_Int16:
11✔
1188
            CheckPacked<Tin, GInt16>(eIn, eOut);
11✔
1189
            break;
11✔
1190
        case GDT_UInt32:
11✔
1191
            CheckPacked<Tin, GUInt32>(eIn, eOut);
11✔
1192
            break;
11✔
1193
        case GDT_Int32:
11✔
1194
            CheckPacked<Tin, GInt32>(eIn, eOut);
11✔
1195
            break;
11✔
1196
        case GDT_UInt64:
11✔
1197
            CheckPacked<Tin, std::uint64_t>(eIn, eOut);
11✔
1198
            break;
11✔
1199
        case GDT_Int64:
11✔
1200
            CheckPacked<Tin, std::int64_t>(eIn, eOut);
11✔
1201
            break;
11✔
1202
        case GDT_Float16:
11✔
1203
            CheckPacked<Tin, GFloat16>(eIn, eOut);
11✔
1204
            break;
11✔
1205
        case GDT_Float32:
11✔
1206
            CheckPacked<Tin, float>(eIn, eOut);
11✔
1207
            break;
11✔
1208
        case GDT_Float64:
11✔
1209
            CheckPacked<Tin, double>(eIn, eOut);
11✔
1210
            break;
11✔
1211
        default:
×
1212
            CPLAssert(false);
×
1213
    }
1214
}
121✔
1215

1216
static void CheckPacked(GDALDataType eIn, GDALDataType eOut)
121✔
1217
{
1218
    switch (eIn)
121✔
1219
    {
1220
        case GDT_Byte:
11✔
1221
            CheckPacked<GByte>(eIn, eOut);
11✔
1222
            break;
11✔
1223
        case GDT_Int8:
11✔
1224
            CheckPacked<GInt8>(eIn, eOut);
11✔
1225
            break;
11✔
1226
        case GDT_UInt16:
11✔
1227
            CheckPacked<GUInt16>(eIn, eOut);
11✔
1228
            break;
11✔
1229
        case GDT_Int16:
11✔
1230
            CheckPacked<GInt16>(eIn, eOut);
11✔
1231
            break;
11✔
1232
        case GDT_UInt32:
11✔
1233
            CheckPacked<GUInt32>(eIn, eOut);
11✔
1234
            break;
11✔
1235
        case GDT_Int32:
11✔
1236
            CheckPacked<GInt32>(eIn, eOut);
11✔
1237
            break;
11✔
1238
        case GDT_UInt64:
11✔
1239
            CheckPacked<std::uint64_t>(eIn, eOut);
11✔
1240
            break;
11✔
1241
        case GDT_Int64:
11✔
1242
            CheckPacked<std::int64_t>(eIn, eOut);
11✔
1243
            break;
11✔
1244
        case GDT_Float16:
11✔
1245
            CheckPacked<GFloat16>(eIn, eOut);
11✔
1246
            break;
11✔
1247
        case GDT_Float32:
11✔
1248
            CheckPacked<float>(eIn, eOut);
11✔
1249
            break;
11✔
1250
        case GDT_Float64:
11✔
1251
            CheckPacked<double>(eIn, eOut);
11✔
1252
            break;
11✔
1253
        default:
×
1254
            CPLAssert(false);
×
1255
    }
1256
}
121✔
1257

1258
class TestCopyWordsCheckPackedFixture
1259
    : public TestCopyWords,
1260
      public ::testing::WithParamInterface<
1261
          std::tuple<GDALDataType, GDALDataType>>
1262
{
1263
};
1264

1265
TEST_P(TestCopyWordsCheckPackedFixture, CheckPacked)
243✔
1266
{
1267
    GDALDataType eIn = std::get<0>(GetParam());
121✔
1268
    GDALDataType eOut = std::get<1>(GetParam());
121✔
1269
    CheckPacked(eIn, eOut);
121✔
1270
}
121✔
1271

1272
static std::vector<std::tuple<GDALDataType, GDALDataType>>
1273
GetGDALDataTypeTupleValues()
1✔
1274
{
1275
    std::vector<std::tuple<GDALDataType, GDALDataType>> ret;
1✔
1276
    for (GDALDataType eIn = GDT_Byte; eIn < GDT_TypeCount;
17✔
1277
         eIn = static_cast<GDALDataType>(eIn + 1))
16✔
1278
    {
1279
        if (GDALDataTypeIsComplex(eIn))
16✔
1280
            continue;
5✔
1281
        for (GDALDataType eOut = GDT_Byte; eOut < GDT_TypeCount;
187✔
1282
             eOut = static_cast<GDALDataType>(eOut + 1))
176✔
1283
        {
1284
            if (GDALDataTypeIsComplex(eOut))
176✔
1285
                continue;
55✔
1286
            ret.emplace_back(std::make_tuple(eIn, eOut));
121✔
1287
        }
1288
    }
1289
    return ret;
1✔
1290
}
1291

1292
INSTANTIATE_TEST_SUITE_P(
365✔
1293
    TestCopyWords, TestCopyWordsCheckPackedFixture,
1294
    ::testing::ValuesIn(GetGDALDataTypeTupleValues()),
1295
    [](const ::testing::TestParamInfo<
1296
        TestCopyWordsCheckPackedFixture::ParamType> &l_info)
1297
    {
1298
        GDALDataType eIn = std::get<0>(l_info.param);
1299
        GDALDataType eOut = std::get<1>(l_info.param);
1300
        return std::string(GDALGetDataTypeName(eIn)) + "_" +
1301
               GDALGetDataTypeName(eOut);
1302
    });
1303

1304
TEST_F(TestCopyWords, ByteToByte)
4✔
1305
{
1306
    for (int k = 0; k < 2; k++)
3✔
1307
    {
1308
        if (k == 1)
2✔
1309
            CPLSetConfigOption("GDAL_USE_SSSE3", "NO");
1✔
1310

1311
        for (int spacing = 2; spacing <= 4; spacing++)
8✔
1312
        {
1313
            memset(pIn, 0xff, 256);
6✔
1314
            for (int i = 0; i < 17; i++)
108✔
1315
            {
1316
                pIn[spacing * i] = (GByte)(17 - i);
102✔
1317
            }
1318
            memset(pOut, 0xff, 256);
6✔
1319
            GDALCopyWords(pIn, GDT_Byte, spacing, pOut, GDT_Byte, 1, 17);
6✔
1320
            for (int i = 0; i < 17; i++)
108✔
1321
            {
1322
                AssertRes(GDT_Byte, 17 - i, GDT_Byte, 17 - i, pOut[i],
102✔
1323
                          __LINE__);
1324
            }
1325

1326
            memset(pIn, 0xff, 256);
6✔
1327
            memset(pOut, 0xff, 256);
6✔
1328
            for (int i = 0; i < 17; i++)
108✔
1329
            {
1330
                pIn[i] = (GByte)(17 - i);
102✔
1331
            }
1332
            GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Byte, spacing, 17);
6✔
1333
            for (int i = 0; i < 17; i++)
108✔
1334
            {
1335
                AssertRes(GDT_Byte, 17 - i, GDT_Byte, 17 - i, pOut[i * spacing],
102✔
1336
                          __LINE__);
1337
                for (int j = 1; j < spacing; j++)
306✔
1338
                {
1339
                    AssertRes(GDT_Byte, 0xff, GDT_Byte, 0xff,
204✔
1340
                              pOut[i * spacing + j], __LINE__);
204✔
1341
                }
1342
            }
1343
        }
1344
    }
1345
    CPLSetConfigOption("GDAL_USE_SSSE3", nullptr);
1✔
1346
}
1✔
1347

1348
TEST_F(TestCopyWords, Int16ToInt16)
4✔
1349
{
1350
    memset(pIn, 0xff, 256);
1✔
1351
    GInt16 *pInShort = (GInt16 *)pIn;
1✔
1352
    GInt16 *pOutShort = (GInt16 *)pOut;
1✔
1353
    for (int i = 0; i < 9; i++)
10✔
1354
    {
1355
        pInShort[2 * i + 0] = 0x1234;
9✔
1356
        pInShort[2 * i + 1] = 0x5678;
9✔
1357
    }
1358
    for (int iSpacing = 0; iSpacing < 4; iSpacing++)
5✔
1359
    {
1360
        memset(pOut, 0xff, 256);
4✔
1361
        GDALCopyWords(pInShort, GDT_Int16, sizeof(short), pOutShort, GDT_Int16,
4✔
1362
                      (iSpacing + 1) * sizeof(short), 18);
4✔
1363
        for (int i = 0; i < 9; i++)
40✔
1364
        {
1365
            AssertRes(GDT_Int16, pInShort[2 * i + 0], GDT_Int16,
36✔
1366
                      pInShort[2 * i + 0],
36✔
1367
                      pOutShort[(iSpacing + 1) * (2 * i + 0)], __LINE__);
36✔
1368
            AssertRes(GDT_Int16, pInShort[2 * i + 1], GDT_Int16,
36✔
1369
                      pInShort[2 * i + 1],
36✔
1370
                      pOutShort[(iSpacing + 1) * (2 * i + 1)], __LINE__);
36✔
1371
        }
1372
    }
1373
    for (int iSpacing = 0; iSpacing < 4; iSpacing++)
5✔
1374
    {
1375
        memset(pIn, 0xff, 256);
4✔
1376
        memset(pOut, 0xff, 256);
4✔
1377
        for (int i = 0; i < 9; i++)
40✔
1378
        {
1379
            pInShort[(iSpacing + 1) * (2 * i + 0)] = 0x1234;
36✔
1380
            pInShort[(iSpacing + 1) * (2 * i + 1)] = 0x5678;
36✔
1381
        }
1382
        GDALCopyWords(pInShort, GDT_Int16, (iSpacing + 1) * sizeof(short),
4✔
1383
                      pOutShort, GDT_Int16, sizeof(short), 18);
1384
        for (int i = 0; i < 9; i++)
40✔
1385
        {
1386
            AssertRes(GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 0)],
36✔
1387
                      GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 0)],
36✔
1388
                      pOutShort[2 * i + 0], __LINE__);
36✔
1389
            AssertRes(GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 1)],
36✔
1390
                      GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 1)],
36✔
1391
                      pOutShort[2 * i + 1], __LINE__);
36✔
1392
        }
1393
    }
1394
}
1✔
1395

1396
}  // namespace
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc