File: tExprNodeSetOpt.cc

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (331 lines) | stat: -rw-r--r-- 13,100 bytes parent folder | download | duplicates (2)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//# tExprNodeSetOpt.cc: Test program for the ExprNodeSeOpt classes
//# Copyright (C) 2022
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//#
//# This program is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
//# more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with this program; if not, write to the Free Software Foundation, Inc.,
//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: casa-feedback@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA

#include <casacore/tables/TaQL/ExprNodeSetOpt.h>
#include <casacore/tables/TaQL/ExprNodeSet.h>
#include <casacore/tables/TaQL/ExprNode.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/iostream.h>

#include <casacore/casa/namespace.h>

// <summary>
// Test program for the ExprNodeSetOpt classes.
// </summary>


// Execute the test for all values in the test vector.
template<typename T>
void doTest (TableExprNodeSetOptBase& set,
             const Vector<T>& testVec, const Vector<Int64>& expFind)
{
  TableExprId id(0);
  for (size_t i=0; i<testVec.size(); ++i) {
    AlwaysAssertExit (set.find(testVec[i]) == expFind[i]);
    AlwaysAssertExit (set.contains(id, testVec[i]) == (expFind[i] >= 0));
  }
  // Do the test for the full array.
  MArray<bool> res = set.contains (id, MArray<T>(testVec));
  AlwaysAssertExit (allEQ(res.array(), expFind>=Int64(0)));
}

// Original and transformed set should give the same results.
template<typename T>
void doTestOrig (TableExprNodeSet& orig, TableExprNodeSetOptBase& set,
                 const Vector<T>& testVec)
{
  TableExprId id(0);
  for (T v : testVec) {
    AlwaysAssertExit (orig.contains(id,v) == set.contains(id,v));
  }
}


void doDoubleContSet()
{
  {
    // Test closed-closed intervals
    std::vector<double> st ({1,3,5,7,9,11,13,15,17,19,21});
    std::vector<double> end({2,4,6,8,10,12,14,16,18,20,22});
    TableExprNodeSetOptContSet<Double,std::less_equal<Double>,std::less_equal<Double>>
      set (TableExprNodeSet(), st, end,
           std::less_equal<Double>(), std::less_equal<Double>(), "CC");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<double> vec({-0.5, 1, 1.5, 2, 2.5, 13, 13.5, 14, 14.5, 21, 21.5, 22, 22.5});
    Vector<Int64>  exp({-1,   0, 0,   0, -1,  6,  6,    6,  -1,   10, 10,   10, -1});
    doTest (set, vec, exp);
  }
  {
    // Test open-closed intervals
    std::vector<double> st ({1,3,5,7,9,11,13,15,17,19,21});
    std::vector<double> end({2,4,6,8,10,12,14,16,18,20,22});
    TableExprNodeSetOptContSet<Double,std::less<Double>,std::less_equal<Double>>
      set (TableExprNodeSet(), st, end,
           std::less<Double>(), std::less_equal<Double>(), "OC");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<double> vec({-0.5, 1, 1.5, 2, 2.5, 13, 13.5, 14, 14.5, 21, 21.5, 22, 22.5});
    Vector<Int64>  exp({-1,  -1, 0,   0, -1,  -1, 6,    6,  -1,   -1, 10,   10, -1});
    doTest (set, vec, exp);
  }
  {
    // Test closed-open intervals
    std::vector<double> st ({1,3,5,7,9,11,13,15,17,19,21});
    std::vector<double> end({2,4,6,8,10,12,14,16,18,20,22});
    TableExprNodeSetOptContSet<Double,std::less_equal<Double>,std::less<Double>>
      set (TableExprNodeSet(), st, end,
           std::less_equal<Double>(), std::less<Double>(), "CO");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<double> vec({-0.5, 1, 1.5, 2, 2.5, 13, 13.5, 14, 14.5, 21, 21.5, 22, 22.5});
    Vector<Int64>  exp({-1,   0, 0,  -1, -1,  6,  6,    -1, -1,   10, 10,   -1, -1});
    doTest (set, vec, exp);
  }
  {
    // Test open-open intervals
    std::vector<double> st ({1,3,5,7,9,11,13,15,17,19,21});
    std::vector<double> end({2,4,6,8,10,12,14,16,18,20,22});
    TableExprNodeSetOptContSet<Double,std::less<Double>,std::less<Double>>
      set (TableExprNodeSet(), st, end,
           std::less<Double>(), std::less<Double>(), "OO");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<double> vec({-0.5, 1, 1.5, 2, 2.5, 13, 13.5, 14, 14.5, 21, 21.5, 22, 22.5});
    Vector<Int64>  exp({-1,  -1, 0,  -1, -1,  -1, 6,    -1, -1,   -1, 10,   -1, -1});
    doTest (set, vec, exp);
  }
  {
    // Test intervals with mix of open and closed
    std::vector<double> st  ({1,    19,   3,   21});
    std::vector<double> end ({2,    20,   5,   22});
    std::vector<bool> leftC ({false,false,true,true});
    std::vector<bool> rightC({false,true,true,false});
    TableExprNodeSetOptContSetMixOC<Double> set (TableExprNodeSet(), st, end,
                                                 leftC, rightC);
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<double> vec({-0.5, 1, 1.5, 2, 2.5, 3, 4, 5, 5.5,
                        19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5});
    Vector<Int64> exp ({-1,  -1,  0, -1, -1,  2, 2, 2, -1,
                        -1,  1,   1,  -1,   3,  3,    -1, -1});
    doTest (set, vec, exp);
  }
}

void doStringContSet()
{
  std::vector<String> st ({"a1"});
  std::vector<String> end({"a5"});
  {
    // Test closed-closed intervals
    TableExprNodeSetOptContSet<String,std::less_equal<String>,std::less_equal<String>>
      set (TableExprNodeSet(), st, end,
           std::less_equal<String>(), std::less_equal<String>(), "CC");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<String> vec({"a0", "a1", "a3", "a5", "a6"});
    Vector<Int64>  exp({ -1,   0,    0,    0,    -1});
    doTest (set, vec, exp);
  }
  {
    // Test closed-open intervals
    TableExprNodeSetOptContSet<String,std::less_equal<String>,std::less<String>>
      set (TableExprNodeSet(), st, end,
           std::less_equal<String>(), std::less<String>(), "CO");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<String> vec({"a0", "a1", "a3", "a5", "a6"});
    Vector<Int64>  exp({ -1,   0,    0,    -1,   -1});
    doTest (set, vec, exp);
  }
  {
    // Test open-closed intervals
    TableExprNodeSetOptContSet<String,std::less<String>,std::less_equal<String>>
      set (TableExprNodeSet(), st, end,
           std::less<String>(), std::less_equal<String>(), "OC");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<String> vec({"a0", "a1", "a3", "a5", "a6"});
    Vector<Int64>  exp({ -1,   -1,   0,    0,    -1});
    doTest (set, vec, exp);
  }
  {
    // Test open-open intervals
    TableExprNodeSetOptContSet<String,std::less<String>,std::less<String>>
      set (TableExprNodeSet(), st, end,
           std::less<String>(), std::less<String>(), "OO");
    set.show(cout, 2);
    // Vectors of test values and expected index.
    Vector<String> vec({"a0", "a1", "a3", "a5", "a6"});
    Vector<Int64>  exp({ -1,   -1,   0,    -1,   -1});
    doTest (set, vec, exp);
  }
}

void doIntSet()
{
  Vector<Int64> vecset({1,3,8,10,5});
  TableExprNodeSetOptUSet<Int64> set(TableExprNodeSet(), vecset);
  set.show(cout, 2);
  // Vectors of test values and expected index.
  Vector<Int64> vec({1, 3, 5, 8, 10,  0, 2, 11});
  Vector<Int64> exp({0, 1, 4, 2,  3, -1,-1, -1});
  doTest (set, vec, exp);
}

void doStringSet()
{
  Vector<String> vecset({"a", "d", "b"});
  TableExprNodeSetOptUSet<String> set(TableExprNodeSet(), vecset);
  set.show(cout, 2);
  // Vectors of test values and expected index.
  Vector<String> vec({"aa", "a", "b", "d", "bd", "e"});
  Vector<Int64>  exp({ -1,   0,   2,   1,   -1,  -1});
  doTest (set, vec, exp);
}

void doDoubleTransform()
{
  TableExprNode  st1(1),  st2(6),   st3(25),  st4(30),  st5(33);
  TableExprNode end1(8), end2(20), end3(30), end4(33), end5(34);
  // Test with different leftC and rightC.
  // Also such that the 3rd interval is combined with the 4th,
  // but not 4th with 5th (because end4 and st5 are open).
  {
    TableExprNodeSet set;
    set.add (TableExprNodeSetElem(False, st1, end1, False));
    set.add (TableExprNodeSetElem(True, st3, end3, True));
    set.add (TableExprNodeSetElem(False, st2, end2, True));
    set.add (TableExprNodeSetElem(True, st1+1, end2+1, False));
    set.add (TableExprNodeSetElem(False, st4, end4, False));
    set.add (TableExprNodeSetElem(False, st5, end5, True));
    // Vectors of test values and expected index.
    Vector<double> vec({ 0, 1, 2, 6, 19, 21, 23, 25, 26, 31, 33, 33.5, 34, 34.1});
    Vector<Int64>  exp({-1,-1, 0, 0,  0, -1, -1,  1,  1,  1, -1,  2,    2, -1});
    {
      // No combine, thus 6 intervals with different leftC/rightC.
      TENShPtr trSet = TableExprNodeSetOptContSetBase<Double>::transform (set, False);
      TableExprNodeSetOptContSetMixOC<Double>* p =
        dynamic_cast<TableExprNodeSetOptContSetMixOC<Double>*>(trSet.get());
      AlwaysAssertExit (p);
      AlwaysAssertExit (p->size() == 6);
      trSet->show (cout, 0);
      doTestOrig (set, *p, vec);
    }
    // Should result in (1,21) [25,33) (33,34]
    TENShPtr trSet = TableExprNodeSetOptContSetBase<Double>::transform (set);
    TableExprNodeSetOptContSetMixOC<Double>* p =
      dynamic_cast<TableExprNodeSetOptContSetMixOC<Double>*>(trSet.get());
    AlwaysAssertExit (p);
    AlwaysAssertExit (p->size() == 3);
    trSet->show (cout, 0);
    doTest (*p, vec, exp);
    doTestOrig (set, *p, vec);
  }
  // Test with equal leftC and rightC (all combinations).
  for (int i=0; i<4; ++i) {
    TableExprNodeSet set;
    set.add (TableExprNodeSetElem(i/2==0, st1, end1, i%2==0));
    set.add (TableExprNodeSetElem(i/2==0, st3, end3, i%2==0));
    set.add (TableExprNodeSetElem(i/2==0, st2, end2, i%2==0));
    set.add (TableExprNodeSetElem(i/2==0, st4, end4, i%2==0));
    set.add (TableExprNodeSetElem(i/2==0, st5, end5, i%2==0));
    TENShPtr trSet = TableExprNodeSetOptContSetBase<Double>::transform (set);
    TableExprNodeSetOptContSetBase<Double>* p =
      dynamic_cast<TableExprNodeSetOptContSetBase<Double>*>(trSet.get());
    // Results in 2 elements, but 4 when left and right side are open.
    trSet->show (cout, 0);
    AlwaysAssertExit (p->size() == (i==3 ? 4:2));
    // Vectors of test values and expected index (depending on open/closed).
    Int64 i3 = (i==3 ? 2 : 1);
    Int64 i4 = (i==3 ? 3 : 1);
    Int64 l1 = (i/2==0 ? 0 : -1);
    Int64 l2 = (i/2==0 ? 1 : -1);
    Int64 r1 = (i%2==0 ? 0 : -1);
    Int64 r2 = (i!=3   ? 1 : -1);
    Int64 r3 = (i%2==0 ? i3 : -1);
    Vector<double> vec({ 0, 1, 2, 6, 19, 20, 23, 25, 26, 30, 31, 33, 33.5, 34, 34.1});
    Vector<Int64>  exp({-1,l1, 0, 0,  0, r1, -1, l2,  1, r2, i3, r2, i4,   r3, -1});
    doTest (*p, vec, exp);
    doTestOrig (set, *p, vec);
  }
}

void doDateTransform()
{
  TableExprNode st(datetime("5Apr09/12:"));    // MJD 54926.5
  TableExprNode end(datetime("7May09/12:"));   // MJD 54958.5
  TableExprNode width(4);                      // 4 days
  {
    TableExprNodeSet set;
    set.add (TableExprNodeSetElem(False, st, end, False));
    TENShPtr trSet = TableExprNodeSetOptContSetBase<Double>::transform (set);
    TableExprNodeSetOptContSetBase<Double>* p =
      dynamic_cast<TableExprNodeSetOptContSetBase<Double>*>(trSet.get());
    AlwaysAssertExit (p);
    AlwaysAssertExit (p->size() == 1);
    trSet->show (cout, 0);
    Vector<double> vec({54926.5, 54926.51, 54934, 54958.49,54958.5});
    Vector<Int64>  exp({   -1,       0,        0,     0,      -1});
    doTest (*p, vec, exp);
    doTestOrig (set, *p, vec);
  }
  {
    TableExprNodeSet set;
    set.add (TableExprNodeSetElem(st, width));
    TENShPtr trSet = TableExprNodeSetOptContSetBase<Double>::transform (set);
    TableExprNodeSetOptContSetBase<Double>* p =
      dynamic_cast<TableExprNodeSetOptContSetBase<Double>*>(trSet.get());
    AlwaysAssertExit (p);
    AlwaysAssertExit (p->size() == 1);
    trSet->show (cout, 0);
    Vector<double> vec({54924.49, 54924.5, 54926, 54928.5, 54928.51});
    Vector<Int64>  exp({   -1,        0,       0,     0,      -1});
    doTest (*p, vec, exp);
    doTestOrig (set, *p, vec);
  }
}

int main()
{
  try {
    doDoubleContSet();
    doStringContSet();
    doIntSet();
    doStringSet();
    doDoubleTransform();
    doDateTransform();
  } catch (std::exception& x) {
    cout << "Unexpected exception: " << x.what() << endl;
    return 1;
  } catch (...) {
    cout << "Unexpected unknown exception" << endl;
    return 1;
  }
  cout << "OK" << endl;
  return 0;
}