1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2005,2018 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#include <cppunit/TextTestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cstring>
#include <string>
#include <unistd.h>
#include "GNURegex.h"
#include "Array.h"
#include "Int16.h"
#include "Float32.h"
#include "Str.h"
#include "Structure.h"
#include "D4Dimensions.h"
#include "run_tests_cppunit.h"
#include "test_config.h"
using namespace CppUnit;
using namespace std;
namespace libdap {
class ArrayTest: public TestFixture {
private:
Array *d_cardinal, *d_string, *d_structure;
Int16 *d_int16;
Str *d_str;
Structure *d_struct;
string svalues[4] = {"0 String", "1 String", "2 String", "3 String" };
char a[1024];
public:
ArrayTest() = default;
~ArrayTest() = default;
void setUp()
{
d_int16 = new Int16("Int16");
DBG(cerr << "d_int16: " << d_int16 << endl);
d_cardinal = new Array("Array_of_Int16", d_int16);
d_cardinal->append_dim(4, "dimension");
dods_int16 buffer[4] = { 0, 1, 2, 3 };
d_cardinal->val2buf(buffer);
#ifdef DODS_DEBUG
for (int i = 0; i < 4; ++i)
cerr << "buffer[" << i << "]: " << buffer[i] << endl;
#endif
d_str = new Str("Str");
d_string = new Array("Array_of_String", d_str);
d_string->append_dim(4, "dimension");
string sbuffer[4] = { "0 String", "1 String", "2 String", "3 String" };
d_string->val2buf(sbuffer);
#ifdef DODS_DEBUG
for (int i = 0; i < 4; ++i)
cerr << "sbuffer[" << i << "]: " << sbuffer[i] << endl;
#endif
d_struct = new Structure("Structure");
d_struct->add_var(d_int16);
d_structure = new Array("Array_of_Strctures", d_struct);
d_structure->append_dim(4, "dimension");
ostringstream oss;
for (int i = 0; i < 4; ++i) {
oss.str("");
oss << "field" << i;
Int16 *n = new Int16(oss.str());
DBG(cerr << "n " << i << ": " << n << endl);
oss.str("");
oss << "element" << i;
Structure *s = new Structure(oss.str());
s->add_var(n);
d_structure->set_vec(i, s);
delete n;
n = 0;
delete s;
s = 0;
}
delete d_int16;
d_int16 = 0;
delete d_str;
d_str = 0;
delete d_struct;
d_struct = 0;
}
void tearDown()
{
delete d_cardinal;
delete d_string;
delete d_structure;
}
bool re_match(Regex &r, const char *s)
{
int match_position = r.match(s, strlen(s));
DBG(cerr << "match position: " << match_position
<< " string length: " << (int)strlen(s) << endl);
return match_position == (int) strlen(s);
}
CPPUNIT_TEST_SUITE (ArrayTest);
CPPUNIT_TEST (cons_test);
CPPUNIT_TEST (assignment_test_1);
CPPUNIT_TEST (assignment_test_2);
CPPUNIT_TEST (assignment_test_3);
CPPUNIT_TEST (prepend_dim_test);
CPPUNIT_TEST (prepend_dim_2_test);
CPPUNIT_TEST (clear_dims_test);
CPPUNIT_TEST (error_handling_test);
CPPUNIT_TEST (error_handling_2_test);
CPPUNIT_TEST (duplicate_cardinal_test);
CPPUNIT_TEST (duplicate_string_test);
CPPUNIT_TEST (duplicate_structure_test);
CPPUNIT_TEST_SUITE_END();
void cons_test()
{
Array a1 = Array("a", "b", d_int16, true);
CPPUNIT_ASSERT(a1.name() == "a");
}
void assignment_test_1()
{
// Are fields from the object copied by the assignment
Array a1 = Array("a", d_int16);
a1.append_dim(17, "bob");
CPPUNIT_ASSERT(a1.dimension_size(a1.dim_begin()) == 17);
a1 = *d_cardinal;
CPPUNIT_ASSERT(a1.dimension_size(a1.dim_begin()) == 4);
}
void assignment_test_2()
{
// Self-assignment; are objects changed by the assignment?
Array a1 = Array("a", d_int16);
Array *before = &a1;
a1 = a1;
Array *after = &a1;
CPPUNIT_ASSERT_MESSAGE("The pointers should be the same", before == after);
}
// This tests that the assignment operator defined in Array correctly copies
// information held in a parent class (Vector). jhrg 2/9/22
void assignment_test_3()
{
// Array copies the proto pointer and manages the storage
#if 0
unique_ptr<Float32> f32(new Float32("float_proto"));
Array a1 = Array("a", f32.get());
#endif
auto f32_ptr = new Float32("float_proto");
Array a1 = Array("a", f32_ptr);
delete f32_ptr;
CPPUNIT_ASSERT_MESSAGE("The type of a1.var() should be dods_float32", a1.var()->type() == dods_float32_c);
a1 = *d_cardinal;
CPPUNIT_ASSERT_MESSAGE("The type of a1.var() should now be dods_int16_c", a1.var()->type() == dods_int16_c);
}
void prepend_dim_test()
{
Array a1 = Array("a", d_int16);
Array::Dim_iter i = a1.dim_begin();
CPPUNIT_ASSERT(a1.dimension_size(i) == 0);
a1.prepend_dim(2, "dim_a");
i = a1.dim_begin();
CPPUNIT_ASSERT(a1.dimension_size(i) == 2);
a1.prepend_dim(3, "dim_b");
}
void prepend_dim_2_test()
{
Array a1 = Array("a", d_int16);
string expected_name[2] = {"dim_b", "myDim"};
int j = 0;
D4Dimensions *dims = new D4Dimensions();
D4Dimension *d = new D4Dimension("dim_b", 2, dims);
a1.prepend_dim(2, "dim_a");
a1.prepend_dim(d);
a1.rename_dim("dim_a", "myDim");
for (Array::Dim_iter i = a1.dim_begin(); i != a1.dim_end(); i++, j++) {
CPPUNIT_ASSERT(a1.dimension_name(i) == expected_name[j]);
}
delete dims;
delete d;
}
void clear_dims_test()
{
Array a1 = Array("a", d_int16);
a1.prepend_dim(2, "dim_a");
a1.prepend_dim(3, "dim_b");
a1.clear_all_dims();
Array::Dim_iter i = a1.dim_begin();
CPPUNIT_ASSERT(a1.dimension_size(i) == 0);
}
void error_handling_test()
{
Array a1 = Array("a", d_int16);
Array::Dim_iter i = a1.dim_begin();
string msg;
CPPUNIT_ASSERT_THROW(a1.dimension_name(i), InternalErr);
CPPUNIT_ASSERT(!a1.check_semantics(msg));
}
void error_handling_2_test()
{
Array a1 = Array("a", d_int16);
a1.append_dim(2, "dim_a");
Array::Dim_iter i = a1.dim_begin();
CPPUNIT_ASSERT(a1.dimension_size(i) == 2);
CPPUNIT_ASSERT_THROW(a1.add_constraint(i, 2, 1, 1), Error);
CPPUNIT_ASSERT_THROW(a1.add_constraint(i, 0, 1, 2), Error);
CPPUNIT_ASSERT_THROW(a1.add_constraint(i, 0, 3, 1), Error);
}
void duplicate_structure_test()
{
Array::Dim_iter i = d_structure->dim_begin();
CPPUNIT_ASSERT(d_structure->dimension_size(i) == 4);
#ifdef DODS_DEBUG
for (int i = 0; i < 4; ++i) {
Structure *s = dynamic_cast<Structure*>(d_structure->var(i));
DBG(cerr << "s: " << s << endl);
if (s)
s->print_decl(cerr);
}
#endif
Array *a = new Array(*d_structure);
// a = *d_structure; I test operator= in duplicate_cardinal_test().
i = a->dim_begin();
CPPUNIT_ASSERT(a->dimension_size(i) == 4);
for (int i = 0; i < 4; ++i) {
// The point of this test is to ensure that the const ctor
// performs a deep copy; first test to make sure the pointers
// to BaseType instnaces are different in the two objects.
Structure *src = dynamic_cast<Structure*>(d_structure->var(i));
Structure *dest = dynamic_cast<Structure*>(a->var(i));
CPPUNIT_ASSERT(src != dest);
// However, for the deep copy to be correct, the two arrays must
// have equivalent elements. We know there's only one field...
CPPUNIT_ASSERT(src->type() == dods_structure_c && dest->type() == dods_structure_c);
Constructor::Vars_iter s = src->var_begin();
Constructor::Vars_iter d = dest->var_begin();
CPPUNIT_ASSERT((*s)->type() == dods_int16_c && (*d)->type() == dods_int16_c);
CPPUNIT_ASSERT((*s)->name() == (*d)->name());
}
delete a;
a = 0;
}
void duplicate_string_test()
{
Array::Dim_iter i = d_string->dim_begin();
CPPUNIT_ASSERT(d_string->dimension_size(i) == 4);
string *s = new string[4];
d_string->buf2val((void**) &s);
for (int i = 0; i < 4; ++i) {
CPPUNIT_ASSERT(s[i] == svalues[i]);
DBG(cerr << "s[" << i << "]: " << s[i] << endl);
}
Array a = *d_string;
i = a.dim_begin();
CPPUNIT_ASSERT(a.dimension_size(i) == 4);
string *s2 = new string[4];
d_string->buf2val((void**) &s2);
for (int i = 0; i < 4; ++i) {
CPPUNIT_ASSERT(s2[i] == svalues[i]);
DBG(cerr << "s2[" << i << "]: " << s2[i] << endl);
}
delete[] s;
s = 0;
delete[] s2;
s2 = 0;
}
void duplicate_cardinal_test()
{
Array::Dim_iter i = d_cardinal->dim_begin();
CPPUNIT_ASSERT(d_cardinal->dimension_size(i) == 4);
dods_int16 *b = new dods_int16[4];
d_cardinal->buf2val((void**) &b);
for (int i = 0; i < 4; ++i) {
CPPUNIT_ASSERT(b[i] == i);
DBG(cerr << "b[" << i << "]: " << b[i] << endl);
}
delete[] b;
b = 0;
Array a = *d_cardinal;
i = a.dim_begin();
CPPUNIT_ASSERT(a.dimension_size(i) == 4);
dods_int16 *b2 = new dods_int16[4];
d_cardinal->buf2val((void**) &b2);
for (int i = 0; i < 4; ++i) {
CPPUNIT_ASSERT(b2[i] == i);
DBG(cerr << "b2[" << i << "]: " << b2[i] << endl);
}
delete[] b2;
b2 = 0;
}
};
CPPUNIT_TEST_SUITE_REGISTRATION (ArrayTest);
} // namespace libdap
int main(int argc, char *argv[])
{
return run_tests<libdap::ArrayTest>(argc, argv) ? 0: 1;
}
|