1 // -*- mode: c++; c-basic-offset:4 -*-
2
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25 #include "config.h"
26
27 #include <cppunit/TextTestRunner.h>
28 #include <cppunit/extensions/TestFactoryRegistry.h>
29 #include <cppunit/extensions/HelperMacros.h>
30
31 #include <sstream>
32 #include <string.h>
33
34 #include "Byte.h"
35 #include "Int8.h"
36 #include "Int16.h"
37 #include "UInt16.h"
38 #include "Int32.h"
39 #include "UInt32.h"
40 #include "Int64.h"
41 #include "UInt64.h"
42 #include "Float32.h"
43 #include "Float64.h"
44 #include "Str.h"
45 #include "Url.h"
46 #include "Array.h"
47 #include "Structure.h"
48 #include "Sequence.h"
49 #include "Grid.h"
50 #include "crc.h"
51
52 #include "DDS.h"
53
54 #include "GNURegex.h"
55 #include "GetOpt.h"
56 #include "util.h"
57 #include "debug.h"
58 #include "ce_expr.tab.hh"
59
60 #include "testFile.h"
61 #include "test_config.h"
62
63 static bool debug = false;
64
65 #undef DBG
66 #define DBG(x) do { if (debug) {x;} } while(false)
67
68 using namespace CppUnit;
69 using namespace std;
70
71 namespace libdap {
72
73 class Float64Test: public TestFixture {
74 private:
75 Float64 *i1, *i2;
76 char a[1024];
77
78 public:
Float64Test()79 Float64Test() : i1(0), i2(0)
80 {
81 }
~Float64Test()82 ~Float64Test()
83 {
84 }
85
setUp()86 void setUp()
87 {
88 i1 = new Float64("a", "b");
89 i2 = new Float64("e");
90 }
91
tearDown()92 void tearDown()
93 {
94 delete i1;
95 delete i2;
96 }
97
98 CPPUNIT_TEST_SUITE(Float64Test);
99
100 CPPUNIT_TEST(cons_Float64_test);
101 CPPUNIT_TEST(checksum_test);
102 CPPUNIT_TEST(val2buf_test);
103 CPPUNIT_TEST(buf2val_test);
104 CPPUNIT_TEST(set_value_test);
105 CPPUNIT_TEST(equals_test);
106 CPPUNIT_TEST(type_compare_test);
107 CPPUNIT_TEST(ops_exception_1_test);
108 CPPUNIT_TEST(ops_exception_2_test);
109 CPPUNIT_TEST(dump_test);
110 CPPUNIT_TEST(print_test);
111 CPPUNIT_TEST(check_types);
112
113 CPPUNIT_TEST_SUITE_END();
114
cons_Float64_test()115 void cons_Float64_test()
116 {
117 CPPUNIT_ASSERT(i1->value() == 0 && i1->dataset() == "b" && i1->name() == "a" &&
118 i1->type() == dods_float64_c);
119 CPPUNIT_ASSERT(i2->value() == 0);
120 }
121
checksum_test()122 void checksum_test()
123 {
124 Crc32 cs;
125 i2->compute_checksum(cs);
126 }
127
val2buf_test()128 void val2buf_test()
129 {
130 double i = 42;
131 i2->val2buf(&i, true);
132 CPPUNIT_ASSERT(i2->value() == 42);
133 CPPUNIT_ASSERT_THROW(i2->val2buf(NULL, true), InternalErr);
134 }
135
buf2val_test()136 void buf2val_test()
137 {
138 double i = 42;
139 void *v = &i;
140 double *v2 = NULL;
141 CPPUNIT_ASSERT(i2->set_value(0));
142 CPPUNIT_ASSERT(i2->buf2val(&v) == 8 && i == 0);
143 CPPUNIT_ASSERT_THROW(i2->buf2val(NULL), InternalErr);
144 CPPUNIT_ASSERT(i2->buf2val((void **)&v2) == 8 && *v2 == 0);
145 delete v2;
146 }
147
set_value_test()148 void set_value_test()
149 {
150 CPPUNIT_ASSERT(i2->set_value(42) && i2->value() == 42);
151 }
152
equals_test()153 void equals_test()
154 {
155 Float64 i3 = Float64("a", "b");
156 Float64 i4 = Float64("e");
157 CPPUNIT_ASSERT(i4.set_value(42) && i4.value() == 42);
158 i3 = i4;
159 CPPUNIT_ASSERT(i3.value() == 42);
160 i3 = i3;
161 }
162
type_compare_test()163 void type_compare_test()
164 {
165 Byte b1 = Byte("a");
166 Int8 i8 = Int8("a");
167 Int16 i16 = Int16("a");
168 UInt16 ui16 = UInt16("a");
169 Int32 i32 = Int32("a", "b");
170 UInt32 ui32 = UInt32("a", "b");
171 Int64 i64 = Int64("a", "b");
172 UInt64 ui64 = UInt64("a", "b");
173 Float32 f32 = Float32("a");
174 Float64 f64 = Float64("a");
175 Url url = Url("a");
176 Str str = Str("a");
177 Array array = Array("a", &i16, true);
178
179 b1.set_value(42);
180 i8.set_value(42);
181 i16.set_value(42);
182 ui16.set_value(42);
183 i32.set_value(42);
184 ui32.set_value(42);
185 i64.set_value(42);
186 ui64.set_value(42);
187 f32.set_value(42);
188 f64.set_value(42);
189 CPPUNIT_ASSERT(f64.value() == 42);
190 CPPUNIT_ASSERT(f64.ops(&b1, SCAN_EQUAL));
191 CPPUNIT_ASSERT(f64.d4_ops(&b1, SCAN_EQUAL));
192 CPPUNIT_ASSERT(f64.d4_ops(&i8, SCAN_EQUAL));
193 CPPUNIT_ASSERT(f64.d4_ops(&i16, SCAN_EQUAL));
194 CPPUNIT_ASSERT(f64.d4_ops(&ui16, SCAN_EQUAL));
195 CPPUNIT_ASSERT(f64.d4_ops(&i32, SCAN_EQUAL));
196 CPPUNIT_ASSERT(f64.d4_ops(&ui32, SCAN_EQUAL));
197 CPPUNIT_ASSERT(f64.d4_ops(&i64, SCAN_EQUAL));
198 CPPUNIT_ASSERT(f64.d4_ops(&ui64, SCAN_EQUAL));
199 CPPUNIT_ASSERT(f64.d4_ops(&f32, SCAN_EQUAL));
200 CPPUNIT_ASSERT(f64.d4_ops(&f64, SCAN_EQUAL));
201
202 CPPUNIT_ASSERT_THROW(f64.d4_ops(&url, SCAN_EQUAL), Error);
203 CPPUNIT_ASSERT_THROW(f64.d4_ops(&str, SCAN_EQUAL), Error);
204 CPPUNIT_ASSERT_THROW(f64.d4_ops(&array, SCAN_EQUAL), Error);
205 CPPUNIT_ASSERT_THROW(f64.ops(0, SCAN_EQUAL), Error);
206 }
207
ops_exception_1_test()208 void ops_exception_1_test()
209 {
210 Byte b1 = Byte("a");
211 Float64 f64 = Float64("a", "b");
212 b1.set_read_p(false);
213 CPPUNIT_ASSERT_THROW(f64.ops(&b1, SCAN_EQUAL), InternalErr);
214 }
215
ops_exception_2_test()216 void ops_exception_2_test()
217 {
218 Byte b1 = Byte("a");
219 Float64 f64 = Float64("a", "b");
220 f64.set_read_p(false);
221 CPPUNIT_ASSERT_THROW(f64.ops(&b1, SCAN_EQUAL), InternalErr);
222 }
223
dump_test()224 void dump_test()
225 {
226 ofstream ofs("Float64Test_dump.output", ios::trunc);
227 i1->set_value(21);
228 i1->dump(ofs);
229 ofs.close();
230 ifstream ifs("Float64Test_dump.output");
231 while(!ifs.eof())
232 ifs >> a;
233 ifs.close();
234 CPPUNIT_ASSERT(!strcmp(a, "21"));
235 }
236
print_test()237 void print_test()
238 {
239 FILE *fp;
240 CPPUNIT_ASSERT(fp = fopen("Float64Test.output", "w"));
241 i1->set_value(22);
242 i1->print_val(fp, " ", true);
243 fclose(fp);
244 ifstream ifs("Float64Test.output");
245 while(!ifs.eof())
246 ifs >> a;
247 ifs.close();
248 CPPUNIT_ASSERT(!strcmp(a, "22;"));
249 }
250
check_types()251 void check_types()
252 {
253 Byte *b1 = new Byte("b");
254 b1->set_value(14);
255 i1->set_value(14);
256 // CPPUNIT_ASSERT(b1 == i1);
257 delete b1;
258 }
259
260 };
261
262 CPPUNIT_TEST_SUITE_REGISTRATION(Float64Test);
263
264 } // namespace libdap
265
main(int argc,char * argv[])266 int main(int argc, char *argv[])
267 {
268 GetOpt getopt(argc, argv, "dh");
269 int option_char;
270
271 while ((option_char = getopt()) != -1)
272 switch (option_char) {
273 case 'd':
274 debug = 1; // debug is a static global
275 break;
276
277 case 'h': { // help - show test names
278 cerr << "Usage: Float64Test has the following tests:" << endl;
279 const std::vector<Test*> &tests = libdap::Float64Test::suite()->getTests();
280 unsigned int prefix_len = libdap::Float64Test::suite()->getName().append("::").length();
281 for (std::vector<Test*>::const_iterator i = tests.begin(), e = tests.end(); i != e; ++i) {
282 cerr << (*i)->getName().replace(0, prefix_len, "") << endl;
283 }
284 return 1;
285 break;
286 }
287
288 default:
289 break;
290 }
291
292 CppUnit::TextTestRunner runner;
293 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
294
295 bool wasSuccessful = true;
296 string test = "";
297 int i = getopt.optind;
298 if (i == argc) {
299 // run them all
300 wasSuccessful = runner.run("");
301 }
302 else {
303 for (; i < argc; ++i) {
304 if (debug) cerr << "Running " << argv[i] << endl;
305 test = libdap::Float64Test::suite()->getName().append("::").append(argv[i]);
306 wasSuccessful = wasSuccessful && runner.run(test);
307 }
308 }
309
310 return wasSuccessful ? 0 : 1;
311 }
312
313