LCOV - code coverage report
Current view: top level - autotest/cpp - testcopywords.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 629 638 98.6 %
Date: 2024-05-14 13:00:50 Functions: 435 529 82.2 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * $Id$
       3             :  *
       4             :  * Project:  GDAL Core
       5             :  * Purpose:  Test GDALCopyWords().
       6             :  * Author:   Even Rouault, <even dot rouault at spatialys.com>
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "cpl_conv.h"
      31             : #include "gdal.h"
      32             : 
      33             : #include <cstdint>
      34             : #include <iostream>
      35             : 
      36             : #include "gtest_include.h"
      37             : 
      38             : namespace
      39             : {
      40             : 
      41             : // ---------------------------------------------------------------------------
      42             : 
      43             : template <class OutType, class ConstantType>
      44       15120 : void AssertRes(GDALDataType intype, ConstantType inval, GDALDataType outtype,
      45             :                ConstantType expected_outval, OutType outval, int numLine)
      46             : {
      47       15120 :     EXPECT_NEAR((double)outval, (double)expected_outval, 1.0)
      48           0 :         << "Test failed at line " << numLine
      49           0 :         << " (intype=" << GDALGetDataTypeName(intype)
      50           0 :         << ",inval=" << (double)inval
      51           0 :         << ",outtype=" << GDALGetDataTypeName(outtype) << ",got "
      52           0 :         << (double)outval << " expected  " << expected_outval;
      53       15120 : }
      54             : 
      55             : #define MY_EXPECT(intype, inval, outtype, expected_outval, outval)             \
      56             :     AssertRes(intype, inval, outtype, expected_outval, outval, numLine)
      57             : 
      58             : class TestCopyWords : public ::testing::Test
      59             : {
      60             :   protected:
      61         114 :     void SetUp() override
      62             :     {
      63         114 :         pIn = (GByte *)malloc(256);
      64         114 :         pOut = (GByte *)malloc(256);
      65         114 :     }
      66             : 
      67         114 :     void TearDown() override
      68             :     {
      69             : 
      70         114 :         free(pIn);
      71         114 :         free(pOut);
      72         114 :     }
      73             : 
      74             :     GByte *pIn;
      75             :     GByte *pOut;
      76             : 
      77             :     template <class InType, class OutType, class ConstantType>
      78         774 :     void Test(GDALDataType intype, ConstantType inval, ConstantType invali,
      79             :               GDALDataType outtype, ConstantType outval, ConstantType outvali,
      80             :               int numLine)
      81             :     {
      82         774 :         memset(pIn, 0xff, 128);
      83         774 :         memset(pOut, 0xff, 128);
      84             : 
      85         774 :         *(InType *)(pIn) = (InType)inval;
      86         774 :         *(InType *)(pIn + 32) = (InType)inval;
      87         774 :         if (GDALDataTypeIsComplex(intype))
      88             :         {
      89         180 :             ((InType *)(pIn))[1] = (InType)invali;
      90         180 :             ((InType *)(pIn + 32))[1] = (InType)invali;
      91             :         }
      92             : 
      93             :         /* Test positive offsets */
      94         774 :         GDALCopyWords(pIn, intype, 32, pOut, outtype, 32, 2);
      95             : 
      96             :         /* Test negative offsets */
      97         774 :         GDALCopyWords(pIn + 32, intype, -32, pOut + 128 - 16, outtype, -32, 2);
      98             : 
      99         774 :         MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut));
     100         774 :         MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut + 32));
     101         774 :         MY_EXPECT(intype, inval, outtype, outval,
     102             :                   *(OutType *)(pOut + 128 - 16));
     103         774 :         MY_EXPECT(intype, inval, outtype, outval,
     104             :                   *(OutType *)(pOut + 128 - 16 - 32));
     105             : 
     106         774 :         if (GDALDataTypeIsComplex(outtype))
     107             :         {
     108         224 :             MY_EXPECT(intype, invali, outtype, outvali, ((OutType *)(pOut))[1]);
     109         224 :             MY_EXPECT(intype, invali, outtype, outvali,
     110             :                       ((OutType *)(pOut + 32))[1]);
     111             : 
     112         224 :             MY_EXPECT(intype, invali, outtype, outvali,
     113             :                       ((OutType *)(pOut + 128 - 16))[1]);
     114         224 :             MY_EXPECT(intype, invali, outtype, outvali,
     115             :                       ((OutType *)(pOut + 128 - 16 - 32))[1]);
     116             :         }
     117             :         else
     118             :         {
     119         550 :             *(InType *)(pIn + GDALGetDataTypeSize(intype) / 8) = (InType)inval;
     120             :             /* Test packed offsets */
     121         550 :             GDALCopyWords(pIn, intype, GDALGetDataTypeSize(intype) / 8, pOut,
     122         550 :                           outtype, GDALGetDataTypeSize(outtype) / 8, 2);
     123             : 
     124         550 :             MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut));
     125         550 :             MY_EXPECT(intype, inval, outtype, outval,
     126             :                       *(OutType *)(pOut + GDALGetDataTypeSize(outtype) / 8));
     127             : 
     128         679 :             *(InType *)(pIn + 2 * GDALGetDataTypeSize(intype) / 8) =
     129         129 :                 (InType)inval;
     130         679 :             *(InType *)(pIn + 3 * GDALGetDataTypeSize(intype) / 8) =
     131         129 :                 (InType)inval;
     132             :             /* Test packed offsets */
     133         550 :             GDALCopyWords(pIn, intype, GDALGetDataTypeSize(intype) / 8, pOut,
     134         550 :                           outtype, GDALGetDataTypeSize(outtype) / 8, 4);
     135             : 
     136         550 :             MY_EXPECT(intype, inval, outtype, outval, *(OutType *)(pOut));
     137         550 :             MY_EXPECT(intype, inval, outtype, outval,
     138             :                       *(OutType *)(pOut + GDALGetDataTypeSize(outtype) / 8));
     139         550 :             MY_EXPECT(
     140             :                 intype, inval, outtype, outval,
     141             :                 *(OutType *)(pOut + 2 * GDALGetDataTypeSize(outtype) / 8));
     142         550 :             MY_EXPECT(
     143             :                 intype, inval, outtype, outval,
     144             :                 *(OutType *)(pOut + 3 * GDALGetDataTypeSize(outtype) / 8));
     145             :         }
     146         774 :     }
     147             : 
     148             :     template <class InType, class ConstantType>
     149         774 :     void FromR_2(GDALDataType intype, ConstantType inval, ConstantType invali,
     150             :                  GDALDataType outtype, ConstantType outval,
     151             :                  ConstantType outvali, int numLine)
     152             :     {
     153         774 :         if (outtype == GDT_Byte)
     154          62 :             Test<InType, GByte, ConstantType>(intype, inval, invali, outtype,
     155             :                                               outval, outvali, numLine);
     156         712 :         else if (outtype == GDT_Int8)
     157          46 :             Test<InType, GInt8, ConstantType>(intype, inval, invali, outtype,
     158             :                                               outval, outvali, numLine);
     159         666 :         else if (outtype == GDT_Int16)
     160          72 :             Test<InType, GInt16, ConstantType>(intype, inval, invali, outtype,
     161             :                                                outval, outvali, numLine);
     162         594 :         else if (outtype == GDT_UInt16)
     163          58 :             Test<InType, GUInt16, ConstantType>(intype, inval, invali, outtype,
     164             :                                                 outval, outvali, numLine);
     165         536 :         else if (outtype == GDT_Int32)
     166          68 :             Test<InType, GInt32, ConstantType>(intype, inval, invali, outtype,
     167             :                                                outval, outvali, numLine);
     168         468 :         else if (outtype == GDT_UInt32)
     169          54 :             Test<InType, GUInt32, ConstantType>(intype, inval, invali, outtype,
     170             :                                                 outval, outvali, numLine);
     171         414 :         else if (outtype == GDT_Int64)
     172          56 :             Test<InType, std::int64_t, ConstantType>(
     173             :                 intype, inval, invali, outtype, outval, outvali, numLine);
     174         358 :         else if (outtype == GDT_UInt64)
     175          42 :             Test<InType, std::uint64_t, ConstantType>(
     176             :                 intype, inval, invali, outtype, outval, outvali, numLine);
     177         316 :         else if (outtype == GDT_Float32)
     178          46 :             Test<InType, float, ConstantType>(intype, inval, invali, outtype,
     179             :                                               outval, outvali, numLine);
     180         270 :         else if (outtype == GDT_Float64)
     181          46 :             Test<InType, double, ConstantType>(intype, inval, invali, outtype,
     182             :                                                outval, outvali, numLine);
     183         224 :         else if (outtype == GDT_CInt16)
     184          68 :             Test<InType, GInt16, ConstantType>(intype, inval, invali, outtype,
     185             :                                                outval, outvali, numLine);
     186         156 :         else if (outtype == GDT_CInt32)
     187          68 :             Test<InType, GInt32, ConstantType>(intype, inval, invali, outtype,
     188             :                                                outval, outvali, numLine);
     189          88 :         else if (outtype == GDT_CFloat32)
     190          44 :             Test<InType, float, ConstantType>(intype, inval, invali, outtype,
     191             :                                               outval, outvali, numLine);
     192          44 :         else if (outtype == GDT_CFloat64)
     193          44 :             Test<InType, double, ConstantType>(intype, inval, invali, outtype,
     194             :                                                outval, outvali, numLine);
     195         774 :     }
     196             : 
     197             :     template <class ConstantType>
     198         774 :     void FromR(GDALDataType intype, ConstantType inval, ConstantType invali,
     199             :                GDALDataType outtype, ConstantType outval, ConstantType outvali,
     200             :                int numLine)
     201             :     {
     202         774 :         if (intype == GDT_Byte)
     203          41 :             FromR_2<GByte, ConstantType>(intype, inval, invali, outtype, outval,
     204             :                                          outvali, numLine);
     205         733 :         else if (intype == GDT_Int8)
     206          42 :             FromR_2<GInt8, ConstantType>(intype, inval, invali, outtype, outval,
     207             :                                          outvali, numLine);
     208         691 :         else if (intype == GDT_Int16)
     209          40 :             FromR_2<GInt16, ConstantType>(intype, inval, invali, outtype,
     210             :                                           outval, outvali, numLine);
     211         651 :         else if (intype == GDT_UInt16)
     212          41 :             FromR_2<GUInt16, ConstantType>(intype, inval, invali, outtype,
     213             :                                            outval, outvali, numLine);
     214         610 :         else if (intype == GDT_Int32)
     215          43 :             FromR_2<GInt32, ConstantType>(intype, inval, invali, outtype,
     216             :                                           outval, outvali, numLine);
     217         567 :         else if (intype == GDT_UInt32)
     218          41 :             FromR_2<GUInt32, ConstantType>(intype, inval, invali, outtype,
     219             :                                            outval, outvali, numLine);
     220         526 :         else if (intype == GDT_Int64)
     221          37 :             FromR_2<std::int64_t, ConstantType>(intype, inval, invali, outtype,
     222             :                                                 outval, outvali, numLine);
     223         489 :         else if (intype == GDT_UInt64)
     224          41 :             FromR_2<std::uint64_t, ConstantType>(intype, inval, invali, outtype,
     225             :                                                  outval, outvali, numLine);
     226         448 :         else if (intype == GDT_Float32)
     227         134 :             FromR_2<float, ConstantType>(intype, inval, invali, outtype, outval,
     228             :                                          outvali, numLine);
     229         314 :         else if (intype == GDT_Float64)
     230         134 :             FromR_2<double, ConstantType>(intype, inval, invali, outtype,
     231             :                                           outval, outvali, numLine);
     232         180 :         else if (intype == GDT_CInt16)
     233          36 :             FromR_2<GInt16, ConstantType>(intype, inval, invali, outtype,
     234             :                                           outval, outvali, numLine);
     235         144 :         else if (intype == GDT_CInt32)
     236          36 :             FromR_2<GInt32, ConstantType>(intype, inval, invali, outtype,
     237             :                                           outval, outvali, numLine);
     238         108 :         else if (intype == GDT_CFloat32)
     239          54 :             FromR_2<float, ConstantType>(intype, inval, invali, outtype, outval,
     240             :                                          outvali, numLine);
     241          54 :         else if (intype == GDT_CFloat64)
     242          54 :             FromR_2<double, ConstantType>(intype, inval, invali, outtype,
     243             :                                           outval, outvali, numLine);
     244         774 :     }
     245             : };
     246             : 
     247             : #define FROM_R(intype, inval, outtype, outval)                                 \
     248             :     FromR<GIntBig>(intype, inval, 0, outtype, outval, 0, __LINE__)
     249             : #define FROM_R_F(intype, inval, outtype, outval)                               \
     250             :     FromR<double>(intype, inval, 0, outtype, outval, 0, __LINE__)
     251             : 
     252             : #define FROM_C(intype, inval, invali, outtype, outval, outvali)                \
     253             :     FromR<GIntBig>(intype, inval, invali, outtype, outval, outvali, __LINE__)
     254             : #define FROM_C_F(intype, inval, invali, outtype, outval, outvali)              \
     255             :     FromR<double>(intype, inval, invali, outtype, outval, outvali, __LINE__)
     256             : 
     257             : #define IS_UNSIGNED(x)                                                         \
     258             :     (x == GDT_Byte || x == GDT_UInt16 || x == GDT_UInt32 || x == GDT_UInt64)
     259             : #define IS_FLOAT(x)                                                            \
     260             :     (x == GDT_Float32 || x == GDT_Float64 || x == GDT_CFloat32 ||              \
     261             :      x == GDT_CFloat64)
     262             : 
     263             : #define CST_3000000000 (((GIntBig)3000) * 1000 * 1000)
     264             : #define CST_5000000000 (((GIntBig)5000) * 1000 * 1000)
     265             : 
     266           4 : TEST_F(TestCopyWords, GDT_Byte)
     267             : {
     268             :     /* GDT_Byte */
     269          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     270          14 :          outtype = (GDALDataType)(outtype + 1))
     271             :     {
     272          14 :         FROM_R(GDT_Byte, 0, outtype, 0);
     273          14 :         FROM_R(GDT_Byte, 127, outtype, 127);
     274          14 :         if (outtype != GDT_Int8)
     275          13 :             FROM_R(GDT_Byte, 255, outtype, 255);
     276             :     }
     277             : 
     278          18 :     for (int i = 0; i < 17; i++)
     279             :     {
     280          17 :         pIn[i] = (GByte)i;
     281             :     }
     282             : 
     283           1 :     memset(pOut, 0xff, 128);
     284           1 :     GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Int32, 4, 17);
     285          18 :     for (int i = 0; i < 17; i++)
     286             :     {
     287          17 :         AssertRes(GDT_Byte, i, GDT_Int32, i, ((int *)pOut)[i], __LINE__);
     288             :     }
     289             : 
     290           1 :     memset(pOut, 0xff, 128);
     291           1 :     GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Float32, 4, 17);
     292          18 :     for (int i = 0; i < 17; i++)
     293             :     {
     294          17 :         AssertRes(GDT_Byte, i, GDT_Float32, i, ((float *)pOut)[i], __LINE__);
     295             :     }
     296           1 : }
     297             : 
     298           4 : TEST_F(TestCopyWords, GDT_Int8)
     299             : {
     300             :     /* GDT_Int8 */
     301           1 :     FROM_R(GDT_Int8, -128, GDT_Byte, 0);    /* clamp */
     302           1 :     FROM_R(GDT_Int8, -128, GDT_Int8, -128); /* clamp */
     303           1 :     FROM_R(GDT_Int8, -128, GDT_Int16, -128);
     304           1 :     FROM_R(GDT_Int8, -128, GDT_UInt16, 0); /* clamp */
     305           1 :     FROM_R(GDT_Int8, -128, GDT_Int32, -128);
     306           1 :     FROM_R(GDT_Int8, -128, GDT_UInt32, 0); /* clamp */
     307           1 :     FROM_R(GDT_Int8, -128, GDT_Int64, -128);
     308           1 :     FROM_R(GDT_Int8, -128, GDT_UInt64, 0); /* clamp */
     309           1 :     FROM_R(GDT_Int8, -128, GDT_Float32, -128);
     310           1 :     FROM_R(GDT_Int8, -128, GDT_Float64, -128);
     311           1 :     FROM_R(GDT_Int8, -128, GDT_CInt16, -128);
     312           1 :     FROM_R(GDT_Int8, -128, GDT_CInt32, -128);
     313           1 :     FROM_R(GDT_Int8, -128, GDT_CFloat32, -128);
     314           1 :     FROM_R(GDT_Int8, -128, GDT_CFloat64, -128);
     315          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     316          14 :          outtype = (GDALDataType)(outtype + 1))
     317             :     {
     318          14 :         FROM_R(GDT_Int8, 127, outtype, 127);
     319             :     }
     320             : 
     321           1 :     FROM_R(GDT_Int8, 127, GDT_Byte, 127);
     322           1 :     FROM_R(GDT_Int8, 127, GDT_Int8, 127);
     323           1 :     FROM_R(GDT_Int8, 127, GDT_Int16, 127);
     324           1 :     FROM_R(GDT_Int8, 127, GDT_UInt16, 127);
     325           1 :     FROM_R(GDT_Int8, 127, GDT_Int32, 127);
     326           1 :     FROM_R(GDT_Int8, 127, GDT_UInt32, 127);
     327           1 :     FROM_R(GDT_Int8, 127, GDT_Int64, 127);
     328           1 :     FROM_R(GDT_Int8, 127, GDT_UInt64, 127);
     329           1 :     FROM_R(GDT_Int8, 127, GDT_Float32, 127);
     330           1 :     FROM_R(GDT_Int8, 127, GDT_Float64, 127);
     331           1 :     FROM_R(GDT_Int8, 127, GDT_CInt16, 127);
     332           1 :     FROM_R(GDT_Int8, 127, GDT_CInt32, 127);
     333           1 :     FROM_R(GDT_Int8, 127, GDT_CFloat32, 127);
     334           1 :     FROM_R(GDT_Int8, 127, GDT_CFloat64, 127);
     335           1 : }
     336             : 
     337           4 : TEST_F(TestCopyWords, GDT_Int16)
     338             : {
     339             :     /* GDT_Int16 */
     340           1 :     FROM_R(GDT_Int16, -32000, GDT_Byte, 0); /* clamp */
     341           1 :     FROM_R(GDT_Int16, -32000, GDT_Int16, -32000);
     342           1 :     FROM_R(GDT_Int16, -32000, GDT_UInt16, 0); /* clamp */
     343           1 :     FROM_R(GDT_Int16, -32000, GDT_Int32, -32000);
     344           1 :     FROM_R(GDT_Int16, -32000, GDT_UInt32, 0); /* clamp */
     345           1 :     FROM_R(GDT_Int16, -32000, GDT_Int64, -32000);
     346           1 :     FROM_R(GDT_Int16, -32000, GDT_UInt64, 0); /* clamp */
     347           1 :     FROM_R(GDT_Int16, -32000, GDT_Float32, -32000);
     348           1 :     FROM_R(GDT_Int16, -32000, GDT_Float64, -32000);
     349           1 :     FROM_R(GDT_Int16, -32000, GDT_CInt16, -32000);
     350           1 :     FROM_R(GDT_Int16, -32000, GDT_CInt32, -32000);
     351           1 :     FROM_R(GDT_Int16, -32000, GDT_CFloat32, -32000);
     352           1 :     FROM_R(GDT_Int16, -32000, GDT_CFloat64, -32000);
     353          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     354          14 :          outtype = (GDALDataType)(outtype + 1))
     355             :     {
     356          14 :         FROM_R(GDT_Int16, 127, outtype, 127);
     357             :     }
     358             : 
     359           1 :     FROM_R(GDT_Int16, 32000, GDT_Byte, 255); /* clamp */
     360           1 :     FROM_R(GDT_Int16, 32000, GDT_Int16, 32000);
     361           1 :     FROM_R(GDT_Int16, 32000, GDT_UInt16, 32000);
     362           1 :     FROM_R(GDT_Int16, 32000, GDT_Int32, 32000);
     363           1 :     FROM_R(GDT_Int16, 32000, GDT_UInt32, 32000);
     364           1 :     FROM_R(GDT_Int16, 32000, GDT_Int64, 32000);
     365           1 :     FROM_R(GDT_Int16, 32000, GDT_UInt64, 32000);
     366           1 :     FROM_R(GDT_Int16, 32000, GDT_Float32, 32000);
     367           1 :     FROM_R(GDT_Int16, 32000, GDT_Float64, 32000);
     368           1 :     FROM_R(GDT_Int16, 32000, GDT_CInt16, 32000);
     369           1 :     FROM_R(GDT_Int16, 32000, GDT_CInt32, 32000);
     370           1 :     FROM_R(GDT_Int16, 32000, GDT_CFloat32, 32000);
     371           1 :     FROM_R(GDT_Int16, 32000, GDT_CFloat64, 32000);
     372           1 : }
     373             : 
     374           4 : TEST_F(TestCopyWords, GDT_UInt16)
     375             : {
     376             :     /* GDT_UInt16 */
     377          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     378          14 :          outtype = (GDALDataType)(outtype + 1))
     379             :     {
     380          14 :         FROM_R(GDT_UInt16, 0, outtype, 0);
     381          14 :         FROM_R(GDT_UInt16, 127, outtype, 127);
     382             :     }
     383             : 
     384           1 :     FROM_R(GDT_UInt16, 65000, GDT_Byte, 255);    /* clamp */
     385           1 :     FROM_R(GDT_UInt16, 65000, GDT_Int16, 32767); /* clamp */
     386           1 :     FROM_R(GDT_UInt16, 65000, GDT_UInt16, 65000);
     387           1 :     FROM_R(GDT_UInt16, 65000, GDT_Int32, 65000);
     388           1 :     FROM_R(GDT_UInt16, 65000, GDT_UInt32, 65000);
     389           1 :     FROM_R(GDT_UInt16, 65000, GDT_Int64, 65000);
     390           1 :     FROM_R(GDT_UInt16, 65000, GDT_UInt64, 65000);
     391           1 :     FROM_R(GDT_UInt16, 65000, GDT_Float32, 65000);
     392           1 :     FROM_R(GDT_UInt16, 65000, GDT_Float64, 65000);
     393           1 :     FROM_R(GDT_UInt16, 65000, GDT_CInt16, 32767); /* clamp */
     394           1 :     FROM_R(GDT_UInt16, 65000, GDT_CInt32, 65000);
     395           1 :     FROM_R(GDT_UInt16, 65000, GDT_CFloat32, 65000);
     396           1 :     FROM_R(GDT_UInt16, 65000, GDT_CFloat64, 65000);
     397           1 : }
     398             : 
     399           4 : TEST_F(TestCopyWords, GDT_Int32)
     400             : {
     401             :     /* GDT_Int32 */
     402           1 :     FROM_R(GDT_Int32, -33000, GDT_Byte, 0);       /* clamp */
     403           1 :     FROM_R(GDT_Int32, -33000, GDT_Int16, -32768); /* clamp */
     404           1 :     FROM_R(GDT_Int32, -33000, GDT_UInt16, 0);     /* clamp */
     405           1 :     FROM_R(GDT_Int32, -33000, GDT_Int32, -33000);
     406           1 :     FROM_R(GDT_Int32, -33000, GDT_UInt32, 0); /* clamp */
     407           1 :     FROM_R(GDT_Int32, -33000, GDT_Int64, -33000);
     408           1 :     FROM_R(GDT_Int32, -33000, GDT_UInt64, 0); /* clamp */
     409           1 :     FROM_R(GDT_Int32, -33000, GDT_Float32, -33000);
     410           1 :     FROM_R(GDT_Int32, -33000, GDT_Float64, -33000);
     411           1 :     FROM_R(GDT_Int32, -33000, GDT_CInt16, -32768); /* clamp */
     412           1 :     FROM_R(GDT_Int32, -33000, GDT_CInt32, -33000);
     413           1 :     FROM_R(GDT_Int32, -33000, GDT_CFloat32, -33000);
     414           1 :     FROM_R(GDT_Int32, -33000, GDT_CFloat64, -33000);
     415          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     416          14 :          outtype = (GDALDataType)(outtype + 1))
     417             :     {
     418          14 :         FROM_R(GDT_Int32, 127, outtype, 127);
     419             :     }
     420             : 
     421           1 :     FROM_R(GDT_Int32, 67000, GDT_Byte, 255);     /* clamp */
     422           1 :     FROM_R(GDT_Int32, 67000, GDT_Int16, 32767);  /* clamp */
     423           1 :     FROM_R(GDT_Int32, 67000, GDT_UInt16, 65535); /* clamp */
     424           1 :     FROM_R(GDT_Int32, 67000, GDT_Int32, 67000);
     425           1 :     FROM_R(GDT_Int32, 67000, GDT_UInt32, 67000);
     426           1 :     FROM_R(GDT_Int32, 67000, GDT_Int64, 67000);
     427           1 :     FROM_R(GDT_Int32, 67000, GDT_UInt64, 67000);
     428           1 :     FROM_R(GDT_Int32, 67000, GDT_Float32, 67000);
     429           1 :     FROM_R(GDT_Int32, 67000, GDT_Float64, 67000);
     430           1 :     FROM_R(GDT_Int32, 67000, GDT_CInt16, 32767); /* clamp */
     431           1 :     FROM_R(GDT_Int32, 67000, GDT_CInt32, 67000);
     432           1 :     FROM_R(GDT_Int32, 67000, GDT_CFloat32, 67000);
     433           1 :     FROM_R(GDT_Int32, 67000, GDT_CFloat64, 67000);
     434           1 : }
     435             : 
     436           4 : TEST_F(TestCopyWords, GDT_UInt32)
     437             : {
     438             :     /* GDT_UInt32 */
     439          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     440          14 :          outtype = (GDALDataType)(outtype + 1))
     441             :     {
     442          14 :         FROM_R(GDT_UInt32, 0, outtype, 0);
     443          14 :         FROM_R(GDT_UInt32, 127, outtype, 127);
     444             :     }
     445             : 
     446           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Byte, 255);         /* clamp */
     447           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Int16, 32767);      /* clamp */
     448           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_UInt16, 65535);     /* clamp */
     449           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Int32, 2147483647); /* clamp */
     450           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_UInt32, 3000000000U);
     451           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Int64, 3000000000U);
     452           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_UInt64, 3000000000U);
     453           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Float32, 3000000000U);
     454           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_Float64, 3000000000U);
     455           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_CInt16, 32767);      /* clamp */
     456           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_CInt32, 2147483647); /* clamp */
     457           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_CFloat32, 3000000000U);
     458           1 :     FROM_R(GDT_UInt32, 3000000000U, GDT_CFloat64, 3000000000U);
     459           1 : }
     460             : 
     461           4 : TEST_F(TestCopyWords, check_GDT_Int64)
     462             : {
     463             :     /* GDT_Int64 */
     464           1 :     FROM_R(GDT_Int64, -33000, GDT_Byte, 0);       /* clamp */
     465           1 :     FROM_R(GDT_Int64, -33000, GDT_Int16, -32768); /* clamp */
     466           1 :     FROM_R(GDT_Int64, -33000, GDT_UInt16, 0);     /* clamp */
     467           1 :     FROM_R(GDT_Int64, -33000, GDT_Int32, -33000);
     468           1 :     FROM_R(GDT_Int64, -33000, GDT_UInt32, 0); /* clamp */
     469           1 :     FROM_R(GDT_Int64, -33000, GDT_Int64, -33000);
     470           1 :     FROM_R(GDT_Int64, -33000, GDT_UInt64, 0); /* clamp */
     471           1 :     FROM_R(GDT_Int64, -33000, GDT_Float32, -33000);
     472           1 :     FROM_R(GDT_Int64, -33000, GDT_Float64, -33000);
     473           1 :     FROM_R(GDT_Int64, -33000, GDT_CInt16, -32768); /* clamp */
     474           1 :     FROM_R(GDT_Int64, -33000, GDT_CInt32, -33000);
     475           1 :     FROM_R(GDT_Int64, -33000, GDT_CFloat32, -33000);
     476           1 :     FROM_R(GDT_Int64, -33000, GDT_CFloat64, -33000);
     477          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     478          14 :          outtype = (GDALDataType)(outtype + 1))
     479             :     {
     480          14 :         FROM_R(GDT_Int64, 127, outtype, 127);
     481             :     }
     482             : 
     483           1 :     FROM_R(GDT_Int64, 67000, GDT_Byte, 255);     /* clamp */
     484           1 :     FROM_R(GDT_Int64, 67000, GDT_Int16, 32767);  /* clamp */
     485           1 :     FROM_R(GDT_Int32, 67000, GDT_UInt16, 65535); /* clamp */
     486           1 :     FROM_R(GDT_Int32, 67000, GDT_Int32, 67000);
     487           1 :     FROM_R(GDT_Int64, 67000, GDT_UInt32, 67000);
     488           1 :     FROM_R(GDT_Int32, 67000, GDT_Int64, 67000);
     489           1 :     FROM_R(GDT_Int64, 67000, GDT_UInt64, 67000);
     490           1 :     FROM_R(GDT_Int64, 67000, GDT_Float32, 67000);
     491           1 :     FROM_R(GDT_Int64, 67000, GDT_Float64, 67000);
     492           1 :     FROM_R(GDT_Int64, 67000, GDT_CInt16, 32767); /* clamp */
     493           1 :     FROM_R(GDT_Int64, 67000, GDT_CInt32, 67000);
     494           1 :     FROM_R(GDT_Int64, 67000, GDT_CFloat32, 67000);
     495           1 :     FROM_R(GDT_Int64, 67000, GDT_CFloat64, 67000);
     496           1 : }
     497             : 
     498           4 : TEST_F(TestCopyWords, GDT_UInt64)
     499             : {
     500             :     /* GDT_UInt64 */
     501          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     502          14 :          outtype = (GDALDataType)(outtype + 1))
     503             :     {
     504          14 :         FROM_R(GDT_UInt64, 0, outtype, 0);
     505          14 :         FROM_R(GDT_UInt64, 127, outtype, 127);
     506             :     }
     507             : 
     508           1 :     std::uint64_t nVal = static_cast<std::uint64_t>(3000000000) * 1000;
     509           1 :     FROM_R(GDT_UInt64, nVal, GDT_Byte, 255);           /* clamp */
     510           1 :     FROM_R(GDT_UInt64, nVal, GDT_Int16, 32767);        /* clamp */
     511           1 :     FROM_R(GDT_UInt64, nVal, GDT_UInt16, 65535);       /* clamp */
     512           1 :     FROM_R(GDT_UInt64, nVal, GDT_Int32, 2147483647);   /* clamp */
     513           1 :     FROM_R(GDT_UInt64, nVal, GDT_UInt32, 4294967295U); /* clamp */
     514           1 :     FROM_R(GDT_UInt64, nVal, GDT_Int64, nVal);
     515           1 :     FROM_R(GDT_UInt64, nVal, GDT_UInt64, nVal);
     516           1 :     FROM_R(GDT_UInt64, nVal, GDT_Float32,
     517             :            static_cast<uint64_t>(static_cast<float>(nVal)));
     518           1 :     FROM_R(GDT_UInt64, nVal, GDT_Float64, nVal);
     519           1 :     FROM_R(GDT_UInt64, nVal, GDT_CInt16, 32767);      /* clamp */
     520           1 :     FROM_R(GDT_UInt64, nVal, GDT_CInt32, 2147483647); /* clamp */
     521           1 :     FROM_R(GDT_UInt64, nVal, GDT_CFloat32,
     522             :            static_cast<uint64_t>(static_cast<float>(nVal)));
     523           1 :     FROM_R(GDT_UInt64, nVal, GDT_CFloat64, nVal);
     524           1 : }
     525             : 
     526           4 : TEST_F(TestCopyWords, GDT_Float32and64)
     527             : {
     528             :     /* GDT_Float32 and GDT_Float64 */
     529           3 :     for (int i = 0; i < 2; i++)
     530             :     {
     531           2 :         GDALDataType intype = (i == 0) ? GDT_Float32 : GDT_Float64;
     532          30 :         for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     533          28 :              outtype = (GDALDataType)(outtype + 1))
     534             :         {
     535          28 :             if (IS_FLOAT(outtype))
     536             :             {
     537           8 :                 FROM_R_F(intype, 127.1, outtype, 127.1);
     538           8 :                 FROM_R_F(intype, -127.1, outtype, -127.1);
     539             :             }
     540             :             else
     541             :             {
     542          20 :                 FROM_R_F(intype, 125.1, outtype, 125);
     543          20 :                 FROM_R_F(intype, 125.9, outtype, 126);
     544             : 
     545          20 :                 FROM_R_F(intype, 0.4, outtype, 0);
     546          20 :                 FROM_R_F(intype, 0.5, outtype,
     547             :                          1); /* We could argue how to do this rounding */
     548          20 :                 FROM_R_F(intype, 0.6, outtype, 1);
     549          20 :                 FROM_R_F(intype, 126.5, outtype,
     550             :                          127); /* We could argue how to do this rounding */
     551             : 
     552          20 :                 if (!IS_UNSIGNED(outtype))
     553             :                 {
     554          12 :                     FROM_R_F(intype, -125.9, outtype, -126);
     555          12 :                     FROM_R_F(intype, -127.1, outtype, -127);
     556             : 
     557          12 :                     FROM_R_F(intype, -0.4, outtype, 0);
     558          12 :                     FROM_R_F(intype, -0.5, outtype,
     559             :                              -1); /* We could argue how to do this rounding */
     560          12 :                     FROM_R_F(intype, -0.6, outtype, -1);
     561          12 :                     FROM_R_F(intype, -127.5, outtype,
     562             :                              -128); /* We could argue how to do this rounding */
     563             :                 }
     564             :             }
     565             :         }
     566           2 :         FROM_R(intype, -CST_3000000000, GDT_Byte, 0);
     567           2 :         FROM_R(intype, -32768, GDT_Byte, 0);
     568           2 :         FROM_R(intype, -1, GDT_Byte, 0);
     569           2 :         FROM_R(intype, 256, GDT_Byte, 255);
     570           2 :         FROM_R(intype, 65536, GDT_Byte, 255);
     571           2 :         FROM_R(intype, CST_3000000000, GDT_Byte, 255);
     572           2 :         FROM_R(intype, -CST_3000000000, GDT_Int16, -32768);
     573           2 :         FROM_R(intype, -33000, GDT_Int16, -32768);
     574           2 :         FROM_R(intype, 33000, GDT_Int16, 32767);
     575           2 :         FROM_R(intype, CST_3000000000, GDT_Int16, 32767);
     576           2 :         FROM_R(intype, -CST_3000000000, GDT_UInt16, 0);
     577           2 :         FROM_R(intype, -1, GDT_UInt16, 0);
     578           2 :         FROM_R(intype, 66000, GDT_UInt16, 65535);
     579           2 :         FROM_R(intype, CST_3000000000, GDT_UInt16, 65535);
     580           2 :         FROM_R(intype, -CST_3000000000, GDT_Int32, INT_MIN);
     581           2 :         FROM_R(intype, CST_3000000000, GDT_Int32, 2147483647);
     582           2 :         FROM_R(intype, -1, GDT_UInt32, 0);
     583           2 :         FROM_R(intype, CST_5000000000, GDT_UInt32, 4294967295UL);
     584           2 :         FROM_R(intype, CST_5000000000, GDT_Float32, CST_5000000000);
     585           2 :         FROM_R(intype, -CST_5000000000, GDT_Float32, -CST_5000000000);
     586           2 :         FROM_R(intype, CST_5000000000, GDT_Float64, CST_5000000000);
     587           2 :         FROM_R(intype, -CST_5000000000, GDT_Float64, -CST_5000000000);
     588           2 :         FROM_R(intype, -33000, GDT_CInt16, -32768);
     589           2 :         FROM_R(intype, 33000, GDT_CInt16, 32767);
     590           2 :         FROM_R(intype, -CST_3000000000, GDT_CInt32, INT_MIN);
     591           2 :         FROM_R(intype, CST_3000000000, GDT_CInt32, 2147483647);
     592           2 :         FROM_R(intype, CST_5000000000, GDT_CFloat32, CST_5000000000);
     593           2 :         FROM_R(intype, -CST_5000000000, GDT_CFloat32, -CST_5000000000);
     594           2 :         FROM_R(intype, CST_5000000000, GDT_CFloat64, CST_5000000000);
     595           2 :         FROM_R(intype, -CST_5000000000, GDT_CFloat64, -CST_5000000000);
     596             :     }
     597           1 : }
     598             : 
     599           4 : TEST_F(TestCopyWords, GDT_CInt16)
     600             : {
     601             :     /* GDT_CInt16 */
     602           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_Byte, 0, 0); /* clamp */
     603           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_Int16, -32000, 0);
     604           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_UInt16, 0, 0); /* clamp */
     605           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_Int32, -32000, 0);
     606           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_UInt32, 0, 0); /* clamp */
     607           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_Float32, -32000, 0);
     608           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_Float64, -32000, 0);
     609           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_CInt16, -32000, -32500);
     610           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_CInt32, -32000, -32500);
     611           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_CFloat32, -32000, -32500);
     612           1 :     FROM_C(GDT_CInt16, -32000, -32500, GDT_CFloat64, -32000, -32500);
     613          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     614          14 :          outtype = (GDALDataType)(outtype + 1))
     615             :     {
     616          14 :         FROM_C(GDT_CInt16, 127, 128, outtype, 127, 128);
     617             :     }
     618             : 
     619           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_Byte, 255, 0); /* clamp */
     620           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_Int16, 32000, 0);
     621           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_UInt16, 32000, 0);
     622           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_Int32, 32000, 0);
     623           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_UInt32, 32000, 0);
     624           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_Float32, 32000, 0);
     625           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_Float64, 32000, 0);
     626           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_CInt16, 32000, 32500);
     627           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_CInt32, 32000, 32500);
     628           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_CFloat32, 32000, 32500);
     629           1 :     FROM_C(GDT_CInt16, 32000, 32500, GDT_CFloat64, 32000, 32500);
     630           1 : }
     631             : 
     632           4 : TEST_F(TestCopyWords, GDT_CInt32)
     633             : {
     634             :     /* GDT_CInt32 */
     635           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_Byte, 0, 0);       /* clamp */
     636           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_Int16, -32768, 0); /* clamp */
     637           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_UInt16, 0, 0);     /* clamp */
     638           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_Int32, -33000, 0);
     639           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_UInt32, 0, 0); /* clamp */
     640           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_Float32, -33000, 0);
     641           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_Float64, -33000, 0);
     642           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_CInt16, -32768, -32768); /* clamp */
     643           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_CInt32, -33000, -33500);
     644           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_CFloat32, -33000, -33500);
     645           1 :     FROM_C(GDT_CInt32, -33000, -33500, GDT_CFloat64, -33000, -33500);
     646          15 :     for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     647          14 :          outtype = (GDALDataType)(outtype + 1))
     648             :     {
     649          14 :         FROM_C(GDT_CInt32, 127, 128, outtype, 127, 128);
     650             :     }
     651             : 
     652           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_Byte, 255, 0);     /* clamp */
     653           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_Int16, 32767, 0);  /* clamp */
     654           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_UInt16, 65535, 0); /* clamp */
     655           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_Int32, 67000, 0);
     656           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_UInt32, 67000, 0);
     657           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_Float32, 67000, 0);
     658           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_Float64, 67000, 0);
     659           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_CInt16, 32767, 32767); /* clamp */
     660           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_CInt32, 67000, 67500);
     661           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_CFloat32, 67000, 67500);
     662           1 :     FROM_C(GDT_CInt32, 67000, 67500, GDT_CFloat64, 67000, 67500);
     663           1 : }
     664             : 
     665           4 : TEST_F(TestCopyWords, GDT_CFloat32and64)
     666             : {
     667             :     /* GDT_CFloat32 and GDT_CFloat64 */
     668           3 :     for (int i = 0; i < 2; i++)
     669             :     {
     670           2 :         GDALDataType intype = (i == 0) ? GDT_CFloat32 : GDT_CFloat64;
     671          30 :         for (GDALDataType outtype = GDT_Byte; outtype < GDT_TypeCount;
     672          28 :              outtype = (GDALDataType)(outtype + 1))
     673             :         {
     674          28 :             if (IS_FLOAT(outtype))
     675             :             {
     676           8 :                 FROM_C_F(intype, 127.1, 127.9, outtype, 127.1, 127.9);
     677           8 :                 FROM_C_F(intype, -127.1, -127.9, outtype, -127.1, -127.9);
     678             :             }
     679             :             else
     680             :             {
     681          20 :                 FROM_C_F(intype, 126.1, 150.9, outtype, 126, 151);
     682          20 :                 FROM_C_F(intype, 126.9, 150.1, outtype, 127, 150);
     683          20 :                 if (!IS_UNSIGNED(outtype))
     684             :                 {
     685          12 :                     FROM_C_F(intype, -125.9, -127.1, outtype, -126, -127);
     686             :                 }
     687             :             }
     688             :         }
     689           2 :         FROM_C(intype, -1, 256, GDT_Byte, 0, 0);
     690           2 :         FROM_C(intype, 256, -1, GDT_Byte, 255, 0);
     691           2 :         FROM_C(intype, -33000, 33000, GDT_Int16, -32768, 0);
     692           2 :         FROM_C(intype, 33000, -33000, GDT_Int16, 32767, 0);
     693           2 :         FROM_C(intype, -1, 66000, GDT_UInt16, 0, 0);
     694           2 :         FROM_C(intype, 66000, -1, GDT_UInt16, 65535, 0);
     695           2 :         FROM_C(intype, -CST_3000000000, -CST_3000000000, GDT_Int32, INT_MIN, 0);
     696           2 :         FROM_C(intype, CST_3000000000, CST_3000000000, GDT_Int32, 2147483647,
     697             :                0);
     698           2 :         FROM_C(intype, -1, CST_5000000000, GDT_UInt32, 0, 0);
     699           2 :         FROM_C(intype, CST_5000000000, -1, GDT_UInt32, 4294967295UL, 0);
     700           2 :         FROM_C(intype, CST_5000000000, -1, GDT_Float32, CST_5000000000, 0);
     701           2 :         FROM_C(intype, CST_5000000000, -1, GDT_Float64, CST_5000000000, 0);
     702           2 :         FROM_C(intype, -CST_5000000000, -1, GDT_Float32, -CST_5000000000, 0);
     703           2 :         FROM_C(intype, -CST_5000000000, -1, GDT_Float64, -CST_5000000000, 0);
     704           2 :         FROM_C(intype, -33000, 33000, GDT_CInt16, -32768, 32767);
     705           2 :         FROM_C(intype, 33000, -33000, GDT_CInt16, 32767, -32768);
     706           2 :         FROM_C(intype, -CST_3000000000, -CST_3000000000, GDT_CInt32, INT_MIN,
     707             :                INT_MIN);
     708           2 :         FROM_C(intype, CST_3000000000, CST_3000000000, GDT_CInt32, 2147483647,
     709             :                2147483647);
     710           2 :         FROM_C(intype, CST_5000000000, -CST_5000000000, GDT_CFloat32,
     711             :                CST_5000000000, -CST_5000000000);
     712           2 :         FROM_C(intype, CST_5000000000, -CST_5000000000, GDT_CFloat64,
     713             :                CST_5000000000, -CST_5000000000);
     714             :     }
     715           1 : }
     716             : 
     717             : template <class Tin, class Tout>
     718         100 : void CheckPackedGeneric(GDALDataType eIn, GDALDataType eOut)
     719             : {
     720         100 :     const int N = 64 + 7;
     721             :     Tin arrayIn[N];
     722             :     Tout arrayOut[N];
     723        7200 :     for (int i = 0; i < N; i++)
     724             :     {
     725        7100 :         arrayIn[i] = static_cast<Tin>(i + 1);
     726        7100 :         arrayOut[i] = 0;
     727             :     }
     728         100 :     GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
     729             :                   GDALGetDataTypeSizeBytes(eOut), N);
     730         100 :     int numLine = 0;
     731        7200 :     for (int i = 0; i < N; i++)
     732             :     {
     733        7100 :         MY_EXPECT(eIn, i + 1, eOut, i + 1, arrayOut[i]);
     734             :     }
     735         100 : }
     736             : 
     737             : template <class Tin, class Tout>
     738          98 : void CheckPacked(GDALDataType eIn, GDALDataType eOut)
     739             : {
     740          98 :     CheckPackedGeneric<Tin, Tout>(eIn, eOut);
     741          98 : }
     742             : 
     743             : template <>
     744           1 : void CheckPacked<GUInt16, GByte>(GDALDataType eIn, GDALDataType eOut)
     745             : {
     746           1 :     CheckPackedGeneric<GUInt16, GByte>(eIn, eOut);
     747             : 
     748           1 :     const int N = 64 + 7;
     749           1 :     GUInt16 arrayIn[N] = {0};
     750           1 :     GByte arrayOut[N] = {0};
     751          72 :     for (int i = 0; i < N; i++)
     752             :     {
     753         130 :         arrayIn[i] = (i % 6) == 0   ? 254
     754          59 :                      : (i % 6) == 1 ? 255
     755          47 :                      : (i % 4) == 2 ? 256
     756          35 :                      : (i % 6) == 3 ? 32767
     757          23 :                      : (i % 6) == 4 ? 32768
     758             :                                     : 65535;
     759             :     }
     760           1 :     GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
     761             :                   GDALGetDataTypeSizeBytes(eOut), N);
     762           1 :     int numLine = 0;
     763          72 :     for (int i = 0; i < N; i++)
     764             :     {
     765          71 :         MY_EXPECT(eIn, (int)arrayIn[i], eOut, (i % 6) == 0 ? 254 : 255,
     766             :                   arrayOut[i]);
     767             :     }
     768           1 : }
     769             : 
     770             : template <>
     771           1 : void CheckPacked<GUInt16, GInt16>(GDALDataType eIn, GDALDataType eOut)
     772             : {
     773           1 :     CheckPackedGeneric<GUInt16, GInt16>(eIn, eOut);
     774             : 
     775           1 :     const int N = 64 + 7;
     776           1 :     GUInt16 arrayIn[N] = {0};
     777           1 :     GInt16 arrayOut[N] = {0};
     778          72 :     for (int i = 0; i < N; i++)
     779             :     {
     780          71 :         arrayIn[i] = 32766 + (i % 4);
     781             :     }
     782           1 :     GDALCopyWords(arrayIn, eIn, GDALGetDataTypeSizeBytes(eIn), arrayOut, eOut,
     783             :                   GDALGetDataTypeSizeBytes(eOut), N);
     784           1 :     int numLine = 0;
     785          72 :     for (int i = 0; i < N; i++)
     786             :     {
     787          71 :         MY_EXPECT(eIn, (int)arrayIn[i], eOut, (i % 4) == 0 ? 32766 : 32767,
     788             :                   arrayOut[i]);
     789             :     }
     790           1 : }
     791             : 
     792         100 : template <class Tin> void CheckPacked(GDALDataType eIn, GDALDataType eOut)
     793             : {
     794         100 :     switch (eOut)
     795             :     {
     796          10 :         case GDT_Byte:
     797          10 :             CheckPacked<Tin, GByte>(eIn, eOut);
     798          10 :             break;
     799          10 :         case GDT_Int8:
     800          10 :             CheckPacked<Tin, GInt8>(eIn, eOut);
     801          10 :             break;
     802          10 :         case GDT_UInt16:
     803          10 :             CheckPacked<Tin, GUInt16>(eIn, eOut);
     804          10 :             break;
     805          10 :         case GDT_Int16:
     806          10 :             CheckPacked<Tin, GInt16>(eIn, eOut);
     807          10 :             break;
     808          10 :         case GDT_UInt32:
     809          10 :             CheckPacked<Tin, GUInt32>(eIn, eOut);
     810          10 :             break;
     811          10 :         case GDT_Int32:
     812          10 :             CheckPacked<Tin, GInt32>(eIn, eOut);
     813          10 :             break;
     814          10 :         case GDT_UInt64:
     815          10 :             CheckPacked<Tin, std::uint64_t>(eIn, eOut);
     816          10 :             break;
     817          10 :         case GDT_Int64:
     818          10 :             CheckPacked<Tin, std::int64_t>(eIn, eOut);
     819          10 :             break;
     820          10 :         case GDT_Float32:
     821          10 :             CheckPacked<Tin, float>(eIn, eOut);
     822          10 :             break;
     823          10 :         case GDT_Float64:
     824          10 :             CheckPacked<Tin, double>(eIn, eOut);
     825          10 :             break;
     826           0 :         default:
     827           0 :             CPLAssert(false);
     828             :     }
     829         100 : }
     830             : 
     831         100 : static void CheckPacked(GDALDataType eIn, GDALDataType eOut)
     832             : {
     833         100 :     switch (eIn)
     834             :     {
     835          10 :         case GDT_Byte:
     836          10 :             CheckPacked<GByte>(eIn, eOut);
     837          10 :             break;
     838          10 :         case GDT_Int8:
     839          10 :             CheckPacked<GInt8>(eIn, eOut);
     840          10 :             break;
     841          10 :         case GDT_UInt16:
     842          10 :             CheckPacked<GUInt16>(eIn, eOut);
     843          10 :             break;
     844          10 :         case GDT_Int16:
     845          10 :             CheckPacked<GInt16>(eIn, eOut);
     846          10 :             break;
     847          10 :         case GDT_UInt32:
     848          10 :             CheckPacked<GUInt32>(eIn, eOut);
     849          10 :             break;
     850          10 :         case GDT_Int32:
     851          10 :             CheckPacked<GInt32>(eIn, eOut);
     852          10 :             break;
     853          10 :         case GDT_UInt64:
     854          10 :             CheckPacked<std::uint64_t>(eIn, eOut);
     855          10 :             break;
     856          10 :         case GDT_Int64:
     857          10 :             CheckPacked<std::int64_t>(eIn, eOut);
     858          10 :             break;
     859          10 :         case GDT_Float32:
     860          10 :             CheckPacked<float>(eIn, eOut);
     861          10 :             break;
     862          10 :         case GDT_Float64:
     863          10 :             CheckPacked<double>(eIn, eOut);
     864          10 :             break;
     865           0 :         default:
     866           0 :             CPLAssert(false);
     867             :     }
     868         100 : }
     869             : 
     870             : class TestCopyWordsCheckPackedFixture
     871             :     : public TestCopyWords,
     872             :       public ::testing::WithParamInterface<
     873             :           std::tuple<GDALDataType, GDALDataType>>
     874             : {
     875             : };
     876             : 
     877         201 : TEST_P(TestCopyWordsCheckPackedFixture, CheckPacked)
     878             : {
     879         100 :     GDALDataType eIn = std::get<0>(GetParam());
     880         100 :     GDALDataType eOut = std::get<1>(GetParam());
     881         100 :     CheckPacked(eIn, eOut);
     882         100 : }
     883             : 
     884             : static std::vector<std::tuple<GDALDataType, GDALDataType>>
     885           1 : GetGDALDataTypeTupleValues()
     886             : {
     887           1 :     std::vector<std::tuple<GDALDataType, GDALDataType>> ret;
     888          15 :     for (GDALDataType eIn = GDT_Byte; eIn < GDT_TypeCount;
     889          14 :          eIn = static_cast<GDALDataType>(eIn + 1))
     890             :     {
     891          14 :         if (GDALDataTypeIsComplex(eIn))
     892           4 :             continue;
     893         150 :         for (GDALDataType eOut = GDT_Byte; eOut < GDT_TypeCount;
     894         140 :              eOut = static_cast<GDALDataType>(eOut + 1))
     895             :         {
     896         140 :             if (GDALDataTypeIsComplex(eOut))
     897          40 :                 continue;
     898         100 :             ret.emplace_back(std::make_tuple(eIn, eOut));
     899             :         }
     900             :     }
     901           1 :     return ret;
     902             : }
     903             : 
     904         302 : INSTANTIATE_TEST_SUITE_P(
     905             :     TestCopyWords, TestCopyWordsCheckPackedFixture,
     906             :     ::testing::ValuesIn(GetGDALDataTypeTupleValues()),
     907             :     [](const ::testing::TestParamInfo<
     908             :         TestCopyWordsCheckPackedFixture::ParamType> &l_info)
     909             :     {
     910             :         GDALDataType eIn = std::get<0>(l_info.param);
     911             :         GDALDataType eOut = std::get<1>(l_info.param);
     912             :         return std::string(GDALGetDataTypeName(eIn)) + "_" +
     913             :                GDALGetDataTypeName(eOut);
     914             :     });
     915             : 
     916           4 : TEST_F(TestCopyWords, ByteToByte)
     917             : {
     918           3 :     for (int k = 0; k < 2; k++)
     919             :     {
     920           2 :         if (k == 1)
     921           1 :             CPLSetConfigOption("GDAL_USE_SSSE3", "NO");
     922             : 
     923           8 :         for (int spacing = 2; spacing <= 4; spacing++)
     924             :         {
     925           6 :             memset(pIn, 0xff, 256);
     926         108 :             for (int i = 0; i < 17; i++)
     927             :             {
     928         102 :                 pIn[spacing * i] = (GByte)(17 - i);
     929             :             }
     930           6 :             memset(pOut, 0xff, 256);
     931           6 :             GDALCopyWords(pIn, GDT_Byte, spacing, pOut, GDT_Byte, 1, 17);
     932         108 :             for (int i = 0; i < 17; i++)
     933             :             {
     934         102 :                 AssertRes(GDT_Byte, 17 - i, GDT_Byte, 17 - i, pOut[i],
     935             :                           __LINE__);
     936             :             }
     937             : 
     938           6 :             memset(pIn, 0xff, 256);
     939           6 :             memset(pOut, 0xff, 256);
     940         108 :             for (int i = 0; i < 17; i++)
     941             :             {
     942         102 :                 pIn[i] = (GByte)(17 - i);
     943             :             }
     944           6 :             GDALCopyWords(pIn, GDT_Byte, 1, pOut, GDT_Byte, spacing, 17);
     945         108 :             for (int i = 0; i < 17; i++)
     946             :             {
     947         102 :                 AssertRes(GDT_Byte, 17 - i, GDT_Byte, 17 - i, pOut[i * spacing],
     948             :                           __LINE__);
     949         306 :                 for (int j = 1; j < spacing; j++)
     950             :                 {
     951         204 :                     AssertRes(GDT_Byte, 0xff, GDT_Byte, 0xff,
     952         204 :                               pOut[i * spacing + j], __LINE__);
     953             :                 }
     954             :             }
     955             :         }
     956             :     }
     957           1 :     CPLSetConfigOption("GDAL_USE_SSSE3", nullptr);
     958           1 : }
     959             : 
     960           4 : TEST_F(TestCopyWords, Int16ToInt16)
     961             : {
     962           1 :     memset(pIn, 0xff, 256);
     963           1 :     GInt16 *pInShort = (GInt16 *)pIn;
     964           1 :     GInt16 *pOutShort = (GInt16 *)pOut;
     965          10 :     for (int i = 0; i < 9; i++)
     966             :     {
     967           9 :         pInShort[2 * i + 0] = 0x1234;
     968           9 :         pInShort[2 * i + 1] = 0x5678;
     969             :     }
     970           5 :     for (int iSpacing = 0; iSpacing < 4; iSpacing++)
     971             :     {
     972           4 :         memset(pOut, 0xff, 256);
     973           4 :         GDALCopyWords(pInShort, GDT_Int16, sizeof(short), pOutShort, GDT_Int16,
     974           4 :                       (iSpacing + 1) * sizeof(short), 18);
     975          40 :         for (int i = 0; i < 9; i++)
     976             :         {
     977          36 :             AssertRes(GDT_Int16, pInShort[2 * i + 0], GDT_Int16,
     978          36 :                       pInShort[2 * i + 0],
     979          36 :                       pOutShort[(iSpacing + 1) * (2 * i + 0)], __LINE__);
     980          36 :             AssertRes(GDT_Int16, pInShort[2 * i + 1], GDT_Int16,
     981          36 :                       pInShort[2 * i + 1],
     982          36 :                       pOutShort[(iSpacing + 1) * (2 * i + 1)], __LINE__);
     983             :         }
     984             :     }
     985           5 :     for (int iSpacing = 0; iSpacing < 4; iSpacing++)
     986             :     {
     987           4 :         memset(pIn, 0xff, 256);
     988           4 :         memset(pOut, 0xff, 256);
     989          40 :         for (int i = 0; i < 9; i++)
     990             :         {
     991          36 :             pInShort[(iSpacing + 1) * (2 * i + 0)] = 0x1234;
     992          36 :             pInShort[(iSpacing + 1) * (2 * i + 1)] = 0x5678;
     993             :         }
     994           4 :         GDALCopyWords(pInShort, GDT_Int16, (iSpacing + 1) * sizeof(short),
     995             :                       pOutShort, GDT_Int16, sizeof(short), 18);
     996          40 :         for (int i = 0; i < 9; i++)
     997             :         {
     998          36 :             AssertRes(GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 0)],
     999          36 :                       GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 0)],
    1000          36 :                       pOutShort[2 * i + 0], __LINE__);
    1001          36 :             AssertRes(GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 1)],
    1002          36 :                       GDT_Int16, pInShort[(iSpacing + 1) * (2 * i + 1)],
    1003          36 :                       pOutShort[2 * i + 1], __LINE__);
    1004             :         }
    1005             :     }
    1006           1 : }
    1007             : 
    1008             : }  // namespace

Generated by: LCOV version 1.14