1 : // $Id: AnyScalarTest.cc 59 2007-07-17 14:43:23Z tb $
2 :
3 : /*
4 : * STX Expression Parser C++ Framework v0.7
5 : * Copyright (C) 2007 Timo Bingmann
6 : *
7 : * This library is free software; you can redistribute it and/or modify it
8 : * under the terms of the GNU Lesser General Public License as published by the
9 : * Free Software Foundation; either version 2.1 of the License, or (at your
10 : * option) any later version.
11 : *
12 : * This library is distributed in the hope that it will be useful, but WITHOUT
13 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 : * for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with this library; if not, write to the Free Software Foundation,
19 : * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 : */
21 :
22 : #include <cppunit/extensions/HelperMacros.h>
23 :
24 : #include "ExpressionParser.h"
25 :
26 : #include <stdlib.h>
27 : #include <boost/lexical_cast.hpp>
28 :
29 : using namespace stx;
30 :
31 : class AnyScalarTest : public CPPUNIT_NS::TestFixture
32 10 : {
33 3 : CPPUNIT_TEST_SUITE( AnyScalarTest );
34 1 : CPPUNIT_TEST(test_boolean);
35 2 : CPPUNIT_TEST(test_char);
36 2 : CPPUNIT_TEST(test_short);
37 2 : CPPUNIT_TEST(test_integer);
38 2 : CPPUNIT_TEST(test1);
39 2 : CPPUNIT_TEST_SUITE_END();
40 :
41 : protected:
42 :
43 1 : void test_boolean()
44 : {
45 1 : CPPUNIT_ASSERT( AnyScalar(bool(false)).getInteger() == 0 );
46 2 : CPPUNIT_ASSERT( AnyScalar(bool(true)).getInteger() == 1 );
47 :
48 1 : AnyScalar as(bool(false));
49 :
50 2 : CPPUNIT_ASSERT( as.setInteger(2) == true );
51 2 : CPPUNIT_ASSERT( as.setInteger(1) == true );
52 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
53 2 : CPPUNIT_ASSERT( as.getString() == "true" );
54 :
55 2 : CPPUNIT_ASSERT( as.setDouble(0.3) );
56 2 : CPPUNIT_ASSERT( as.getBoolean() == false );
57 2 : CPPUNIT_ASSERT( as.getString() == "false" );
58 :
59 2 : CPPUNIT_ASSERT( as.setString("t") );
60 2 : CPPUNIT_ASSERT( as.getString() == "true" );
61 :
62 2 : CPPUNIT_ASSERT( as.getTypeString() == "bool" );
63 2 : CPPUNIT_ASSERT( as.getTypeLength() == 0 );
64 2 : CPPUNIT_ASSERT( as.getValueLength() == 0 );
65 2 : CPPUNIT_ASSERT( AnyScalar::stringToType("bool") == AnyScalar::ATTRTYPE_BOOL );
66 1 : }
67 :
68 1 : void test_char()
69 : {
70 1 : CPPUNIT_ASSERT( AnyScalar(char(42)).getInteger() == 42 );
71 :
72 1 : AnyScalar as(char(42));
73 :
74 2 : CPPUNIT_ASSERT( as.setInteger(-32) == true );
75 2 : CPPUNIT_ASSERT( as.setInteger(10) == true );
76 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
77 2 : CPPUNIT_ASSERT( as.getString() == "10" );
78 :
79 2 : CPPUNIT_ASSERT( as.setInteger(10010) == false );
80 2 : CPPUNIT_ASSERT( as.getString() == "127" );
81 :
82 2 : CPPUNIT_ASSERT( as.setDouble(0.3) );
83 2 : CPPUNIT_ASSERT( as.getBoolean() == false );
84 2 : CPPUNIT_ASSERT( as.getString() == "0" );
85 :
86 2 : CPPUNIT_ASSERT( as.setString("115") );
87 2 : CPPUNIT_ASSERT( as.getString() == "115" );
88 2 : CPPUNIT_ASSERT( as == AnyScalar(char(115)) );
89 :
90 2 : CPPUNIT_ASSERT( (as + AnyScalar(char(2))).getString() == "117" );
91 :
92 2 : CPPUNIT_ASSERT( as.getTypeString() == "char" );
93 2 : CPPUNIT_ASSERT( as.getTypeLength() == 1 );
94 2 : CPPUNIT_ASSERT( as.getValueLength() == 1 );
95 2 : CPPUNIT_ASSERT( AnyScalar::stringToType("char") == AnyScalar::ATTRTYPE_CHAR );
96 1 : }
97 :
98 1 : void test_short()
99 : {
100 1 : CPPUNIT_ASSERT( AnyScalar(short(442)).getInteger() == 442 );
101 :
102 1 : AnyScalar as(short(442));
103 :
104 2 : CPPUNIT_ASSERT( as.setInteger(-432) == true );
105 2 : CPPUNIT_ASSERT( as.setInteger(310) == true );
106 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
107 2 : CPPUNIT_ASSERT( as.getString() == "310" );
108 :
109 2 : CPPUNIT_ASSERT( as.setInteger(1340000010L) == false );
110 2 : CPPUNIT_ASSERT( as.getString() == "32767" );
111 :
112 2 : CPPUNIT_ASSERT( as.setDouble(300.3) );
113 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
114 2 : CPPUNIT_ASSERT( as.getString() == "300" );
115 :
116 2 : CPPUNIT_ASSERT( as.setString("1115") );
117 2 : CPPUNIT_ASSERT( as.getString() == "1115" );
118 2 : CPPUNIT_ASSERT( as == AnyScalar(short(1115)) );
119 :
120 2 : CPPUNIT_ASSERT( (as + AnyScalar(char(2))).getString() == "1117" );
121 :
122 2 : CPPUNIT_ASSERT( as.getTypeString() == "short" );
123 2 : CPPUNIT_ASSERT( as.getTypeLength() == 2 );
124 2 : CPPUNIT_ASSERT( as.getValueLength() == 2 );
125 1 : }
126 :
127 1 : void test_integer()
128 : {
129 1 : CPPUNIT_ASSERT( AnyScalar(int(132442)).getInteger() == 132442 );
130 :
131 1 : AnyScalar as(int(132442));
132 :
133 2 : CPPUNIT_ASSERT( as.setInteger(-132432) == true );
134 2 : CPPUNIT_ASSERT( as.setInteger(132310) == true );
135 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
136 2 : CPPUNIT_ASSERT( as.getString() == "132310" );
137 :
138 2 : CPPUNIT_ASSERT( as.setLong(1340000000010LLU) == false );
139 :
140 2 : CPPUNIT_ASSERT( as.setDouble(322300.3) );
141 2 : CPPUNIT_ASSERT( as.getBoolean() == true );
142 2 : CPPUNIT_ASSERT( as.getString() == "322300" );
143 2 : CPPUNIT_ASSERT( as == AnyScalar(int(322300)) );
144 :
145 2 : CPPUNIT_ASSERT( as.setString("32221115") );
146 2 : CPPUNIT_ASSERT( as.getString() == "32221115" );
147 :
148 2 : CPPUNIT_ASSERT( (as + AnyScalar(char(2))).getString() == "32221117" );
149 :
150 2 : CPPUNIT_ASSERT( as.getTypeString() == "integer" );
151 2 : CPPUNIT_ASSERT( as.getTypeLength() == 4 );
152 2 : CPPUNIT_ASSERT( as.getValueLength() == 4 );
153 1 : }
154 :
155 1 : void test1()
156 : {
157 1 : AnyScalar t_integer (AnyScalar::ATTRTYPE_INTEGER);
158 1 : AnyScalar t_float (AnyScalar::ATTRTYPE_FLOAT);
159 1 : AnyScalar t_string (AnyScalar::ATTRTYPE_STRING);
160 :
161 1 : CPPUNIT_ASSERT( t_integer.setInteger(42) );
162 2 : CPPUNIT_ASSERT( t_integer.getBoolean() == true );
163 2 : CPPUNIT_ASSERT( t_integer.getInteger() == 42 );
164 2 : CPPUNIT_ASSERT( t_integer.getString() == "42" );
165 :
166 2 : CPPUNIT_ASSERT( t_integer.setDouble(42.2) );
167 2 : CPPUNIT_ASSERT( t_integer.getBoolean() == true );
168 2 : CPPUNIT_ASSERT( t_integer.getInteger() == 42 );
169 2 : CPPUNIT_ASSERT( t_integer.getString() == "42" );
170 :
171 2 : CPPUNIT_ASSERT( t_integer.setString("42.2") == false );
172 2 : CPPUNIT_ASSERT( t_integer.setString("42") == true );
173 2 : CPPUNIT_ASSERT( t_integer.getBoolean() == true );
174 2 : CPPUNIT_ASSERT( t_integer.getInteger() == 42 );
175 2 : CPPUNIT_ASSERT( t_integer.getString() == "42" );
176 :
177 2 : CPPUNIT_ASSERT( t_float.setDouble(42) );
178 2 : CPPUNIT_ASSERT( t_float.getBoolean() == true );
179 2 : CPPUNIT_ASSERT( t_float.getInteger() == 42 );
180 2 : CPPUNIT_ASSERT( t_float.getDouble() == 42.0 );
181 2 : CPPUNIT_ASSERT( t_float.getString() == "42" );
182 :
183 2 : CPPUNIT_ASSERT( t_float.setDouble(42.42) );
184 2 : CPPUNIT_ASSERT( t_float.getInteger() == 42 );
185 2 : CPPUNIT_ASSERT( t_float.getString() == "42.42" );
186 :
187 2 : CPPUNIT_ASSERT( t_float.setString("42.42") );
188 2 : CPPUNIT_ASSERT( t_float.getInteger() == 42 );
189 2 : CPPUNIT_ASSERT( t_float.getString() == "42.42" );
190 :
191 :
192 2 : CPPUNIT_ASSERT( t_string.setInteger(20) );
193 2 : CPPUNIT_ASSERT_THROW( t_string.getBoolean(), ConversionException );
194 1 : CPPUNIT_ASSERT( t_string.getString() == "20" );
195 :
196 2 : CPPUNIT_ASSERT( t_string.setInteger(0) );
197 2 : CPPUNIT_ASSERT( t_string.getBoolean() == false );
198 2 : CPPUNIT_ASSERT( t_string.getString() == "0" );
199 :
200 2 : CPPUNIT_ASSERT( t_string.setInteger(-20) );
201 2 : CPPUNIT_ASSERT( t_string.getString() == "-20" );
202 :
203 2 : CPPUNIT_ASSERT( t_string.setStringQuoted("\"bla\\n\\\\\\\"h\\a\\b\\f\\r\\t\\v\\'x\"") );
204 2 : CPPUNIT_ASSERT( t_string.getString() == "bla\n\\\"h\a\b\f\r\t\v'x" );
205 :
206 1 : AnyScalar t_other = t_string;
207 1 : CPPUNIT_ASSERT( t_other.getType() == AnyScalar::ATTRTYPE_STRING );
208 2 : CPPUNIT_ASSERT( t_other.getString() == t_string.getString() );
209 2 : CPPUNIT_ASSERT( t_other == t_string );
210 :
211 1 : t_other = t_integer;
212 1 : CPPUNIT_ASSERT( t_other.getType() == AnyScalar::ATTRTYPE_INTEGER );
213 2 : CPPUNIT_ASSERT( t_other != t_string );
214 2 : CPPUNIT_ASSERT( t_other == t_integer );
215 :
216 2 : CPPUNIT_ASSERT( t_other.convertType(AnyScalar::ATTRTYPE_STRING) );
217 2 : CPPUNIT_ASSERT( t_other.getInteger() == 42 );
218 :
219 : // Arithmetic and Comparison Operators
220 :
221 2 : CPPUNIT_ASSERT( (AnyScalar(5) + AnyScalar(6)) == 11 );
222 2 : CPPUNIT_ASSERT( (AnyScalar(5) + AnyScalar(6)).getDouble() == 11.0 );
223 :
224 2 : CPPUNIT_ASSERT( (AnyScalar(5.0) + AnyScalar(4)).getString() == "9" );
225 :
226 2 : CPPUNIT_ASSERT( (AnyScalar("abc") + AnyScalar("def")) == "abcdef" );
227 :
228 2 : CPPUNIT_ASSERT( (AnyScalar("1.1") + AnyScalar(3.0)) == 4.1 );
229 :
230 : {
231 1 : AnyScalar s;
232 :
233 1 : CPPUNIT_ASSERT( s.setAutoString("abc").getType() == AnyScalar::ATTRTYPE_STRING );
234 2 : CPPUNIT_ASSERT( s.setAutoString("0").getType() == AnyScalar::ATTRTYPE_INTEGER );
235 2 : CPPUNIT_ASSERT( s.setAutoString("10045123").getTypeString() == "integer" );
236 2 : CPPUNIT_ASSERT( s.setAutoString("10045123345343434").getType() == AnyScalar::ATTRTYPE_LONG );
237 2 : CPPUNIT_ASSERT( s.setAutoString("-4554454").getType() == AnyScalar::ATTRTYPE_INTEGER );
238 2 : CPPUNIT_ASSERT( s.setAutoString("-3402359829865").getTypeString() == "long" );
239 2 : CPPUNIT_ASSERT( s.setAutoString("-3402359829865.3334").getTypeString() == "double" );
240 2 : CPPUNIT_ASSERT( s.setAutoString("-34023598298abc65.3334").getTypeString() == "string" );
241 1 : }
242 1 : }
243 : };
244 0 :
245 3 : CPPUNIT_TEST_SUITE_REGISTRATION( AnyScalarTest );
|