IODA Bundle
TestMissingValue.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 /// \file TestMissingValue.h
12 ///
13 /// @author Piotr Kuchta, ECMWF, Feb 2009
14 
15 #include "eckit/sql/SQLTypedefs.h"
16 
17 #include "odc/Comparator.h"
18 #include "odc/Select.h"
19 #include "odc/Reader.h"
20 
21 #include "odc/utility/Tracer.h"
22 #include "odc/Writer.h"
23 #include "TestCase.h"
24 
25 using namespace std;
26 using namespace eckit;
27 using namespace odc;
28 using namespace odc::utility;
29 using namespace odc::core;
30 
31 static void setUp()
32 {
33  Tracer t(Log::debug(), "setUp");
34 
35  odc::Writer<> f("TestMissingValue.odb");
36  odc::Writer<>::iterator it = f.begin();
37 
38  it->setNumberOfColumns(2);
39 
40  it->setColumn(0, "lat@hdr", odc::api::REAL);
41  it->missingValue(0, 1);
42 
43  eckit::sql::BitfieldDef bfDef;
44  bfDef.first.push_back("x");
45  bfDef.second.push_back(1);
46  bfDef.first.push_back("y");
47  bfDef.second.push_back(2);
48 
49  it->setBitfieldColumn(1, "bf", odc::api::BITFIELD, bfDef);
50 
51  it->writeHeader();
52 
53  for (size_t i = 0; i <= 2; i++)
54  {
55  (*it)[0] = i;
56  (*it)[1] = i;
57  ++it;
58  }
59 }
60 
61 static void selectIntoSecondFile()
62 {
63  Tracer t(Log::debug(), "selectIntoSecondFile");
64 
65  const string fileName = "TestMissingValue.odb";
66  string sql = "select lat,bf into \"TestMissingValue2.odb\"";
67  sql += " from \"" + fileName + "\" ;";
68 
69  odc::Select f(sql); //, fileName);
70  odc::Select::iterator it = f.begin();
71 
72  ++it; // this is needed to push the second row to the INTO file
73  ++it; // this is needed to push the third row to the INTO file
74 }
75 
76 
77 static void test()
78 {
80 
81  odc::Comparator().compare("TestMissingValue.odb", "TestMissingValue2.odb");
82 
83  {
84  odc::Reader f("TestMissingValue.odb");
85  odc::Reader::iterator fbegin(f.begin());
86  odc::Reader::iterator fend(f.end());
87 
88  odc::Select s("select * from \"TestMissingValue2.odb\";");
89  odc::Select::iterator sbegin(s.begin());
91 
92  odc::Comparator().compare(fbegin, fend, sbegin, send, "TestMissingValue.odb", "SELECT TestMissingValue2.odb");
93  }
94 
95  {
96  odc::Reader f("TestMissingValue2.odb");
97  odc::Reader::iterator it = f.begin();
98  odc::Reader::iterator end = f.end();
99 
100  Column& column = *it->columns()[0];
101  Codec& codec = column.coder();
102 
103  Log::info() << "test: codec: " << codec << std::endl;
104 
105  ASSERT(codec.hasMissing());
106  ASSERT(codec.missingValue() == 1);
107 
108 
109  for (; it != end; ++it)
110  {
111  ASSERT( (*it).missingValue(0) == 1 );
112 
113  if ( (*it)[0] == 1 )
114  ASSERT( (*it).isMissing(0) );
115  else
116  ASSERT( ! (*it).isMissing(0) );
117  }
118  }
119 
120  {
121  // Check the isMissing and missingValue API of SelectIterator
122  odc::Select s("select * from \"TestMissingValue2.odb\";"); //, fileName);
124  odc::Select::iterator e = s.end();
125  for (; i != e; ++i)
126  {
127  ASSERT( (*i).missingValue(0) == 1 );
128  ASSERT( (*i).missingValue(1) == 0 );
129 
130  if ( (*i)[0] == 1 )
131  ASSERT( (*i).isMissing(0) );
132  else
133  ASSERT( ! (*i).isMissing(0) );
134 
135  // For Bitfields missing value by default equals 0
136  if ( (*i)[1] == 0 )
137  ASSERT( (*i).isMissing(1) );
138  else
139  ASSERT( ! (*i).isMissing(1) );
140  }
141  }
142 
143 }
144 
145 
146 static void tearDown() { }
147 
148 
149 
150 SIMPLE_TEST(MissingValue)
#define SIMPLE_TEST(name)
Definition: TestCase.h:66
static void tearDown()
static void test()
static void selectIntoSecondFile()
static void setUp()
bool compare(T1 &it1, const T1 &end1, T2 &it2, const T2 &end2, const std::string &desc1, const std::string &desc2)
Definition: Comparator.h:108
double missingValue(size_t i)
Definition: IteratorProxy.h:99
void writeHeader()
void setNumberOfColumns(size_t n)
Definition: IteratorProxy.h:95
const core::MetaData & columns() const
Definition: IteratorProxy.h:94
int setColumn(size_t index, const std::string &name, api::ColumnType type)
int setBitfieldColumn(size_t index, const std::string &name, api::ColumnType type, eckit::sql::BitfieldDef b)
const iterator end()
Definition: Select.cc:77
iterator begin()
Definition: Select.cc:81
void hasMissing(bool h)
Definition: Codec.h:63
void missingValue(double v)
Definition: Codec.cc:92
Codec & coder() const
Definition: Column.h:51
@ BITFIELD
Definition: ColumnType.h:27
Definition: ColumnInfo.h:23
void send(const eckit::mpi::Comm &comm, const SERIALIZABLE &sendobj, const int dest, const int tag)
Extend eckit Comm for Serializable oops objects.
Definition: oops/mpi/mpi.h:44
Definition: encode.cc:30