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

« back to all changes in this revision

Viewing changes to xtra/pgscript/test/pgsTestExpressionCast.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: pgsTestExpressionCast.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/pgsCastException.h"
 
14
#include "pgscript/objects/pgsVariable.h"
 
15
 
 
16
#include "pgscript/parser.tab.hh"
 
17
typedef pgscript::pgsParser::token token;
 
18
 
 
19
void pgsTestSuite::test_expression_cast(void)
 
20
{
 
21
        pgsCast * cast = 0;
 
22
        pgsVariable * expr = 0;
 
23
        pgsOperand res;
 
24
        pgsVarMap vars;
 
25
 
 
26
        // Convert an integer
 
27
        {
 
28
                // [1] Create source variable
 
29
                expr = pnew pgsNumber(wxT("123"), pgsInt);
 
30
                TS_ASSERT(expr->is_number() && expr->is_integer() && !expr->is_real());
 
31
                TS_ASSERT(!expr->is_string() && !expr->is_record());
 
32
 
 
33
                // [2] Convert it to the first format
 
34
                cast = pnew pgsCast(token::PGS_REAL, expr->clone());
 
35
                res = cast->eval(vars);
 
36
                TS_ASSERT(res->is_number() && !res->is_integer() && res->is_real());
 
37
                TS_ASSERT(!res->is_string() && !res->is_record());
 
38
                TS_ASSERT(res->value() == expr->value());
 
39
                pdelete(cast);
 
40
 
 
41
                // [3] Convert it to the second format
 
42
                cast = pnew pgsCast(token::PGS_STRING, expr->clone());
 
43
                res = cast->eval(vars);
 
44
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
45
                TS_ASSERT(res->is_string() && !res->is_record());
 
46
                TS_ASSERT(res->value() == expr->value());
 
47
                pdelete(cast);
 
48
 
 
49
                // [4] Convert it to the third format
 
50
                cast = pnew pgsCast(token::PGS_RECORD, expr->clone());
 
51
                res = cast->eval(vars);
 
52
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
53
                TS_ASSERT(!res->is_string() && res->is_record());
 
54
                pdelete(cast);
 
55
 
 
56
                // [5] Test copy constructor
 
57
                cast = pnew pgsCast(token::PGS_STRING, expr->clone());
 
58
                pgsCast copy(*cast);
 
59
                res = copy.eval(vars);
 
60
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
61
                TS_ASSERT(res->is_string() && !res->is_record());
 
62
                TS_ASSERT(res->value() == expr->value());
 
63
                pdelete(cast);
 
64
 
 
65
                // [6] Test assignment
 
66
                cast = pnew pgsCast(token::PGS_RECORD, expr->clone());
 
67
                copy = *cast;
 
68
                res = copy.eval(vars);
 
69
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
70
                TS_ASSERT(!res->is_string() && res->is_record());
 
71
                pdelete(cast);
 
72
 
 
73
                // [7] Delete source variable
 
74
                pdelete(expr);
 
75
        }
 
76
 
 
77
        // Convert a real
 
78
        {
 
79
                // [1] Create source variable
 
80
                expr = pnew pgsNumber(wxT("123.5"), pgsReal);
 
81
                TS_ASSERT(expr->is_number() && !expr->is_integer() && expr->is_real());
 
82
                TS_ASSERT(!expr->is_string() && !expr->is_record());
 
83
 
 
84
                // [2] Convert it to the first format
 
85
                cast = pnew pgsCast(token::PGS_INTEGER, expr->clone());
 
86
                res = cast->eval(vars);
 
87
                TS_ASSERT(res->is_number() && res->is_integer() && !res->is_real());
 
88
                TS_ASSERT(!res->is_string() && !res->is_record());
 
89
                TS_ASSERT(res->value() == wxT("123"));
 
90
                pdelete(cast);
 
91
 
 
92
                // [3] Convert it to the second format
 
93
                cast = pnew pgsCast(token::PGS_STRING, expr->clone());
 
94
                res = cast->eval(vars);
 
95
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
96
                TS_ASSERT(res->is_string() && !res->is_record());
 
97
                TS_ASSERT(res->value() == expr->value());
 
98
                pdelete(cast);
 
99
 
 
100
                // [4] Convert it to the third format
 
101
                cast = pnew pgsCast(token::PGS_RECORD, expr->clone());
 
102
                res = cast->eval(vars);
 
103
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
104
                TS_ASSERT(!res->is_string() && res->is_record());
 
105
                pdelete(cast);
 
106
 
 
107
                // [5] Test copy constructor
 
108
                cast = pnew pgsCast(token::PGS_RECORD, expr->clone());
 
109
                pgsCast copy(*cast);
 
110
                res = copy.eval(vars);
 
111
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
112
                TS_ASSERT(!res->is_string() && res->is_record());
 
113
                pdelete(cast);
 
114
 
 
115
                // [6] Test assignment
 
116
                cast = pnew pgsCast(token::PGS_STRING, expr->clone());
 
117
                copy = *cast;
 
118
                res = copy.eval(vars);
 
119
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
120
                TS_ASSERT(res->is_string() && !res->is_record());
 
121
                TS_ASSERT(res->value() == expr->value());
 
122
                pdelete(cast);
 
123
 
 
124
                // [7] Delete source variable
 
125
                pdelete(expr);
 
126
        }
 
127
 
 
128
        // Convert a string
 
129
        {
 
130
                // [1] Create source variable
 
131
                expr = pnew pgsString(wxT("azerty"));
 
132
                TS_ASSERT(!expr->is_number() && !expr->is_integer() && !expr->is_real());
 
133
                TS_ASSERT(expr->is_string() && !expr->is_record());
 
134
 
 
135
                // [2] Convert it to the first format
 
136
                cast = pnew pgsCast(token::PGS_INTEGER, expr->clone());
 
137
                try
 
138
                {
 
139
                        res = cast->eval(vars);
 
140
                        TS_ASSERT(false);
 
141
                }
 
142
                catch (const pgsCastException &)
 
143
                {
 
144
                        pdelete(cast);
 
145
                }
 
146
 
 
147
                // [3] Convert it to the second format
 
148
                cast = pnew pgsCast(token::PGS_REAL, expr->clone());
 
149
                try
 
150
                {
 
151
                        res = cast->eval(vars);
 
152
                        TS_ASSERT(false);
 
153
                }
 
154
                catch (const pgsCastException &)
 
155
                {
 
156
                        pdelete(cast);
 
157
                }
 
158
 
 
159
                // [4] Convert it to the third format
 
160
                cast = pnew pgsCast(token::PGS_RECORD, expr->clone());
 
161
                res = cast->eval(vars);
 
162
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
163
                TS_ASSERT(!res->is_string() && res->is_record());
 
164
                pdelete(cast);
 
165
 
 
166
                // [5] Delete source variable
 
167
                pdelete(expr);
 
168
        }
 
169
 
 
170
        // Convert a record
 
171
        {
 
172
                // [1] Create source variable
 
173
                pgsRecord * rec = 0;
 
174
                rec = pnew pgsRecord(2);
 
175
                rec->insert(0, 0, pnew pgsString(wxT("abc")));
 
176
                rec->insert(0, 1, pnew pgsString(wxT("def")));
 
177
                expr = dynamic_cast<pgsVariable *>(rec);
 
178
                TS_ASSERT(!expr->is_number() && !expr->is_integer() && !expr->is_real());
 
179
                TS_ASSERT(!expr->is_string() && expr->is_record());
 
180
 
 
181
                // [2] Convert it to the first format
 
182
                cast = pnew pgsCast(token::PGS_INTEGER, expr->clone());
 
183
                try
 
184
                {
 
185
                        res = cast->eval(vars);
 
186
                        TS_ASSERT(false);
 
187
                }
 
188
                catch (const pgsCastException &)
 
189
                {
 
190
                        pdelete(cast);
 
191
                }
 
192
 
 
193
                // [3] Convert it to the second format
 
194
                cast = pnew pgsCast(token::PGS_REAL, expr->clone());
 
195
                try
 
196
                {
 
197
                        res = cast->eval(vars);
 
198
                        TS_ASSERT(false);
 
199
                }
 
200
                catch (const pgsCastException &)
 
201
                {
 
202
                        pdelete(cast);
 
203
                }
 
204
 
 
205
                // [4] Convert it to the third format
 
206
                cast = pnew pgsCast(token::PGS_STRING, expr->clone());
 
207
                res = cast->eval(vars);
 
208
                TS_ASSERT(!res->is_number() && !res->is_integer() && !res->is_real());
 
209
                TS_ASSERT(res->is_string() && !res->is_record());
 
210
                TS_ASSERT(res->value() == rec->value());
 
211
                pdelete(cast);
 
212
 
 
213
                // [5] Delete source variable
 
214
                pdelete(expr);
 
215
        }
 
216
}