IODA Bundle
test_codecs_end_to_end2.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "eckit/io/Buffer.h"
12 #include "eckit/io/DataHandle.h"
13 #include "eckit/io/MemoryHandle.h"
14 #include "eckit/testing/Test.h"
15 
16 #include "odc/core/MetaData.h"
17 #include "odc/Reader.h"
18 #include "odc/Writer.h"
19 #include "odc/tools/MockReader.h"
20 #include "odc/codec/Integer.h"
21 #include "odc/codec/String.h"
22 
23 using namespace eckit::testing;
24 
25 // Constant codecs are a little different from the others, as they can store multiple
26 // different types within the same codec...
27 
28 
29 /// Encoding/decoding using codecs and the reader/writer are tested elsewhere.
30 /// This file is for miscelaneous tests, in case the edge cases elsewhere are insufficient.
31 ///
32 /// @note This is mainly SDS being paranoid about removing apparently duplicate tests when restructuring
33 
34 /// @note This test should be in test_codecs_end_to_end.cc, but it was causing segfaults with the cray
35 /// compiler to put them in the same file. Sigh
36 
37 // ------------------------------------------------------------------------------------------------------
38 
39 namespace {
40 
41  // This looks-like a read-iterator. It isn't, but that doesn't matter!
42 
43  const int num_rows_to_write = 10;
44 
46  public:
48  columns_(1),
49  type_(type),
50  data_(data),
51  nRows_(num_rows_to_write),
52  refCount_(0),
53  noMore_(false) {
54 
55  columns_[0] = new odc::core::Column(columns_);
56  ASSERT(columns_[0]);
57 
58  columns_[0]->name("a-col");
59  columns_[0]->type<odc::core::SameByteOrder>(type_);
60  columns_[0]->hasMissing(false);
61  }
62 
63  odc::core::MetaData& columns() { return columns_; }
64  bool isNewDataset() { return false; }
65  double* data() { return &data_; }
66 
67  bool next() {
68  if (nRows_ == 0) return false;
69  nRows_--;
70  if (nRows_ == 0) noMore_ = true;
71  return true;
72  }
73 
74  protected:
77  double data_;
78  int nRows_;
79 
80  public: // Required for IteratorProxy
81  int refCount_;
82  bool noMore_;
83  };
84 
85  // n.b. Cannot use local classes as template arguments until c++11, so declare it here.
86 
87  // A constant string value (shorter than 8 bytes)
88 
89  const char* const_string_2 = "pies\0\0\0\0";
90 
92  MockReadIteratorConstString2() : MockReadIterator(odc::api::STRING, *reinterpret_cast<const double*>(const_string_2)) {
93  columns_[0]->coder(std::unique_ptr<odc::core::Codec>(new odc::codec::CodecChars<odc::core::SameByteOrder>(odc::api::STRING)));
94  }
95  };
96 }
97 
98 CASE("The constant codec can also store strings shorter than 8 bytes") {
99 
100 
101  // Construct the encoded stuff
102 
103  eckit::Buffer buf(4096);
104 
105  eckit::MemoryHandle writeDH(buf);
106 
107  {
108  odc::Writer<> oda(writeDH);
109  odc::Writer<>::iterator outit = oda.begin();
110 
112  outit->pass1(reader.begin(), reader.end());
113  }
114 
115  // And test that this decodes correctly
116 
117  {
118  eckit::MemoryHandle dh(buf.data(), static_cast<size_t>(writeDH.position()));
119  dh.openForRead();
120  odc::Reader oda(dh);
121 
122  odc::Reader::iterator it = oda.begin();
123  odc::Reader::iterator end = oda.end();
124 
125  EXPECT(it->columns()[0]->name() == "a-col");
126 
127  size_t count = 0;
128  for ( ; it != end; ++it) {
129  double val = (*it)[0];
130  EXPECT(::memcmp(const_string_2, &val, sizeof(val)) == 0);
131  count++;
132  }
133 
134  EXPECT(count == num_rows_to_write);
135 
136  // Check that this has used the constant codec.
137  EXPECT(it->columns()[0]->coder().name() == "constant_string");
138  EXPECT(it->columns()[0]->type() == odc::api::STRING);
139  }
140 }
141 
142 // ------------------------------------------------------------------------------------------------------
143 
144 int main(int argc, char* argv[]) {
145  return run_tests(argc, argv);
146 }
147 
void oda
static void count(void *counter, const double *data, size_t n)
Definition: UnitTests.cc:531
unsigned long pass1(T b, const T e)
const core::MetaData & columns() const
Definition: IteratorProxy.h:94
const iterator end()
Definition: MockReader.h:27
Definition: ColumnInfo.h:23
int main(int argc, char *argv[])
CASE("The constant codec can also store strings shorter than 8 bytes")