1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. In applying
* this licence, ECMWF does not waive the privileges and immunities granted to it by virtue
* of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
/// @file test_streamable.cc
/// @date Jan 2015
/// @author Florian Rathgeber
#include <sys/types.h>
#include <cstdint>
#include <limits>
#include "eckit/filesystem/PathName.h"
#include "eckit/io/AutoCloser.h"
#include "eckit/serialisation/FileStream.h"
#include "eckit/serialisation/Streamable.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
template <typename T>
class TestItem : public Streamable {
public:
TestItem(const T& s) : payload_(s) {}
TestItem(Stream& s) : Streamable(s), payload_() { s >> payload_; }
// From Streamble
virtual void encode(eckit::Stream& s) const {
Streamable::encode(s);
s << payload_;
}
virtual const eckit::ReanimatorBase& reanimator() const;
// Class members
static const eckit::ClassSpec& classSpec() { return classSpec_; }
T payload_;
protected:
virtual void print(std::ostream& s) const { s << "TestItem " << payload_; }
private:
// -- Class members
static eckit::ClassSpec classSpec_;
// -- Friends
friend std::ostream& operator<<(std::ostream& s, const TestItem& p) {
p.print(s);
return s;
}
};
template <typename T>
const ReanimatorBase& TestItem<T>::reanimator() const {
static Reanimator<TestItem<T> > reanimator;
return reanimator;
}
typedef unsigned char uchar;
typedef long long llong;
#ifndef ulong
typedef unsigned long ulong;
#endif
typedef unsigned long long ullong;
/// Partially specialise ClassSpecs since they must have unique names
template <>
ClassSpec TestItem<char>::classSpec_ = {
&Streamable::classSpec(),
"TestItemChar",
};
template <>
ClassSpec TestItem<uchar>::classSpec_ = {
&Streamable::classSpec(),
"TestItemUChar",
};
template <>
ClassSpec TestItem<bool>::classSpec_ = {
&Streamable::classSpec(),
"TestItemBool",
};
template <>
ClassSpec TestItem<int>::classSpec_ = {
&Streamable::classSpec(),
"TestItemInt",
};
template <>
ClassSpec TestItem<uint>::classSpec_ = {
&Streamable::classSpec(),
"TestItemUInt",
};
template <>
ClassSpec TestItem<short>::classSpec_ = {
&Streamable::classSpec(),
"TestItemShort",
};
template <>
ClassSpec TestItem<ushort>::classSpec_ = {
&Streamable::classSpec(),
"TestItemUShort",
};
template <>
ClassSpec TestItem<long>::classSpec_ = {
&Streamable::classSpec(),
"TestItemLong",
};
template <>
ClassSpec TestItem<ulong>::classSpec_ = {
&Streamable::classSpec(),
"TestItemULong",
};
template <>
ClassSpec TestItem<llong>::classSpec_ = {
&Streamable::classSpec(),
"TestItemLLong",
};
template <>
ClassSpec TestItem<ullong>::classSpec_ = {
&Streamable::classSpec(),
"TestItemULLong",
};
// NOTE: float is not implemented!
// template <>
// ClassSpec TestItem<float>::classSpec_ = {&Streamable::classSpec(),"TestItemFloat",};
template <>
ClassSpec TestItem<double>::classSpec_ = {
&Streamable::classSpec(),
"TestItemDouble",
};
template <>
ClassSpec TestItem<string>::classSpec_ = {
&Streamable::classSpec(),
"TestItemString",
};
//----------------------------------------------------------------------------------------------------------------------
#define test_decode(TYPE, INITIAL, SUFFIX) \
CASE("test_decode_" #TYPE "_" #SUFFIX) { \
Log::info() << "Manually (de)serialise Streamable with " #TYPE " member" << std::endl; \
PathName filename = PathName::unique("data"); \
std::string filepath = filename.asString(); \
TestItem<TYPE> t(INITIAL); \
{ \
FileStream sout(filepath.c_str(), "w"); \
auto c = closer(sout); \
t.encode(sout); \
} \
{ \
FileStream sin(filepath.c_str(), "r"); \
auto c = closer(sin); \
TestItem<TYPE> t2(sin); \
Log::info() << "original: " << t.payload_ << std::endl; \
Log::info() << "streamed: " << t2.payload_ << std::endl; \
EXPECT(t.payload_ == t2.payload_); \
} \
if (filename.exists()) \
filename.unlink(); \
}
#define test_reanimate(TYPE, INITIAL, SUFFIX) \
CASE("test_reanimate_" #TYPE "_" #SUFFIX) { \
Log::info() << "(de)serialise Streamable with " #TYPE " member via Reanimator" << std::endl; \
PathName filename = PathName::unique("data"); \
std::string filepath = filename.asString(); \
TestItem<TYPE> t(INITIAL); \
{ \
FileStream sout(filepath.c_str(), "w"); \
auto c = closer(sout); \
sout << t; \
} \
{ \
FileStream sin(filepath.c_str(), "r"); \
auto c = closer(sin); \
TestItem<TYPE>* t2 = eckit::Reanimator<TestItem<TYPE> >::reanimate(sin); \
Log::info() << "orginal: " << t.payload_ << std::endl; \
Log::info() << "streamed: " << t2->payload_ << std::endl; \
EXPECT(t.payload_ == t2->payload_); \
delete t2; \
} \
if (filename.exists()) \
filename.unlink(); \
}
test_decode(char, 'A', max);
test_decode(char, 'z', min);
test_reanimate(char, 'A', max);
test_reanimate(char, 'z', min);
test_decode(uchar, 'A', max);
test_reanimate(uchar, 'A', max);
test_decode(bool, true, true);
test_reanimate(bool, true, true);
test_decode(bool, false, false);
test_reanimate(bool, false, false);
test_decode(int, numeric_limits<int32_t>::max(), max);
test_decode(int, numeric_limits<int32_t>::min(), min);
test_reanimate(int, numeric_limits<int32_t>::max(), max);
test_reanimate(int, numeric_limits<int32_t>::min(), min);
test_decode(uint, numeric_limits<uint32_t>::max(), max);
test_reanimate(uint, numeric_limits<uint32_t>::max(), max);
test_decode(short, numeric_limits<short>::max(), max);
test_decode(short, numeric_limits<short>::min(), min);
test_reanimate(short, numeric_limits<short>::max(), max);
test_reanimate(short, numeric_limits<short>::min(), min);
test_decode(ushort, numeric_limits<ushort>::max(), max);
test_reanimate(ushort, numeric_limits<ushort>::max(), max);
// NOTE: long in eckit is always 32 bit!
test_decode(long, numeric_limits<int32_t>::max(), max);
test_decode(long, numeric_limits<int32_t>::min(), min);
test_reanimate(long, numeric_limits<int32_t>::max(), max);
test_reanimate(long, numeric_limits<int32_t>::min(), min);
test_decode(ulong, numeric_limits<uint32_t>::max(), max);
test_reanimate(ulong, numeric_limits<uint32_t>::max(), max);
test_decode(llong, numeric_limits<llong>::max(), max);
test_decode(llong, numeric_limits<llong>::min(), min);
test_reanimate(llong, numeric_limits<llong>::max(), max);
test_reanimate(llong, numeric_limits<llong>::min(), min);
test_decode(ullong, numeric_limits<ullong>::max(), max);
test_reanimate(ullong, numeric_limits<ullong>::max(), max);
// NOTE: float is not implemented!
// test_decode(float, numeric_limits<float>::max(), max);
// test_decode(float, numeric_limits<float>::min(), min);
// test_reanimate(float, numeric_limits<float>::max(), max);
// test_reanimate(float, numeric_limits<float>::min(), min);
test_decode(double, numeric_limits<double>::max(), max);
test_decode(double, numeric_limits<double>::min(), min);
test_reanimate(double, numeric_limits<double>::max(), max);
test_reanimate(double, numeric_limits<double>::min(), min);
test_decode(string, "Hello, World!", Hello);
test_reanimate(string, "Hello, World!", Hello);
test_decode(string, "", empty);
test_reanimate(string, "", empty);
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|