00001 #include "ByteVector.hpp"
00002 #include <limits>
00003 #include "gtest/gtest.h"
00004
00005 namespace msp {
00006
00007
00008 template <typename T> class ByteVectorBasicTest : public ::testing::Test {};
00009 typedef ::testing::Types<bool, uint8_t, uint16_t, uint32_t, int8_t, int16_t,
00010 int32_t, float, double>
00011 basicTypes;
00012 TYPED_TEST_CASE(ByteVectorBasicTest, basicTypes);
00013
00014 template <typename T> class ByteVectorScaledTest : public ::testing::Test {};
00015 typedef ::testing::Types<uint8_t, uint16_t, uint32_t, int8_t, int16_t, int32_t>
00016 scaleOutputTypes;
00017 TYPED_TEST_CASE(ByteVectorScaledTest, scaleOutputTypes);
00018
00019
00020 TEST(ByteVectorBasicTest, Initialzation) {
00021 ByteVector b;
00022 EXPECT_EQ(std::size_t(0), b.size());
00023 EXPECT_EQ(std::size_t(0), b.unpacking_offset());
00024 EXPECT_EQ(std::size_t(0), b.unpacking_remaining());
00025 }
00026
00027 TYPED_TEST(ByteVectorBasicTest, Pack1zero) {
00028 ByteVector b;
00029 TypeParam ref = 0;
00030 EXPECT_TRUE(b.pack(ref));
00031 EXPECT_EQ(sizeof(TypeParam), b.size());
00032 EXPECT_TRUE(b.unpack(ref));
00033 EXPECT_EQ(TypeParam(0), ref);
00034 }
00035
00036 TYPED_TEST(ByteVectorBasicTest, Pack1one) {
00037 ByteVector b;
00038 TypeParam ref = 1;
00039 EXPECT_TRUE(b.pack(ref));
00040 EXPECT_EQ(sizeof(TypeParam), b.size());
00041 EXPECT_TRUE(b.unpack(ref));
00042 EXPECT_EQ(TypeParam(1), ref);
00043 }
00044
00045 TYPED_TEST(ByteVectorBasicTest, Pack1max) {
00046 ByteVector b;
00047 TypeParam ref = std::numeric_limits<TypeParam>::max();
00048 EXPECT_TRUE(b.pack(ref));
00049 EXPECT_EQ(sizeof(TypeParam), b.size());
00050 EXPECT_TRUE(b.unpack(ref));
00051 EXPECT_EQ(std::numeric_limits<TypeParam>::max(), ref);
00052 }
00053
00054 TYPED_TEST(ByteVectorBasicTest, Pack1min) {
00055 ByteVector b;
00056 TypeParam ref = std::numeric_limits<TypeParam>::min();
00057 EXPECT_TRUE(b.pack(ref));
00058 EXPECT_EQ(sizeof(TypeParam), b.size());
00059 EXPECT_TRUE(b.unpack(ref));
00060 EXPECT_EQ(std::numeric_limits<TypeParam>::min(), ref);
00061 }
00062
00063 TYPED_TEST(ByteVectorBasicTest, Pack1valzero) {
00064 ByteVector b;
00065 Value<TypeParam> v;
00066 TypeParam ref = 0;
00067 v = ref;
00068 EXPECT_TRUE(b.pack(v));
00069 EXPECT_EQ(sizeof(TypeParam), b.size());
00070 EXPECT_TRUE(b.unpack(v));
00071 EXPECT_EQ(ref, v());
00072 }
00073
00074 TYPED_TEST(ByteVectorBasicTest, Pack1valone) {
00075 ByteVector b;
00076 Value<TypeParam> v;
00077 TypeParam ref = 1;
00078 v = ref;
00079 EXPECT_TRUE(b.pack(v));
00080 EXPECT_EQ(sizeof(TypeParam), b.size());
00081 EXPECT_TRUE(b.unpack(v));
00082 EXPECT_EQ(ref, v());
00083 }
00084
00085 TYPED_TEST(ByteVectorBasicTest, Pack1valmax) {
00086 ByteVector b;
00087 Value<TypeParam> v;
00088 TypeParam ref = std::numeric_limits<TypeParam>::max();
00089 v = ref;
00090 EXPECT_TRUE(b.pack(v));
00091 EXPECT_EQ(sizeof(TypeParam), b.size());
00092 EXPECT_TRUE(b.unpack(v));
00093 EXPECT_EQ(ref, v());
00094 }
00095
00096 TYPED_TEST(ByteVectorBasicTest, Pack1valmin) {
00097 ByteVector b;
00098 Value<TypeParam> v;
00099 TypeParam ref = std::numeric_limits<TypeParam>::min();
00100 v = ref;
00101 EXPECT_TRUE(b.pack(v));
00102 EXPECT_EQ(sizeof(TypeParam), b.size());
00103 EXPECT_TRUE(b.unpack(v));
00104 EXPECT_EQ(ref, v());
00105 }
00106
00107 TYPED_TEST(ByteVectorBasicTest, Pack10zero) {
00108 ByteVector b;
00109 TypeParam ref = 0;
00110 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
00111 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00112 for(int i = 0; i < 10; ++i) {
00113 EXPECT_TRUE(b.unpack(ref));
00114 EXPECT_EQ(TypeParam(0), ref);
00115 }
00116 }
00117
00118 TYPED_TEST(ByteVectorBasicTest, Pack10one) {
00119 ByteVector b;
00120 TypeParam ref = 1;
00121 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
00122 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00123 for(int i = 0; i < 10; ++i) {
00124 EXPECT_TRUE(b.unpack(ref));
00125 EXPECT_EQ(TypeParam(1), ref);
00126 }
00127 }
00128
00129 TYPED_TEST(ByteVectorBasicTest, Pack10max) {
00130 ByteVector b;
00131 TypeParam ref = std::numeric_limits<TypeParam>::max();
00132 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
00133 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00134 for(int i = 0; i < 10; ++i) {
00135 EXPECT_TRUE(b.unpack(ref));
00136 EXPECT_EQ(std::numeric_limits<TypeParam>::max(), ref);
00137 }
00138 }
00139
00140 TYPED_TEST(ByteVectorBasicTest, Pack10min) {
00141 ByteVector b;
00142 TypeParam ref = std::numeric_limits<TypeParam>::min();
00143 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
00144 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00145 for(int i = 0; i < 10; ++i) {
00146 EXPECT_TRUE(b.unpack(ref));
00147 EXPECT_EQ(std::numeric_limits<TypeParam>::min(), ref);
00148 }
00149 }
00150
00151 TYPED_TEST(ByteVectorBasicTest, Pack10valzero) {
00152 ByteVector b;
00153 Value<TypeParam> v;
00154 TypeParam ref = 0;
00155 v = ref;
00156 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
00157 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00158 for(int i = 0; i < 10; ++i) {
00159 EXPECT_TRUE(b.unpack(v));
00160 EXPECT_EQ(ref, v());
00161 }
00162 }
00163
00164 TYPED_TEST(ByteVectorBasicTest, Pack10valone) {
00165 ByteVector b;
00166 Value<TypeParam> v;
00167 TypeParam ref = 1;
00168 v = ref;
00169 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
00170 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00171 for(int i = 0; i < 10; ++i) {
00172 EXPECT_TRUE(b.unpack(v));
00173 EXPECT_EQ(ref, v());
00174 }
00175 }
00176
00177 TYPED_TEST(ByteVectorBasicTest, Pack10valmax) {
00178 ByteVector b;
00179 Value<TypeParam> v;
00180 TypeParam ref = std::numeric_limits<TypeParam>::max();
00181 v = ref;
00182 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
00183 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00184 for(int i = 0; i < 10; ++i) {
00185 EXPECT_TRUE(b.unpack(v));
00186 EXPECT_EQ(ref, v());
00187 }
00188 }
00189
00190 TYPED_TEST(ByteVectorBasicTest, Pack10valmin) {
00191 ByteVector b;
00192 Value<TypeParam> v;
00193 TypeParam ref = std::numeric_limits<TypeParam>::min();
00194 v = ref;
00195 for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
00196 EXPECT_EQ(10 * sizeof(TypeParam), b.size());
00197 for(int i = 0; i < 10; ++i) {
00198 EXPECT_TRUE(b.unpack(v));
00199 EXPECT_EQ(ref, v());
00200 }
00201 }
00202
00203 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleZero) {
00204 ByteVector b;
00205 double ref = 0.0;
00206 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00207 EXPECT_EQ(sizeof(TypeParam), b.size());
00208 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00209 EXPECT_DOUBLE_EQ(0, ref);
00210 }
00211
00212 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleTen) {
00213 ByteVector b;
00214 double ref = 10.0;
00215 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00216 EXPECT_EQ(sizeof(TypeParam), b.size());
00217 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00218 EXPECT_DOUBLE_EQ(10.f, ref);
00219 }
00220
00221 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleSaturationMax) {
00222 ByteVector b;
00223 double ref = (double)std::numeric_limits<TypeParam>::max() + 1.0;
00224 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00225 EXPECT_EQ(sizeof(TypeParam), b.size());
00226 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00227 EXPECT_DOUBLE_EQ(std::numeric_limits<TypeParam>::max(), ref);
00228 }
00229
00230 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleSaturationMin) {
00231 ByteVector b;
00232 double ref = (double)std::numeric_limits<TypeParam>::min() - 1.0;
00233 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00234 EXPECT_EQ(sizeof(TypeParam), b.size());
00235 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00236 EXPECT_DOUBLE_EQ(std::numeric_limits<TypeParam>::min(), ref);
00237 }
00238
00239 TYPED_TEST(ByteVectorScaledTest, Pack1FloatZero) {
00240 ByteVector b;
00241 float ref = 0.0;
00242 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00243 EXPECT_EQ(sizeof(TypeParam), b.size());
00244 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00245 EXPECT_FLOAT_EQ(0.f, ref);
00246 }
00247
00248 TYPED_TEST(ByteVectorScaledTest, Pack1FloatTen) {
00249 ByteVector b;
00250 float ref = 10.0;
00251 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00252 EXPECT_EQ(sizeof(TypeParam), b.size());
00253 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00254 EXPECT_FLOAT_EQ(10.f, ref);
00255 }
00256
00257 TYPED_TEST(ByteVectorScaledTest, Pack1FloatSaturationMax) {
00258 ByteVector b;
00259 float ref = (float)std::numeric_limits<TypeParam>::max() + 1.0;
00260 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00261 EXPECT_EQ(sizeof(TypeParam), b.size());
00262 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00263 EXPECT_FLOAT_EQ(std::numeric_limits<TypeParam>::max(), ref);
00264 }
00265
00266 TYPED_TEST(ByteVectorScaledTest, Pack1FloatSaturationMin) {
00267 ByteVector b;
00268 float ref = (float)std::numeric_limits<TypeParam>::min() - 1.0;
00269 EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
00270 EXPECT_EQ(sizeof(TypeParam), b.size());
00271 EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
00272 EXPECT_FLOAT_EQ(std::numeric_limits<TypeParam>::min(), ref);
00273 }
00274
00275 }
00276
00277 int main(int argc, char **argv) {
00278 ::testing::InitGoogleTest(&argc, argv);
00279 return RUN_ALL_TESTS();
00280 }