~ubuntu-branches/debian/sid/pgadmin3/sid

« back to all changes in this revision

Viewing changes to xtra/pgscript/test/pgsTestObjectRecord.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2009-07-30 12:27:16 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730122716-fddbh42on721bbs2
Tags: 1.10.0-1
* New upstream release.
* Adjusted watch file to match release candidates.
* Updated to Standards-Version 3.8.2:
  - Moved to Section: database.
  - Add DEB_BUILD_OPTIONS support for parallel building.
  - Move from findstring to filter suggestion for DEB_BUILD_OPTIONS parsing.
* pgagent got split into its own separate source package by upstream.
* Exclude Docs.vcproj from installation.
* Move doc-base.enus from pgadmin3 to pgadmin3-data package, the files are
  in there too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
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
 
7
//
 
8
//////////////////////////////////////////////////////////////////////////
 
9
 
 
10
 
 
11
#include "pgsTestSuite.h"
 
12
 
 
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"
 
25
 
 
26
void pgsTestSuite::test_object_record(void)
 
27
{
 
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
 
32
        
 
33
        pgsVarMap vars;
 
34
        
 
35
        // Test record with 0 column
 
36
        {
 
37
                pgsRecord rec(0);
 
38
                TS_ASSERT(!rec.insert(0, 0, pnew pgsString(wxT(""))));
 
39
                TS_ASSERT(rec.count_lines() == 1);
 
40
                TS_ASSERT(rec.count_columns() == 0);
 
41
        }
 
42
 
 
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, ...)
 
46
        {
 
47
                pgsRecord rec(1);
 
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\")"));
 
52
        }
 
53
 
 
54
        // Insert data and compare two records
 
55
        {
 
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);
 
60
                
 
61
                
 
62
                for (int i = 0; i < nb_iterations; i++)
 
63
                {
 
64
                        pgsRecord rec(nb_columns);
 
65
 
 
66
                        // Basic TS_ASSERTs (no line, 'nb_columns' columns)
 
67
                        {
 
68
                                TS_ASSERT(rec.count_lines() == 0
 
69
                                                && rec.count_columns() == nb_columns);
 
70
                                TS_ASSERT(rec.is_record() && !rec.is_string()
 
71
                                                && !rec.is_number());
 
72
                        }
 
73
 
 
74
                        // Get columns and elements (but there is nothing)
 
75
                        {
 
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(""));
 
80
                        }
 
81
 
 
82
                        USHORT line = rand() % 9 + 1; // Index of the line (1 < line < 10)
 
83
 
 
84
                        // Give names to columns and check them
 
85
                        {
 
86
                                TS_ASSERT(!rec.set_column_name(nb_columns, wxT("a")));
 
87
                                for (USHORT j = 0; j < nb_columns; j++)
 
88
                                {
 
89
                                        TS_ASSERT(rec.set_column_name(j, (wxString() << j)));
 
90
                                }
 
91
                                for (USHORT j = 0; j < nb_columns; j++)
 
92
                                {
 
93
                                        TS_ASSERT(rec.get_column((wxString() << j)) == j);
 
94
                                }
 
95
                        }
 
96
 
 
97
                        // Insert in a wrong location
 
98
                        {
 
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())));
 
104
                        }
 
105
 
 
106
                        // Insert random elements in the first 3 columns
 
107
                        {
 
108
                                // (1 <= line <= 10), 0
 
109
                                TS_ASSERT(rec.insert(line, 0, pnew pgsNumber(int_gen.random(),
 
110
                                                pgsInt)));
 
111
                                // (1 <= line <= 10), 1
 
112
                                TS_ASSERT(rec.insert(line, 1, pnew pgsNumber(real_gen.random(),
 
113
                                                pgsReal)));
 
114
                                // (1 <= line <= 10), 2
 
115
                                TS_ASSERT(rec.insert(line, 2,
 
116
                                                pnew pgsString(string_gen.random())));
 
117
                        }
 
118
 
 
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
 
121
                        {
 
122
                                TS_ASSERT(rec.count_lines()   == line + 1);
 
123
                                TS_ASSERT(rec.count_columns() == nb_columns);
 
124
                                for (USHORT i = 0; i < 3; i++)
 
125
                                {
 
126
                                        TS_ASSERT(rec.get(line, rec.get_column((wxString()
 
127
                                                        << i)))->value() != wxT(""));
 
128
                                }
 
129
                                for (USHORT i = 3; i < nb_columns; i++)
 
130
                                {
 
131
                                        TS_ASSERT(rec.get(line, rec.get_column((wxString()
 
132
                                                        << i)))->value() == wxT(""));
 
133
                                }
 
134
                        }
 
135
                        
 
136
                        // Assignment operator is forbidden
 
137
                        // No test
 
138
 
 
139
                        // Test the copy constructor
 
140
                        pgsRecord copy(rec);
 
141
 
 
142
                        // Check that the copy is equal with the original
 
143
                        // pgsRecord way
 
144
                        {
 
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));
 
151
                        }
 
152
                        
 
153
                        // Check that the copy is equal with the original
 
154
                        // pgsOperation way
 
155
                        {
 
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"));
 
170
                        }
 
171
                        
 
172
                        wxString aux = rec.get(line, 2)->value();
 
173
                        rec.insert(line, 2, pnew pgsString(aux.Upper()));
 
174
                        
 
175
                        // Test case sensitivity
 
176
                        // pgsRecord way
 
177
                        {
 
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));
 
184
                        }
 
185
                        
 
186
                        // Test case sensitivity
 
187
                        // pgsOperation way
 
188
                        {
 
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"));
 
193
                        }
 
194
                        
 
195
                        rec.insert(line, 2, pnew pgsString(aux));
 
196
 
 
197
                        // Add some elements to the copy
 
198
                        {
 
199
                                bool ins1 = copy.insert(0, 0, pnew pgsNumber(int_gen.random(),
 
200
                                                pgsInt));
 
201
                                TS_ASSERT(ins1);
 
202
                                bool ins2 = copy.insert(0, 1, pnew pgsNumber(real_gen.random(),
 
203
                                                pgsReal));
 
204
                                TS_ASSERT(ins2);
 
205
                                // Overwrites the previous element
 
206
                                bool ins3 = copy.insert(0, 1,
 
207
                                                pnew pgsString(string_gen.random()));
 
208
                                TS_ASSERT(ins3);
 
209
                        }
 
210
 
 
211
                        // New checks
 
212
                        {
 
213
                                TS_ASSERT(copy.count_lines()   == line + 1);
 
214
                                TS_ASSERT(copy.count_columns() == nb_columns);
 
215
                        }
 
216
                        
 
217
                        // Destroy copy automatically
 
218
                }
 
219
        }
 
220
 
 
221
        // Remove lines and compare two records
 
222
        {
 
223
                pgsIntegerGen int_gen(min, max, false);
 
224
                pgsRealGen real_gen(min, max, 3, false);
 
225
                pgsStringGen string_gen(10, 20, 1);
 
226
 
 
227
                pgsRecord rec(3);
 
228
 
 
229
                // Insert random data
 
230
                {
 
231
                        for (int i = 0; i < nb_iterations; i++)
 
232
                        {
 
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()));
 
236
                        }
 
237
                }
 
238
 
 
239
                // Check a few things
 
240
                {
 
241
                        TS_ASSERT(rec.count_lines() == nb_iterations);
 
242
                        TS_ASSERT(rec.get(nb_iterations - 1, 0)->value() != wxT(""));
 
243
                }
 
244
 
 
245
                // Create a fresh copy
 
246
                pgsRecord copy(rec);
 
247
 
 
248
                // Remove a line
 
249
                {
 
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(""));
 
254
                }
 
255
 
 
256
                // Comparisons (copy is included in rec)
 
257
                // Both pgsRecord and pgsOperation ways
 
258
                {
 
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));
 
264
 
 
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"));
 
277
                }
 
278
 
 
279
                // Remove a line
 
280
                {
 
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(""));
 
284
                }
 
285
 
 
286
                // Comparisons (copy should be included in rec)
 
287
                {
 
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));
 
293
                }
 
294
 
 
295
                // Remove everything
 
296
                // Copy should still be included in rec as it contains nothing
 
297
                {
 
298
                        while (copy.count_lines() > 0)
 
299
                        {
 
300
                                TS_ASSERT(copy.remove_line(copy.count_lines() - 1));
 
301
                        }
 
302
                        
 
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));
 
308
                }
 
309
        }
 
310
        
 
311
        // Compare records
 
312
        {
 
313
                pgsRecord rec(2);
 
314
                pgsRecord cmp(2);
 
315
                
 
316
                // Nothing in the records: they should be equal
 
317
                {
 
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));
 
324
                }
 
325
                
 
326
                rec.insert(0, 0, pnew pgsString(wxT("a")));
 
327
                
 
328
                // cmp should be included in rec
 
329
                {
 
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));
 
336
                }
 
337
                
 
338
                rec.insert(1, 0, pnew pgsString(wxT("b")));
 
339
                
 
340
                // cmp should be included in rec
 
341
                {
 
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));
 
348
                }
 
349
                
 
350
                cmp.insert(0, 0, pnew pgsString(wxT("b")));
 
351
                
 
352
                // cmp should be included in rec
 
353
                {
 
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));
 
360
                }
 
361
                
 
362
                cmp.insert(1, 0, pnew pgsString(wxT("A")));
 
363
                
 
364
                // cmp should be almost equal to rec (case insensitive)
 
365
                // The order does not matter
 
366
                {
 
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));
 
373
                }
 
374
                
 
375
                cmp.insert(1, 0, pnew pgsString(wxT("a")));
 
376
                
 
377
                // cmp should be equal to rec
 
378
                // The order does not matter
 
379
                {
 
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));
 
386
                }
 
387
                
 
388
                cmp.insert(2, 0, pnew pgsString(wxT("a")));
 
389
                
 
390
                // rec should be included in cmp
 
391
                {
 
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));
 
398
                }
 
399
                
 
400
                cmp.remove_line(1);
 
401
                
 
402
                // cmp should be equal to rec
 
403
                // The order does not matter
 
404
                {
 
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));
 
411
                }
 
412
                
 
413
                // Test get_line
 
414
                {
 
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());
 
417
                }
 
418
        }
 
419
}