18
18
#include < cstdint>
19
19
#include < iostream>
20
20
#include < limits>
21
+ #include < type_traits>
21
22
22
23
#include " gtest_include.h"
23
24
@@ -26,9 +27,9 @@ namespace
26
27
27
28
// ---------------------------------------------------------------------------
28
29
29
- template <class OutType , class ConstantType >
30
- void AssertRes (GDALDataType intype, ConstantType inval, GDALDataType outtype,
31
- ConstantType expected_outval, OutType outval, int numLine)
30
+ template <class OutType , class CT1 , class CT2 >
31
+ void AssertRes (GDALDataType intype, CT1 inval, GDALDataType outtype,
32
+ CT2 expected_outval, OutType outval, int numLine)
32
33
{
33
34
if (static_cast <double >(expected_outval) == static_cast <double >(outval) ||
34
35
(std::isnan (static_cast <double >(expected_outval)) &&
@@ -55,8 +56,8 @@ class TestCopyWords : public ::testing::Test
55
56
protected:
56
57
void SetUp () override
57
58
{
58
- pIn = (GByte *)malloc (256 );
59
- pOut = (GByte *)malloc (256 );
59
+ pIn = (GByte *)malloc (2048 );
60
+ pOut = (GByte *)malloc (2048 );
60
61
}
61
62
62
63
void TearDown () override
@@ -74,8 +75,8 @@ class TestCopyWords : public ::testing::Test
74
75
GDALDataType outtype, ConstantType outval, ConstantType outvali,
75
76
int numLine)
76
77
{
77
- memset (pIn, 0xff , 128 );
78
- memset (pOut, 0xff , 128 );
78
+ memset (pIn, 0xff , 1024 );
79
+ memset (pOut, 0xff , 1024 );
79
80
80
81
*(InType *)(pIn) = (InType)inval;
81
82
*(InType *)(pIn + 32 ) = (InType)inval;
@@ -89,14 +90,14 @@ class TestCopyWords : public ::testing::Test
89
90
GDALCopyWords (pIn, intype, 32 , pOut, outtype, 32 , 2 );
90
91
91
92
/* Test negative offsets */
92
- GDALCopyWords (pIn + 32 , intype, -32 , pOut + 128 - 16 , outtype, -32 , 2 );
93
+ GDALCopyWords (pIn + 32 , intype, -32 , pOut + 1024 - 16 , outtype, -32 , 2 );
93
94
94
95
MY_EXPECT (intype, inval, outtype, outval, *(OutType *)(pOut));
95
96
MY_EXPECT (intype, inval, outtype, outval, *(OutType *)(pOut + 32 ));
96
97
MY_EXPECT (intype, inval, outtype, outval,
97
- *(OutType *)(pOut + 128 - 16 ));
98
+ *(OutType *)(pOut + 1024 - 16 ));
98
99
MY_EXPECT (intype, inval, outtype, outval,
99
- *(OutType *)(pOut + 128 - 16 - 32 ));
100
+ *(OutType *)(pOut + 1024 - 16 - 32 ));
100
101
101
102
if (GDALDataTypeIsComplex (outtype))
102
103
{
@@ -105,38 +106,29 @@ class TestCopyWords : public ::testing::Test
105
106
((OutType *)(pOut + 32 ))[1 ]);
106
107
107
108
MY_EXPECT (intype, invali, outtype, outvali,
108
- ((OutType *)(pOut + 128 - 16 ))[1 ]);
109
+ ((OutType *)(pOut + 1024 - 16 ))[1 ]);
109
110
MY_EXPECT (intype, invali, outtype, outvali,
110
- ((OutType *)(pOut + 128 - 16 - 32 ))[1 ]);
111
+ ((OutType *)(pOut + 1024 - 16 - 32 ))[1 ]);
111
112
}
112
113
else
113
114
{
114
- *(InType *)(pIn + GDALGetDataTypeSizeBytes (intype)) = (InType)inval;
115
- /* Test packed offsets */
116
- GDALCopyWords (pIn, intype, GDALGetDataTypeSizeBytes (intype), pOut,
117
- outtype, GDALGetDataTypeSizeBytes (outtype), 2 );
118
-
119
- MY_EXPECT (intype, inval, outtype, outval, *(OutType *)(pOut));
120
- MY_EXPECT (intype, inval, outtype, outval,
121
- *(OutType *)(pOut + GDALGetDataTypeSizeBytes (outtype)));
115
+ constexpr int N = 32 + 31 ;
116
+ for (int i = 0 ; i < N; ++i)
117
+ {
118
+ *(InType *)(pIn + i * GDALGetDataTypeSizeBytes (intype)) =
119
+ (InType)inval;
120
+ }
122
121
123
- *(InType *)(pIn + 2 * GDALGetDataTypeSizeBytes (intype)) =
124
- (InType)inval;
125
- *(InType *)(pIn + 3 * GDALGetDataTypeSizeBytes (intype)) =
126
- (InType)inval;
127
122
/* Test packed offsets */
128
123
GDALCopyWords (pIn, intype, GDALGetDataTypeSizeBytes (intype), pOut,
129
- outtype, GDALGetDataTypeSizeBytes (outtype), 4 );
130
-
131
- MY_EXPECT (intype, inval, outtype, outval, *(OutType *)(pOut));
132
- MY_EXPECT (intype, inval, outtype, outval,
133
- *(OutType *)(pOut + GDALGetDataTypeSizeBytes (outtype)));
134
- MY_EXPECT (
135
- intype, inval, outtype, outval,
136
- *(OutType *)(pOut + 2 * GDALGetDataTypeSizeBytes (outtype)));
137
- MY_EXPECT (
138
- intype, inval, outtype, outval,
139
- *(OutType *)(pOut + 3 * GDALGetDataTypeSizeBytes (outtype)));
124
+ outtype, GDALGetDataTypeSizeBytes (outtype), N);
125
+
126
+ for (int i = 0 ; i < N; ++i)
127
+ {
128
+ MY_EXPECT (
129
+ intype, inval, outtype, outval,
130
+ *(OutType *)(pOut + i * GDALGetDataTypeSizeBytes (outtype)));
131
+ }
140
132
}
141
133
}
142
134
@@ -1080,15 +1072,47 @@ void CheckPackedGeneric(GDALDataType eIn, GDALDataType eOut)
1080
1072
Tout arrayOut[N];
1081
1073
for (int i = 0 ; i < N; i++)
1082
1074
{
1083
- arrayIn[i] = static_cast <Tin>(i + 1 );
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>)
1079
+ arrayIn[i] = cpl::NumericLimits<Tin>::quiet_NaN ();
1080
+ else if ((i % 2 ) != 0 )
1081
+ arrayIn[i] = static_cast <Tin>(i + 0.4 );
1082
+ else
1083
+ arrayIn[i] = static_cast <Tin>(i + 0.6 );
1084
+ }
1085
+ else
1086
+ {
1087
+ arrayIn[i] = static_cast <Tin>(i + 1 );
1088
+ }
1084
1089
arrayOut[i] = 0 ;
1085
1090
}
1086
1091
GDALCopyWords (arrayIn, eIn, GDALGetDataTypeSizeBytes (eIn), arrayOut, eOut,
1087
1092
GDALGetDataTypeSizeBytes (eOut), N);
1088
1093
int numLine = 0 ;
1089
1094
for (int i = 0 ; i < N; i++)
1090
1095
{
1091
- MY_EXPECT (eIn, i + 1 , eOut, i + 1 , arrayOut[i]);
1096
+ if constexpr (!std::is_integral_v<Tin> && std::is_integral_v<Tout>)
1097
+ {
1098
+ if (i == 0 && std::is_unsigned_v<Tout>)
1099
+ {
1100
+ MY_EXPECT (eIn, cpl::NumericLimits<Tin>::quiet_NaN (), eOut, 0 ,
1101
+ arrayOut[i]);
1102
+ }
1103
+ else if ((i % 2 ) != 0 )
1104
+ {
1105
+ MY_EXPECT (eIn, i + 0.4 , eOut, i, arrayOut[i]);
1106
+ }
1107
+ else
1108
+ {
1109
+ MY_EXPECT (eIn, i + 0.6 , eOut, i + 1 , arrayOut[i]);
1110
+ }
1111
+ }
1112
+ else
1113
+ {
1114
+ MY_EXPECT (eIn, i + 1 , eOut, i + 1 , arrayOut[i]);
1115
+ }
1092
1116
}
1093
1117
}
1094
1118
0 commit comments