IODA Bundle
TestOrderBy.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 UnitTest.h
12 ///
13 /// @author Piotr Kuchta, ECMWF, September 2010
14 
15 #include "eckit/filesystem/PathName.h"
16 #include "eckit/io/FileHandle.h"
17 #include "eckit/utils/StringTools.h"
18 
19 #include "odc/api/Odb.h"
20 #include "odc/core/MetaData.h"
21 #include "odc/Select.h"
22 
23 #include "TestCase.h"
24 
25 using namespace std;
26 using namespace eckit;
27 using namespace odc;
28 
29 ///
30 static void test()
31 {
32  {
33  string sql = "select distinct a from \"TestOrderBy_a1to10twice.odb\" order by a;";
34 
35  odc::Select sel(sql);
36  odc::Select::iterator it = sel.begin();
37  odc::Select::iterator end = sel.end();
38 
39  int i = 0;
40  for (; it != end; ++it)
41  {
42  int v = (*it)[0];
43  ASSERT(v == ++i);
44  }
45  ASSERT(i == 10);
46  }
47 
48  {
49  string sql = "select a from \"TestOrderBy_a1to10twice.odb\" order by a;";
50 
51  odc::Select sel(sql);
52  odc::Select::iterator it = sel.begin();
53  odc::Select::iterator end = sel.end();
54 
55  int i = 0, j = 0;
56  for (; it != end; ++it, ++j)
57  {
58  int v = (*it)[0];
59  ASSERT(i <= v);
60  i = v;
61  }
62  ASSERT(i == 10);
63  ASSERT(j == 20);
64  }
65 
66  {
67  string sql = "select distinct a from \"TestOrderBy_a1to10twice.odb\" order by a desc;";
68 
69  odc::Select sel(sql);
70  odc::Select::iterator it = sel.begin();
71  odc::Select::iterator end = sel.end();
72 
73  int i = 10, j = 0;
74  for (; it != end; ++it, ++j)
75  {
76  int v = (*it)[0];
77  ASSERT(i-- == v);
78  }
79  ASSERT(i == 0);
80  ASSERT(j == 10);
81  }
82 
83  {
84  const char *in =
85  "a:REAL,b:REAL,c:STRING\n"
86  "1,10,'one'\n"
87  "1,20,'two'\n"
88  "2,30,'three'\n"
89  "2,40,'four'\n";
90  {
91  FileHandle dh("TestOrderBy.odb");
92  dh.openForWrite(0);
93  AutoClose close(dh);
94  odc::api::odbFromCSV(in, dh);
95  }
96 
97  string sql = "select distinct a,b,c from \"TestOrderBy.odb\" order by a desc, b asc;";
98 
99  odc::Select sel(sql);
100  odc::Select::iterator it = sel.begin();
101  odc::Select::iterator end = sel.end();
102 
103  int i = 0, v1 = 0 , v2 = 0;
104  string s;
105  for (; it != end; ++it, ++i)
106  {
107  v1 = (*it)[0];
108  v2 = (*it)[1];
109  s = (*it).string(2);
110  }
111  ASSERT(i == 4);
112  ASSERT(v1 == 1 && v2 == 20 && StringTools::trim(s) == "two");
113  }
114 }
115 
116 
117 static void setUp()
118 {
119  stringstream s;
120  s << "a:REAL" << std::endl;
121  for (size_t i = 1; i <= 10; ++i) s << i << std::endl;
122  for (size_t i = 1; i <= 10; ++i) s << i << std::endl;
123  FileHandle dh("TestOrderBy_a1to10twice.odb");
124  dh.openForWrite(0);
125  AutoClose close(dh);
126  odc::api::odbFromCSV(s, dh);
127 }
128 static void tearDown(){}
129 
130 SIMPLE_TEST(OrderBy)
#define SIMPLE_TEST(name)
Definition: TestCase.h:66
static void tearDown()
Definition: TestOrderBy.cc:128
static void test()
Definition: TestOrderBy.cc:30
static void setUp()
Definition: TestOrderBy.cc:117
const iterator end()
Definition: Select.cc:77
iterator begin()
Definition: Select.cc:81
std::string trim(const std::string &str)
size_t odbFromCSV(DataHandle &dh_in, DataHandle &dh_out, const std::string &delimiter)
odbFromCSV returns number of lines imported
Definition: Odb.cc:375
Definition: ColumnInfo.h:23
Definition: encode.cc:30