16 #include "my_config.h"
17 #include <gtest/gtest.h>
20 #include "my_global.h"
22 namespace byteorder_unittest {
24 using std::numeric_limits;
26 #if defined(GTEST_HAS_PARAM_TEST)
40 class Float4Test :
public FloatingTest<float>,
41 public ::testing::TestWithParam<float>
46 output= numeric_limits<float>::quiet_NaN();
50 INSTANTIATE_TEST_CASE_P(Foo, Float4Test,
51 ::testing::Values(numeric_limits<float>::min(),
52 numeric_limits<float>::max(),
53 numeric_limits<float>::epsilon(),
54 -numeric_limits<float>::min(),
55 -numeric_limits<float>::max(),
56 -numeric_limits<float>::epsilon(),
61 TEST_P(Float4Test, PutAndGet)
63 float4store(
buf, input);
64 float4get(output,
buf);
65 EXPECT_EQ(input, output);
66 floatstore(
buf, input);
67 floatget(output,
buf);
68 EXPECT_EQ(input, output);
72 class Float8Test :
public FloatingTest<double>,
73 public ::testing::TestWithParam<double>
78 output= numeric_limits<double>::quiet_NaN();
82 INSTANTIATE_TEST_CASE_P(Foo, Float8Test,
83 ::testing::Values(numeric_limits<double>::min(),
84 numeric_limits<double>::max(),
85 numeric_limits<double>::epsilon(),
86 -numeric_limits<double>::min(),
87 -numeric_limits<double>::max(),
88 -numeric_limits<double>::epsilon(),
93 TEST_P(Float8Test, PutAndGet)
95 float8store(
buf, input);
96 float8get(output,
buf);
97 EXPECT_EQ(input, output);
98 doublestore(
buf, input);
99 doubleget(output,
buf);
100 EXPECT_EQ(input, output);
103 #endif // GTEST_HAS_PARAM_TEST
106 #if defined(GTEST_HAS_TYPED_TEST)
113 class IntegralTest :
public ::testing::Test
116 typedef std::numeric_limits<T> Limit;
120 uchar
buf[
sizeof(T)];
122 typename std::vector<T> values;
124 IntegralTest() : input(0), output(0) {}
128 values.push_back(Limit::min());
129 values.push_back(Limit::min() / T(2));
130 values.push_back(T(0));
131 values.push_back(T(42));
132 values.push_back(Limit::max() / T(2));
133 values.push_back(Limit::max());
140 template<
int ndigits>
144 typedef ulonglong value_type;
146 sizeNint() : value(0) {}
147 explicit sizeNint(ulonglong v)
151 case 3: value= v & 0xFFFFFFULL;
break;
152 case 5: value= v & 0xFFFFFFFFFFULL;
break;
153 case 6: value= v & 0xFFFFFFFFFFFFULL;
break;
154 default: ADD_FAILURE() <<
"unxpected number of digits";
158 sizeNint operator/(
const sizeNint &that)
const
159 {
return sizeNint(this->value / that.value); }
161 bool operator==(
const sizeNint &that)
const
162 {
return this->value == that.value; }
168 template<
int ndigits>
169 std::ostream &operator<<(std::ostream &s, const sizeNint<ndigits> &v)
170 {
return s << v.value; }
173 typedef ::testing::Types<short, ushort,
174 sizeNint<3>, sizeNint<5>, sizeNint<6>,
176 longlong, ulonglong> IntegralTypes;
178 TYPED_TEST_CASE(IntegralTest, IntegralTypes);
183 template<
typename T>
void put_integral(uchar *
buf, T val)
184 { ADD_FAILURE() <<
"unknown type in put_integral"; }
185 template<
typename T>
void get_integral(T &val, uchar *
buf)
186 { ADD_FAILURE() <<
"unknown type in get_integral"; }
188 template<>
void put_integral(uchar *
buf,
short val) { shortstore(buf, val); }
189 template<>
void get_integral(
short &val, uchar *
buf) { shortget(val, buf); }
192 template<>
void put_integral(uchar *
buf, ushort val) { shortstore(buf, val); }
193 template<>
void get_integral(ushort &val, uchar *
buf) { ushortget(val, buf); }
195 template<>
void put_integral(uchar *
buf,
int val) { longstore(buf, val); }
196 template<>
void get_integral(
int &val, uchar *
buf) { longget(val, buf); }
199 template<>
void put_integral(uchar *
buf,
unsigned val) { longstore(buf, val); }
200 template<>
void get_integral(
unsigned &val, uchar *
buf) { ulongget(val, buf); }
202 template<>
void put_integral(uchar *
buf, longlong val)
203 { longlongstore(buf, val); }
204 template<>
void get_integral(longlong &val, uchar *
buf)
205 { longlongget(val, buf); }
208 template<>
void put_integral(uchar *
buf, ulonglong val)
209 { int8store(buf, val); }
210 template<>
void get_integral(ulonglong &val, uchar *
buf)
211 { val= uint8korr(buf); }
213 template<>
void put_integral(uchar *
buf, sizeNint<3> val)
214 { int3store(buf, val.value); }
215 template<>
void get_integral(sizeNint<3> &val, uchar *
buf)
216 { val.value= uint3korr(buf); }
218 template<>
void put_integral(uchar *
buf, sizeNint<5> val)
219 { int5store(buf, val.value); }
220 template<>
void get_integral(sizeNint<5> &val, uchar *
buf)
221 { val.value= uint5korr(buf); }
223 template<>
void put_integral(uchar *
buf, sizeNint<6> val)
224 { int6store(buf, val.value); }
225 template<>
void get_integral(sizeNint<6> &val, uchar *
buf)
226 { val.value= uint6korr(buf); }
231 TYPED_TEST(IntegralTest, PutAndGet)
233 for (
size_t ix= 0; ix < this->values.size(); ++ix)
235 this->input= this->values[ix];
236 put_integral(this->
buf, this->input);
237 get_integral(this->output, this->
buf);
239 TypeParam myinput= this->input;
240 TypeParam myoutput= this->output;
241 EXPECT_EQ(myinput, myoutput);
245 #endif // GTEST_HAS_TYPED_TEST