Line data Source code
1 : // SPDX-License-Identifier: MIT
2 : // Copyright 2024, Even Rouault <even.rouault at spatialys.com>
3 :
4 : #include "gdal_unit_test.h"
5 :
6 : #include "gdal_typetraits.h"
7 :
8 : #include "gtest_include.h"
9 :
10 : namespace
11 : {
12 :
13 : struct test_gdal_typetraits : public ::testing::Test
14 : {
15 : };
16 :
17 4 : TEST_F(test_gdal_typetraits, CXXTypeTraits)
18 : {
19 : static_assert(gdal::CXXTypeTraits<int8_t>::gdal_type == GDT_Int8);
20 : static_assert(gdal::CXXTypeTraits<int8_t>::size == 1);
21 1 : EXPECT_EQ(
22 : gdal::CXXTypeTraits<int8_t>::GetExtendedDataType().GetNumericDataType(),
23 : GDT_Int8);
24 : static_assert(gdal::CXXTypeTraits<uint8_t>::gdal_type == GDT_Byte);
25 : static_assert(gdal::CXXTypeTraits<uint8_t>::size == 1);
26 1 : EXPECT_EQ(gdal::CXXTypeTraits<uint8_t>::GetExtendedDataType()
27 : .GetNumericDataType(),
28 : GDT_Byte);
29 : static_assert(gdal::CXXTypeTraits<int16_t>::gdal_type == GDT_Int16);
30 : static_assert(gdal::CXXTypeTraits<int16_t>::size == 2);
31 1 : EXPECT_EQ(gdal::CXXTypeTraits<int16_t>::GetExtendedDataType()
32 : .GetNumericDataType(),
33 : GDT_Int16);
34 : static_assert(gdal::CXXTypeTraits<uint16_t>::gdal_type == GDT_UInt16);
35 : static_assert(gdal::CXXTypeTraits<uint16_t>::size == 2);
36 1 : EXPECT_EQ(gdal::CXXTypeTraits<uint16_t>::GetExtendedDataType()
37 : .GetNumericDataType(),
38 : GDT_UInt16);
39 : static_assert(gdal::CXXTypeTraits<int32_t>::gdal_type == GDT_Int32);
40 : static_assert(gdal::CXXTypeTraits<int32_t>::size == 4);
41 1 : EXPECT_EQ(gdal::CXXTypeTraits<int32_t>::GetExtendedDataType()
42 : .GetNumericDataType(),
43 : GDT_Int32);
44 : static_assert(gdal::CXXTypeTraits<uint32_t>::gdal_type == GDT_UInt32);
45 : static_assert(gdal::CXXTypeTraits<uint32_t>::size == 4);
46 1 : EXPECT_EQ(gdal::CXXTypeTraits<uint32_t>::GetExtendedDataType()
47 : .GetNumericDataType(),
48 : GDT_UInt32);
49 : static_assert(gdal::CXXTypeTraits<int64_t>::gdal_type == GDT_Int64);
50 : static_assert(gdal::CXXTypeTraits<int64_t>::size == 8);
51 1 : EXPECT_EQ(gdal::CXXTypeTraits<int64_t>::GetExtendedDataType()
52 : .GetNumericDataType(),
53 : GDT_Int64);
54 : static_assert(gdal::CXXTypeTraits<uint64_t>::gdal_type == GDT_UInt64);
55 : static_assert(gdal::CXXTypeTraits<uint64_t>::size == 8);
56 1 : EXPECT_EQ(gdal::CXXTypeTraits<uint64_t>::GetExtendedDataType()
57 : .GetNumericDataType(),
58 : GDT_UInt64);
59 : static_assert(gdal::CXXTypeTraits<float>::gdal_type == GDT_Float32);
60 : static_assert(gdal::CXXTypeTraits<float>::size == 4);
61 1 : EXPECT_EQ(
62 : gdal::CXXTypeTraits<float>::GetExtendedDataType().GetNumericDataType(),
63 : GDT_Float32);
64 : static_assert(gdal::CXXTypeTraits<double>::gdal_type == GDT_Float64);
65 : static_assert(gdal::CXXTypeTraits<double>::size == 8);
66 1 : EXPECT_EQ(
67 : gdal::CXXTypeTraits<double>::GetExtendedDataType().GetNumericDataType(),
68 : GDT_Float64);
69 : static_assert(gdal::CXXTypeTraits<std::complex<float>>::gdal_type ==
70 : GDT_CFloat32);
71 : static_assert(gdal::CXXTypeTraits<std::complex<float>>::size == 8);
72 1 : EXPECT_EQ(gdal::CXXTypeTraits<std::complex<float>>::GetExtendedDataType()
73 : .GetNumericDataType(),
74 : GDT_CFloat32);
75 : static_assert(gdal::CXXTypeTraits<std::complex<double>>::gdal_type ==
76 : GDT_CFloat64);
77 : static_assert(gdal::CXXTypeTraits<std::complex<double>>::size == 16);
78 1 : EXPECT_EQ(gdal::CXXTypeTraits<std::complex<double>>::GetExtendedDataType()
79 : .GetNumericDataType(),
80 : GDT_CFloat64);
81 : static_assert(gdal::CXXTypeTraits<std::string>::size == 0);
82 1 : EXPECT_EQ(
83 : gdal::CXXTypeTraits<std::string>::GetExtendedDataType().GetClass(),
84 : GEDTC_STRING);
85 1 : }
86 :
87 4 : TEST_F(test_gdal_typetraits, GDALDataTypeTraits)
88 : {
89 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Byte>::GetExtendedDataType()
90 : .GetNumericDataType(),
91 : GDT_Byte);
92 : static_assert(
93 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Byte>::type, uint8_t>);
94 : static_assert(gdal::GDALDataTypeTraits<GDT_Byte>::size == 1);
95 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Int8>::GetExtendedDataType()
96 : .GetNumericDataType(),
97 : GDT_Int8);
98 : static_assert(
99 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Int8>::type, int8_t>);
100 : static_assert(gdal::GDALDataTypeTraits<GDT_Int8>::size == 1);
101 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Int16>::GetExtendedDataType()
102 : .GetNumericDataType(),
103 : GDT_Int16);
104 : static_assert(
105 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Int16>::type, int16_t>);
106 : static_assert(gdal::GDALDataTypeTraits<GDT_Int16>::size == 2);
107 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_UInt16>::GetExtendedDataType()
108 : .GetNumericDataType(),
109 : GDT_UInt16);
110 : static_assert(
111 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_UInt16>::type, uint16_t>);
112 : static_assert(gdal::GDALDataTypeTraits<GDT_UInt16>::size == 2);
113 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Int32>::GetExtendedDataType()
114 : .GetNumericDataType(),
115 : GDT_Int32);
116 : static_assert(
117 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Int32>::type, int32_t>);
118 : static_assert(gdal::GDALDataTypeTraits<GDT_Int32>::size == 4);
119 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_UInt32>::GetExtendedDataType()
120 : .GetNumericDataType(),
121 : GDT_UInt32);
122 : static_assert(
123 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_UInt32>::type, uint32_t>);
124 : static_assert(gdal::GDALDataTypeTraits<GDT_UInt32>::size == 4);
125 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Int64>::GetExtendedDataType()
126 : .GetNumericDataType(),
127 : GDT_Int64);
128 : static_assert(
129 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Int64>::type, int64_t>);
130 : static_assert(gdal::GDALDataTypeTraits<GDT_Int64>::size == 8);
131 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_UInt64>::GetExtendedDataType()
132 : .GetNumericDataType(),
133 : GDT_UInt64);
134 : static_assert(
135 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_UInt64>::type, uint64_t>);
136 : static_assert(gdal::GDALDataTypeTraits<GDT_UInt64>::size == 8);
137 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Float32>::GetExtendedDataType()
138 : .GetNumericDataType(),
139 : GDT_Float32);
140 : static_assert(
141 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Float32>::type, float>);
142 : static_assert(gdal::GDALDataTypeTraits<GDT_Float32>::size == 4);
143 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_Float64>::GetExtendedDataType()
144 : .GetNumericDataType(),
145 : GDT_Float64);
146 : static_assert(
147 : std::is_same_v<gdal::GDALDataTypeTraits<GDT_Float64>::type, double>);
148 : static_assert(gdal::GDALDataTypeTraits<GDT_Float64>::size == 8);
149 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_CInt16>::GetExtendedDataType()
150 : .GetNumericDataType(),
151 : GDT_CInt16);
152 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_CInt32>::GetExtendedDataType()
153 : .GetNumericDataType(),
154 : GDT_CInt32);
155 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_CFloat32>::GetExtendedDataType()
156 : .GetNumericDataType(),
157 : GDT_CFloat32);
158 : static_assert(std::is_same_v<gdal::GDALDataTypeTraits<GDT_CFloat32>::type,
159 : std::complex<float>>);
160 : static_assert(gdal::GDALDataTypeTraits<GDT_CFloat32>::size == 8);
161 1 : EXPECT_EQ(gdal::GDALDataTypeTraits<GDT_CFloat64>::GetExtendedDataType()
162 : .GetNumericDataType(),
163 : GDT_CFloat64);
164 : static_assert(std::is_same_v<gdal::GDALDataTypeTraits<GDT_CFloat64>::type,
165 : std::complex<double>>);
166 : static_assert(gdal::GDALDataTypeTraits<GDT_CFloat64>::size == 16);
167 1 : }
168 :
169 4 : TEST_F(test_gdal_typetraits, GetOGRFieldType)
170 : {
171 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Byte), OFTInteger);
172 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Int8), OFTInteger);
173 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Int16), OFTInteger);
174 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Int32), OFTInteger);
175 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_UInt16), OFTInteger);
176 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_UInt32), OFTInteger64);
177 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Int64), OFTInteger64);
178 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_UInt64), OFTReal);
179 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Float32), OFTReal);
180 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Float64), OFTReal);
181 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_CInt16), OFTMaxType);
182 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_CInt32), OFTMaxType);
183 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_CFloat32), OFTMaxType);
184 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_CFloat64), OFTMaxType);
185 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_Unknown), OFTMaxType);
186 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDT_TypeCount), OFTMaxType);
187 :
188 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDALExtendedDataType::Create(GDT_Byte)),
189 : OFTInteger);
190 1 : EXPECT_EQ(gdal::GetOGRFieldType(GDALExtendedDataType::CreateString()),
191 : OFTString);
192 1 : EXPECT_EQ(
193 : gdal::GetOGRFieldType(GDALExtendedDataType::Create("compound", 0, {})),
194 : OFTMaxType);
195 1 : }
196 :
197 : } // namespace
|