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

« back to all changes in this revision

Viewing changes to xtra/pgscript/test/pgsTestObjectVariable.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: pgsTestObjectVariable.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/exceptions/pgsArithmeticException.h"
 
14
#include "pgscript/expressions/pgsPlus.h"
 
15
#include "pgscript/objects/pgsNumber.h"
 
16
#include "pgscript/objects/pgsString.h"
 
17
 
 
18
void pgsTestSuite::test_object_variable(void)
 
19
{
 
20
        // A check is done when a pgsNumber is assigned to verify whether it is
 
21
        // a valid number. Default is an integer otherwise pgsReal must be
 
22
        // specified as second parameter of the constructor
 
23
        // pgsString does not perform any check but does not authorize arithmetic
 
24
        // on it even if it stores a number
 
25
        
 
26
        // Test string type
 
27
        {
 
28
                pgsString a(wxT("test"));
 
29
                TS_ASSERT(a.is_string() && !a.is_number() && !a.is_record());
 
30
                pgsString b(wxT("123456."));
 
31
                TS_ASSERT(b.is_string() && !b.is_number() && !b.is_record());
 
32
                pgsString c(wxT("423432"));
 
33
                TS_ASSERT(c.is_string() && !c.is_number() && !c.is_record());
 
34
                pgsString d(wxT("+.644e5"));
 
35
                TS_ASSERT(d.is_string() && !d.is_number() && !d.is_record());
 
36
                pgsString e(wxT("0x0"));
 
37
                TS_ASSERT(e.is_string() && !e.is_number() && !e.is_record());
 
38
        }
 
39
 
 
40
        // Test number type
 
41
        {
 
42
                pgsNumber a(wxT("123456."), pgsReal);
 
43
                TS_ASSERT(a.is_real() && !a.is_string() && !a.is_record());
 
44
                pgsNumber b(wxT("423432"));
 
45
                TS_ASSERT(b.is_integer() && !b.is_string() && !b.is_record());
 
46
                pgsNumber c(wxT("+.644e5"), pgsReal);
 
47
                TS_ASSERT(c.is_real() && !c.is_string() && !c.is_record());
 
48
        }
 
49
 
 
50
        // Test integers
 
51
        {
 
52
                for (int i = 1; i <= 100; i++)
 
53
                {
 
54
                        // [1] Generate a random integer as a string
 
55
                        wxString str_rnd;
 
56
                        for (int j = 0; j < i; j++)
 
57
                        {
 
58
                                char c = rand() % 9 + 48;
 
59
                                str_rnd << c;
 
60
                        }
 
61
                        
 
62
                        // [2] Allocate a number and test type properties
 
63
                        pgsNumber exp(str_rnd);
 
64
                        TS_ASSERT(exp.value() == str_rnd);
 
65
                        TS_ASSERT(exp.is_number() && !exp.is_string() && !exp.is_record());
 
66
                        TS_ASSERT(exp.is_integer() && !exp.is_real());
 
67
                        
 
68
                        // [3] Test copy constructor
 
69
                        pgsNumber copy(exp);
 
70
                        TS_ASSERT(copy.value() == exp.value() && copy.is_integer());
 
71
                        
 
72
                        // [4] Test assignment operator
 
73
                        exp = pgsNumber(wxT("1") + str_rnd, pgsReal);
 
74
                        TS_ASSERT(exp.is_number() && !exp.is_string() && !exp.is_record());
 
75
                        TS_ASSERT(!exp.is_integer() && exp.is_real());
 
76
                }
 
77
        }
 
78
 
 
79
        // Test reals
 
80
        {
 
81
                for (int i = 2; i <= 16; i++)
 
82
                {
 
83
                        // [1] Generate a random real as a string
 
84
                        wxString str_rnd;
 
85
                        for (int j = 0; j < i / 2; j++)
 
86
                        {
 
87
                                char c = rand() % 9 + 48;
 
88
                                str_rnd << c;
 
89
                        }
 
90
                        str_rnd << wxT(".");
 
91
                        for (int j = 0; j < i / 2; j++)
 
92
                        {
 
93
                                char c = rand() % 9 + 48;
 
94
                                str_rnd << c;
 
95
                        }
 
96
                        
 
97
                        // [2] Allocate a number and test type properties
 
98
                        pgsNumber exp(str_rnd, pgsReal);
 
99
                        TS_ASSERT(exp.is_number() && !exp.is_string() && !exp.is_record());
 
100
                        TS_ASSERT(!exp.is_integer() && exp.is_real());
 
101
                        
 
102
                        // [3] Test copy constructor
 
103
                        pgsNumber copy(exp);
 
104
                        TS_ASSERT(copy.value() == exp.value() && copy.is_real());
 
105
                        
 
106
                        // [4] Test assignment operator
 
107
                        exp = pgsNumber(wxT("1") + str_rnd, pgsReal);
 
108
                        TS_ASSERT(exp.is_number() && !exp.is_string() && !exp.is_record());
 
109
                        TS_ASSERT(!exp.is_integer() && exp.is_real());
 
110
                }
 
111
        }
 
112
 
 
113
        // Test real
 
114
        {
 
115
                pgsNumber exp(wxT("+1.5e-300000000000657788"), pgsReal);
 
116
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
117
        }
 
118
 
 
119
        // Test real
 
120
        {
 
121
                pgsNumber exp(wxT("-1.e+0"), pgsReal);
 
122
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
123
        }
 
124
 
 
125
        // Test real
 
126
        {
 
127
                pgsNumber exp(wxT("+.0e-1"), pgsReal);
 
128
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
129
        }
 
130
 
 
131
        // Test real
 
132
        {
 
133
                pgsNumber exp(wxT("-0.0E5"), pgsReal);
 
134
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
135
        }
 
136
 
 
137
        // Test real
 
138
        {
 
139
                pgsNumber exp(wxT("0."), pgsReal);
 
140
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
141
        }
 
142
 
 
143
        // Test real
 
144
        {
 
145
                pgsNumber exp(wxT(".1234567890098765432"), pgsReal);
 
146
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
147
        }
 
148
 
 
149
        // Test string
 
150
        {
 
151
                pgsString exp(wxT("."));
 
152
                TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
 
153
        }
 
154
 
 
155
        // Test string
 
156
        {
 
157
                pgsString exp(wxT(""));
 
158
                TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
 
159
        }
 
160
 
 
161
        // Test string
 
162
        {
 
163
                pgsString exp(wxT("e5"));
 
164
                TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
 
165
        }
 
166
 
 
167
        // Test real
 
168
        {
 
169
                pgsNumber exp(wxT("0e0"), pgsReal);
 
170
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
171
        }
 
172
 
 
173
        // Test real
 
174
        {
 
175
                pgsNumber exp(wxT("100000000000000000e1"), pgsReal);
 
176
                TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
 
177
        }
 
178
 
 
179
        // Test string
 
180
        {
 
181
                pgsString exp(wxT("100000000000000000e"));
 
182
                TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
 
183
        }
 
184
 
 
185
        // Test some operations
 
186
        {
 
187
                pgsVariable * a = pnew pgsNumber(wxT("123."), pgsReal);
 
188
                pgsVariable * b = pnew pgsNumber(wxT("2"), pgsInt);
 
189
                pgsVariable * c = pnew pgsString(wxT("0x1"));
 
190
                
 
191
                pgsVarMap vars;
 
192
                
 
193
                // 123. + 2 gives 125
 
194
                pgsPlus * d = 0;
 
195
                d = pnew pgsPlus(a, b); // Deletes a and b              
 
196
                pgsOperand v = d->eval(vars);
 
197
                TS_ASSERT(v->value() == wxT("125") && v->is_real());
 
198
                
 
199
                // (123. + 2) + 0x1 gives the concatenation of the strings
 
200
                pgsPlus * e = 0;
 
201
                e = pnew pgsPlus(d, c); // Deletes d and c
 
202
                try
 
203
                {
 
204
                        e->eval(vars);
 
205
                        TS_ASSERT(false);
 
206
                }
 
207
                catch (const pgsArithmeticException &)
 
208
                {
 
209
                        
 
210
                }
 
211
                
 
212
                // Test copy
 
213
                pgsPlus f(*e); // f is automatically deleted
 
214
                pdelete(e); // Deletes e
 
215
        }
 
216
}