1
//////////////////////////////////////////////////////////////////////////
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
8
//////////////////////////////////////////////////////////////////////////
11
#include "pgsTestSuite.h"
13
#include "pgscript/exceptions/pgsArithmeticException.h"
14
#include "pgscript/expressions/pgsPlus.h"
15
#include "pgscript/objects/pgsNumber.h"
16
#include "pgscript/objects/pgsString.h"
18
void pgsTestSuite::test_object_variable(void)
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
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());
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());
52
for (int i = 1; i <= 100; i++)
54
// [1] Generate a random integer as a string
56
for (int j = 0; j < i; j++)
58
char c = rand() % 9 + 48;
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());
68
// [3] Test copy constructor
70
TS_ASSERT(copy.value() == exp.value() && copy.is_integer());
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());
81
for (int i = 2; i <= 16; i++)
83
// [1] Generate a random real as a string
85
for (int j = 0; j < i / 2; j++)
87
char c = rand() % 9 + 48;
91
for (int j = 0; j < i / 2; j++)
93
char c = rand() % 9 + 48;
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());
102
// [3] Test copy constructor
104
TS_ASSERT(copy.value() == exp.value() && copy.is_real());
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());
115
pgsNumber exp(wxT("+1.5e-300000000000657788"), pgsReal);
116
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
121
pgsNumber exp(wxT("-1.e+0"), pgsReal);
122
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
127
pgsNumber exp(wxT("+.0e-1"), pgsReal);
128
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
133
pgsNumber exp(wxT("-0.0E5"), pgsReal);
134
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
139
pgsNumber exp(wxT("0."), pgsReal);
140
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
145
pgsNumber exp(wxT(".1234567890098765432"), pgsReal);
146
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
151
pgsString exp(wxT("."));
152
TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
157
pgsString exp(wxT(""));
158
TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
163
pgsString exp(wxT("e5"));
164
TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
169
pgsNumber exp(wxT("0e0"), pgsReal);
170
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
175
pgsNumber exp(wxT("100000000000000000e1"), pgsReal);
176
TS_ASSERT(exp.is_real() && !exp.is_integer() && !exp.is_string());
181
pgsString exp(wxT("100000000000000000e"));
182
TS_ASSERT(!exp.is_real() && !exp.is_integer() && exp.is_string());
185
// Test some operations
187
pgsVariable * a = pnew pgsNumber(wxT("123."), pgsReal);
188
pgsVariable * b = pnew pgsNumber(wxT("2"), pgsInt);
189
pgsVariable * c = pnew pgsString(wxT("0x1"));
193
// 123. + 2 gives 125
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());
199
// (123. + 2) + 0x1 gives the concatenation of the strings
201
e = pnew pgsPlus(d, c); // Deletes d and c
207
catch (const pgsArithmeticException &)
213
pgsPlus f(*e); // f is automatically deleted
214
pdelete(e); // Deletes e