16 #include "eckit/testing/Test.h"
17 #include "eckit/system/SystemInfo.h"
18 #include "eckit/eckit_ecbuild_config.h"
25 using namespace eckit::testing;
41 const std::string& codec_name,
42 bool bigEndian=
false) {
44 data.insert(data.begin(), 4, 0);
45 data[bigEndian ? 3 : 0] =
static_cast<unsigned char>(codec_name.size());
47 data.insert(data.begin() + 4, codec_name.begin(), codec_name.end());
49 return 4 + codec_name.length();
53 CASE(
"Character strings can be stored in a flat list, and indexed") {
57 const char* source_data[] = {
61 "\x00\x00\x00\x00\x00\x00\x00\x00",
62 "\x00\x00\x00\x00\x00\x00\x00\x00",
63 "\x00\x00\x00\x00\x00\x00\x00\x00",
71 "\x08\x00\x00\x00",
"ghijklmn",
"\x00\x00\x00\x00",
"\x00\x00\x00\x00",
72 "\x0c\x00\x00\x00",
"uvwxyzabcdef",
"\x00\x00\x00\x00",
"\x01\x00\x00\x00",
73 "\x08\x00\x00\x00",
"opqrstuv",
"\x00\x00\x00\x00",
"\x02\x00\x00\x00",
74 "\x02\x00\x00\x00",
"ab",
"\x00\x00\x00\x00",
"\x03\x00\x00\x00",
75 "\x06\x00\x00\x00",
"ghijkl",
"\x00\x00\x00\x00",
"\x04\x00\x00\x00",
76 "\x08\x00\x00\x00",
"mnopqrst",
"\x00\x00\x00\x00",
"\x05\x00\x00\x00",
81 for (
int i = 0;
i < 4;
i++) {
83 bool bigEndianSource = (
i % 2 == 0);
85 bool bits16 = (
i > 1);
87 std::vector<unsigned char> data;
89 for (
size_t j = 0; j <
sizeof(source_data) /
sizeof(
const char*); j++) {
91 (j < 5) ? ((j == 0 || j == 4) ? 4 : 8)
92 : ((j+2) % 4 == 0 ? ::strlen(source_data[j]) : 4);
93 data.insert(data.end(), source_data[j], source_data[j] + len);
96 if (bigEndianSource && !((j > 5) && ((j+2) % 4 == 0)))
97 std::reverse(data.end()-len, data.end());
102 for (
int n = 5; n >= 0; n--) {
103 if (bits16 && bigEndianSource)
105 data.push_back(
static_cast<unsigned char>(n));
106 if (bits16 && !bigEndianSource)
113 GeneralDataStream ds(bigEndianSource != eckit::system::SystemInfo::isBigEndian(), &data[0], data.size());
115 std::unique_ptr<Codec>
c;
116 if (bigEndianSource == eckit::system::SystemInfo::isBigEndian()) {
131 EXPECT(ds.
position() == eckit::Offset(148));
135 eckit::Buffer writeBuffer(4096);
136 ::memset(writeBuffer, 0, writeBuffer.size());
137 GeneralDataStream ds2(bigEndianSource != eckit::system::SystemInfo::isBigEndian(), writeBuffer);
142 EXPECT(ds2.
position() == eckit::Offset(148));
152 EXPECT(::memcmp(writeBuffer, &data[0], 148) == 0);
155 for (
size_t i = 148;
i < 154;
i++) {
156 EXPECT(writeBuffer[
i] == 0);
163 int main(
int argc,
char* argv[]) {
165 return run_tests(argc, argv);
eckit::Offset position() const
int main(int argc, char *argv[])
CASE("Character strings can be stored in a flat list, and indexed")
size_t prepend_codec_selection_header(std::vector< unsigned char > &data, const std::string &codec_name, bool bigEndian=false)