File size: 6,851 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2012-07-09

  (C) Copyright 2012-2020 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include "vctDataFunctionsFixedSizeVectorTest.h"

#include <cisstCommon/cmnDataFunctionsString.h>

#include <cisstVector/vctFixedSizeVector.h>
#include <cisstVector/vctDataFunctionsFixedSizeVector.h>
#include <cisstVector/vctRandomFixedSizeVector.h>

#include <cisstVector/vctDynamicVector.h>
#include <cisstVector/vctDataFunctionsDynamicVector.h>
#include <cisstVector/vctRandomDynamicVector.h>

void vctDataFunctionsFixedSizeVectorTest::TestDataCopy(void)
{
    typedef vctFixedSizeVector<double, 7> DataType;
    DataType source, data;
    vctRandom(source, -1.0, 1.0);
    cmnData<DataType>::Copy(data, source);
    CPPUNIT_ASSERT(source.Equal(data));
}


void vctDataFunctionsFixedSizeVectorTest::TestBinarySerializationStream(void)
{
    cmnDataFormat local, remote;
    std::stringstream stream;
    typedef vctFixedSizeVector<double, 12> DataType;
    DataType v1, v2, vReference;
    vctRandom(vReference, -10.0, 10.0);
    v1 = vReference;
    cmnData<DataType>::SerializeBinary(v1, stream);
    v1.SetAll(0);
    cmnData<DataType>::DeSerializeBinary(v2, stream, local, remote);
    CPPUNIT_ASSERT_EQUAL(vReference, v2);
    CPPUNIT_ASSERT(!stream.fail());
}



void vctDataFunctionsFixedSizeVectorTest::TestTextSerializationStream(void)
{
    std::stringstream stream;
    typedef vctFixedSizeVector<double, 12> DataType;
    DataType v1, v2, vReference;
    vctRandom(vReference, -10.0, 10.0);
    v1 = vReference;
    cmnData<DataType>::SerializeText(v1, stream, ',');
    v1.SetAll(0);
    cmnData<DataType>::DeSerializeText(v2, stream, ',');
    CPPUNIT_ASSERT(vReference.AlmostEqual(v2, 0.01)); // low precision due to stream out loss
    CPPUNIT_ASSERT(!stream.fail());
    // try without delimiter, using space
    stream.clear();
    vctRandom(vReference, -20.0, 20.0);
    v1 = vReference;
    cmnData<DataType>::SerializeText(v1, stream, ' ');
    v2.SetAll(0.0);
    cmnData<DataType>::DeSerializeText(v2, stream, ' ');
    CPPUNIT_ASSERT(vReference.AlmostEqual(v2, 0.01)); // low precision due to stream out loss
    CPPUNIT_ASSERT(!stream.fail());
    // try with the wrong delimiter
    bool exceptionReceived = false;
    stream.clear();
    vctRandom(vReference, -20.0, 20.0);
    v1 = vReference;
    cmnData<DataType>::SerializeText(v1, stream, ',');
    v2.SetAll(0.0);
    try {
        cmnData<DataType>::DeSerializeText(v2, stream, '!');
    } catch (std::runtime_error &) {
        exceptionReceived = true;
    }
    CPPUNIT_ASSERT(exceptionReceived);
}


void vctDataFunctionsFixedSizeVectorTest::TestScalar(void)
{
    typedef vctFixedSizeVector<int, 6> DataType;
    DataType vInt;
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(6), cmnData<DataType>::ScalarNumber(vInt));
    CPPUNIT_ASSERT_EQUAL(true, cmnData<DataType>::ScalarNumberIsFixed(vInt));

    typedef vctFixedSizeVector<vctFixedSizeVector<double, 12>, 4> DataType2;
    DataType2 vvDouble;
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(48), cmnData<DataType2>::ScalarNumber(vvDouble));
    CPPUNIT_ASSERT_EQUAL(true, cmnData<DataType2>::ScalarNumberIsFixed(vvDouble));

    typedef vctFixedSizeVector<std::string, 3> DataType3;
    DataType3 vString;
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), cmnData<DataType3>::ScalarNumber(vString));
    CPPUNIT_ASSERT_EQUAL(true, cmnData<DataType3>::ScalarNumberIsFixed(vString));

    typedef vctFixedSizeVector<vctDynamicVector<double>, 4> DataType4;
    DataType4 vvMixed;
    for (size_t i = 0; i < vvMixed.size(); i++) {
        vvMixed.Element(i).SetSize(10 + i);
    }
    // total scalar, 4 sizes of each contained dynamic vector + 10 + 11 + 12 + 13
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(50), cmnData<DataType4>::ScalarNumber(vvMixed));
    CPPUNIT_ASSERT_EQUAL(false, cmnData<DataType4>::ScalarNumberIsFixed(vvMixed));

    size_t index, subIndex, position;
    bool exceptionReceived = false;
    std::string description;

    // exception expected if index too high
    try {
        description = cmnData<DataType>::ScalarDescription(vInt, cmnData<DataType>::ScalarNumber(vInt) + 1);
    } catch (std::out_of_range &) {
        exceptionReceived = true;
    }
    CPPUNIT_ASSERT(exceptionReceived);

    exceptionReceived = false;
    try {
        description = cmnData<DataType2>::ScalarDescription(vvDouble, cmnData<DataType2>::ScalarNumber(vvDouble) + 1);
    } catch (std::out_of_range &) {
        exceptionReceived = true;
    }
    CPPUNIT_ASSERT(exceptionReceived);

    // exception expected for any index
    exceptionReceived = false;
    try {
        description = cmnData<DataType3>::ScalarDescription(vString, cmnData<DataType3>::ScalarNumber(vString));
    } catch (std::out_of_range &) {
        exceptionReceived = true;
    }
    CPPUNIT_ASSERT(exceptionReceived);

    // get scalar
    position = 0;
    for (index = 0; index < vInt.size(); ++index) {
        vInt.Element(index) = static_cast<int>(index);
        CPPUNIT_ASSERT_EQUAL(static_cast<double>(index), cmnData<DataType>::Scalar(vInt, position));
        position++;
    }

    position = 0;
    for (index = 0; index < vvDouble.size(); ++index) {
        for (subIndex = 0; subIndex < vvDouble.Element(index).size(); ++subIndex) {
            vvDouble.Element(index).Element(subIndex) = static_cast<double>(index * 100 + subIndex);
            CPPUNIT_ASSERT_EQUAL(static_cast<double>(index * 100 + subIndex),
                                 cmnData<DataType2>::Scalar(vvDouble, position));
            position++;
        }
    }

    // try with fixed size vector of dynamic vectors
    position = 0;
    for (index = 0; index < vvMixed.size(); ++index) {
        // size of dynamic vectors are treated as scalars
        CPPUNIT_ASSERT_EQUAL(static_cast<double>(vvMixed.Element(index).size()),
                             cmnData<DataType4>::Scalar(vvMixed, position));
        position++;
        for (subIndex = 0; subIndex < vvMixed.Element(index).size(); ++subIndex) {
            vvMixed.Element(index).Element(subIndex) = static_cast<double>(index * 100 + subIndex);
            CPPUNIT_ASSERT_EQUAL(static_cast<double>(index * 100 + subIndex),
                                 cmnData<DataType4>::Scalar(vvMixed, position));
            position++;
        }
    }

}


CPPUNIT_TEST_SUITE_REGISTRATION(vctDataFunctionsFixedSizeVectorTest);