1
//////////////////////////////////////////////////////////////////////////
3
// pgScript - PostgreSQL Tools
4
// RCS-ID: $Id: pgsTestObjectRecord.cpp 7758 2009-03-26 20:49:59Z dpage $
5
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
6
// This software is released under the BSD Licence
8
//////////////////////////////////////////////////////////////////////////
11
#include "pgsTestSuite.h"
13
#include "pgscript/expressions/pgsEqual.h"
14
#include "pgscript/expressions/pgsDifferent.h"
15
#include "pgscript/expressions/pgsGreater.h"
16
#include "pgscript/expressions/pgsGreaterEqual.h"
17
#include "pgscript/expressions/pgsLower.h"
18
#include "pgscript/expressions/pgsLowerEqual.h"
19
#include "pgscript/generators/pgsIntegerGen.h"
20
#include "pgscript/generators/pgsRealGen.h"
21
#include "pgscript/generators/pgsStringGen.h"
22
#include "pgscript/objects/pgsNumber.h"
23
#include "pgscript/objects/pgsRecord.h"
24
#include "pgscript/objects/pgsString.h"
26
void pgsTestSuite::test_object_record(void)
28
const int max = +16383;
29
const int min = -16384;
30
const int nb_iterations = 10; // Must be greater than 3
31
const USHORT nb_columns = 5; // Must be greater than 2
35
// Test record with 0 column
38
TS_ASSERT(!rec.insert(0, 0, pnew pgsString(wxT(""))));
39
TS_ASSERT(rec.count_lines() == 1);
40
TS_ASSERT(rec.count_columns() == 0);
43
// Test the string value of a record
44
// Special characters must be escaped an each line must be returned
45
// as a composite structure ("string val", 12345, ...)
48
TS_ASSERT(rec.insert(0, 0, pnew pgsString(wxT("\"string\""))));
49
TS_ASSERT(rec.value() == wxT("(\"\\\"string\\\"\")"));
50
TS_ASSERT(rec.insert(1, 0, pnew pgsString(wxT("str\\i\nng"))));
51
TS_ASSERT(rec.value() == wxT("(\"\\\"string\\\"\")\n(\"str\\\\i\nng\")"));
54
// Insert data and compare two records
56
// Prepare random generator to generate random data
57
pgsIntegerGen int_gen(min, max, false);
58
pgsRealGen real_gen(min, max, 3, false);
59
pgsStringGen string_gen(10, 20, 1);
62
for (int i = 0; i < nb_iterations; i++)
64
pgsRecord rec(nb_columns);
66
// Basic TS_ASSERTs (no line, 'nb_columns' columns)
68
TS_ASSERT(rec.count_lines() == 0
69
&& rec.count_columns() == nb_columns);
70
TS_ASSERT(rec.is_record() && !rec.is_string()
74
// Get columns and elements (but there is nothing)
76
TS_ASSERT(rec.get_column(wxT("")) == rec.count_columns());
77
TS_ASSERT(rec.get_column(wxT("a")) == rec.count_columns());
78
TS_ASSERT(rec.get(0, 0)->value() == wxT("")); // Nothing exists
79
TS_ASSERT(rec.get(1, 0)->value() == wxT(""));
82
USHORT line = rand() % 9 + 1; // Index of the line (1 < line < 10)
84
// Give names to columns and check them
86
TS_ASSERT(!rec.set_column_name(nb_columns, wxT("a")));
87
for (USHORT j = 0; j < nb_columns; j++)
89
TS_ASSERT(rec.set_column_name(j, (wxString() << j)));
91
for (USHORT j = 0; j < nb_columns; j++)
93
TS_ASSERT(rec.get_column((wxString() << j)) == j);
97
// Insert in a wrong location
99
TS_ASSERT(!rec.insert(0, nb_columns,
100
pnew pgsString(int_gen.random())));
101
TS_ASSERT(!rec.insert(0, wx_static_cast(USHORT, -1), 0));
102
TS_ASSERT(!rec.insert(0, wx_static_cast(USHORT, -1),
103
pnew pgsString(int_gen.random())));
106
// Insert random elements in the first 3 columns
108
// (1 <= line <= 10), 0
109
TS_ASSERT(rec.insert(line, 0, pnew pgsNumber(int_gen.random(),
111
// (1 <= line <= 10), 1
112
TS_ASSERT(rec.insert(line, 1, pnew pgsNumber(real_gen.random(),
114
// (1 <= line <= 10), 2
115
TS_ASSERT(rec.insert(line, 2,
116
pnew pgsString(string_gen.random())));
119
// Verify the number of lines and the number of columns
120
// Verify that the first 3 columns contain something and not the other ones
122
TS_ASSERT(rec.count_lines() == line + 1);
123
TS_ASSERT(rec.count_columns() == nb_columns);
124
for (USHORT i = 0; i < 3; i++)
126
TS_ASSERT(rec.get(line, rec.get_column((wxString()
127
<< i)))->value() != wxT(""));
129
for (USHORT i = 3; i < nb_columns; i++)
131
TS_ASSERT(rec.get(line, rec.get_column((wxString()
132
<< i)))->value() == wxT(""));
136
// Assignment operator is forbidden
139
// Test the copy constructor
142
// Check that the copy is equal with the original
145
TS_ASSERT(rec == copy && copy == rec);
146
TS_ASSERT(rec <= copy && copy <= rec);
147
TS_ASSERT(rec >= copy && copy >= rec);
148
TS_ASSERT(!(rec < copy) && !(copy < rec));
149
TS_ASSERT(!(rec > copy) && !(copy > rec));
150
TS_ASSERT(rec.almost_equal(copy) && copy.almost_equal(rec));
153
// Check that the copy is equal with the original
156
pgsEqual eq(rec.clone(), copy.clone());
157
TS_ASSERT(eq.eval(vars)->value() == wxT("1"));
158
eq = pgsEqual(rec.clone(), copy.clone(), false);
159
TS_ASSERT(eq.eval(vars)->value() == wxT("1"));
160
pgsDifferent neq(rec.clone(), copy.clone());
161
TS_ASSERT(neq.eval(vars)->value() == wxT("0"));
162
pgsLowerEqual leq(rec.clone(), copy.clone());
163
TS_ASSERT(leq.eval(vars)->value() == wxT("1"));
164
pgsGreaterEqual geq(rec.clone(), copy.clone());
165
TS_ASSERT(geq.eval(vars)->value() == wxT("1"));
166
pgsLower lo(rec.clone(), copy.clone());
167
TS_ASSERT(lo.eval(vars)->value() == wxT("0"));
168
pgsGreater gr(rec.clone(), copy.clone());
169
TS_ASSERT(gr.eval(vars)->value() == wxT("0"));
172
wxString aux = rec.get(line, 2)->value();
173
rec.insert(line, 2, pnew pgsString(aux.Upper()));
175
// Test case sensitivity
178
TS_ASSERT(rec != copy && copy != rec);
179
TS_ASSERT(!(rec <= copy) && !(copy <= rec));
180
TS_ASSERT(!(rec >= copy) && !(copy >= rec));
181
TS_ASSERT(!(rec < copy) && !(copy < rec));
182
TS_ASSERT(!(rec > copy) && !(copy > rec));
183
TS_ASSERT(rec.almost_equal(copy) && copy.almost_equal(rec));
186
// Test case sensitivity
189
pgsEqual eq(rec.clone(), copy.clone());
190
TS_ASSERT(eq.eval(vars)->value() == wxT("0"));
191
eq = pgsEqual(rec.clone(), copy.clone(), false);
192
TS_ASSERT(eq.eval(vars)->value() == wxT("1"));
195
rec.insert(line, 2, pnew pgsString(aux));
197
// Add some elements to the copy
199
bool ins1 = copy.insert(0, 0, pnew pgsNumber(int_gen.random(),
202
bool ins2 = copy.insert(0, 1, pnew pgsNumber(real_gen.random(),
205
// Overwrites the previous element
206
bool ins3 = copy.insert(0, 1,
207
pnew pgsString(string_gen.random()));
213
TS_ASSERT(copy.count_lines() == line + 1);
214
TS_ASSERT(copy.count_columns() == nb_columns);
217
// Destroy copy automatically
221
// Remove lines and compare two records
223
pgsIntegerGen int_gen(min, max, false);
224
pgsRealGen real_gen(min, max, 3, false);
225
pgsStringGen string_gen(10, 20, 1);
229
// Insert random data
231
for (int i = 0; i < nb_iterations; i++)
233
rec.insert(i, 0, pnew pgsString(int_gen.random()));
234
rec.insert(i, 1, pnew pgsString(real_gen.random()));
235
rec.insert(i, 2, pnew pgsString(string_gen.random()));
239
// Check a few things
241
TS_ASSERT(rec.count_lines() == nb_iterations);
242
TS_ASSERT(rec.get(nb_iterations - 1, 0)->value() != wxT(""));
245
// Create a fresh copy
250
TS_ASSERT(!copy.remove_line(nb_iterations));
251
TS_ASSERT(copy.remove_line(1));
252
TS_ASSERT(copy.count_lines() == nb_iterations - 1);
253
TS_ASSERT(copy.get(nb_iterations - 1, 0)->value() == wxT(""));
256
// Comparisons (copy is included in rec)
257
// Both pgsRecord and pgsOperation ways
259
TS_ASSERT(rec != copy && copy != rec);
260
TS_ASSERT(!(rec <= copy) && copy <= rec);
261
TS_ASSERT(rec >= copy && !(copy >= rec));
262
TS_ASSERT(!(rec < copy) && copy < rec);
263
TS_ASSERT(rec > copy && !(copy > rec));
265
pgsEqual eq(rec.clone(), copy.clone());
266
TS_ASSERT(eq.eval(vars)->value() == wxT("0"));
267
pgsDifferent neq(rec.clone(), copy.clone());
268
TS_ASSERT(neq.eval(vars)->value() == wxT("1"));
269
pgsLowerEqual leq(rec.clone(), copy.clone());
270
TS_ASSERT(leq.eval(vars)->value() == wxT("0"));
271
pgsGreaterEqual geq(rec.clone(), copy.clone());
272
TS_ASSERT(geq.eval(vars)->value() == wxT("1"));
273
pgsLower lo(rec.clone(), copy.clone());
274
TS_ASSERT(lo.eval(vars)->value() == wxT("0"));
275
pgsGreater gr(rec.clone(), copy.clone());
276
TS_ASSERT(gr.eval(vars)->value() == wxT("1"));
281
TS_ASSERT(copy.remove_line(nb_iterations - 2));
282
TS_ASSERT(copy.count_lines() == nb_iterations - 2);
283
TS_ASSERT(copy.get(nb_iterations - 2, 0)->value() == wxT(""));
286
// Comparisons (copy should be included in rec)
288
TS_ASSERT(rec != copy && copy != rec);
289
TS_ASSERT(!(rec <= copy) && copy <= rec);
290
TS_ASSERT(rec >= copy && !(copy >= rec));
291
TS_ASSERT(!(rec < copy) && copy < rec);
292
TS_ASSERT(rec > copy && !(copy > rec));
296
// Copy should still be included in rec as it contains nothing
298
while (copy.count_lines() > 0)
300
TS_ASSERT(copy.remove_line(copy.count_lines() - 1));
303
TS_ASSERT(rec != copy && copy != rec);
304
TS_ASSERT(!(rec <= copy) && copy <= rec);
305
TS_ASSERT(rec >= copy && !(copy >= rec));
306
TS_ASSERT(!(rec < copy) && copy < rec);
307
TS_ASSERT(rec > copy && !(copy > rec));
316
// Nothing in the records: they should be equal
318
TS_ASSERT(rec == cmp && cmp == rec);
319
TS_ASSERT(rec <= cmp && cmp <= rec);
320
TS_ASSERT(rec >= cmp && cmp >= rec);
321
TS_ASSERT(!(rec < cmp) && !(cmp < rec));
322
TS_ASSERT(!(rec > cmp) && !(cmp > rec));
323
TS_ASSERT(rec.almost_equal(cmp));
326
rec.insert(0, 0, pnew pgsString(wxT("a")));
328
// cmp should be included in rec
330
TS_ASSERT(rec != cmp && cmp != rec);
331
TS_ASSERT(!(rec <= cmp) && cmp <= rec);
332
TS_ASSERT(rec >= cmp && !(cmp >= rec));
333
TS_ASSERT(!(rec < cmp) && cmp < rec);
334
TS_ASSERT(rec > cmp && !(cmp > rec));
335
TS_ASSERT(!rec.almost_equal(cmp));
338
rec.insert(1, 0, pnew pgsString(wxT("b")));
340
// cmp should be included in rec
342
TS_ASSERT(rec != cmp && cmp != rec);
343
TS_ASSERT(!(rec <= cmp) && cmp <= rec);
344
TS_ASSERT(rec >= cmp && !(cmp >= rec));
345
TS_ASSERT(!(rec < cmp) && cmp < rec);
346
TS_ASSERT(rec > cmp && !(cmp > rec));
347
TS_ASSERT(!rec.almost_equal(cmp));
350
cmp.insert(0, 0, pnew pgsString(wxT("b")));
352
// cmp should be included in rec
354
TS_ASSERT(rec != cmp && cmp != rec);
355
TS_ASSERT(!(rec <= cmp) && cmp <= rec);
356
TS_ASSERT(rec >= cmp && !(cmp >= rec));
357
TS_ASSERT(!(rec < cmp) && cmp < rec);
358
TS_ASSERT(rec > cmp && !(cmp > rec));
359
TS_ASSERT(!rec.almost_equal(cmp));
362
cmp.insert(1, 0, pnew pgsString(wxT("A")));
364
// cmp should be almost equal to rec (case insensitive)
365
// The order does not matter
367
TS_ASSERT(rec != cmp && cmp != rec);
368
TS_ASSERT(!(rec <= cmp) && !(cmp <= rec));
369
TS_ASSERT(!(rec >= cmp) && !(cmp >= rec));
370
TS_ASSERT(!(rec < cmp) && !(cmp < rec));
371
TS_ASSERT(!(rec > cmp) && !(cmp > rec));
372
TS_ASSERT(rec.almost_equal(cmp));
375
cmp.insert(1, 0, pnew pgsString(wxT("a")));
377
// cmp should be equal to rec
378
// The order does not matter
380
TS_ASSERT(rec == cmp && cmp == rec);
381
TS_ASSERT(rec <= cmp && cmp <= rec);
382
TS_ASSERT(rec >= cmp && cmp >= rec);
383
TS_ASSERT(!(rec < cmp) && !(cmp < rec));
384
TS_ASSERT(!(rec > cmp) && !(cmp > rec));
385
TS_ASSERT(rec.almost_equal(cmp));
388
cmp.insert(2, 0, pnew pgsString(wxT("a")));
390
// rec should be included in cmp
392
TS_ASSERT(rec != cmp && cmp != rec);
393
TS_ASSERT(rec <= cmp && !(cmp <= rec));
394
TS_ASSERT(!(rec >= cmp) && cmp >= rec);
395
TS_ASSERT(rec < cmp && !(cmp < rec));
396
TS_ASSERT(!(rec > cmp) && cmp > rec);
397
TS_ASSERT(!rec.almost_equal(cmp));
402
// cmp should be equal to rec
403
// The order does not matter
405
TS_ASSERT(rec == cmp && cmp == rec);
406
TS_ASSERT(rec <= cmp && cmp <= rec);
407
TS_ASSERT(rec >= cmp && cmp >= rec);
408
TS_ASSERT(!(rec < cmp) && !(cmp < rec));
409
TS_ASSERT(!(rec > cmp) && !(cmp > rec));
410
TS_ASSERT(rec.almost_equal(cmp));
415
TS_ASSERT(rec.get_line(0)->value() != cmp.get_line(0)->value());
416
TS_ASSERT(rec.get_line(0)->value() == cmp.get_line(1)->value());