File: Value.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (203 lines) | stat: -rw-r--r-- 5,865 bytes parent folder | download | duplicates (5)
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
#define BOOST_TEST_MODULE Value
#include <boost/test/unit_test.hpp>

#include "odil/DataSet.h"
#include "odil/Exception.h"
#include "odil/Value.h"

template<typename TContainer>
void test_contents(
    odil::Value const & value,
    TContainer const & contents, odil::Value::Type type,
    TContainer const & (odil::Value::*getter)() const)
{
    BOOST_CHECK(value.get_type() == type);
    BOOST_CHECK_EQUAL(value.empty(), contents.empty());
    BOOST_CHECK_EQUAL(value.size(), contents.size());
    BOOST_CHECK((value.*getter)() == contents);

    if(type != odil::Value::Type::Integers)
    {
        BOOST_CHECK_THROW(value.as_integers(), odil::Exception);
    }
    if(type != odil::Value::Type::Reals)
    {
        BOOST_CHECK_THROW(value.as_reals(), odil::Exception);
    }
    if(type != odil::Value::Type::Strings)
    {
        BOOST_CHECK_THROW(value.as_strings(), odil::Exception);
    }
    if(type != odil::Value::Type::DataSets)
    {
        BOOST_CHECK_THROW(value.as_data_sets(), odil::Exception);
    }
    if(type != odil::Value::Type::Binary)
    {
        BOOST_CHECK_THROW(value.as_binary(), odil::Exception);
    }
}

template<typename TContainer>
void test_container(
    TContainer const & contents, odil::Value::Type type, 
    TContainer const & (odil::Value::*getter)() const)
{
    odil::Value const value(contents);
    test_contents(value, contents, type, getter);

    auto contents_copy(contents);
    odil::Value const other_value(std::move(contents_copy));
    BOOST_CHECK(contents_copy.empty());
}

template<typename TContainer>
void test_initializer_list(
    std::initializer_list<typename TContainer::value_type> const & contents,
    odil::Value::Type type, 
    TContainer const & (odil::Value::*getter)() const)
{
    odil::Value const value(contents);
    test_contents(value, TContainer(contents), type, getter);
}

template<typename TContainer>
void test_modify(
    TContainer const & contents,
    TContainer const & (odil::Value::*getter)() const,
    TContainer & (odil::Value::*setter)())
{
    odil::Value value(TContainer{{contents[0]}});
    (value.*setter)().push_back(contents[1]);
    BOOST_CHECK((value.*getter)() == contents);
}

template<typename TContainer>
void test_clear(TContainer const & contents, odil::Value::Type type) 
{
    odil::Value value(contents);
    value.clear();
    BOOST_CHECK(value.get_type() == type);
    BOOST_CHECK(value.empty());
}

template<typename TContainer>
void test_equality(
    TContainer const & contents_1, TContainer const & contents_2)
{
    odil::Value const value_1(contents_1);
    odil::Value const value_2(contents_1);
    odil::Value const value_3(contents_2);
    odil::Value const value_4(contents_2);

    BOOST_CHECK(value_1 == value_2);
    BOOST_CHECK( ! (value_1 == value_3));
    BOOST_CHECK( ! (value_1 == value_4));

    BOOST_CHECK(! (value_1 != value_2));
    BOOST_CHECK(value_1 != value_3);
    BOOST_CHECK(value_1 != value_4);
}

struct Visitor
{
    typedef std::string result_type;

    template<typename T>
    result_type operator()(T const & container) const
    {
        return typeid(container).name();
    }
};

template<typename TContainer>
void test_visitor(
    TContainer const & contents)
{
    odil::Value const value(contents);
    BOOST_REQUIRE_EQUAL(
        odil::apply_visitor(Visitor(), value),
        typeid(TContainer).name());
}

template<typename TContainer>
void test(
    std::initializer_list<typename TContainer::value_type> const & contents,
    std::initializer_list<typename TContainer::value_type> const & other_contents,
    odil::Value::Type type, 
    TContainer const & (odil::Value::*getter)() const,
    TContainer & (odil::Value::*setter)())
{
    TContainer const container(contents);
    TContainer const other_container(other_contents);

    test_container(TContainer(), type, getter);
    test_container(container, type, getter);
    test_initializer_list(contents, type, getter);

    test_modify(container, getter, setter);

    test_clear(container, type);

    test_equality(container, other_container);

    test_visitor(container);
}
 
BOOST_AUTO_TEST_CASE(Integers)
{
    BOOST_CHECK(
        odil::Value({1234, 5678}).get_type() == odil::Value::Type::Integers);
    test<odil::Value::Integers>(
        {1234, 5678}, {9012, 3456},
        odil::Value::Type::Integers,
        &odil::Value::as_integers, &odil::Value::as_integers);
}

BOOST_AUTO_TEST_CASE(Reals)
{
    BOOST_CHECK(
        odil::Value({12.34, 56.78}).get_type() == odil::Value::Type::Reals);
    test<odil::Value::Reals>(
        {12.34, 56.78}, {1., 2.},
        odil::Value::Type::Reals,
        &odil::Value::as_reals, &odil::Value::as_reals);
}

BOOST_AUTO_TEST_CASE(Strings)
{
    BOOST_CHECK(
        odil::Value({"foo", "bar"}).get_type() == odil::Value::Type::Strings);
    test<odil::Value::Strings>(
        {"foo", "bar"}, {"plip", "plop"},
        odil::Value::Type::Strings,
        &odil::Value::as_strings, &odil::Value::as_strings);
}

BOOST_AUTO_TEST_CASE(DataSets)
{
    auto data_set_1 = std::make_shared<odil::DataSet>();
    data_set_1->add("PatientID", {"DJ1234"});

    auto data_set_2 = std::make_shared<odil::DataSet>();
    data_set_2->add("EchoTime", {100});

    BOOST_CHECK(
        odil::Value({data_set_1, data_set_2}).get_type()
            == odil::Value::Type::DataSets);
    test<odil::Value::DataSets>(
        {data_set_1, data_set_2}, {data_set_2, data_set_1},
        odil::Value::Type::DataSets,
        &odil::Value::as_data_sets, &odil::Value::as_data_sets);
}

BOOST_AUTO_TEST_CASE(Binary)
{
    BOOST_CHECK(
        odil::Value({{0x1, 0x2}, {0x3}}).get_type()
            == odil::Value::Type::Binary);
    test<odil::Value::Binary>(
        {{0x1, 0x2}, {0x3}}, {{0x4}, {0x5, 0x6}},
        odil::Value::Type::Binary,
        &odil::Value::as_binary, &odil::Value::as_binary);
}