File: MaeBlockTest.cpp

package info (click to toggle)
schroedinger-maeparser 1.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 356 kB
  • sloc: cpp: 4,182; makefile: 4
file content (321 lines) | stat: -rw-r--r-- 9,998 bytes parent folder | download | duplicates (4)
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
#include <iostream>
#include <memory>
#include <stdexcept>

#include <boost/test/unit_test.hpp>

#include "MaeBlock.hpp"
#include "MaeConstants.hpp"

using namespace schrodinger;

BOOST_AUTO_TEST_SUITE(MaeBlockSuite)

BOOST_AUTO_TEST_CASE(maeBlock)
{
    double tolerance = std::numeric_limits<double>::epsilon();

    mae::Block b("dummy");
    b.setRealProperty("a", 1.0);
    BOOST_REQUIRE(b.hasRealProperty("a"));
    BOOST_REQUIRE(!b.hasRealProperty("b"));
    BOOST_REQUIRE_CLOSE(b.getRealProperty("a"), 1.0, tolerance);
    BOOST_REQUIRE_THROW(b.getRealProperty("b"), std::out_of_range);

    b.setIntProperty("a", 3);
    BOOST_REQUIRE(b.hasIntProperty("a"));
    BOOST_REQUIRE(!b.hasIntProperty("b"));
    BOOST_REQUIRE_EQUAL(b.getIntProperty("a"), 3);
    BOOST_REQUIRE_THROW(b.getIntProperty("b"), std::out_of_range);

    b.setBoolProperty("a", true);
    BOOST_REQUIRE(b.hasBoolProperty("a"));
    BOOST_REQUIRE(!b.hasBoolProperty("b"));
    BOOST_REQUIRE(b.getBoolProperty("a"));
    BOOST_REQUIRE_THROW(b.getBoolProperty("b"), std::out_of_range);

    const std::vector<std::string> strings = {"Regular", "Spaced String"};
    for (const auto& value : strings) {
        b.setStringProperty("a", value);
        BOOST_REQUIRE(b.hasStringProperty("a"));
        BOOST_REQUIRE(!b.hasStringProperty("b"));
        BOOST_REQUIRE_EQUAL(b.getStringProperty("a"), value);
        BOOST_REQUIRE_THROW(b.getStringProperty("b"), std::out_of_range);
    }

    // All the properties we set have the same name, but different types
    const std::string property_name("a");

    auto bool_props = b.getProperties<mae::BoolProperty>();
    BOOST_CHECK_EQUAL(bool_props.size(), 1u);
    BOOST_CHECK_EQUAL(bool_props.begin()->first, property_name);

    auto int_props = b.getProperties<int>();
    BOOST_CHECK_EQUAL(int_props.size(), 1u);
    BOOST_CHECK_EQUAL(int_props.begin()->first, property_name);

    auto double_props = b.getProperties<double>();
    BOOST_CHECK_EQUAL(double_props.size(), 1u);
    BOOST_CHECK_EQUAL(double_props.begin()->first, property_name);

    auto string_props = b.getProperties<std::string>();
    BOOST_CHECK_EQUAL(string_props.size(), 1u);
    BOOST_CHECK_EQUAL(string_props.begin()->first, property_name);
}

BOOST_AUTO_TEST_CASE(maeIndexedRealProperty)
{
    double tolerance = std::numeric_limits<double>::epsilon();
    {
        auto dv = std::make_shared<std::vector<double>>();
        dv->push_back(1.0);
        dv->push_back(2.0);
        dv->push_back(3.0);
        mae::IndexedRealProperty irp(*dv);
        BOOST_REQUIRE_CLOSE(irp[0], 1.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp[1], 2.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp[2], 3.0, tolerance);
    }
    {
        std::vector<double> dv;
        dv.push_back(1.0);
        dv.push_back(2.0);
        dv.push_back(3.0);
        mae::IndexedRealProperty irp(dv);
        BOOST_REQUIRE_CLOSE(irp[0], 1.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp[1], 2.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp[2], 3.0, tolerance);
    }
    {
        auto dv = std::make_shared<std::vector<double>>();
        boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
        bs->set(1);

        dv->push_back(1.0);
        dv->push_back(0.0);
        dv->push_back(3.0);
        mae::IndexedRealProperty irp(*dv, bs);
        BOOST_REQUIRE(irp.isDefined(0));
        BOOST_REQUIRE_CLOSE(irp[0], 1.0, tolerance);
        BOOST_REQUIRE(!irp.isDefined(1));
        BOOST_REQUIRE_THROW(irp[1], std::runtime_error);
        BOOST_REQUIRE(irp.isDefined(2));
        BOOST_REQUIRE_CLOSE(irp[2], 3.0, tolerance);
    }
    {
        std::vector<double> dv;
        boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
        bs->set(1);

        dv.push_back(1.0);
        dv.push_back(0.0);
        dv.push_back(3.0);
        mae::IndexedRealProperty irp(dv, bs);
        BOOST_REQUIRE(irp.isDefined(0));
        BOOST_REQUIRE_CLOSE(irp[0], 1.0, tolerance);
        BOOST_REQUIRE(!irp.isDefined(1));
        BOOST_REQUIRE_THROW(irp[1], std::runtime_error);
        BOOST_REQUIRE(irp.isDefined(2));
        BOOST_REQUIRE_CLOSE(irp[2], 3.0, tolerance);
    }
}

BOOST_AUTO_TEST_CASE(maeIndexedBlock)
{
    using namespace mae;
    double tolerance = std::numeric_limits<double>::epsilon();
    {
        std::vector<double> dv;
        boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
        bs->set(1);

        dv.push_back(1.0);
        dv.push_back(0.0);
        dv.push_back(3.0);
        IndexedBlock ib("m_atom");
        BOOST_REQUIRE(!ib.hasRealProperty("r_m_float"));
        auto irps = std::make_shared<IndexedRealProperty>(dv, bs);

        ib.setRealProperty("r_m_float", irps);
        BOOST_REQUIRE(ib.hasRealProperty("r_m_float"));

        auto irpg = ib.getRealProperty("r_m_float");
        IndexedRealProperty& irp = *irpg;
        BOOST_REQUIRE(irp.isDefined(0));
        BOOST_REQUIRE_CLOSE(irp[0], 1.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp.at(0, 999.0), 1.0, tolerance);
        BOOST_REQUIRE(!irp.isDefined(1));
        BOOST_REQUIRE_THROW(irp[1], std::runtime_error);
        BOOST_REQUIRE_CLOSE(irp.at(1, 999.0), 999.0, tolerance);
        BOOST_REQUIRE(irp.isDefined(2));
        BOOST_REQUIRE_CLOSE(irp[2], 3.0, tolerance);
        BOOST_REQUIRE_CLOSE(irp.at(2, 999.0), 3.0, tolerance);

        auto no_prop = ib.getRealProperty("r_m_nonexistant");
        BOOST_REQUIRE(no_prop.get() == nullptr);
    }
}

BOOST_AUTO_TEST_CASE(maeIndexedBlockBool)
{
    using namespace mae;
    {
        std::vector<BoolProperty> dv;
        boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
        bs->set(1);

        dv.push_back(true);
        dv.push_back(false);
        dv.push_back(true);
        IndexedBlock ib("m_atom");
        BOOST_REQUIRE(!ib.hasBoolProperty("b_m_bool"));
        auto ibps = std::make_shared<IndexedBoolProperty>(dv, bs);

        ib.setBoolProperty("b_m_bool", ibps);
        BOOST_REQUIRE(ib.hasBoolProperty("b_m_bool"));

        auto ibpg = ib.getBoolProperty("b_m_bool");
        IndexedBoolProperty& ibp = *ibpg;
        BOOST_REQUIRE(ibp.isDefined(0));
        BOOST_REQUIRE_EQUAL(ibp[0], static_cast<BoolProperty>(true));
        BOOST_REQUIRE(!ibp.isDefined(1));
        BOOST_REQUIRE_THROW(ibp[1], std::runtime_error);
        BOOST_REQUIRE(ibp.isDefined(2));
        BOOST_REQUIRE_EQUAL(ibp[2], static_cast<BoolProperty>(true));
    }
}

BOOST_AUTO_TEST_CASE(maeIndexedBlockString)
{
    using namespace mae;
    {
        std::vector<std::string> dv;
        boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
        bs->set(1);

        dv.push_back("Hi with space");
        dv.push_back("ignore me");
        dv.push_back("Bye");
        IndexedBlock ib("m_atom");
        BOOST_REQUIRE(!ib.hasStringProperty("s_m_string"));
        auto isps = std::make_shared<IndexedStringProperty>(dv, bs);

        ib.setStringProperty("s_m_string", isps);
        BOOST_REQUIRE(ib.hasStringProperty("s_m_string"));

        auto ispg = ib.getStringProperty("s_m_string");
        IndexedStringProperty& isp = *ispg;
        BOOST_REQUIRE(isp.isDefined(0));
        BOOST_REQUIRE_EQUAL(isp[0], "Hi with space");
        BOOST_REQUIRE(!isp.isDefined(1));
        BOOST_REQUIRE_THROW(isp[1], std::runtime_error);
        BOOST_REQUIRE(isp.isDefined(2));
        BOOST_REQUIRE_EQUAL(isp[2], "Bye");
    }
}

std::shared_ptr<mae::IndexedBlock> getExampleIndexedBlock()
{
    using namespace mae;
    auto ib = std::make_shared<IndexedBlock>("m_atom");

    // Set up a bool property
    std::vector<BoolProperty> dv = {true, false, true};
    boost::dynamic_bitset<>* bs = new boost::dynamic_bitset<>(3);
    bs->set(1);

    auto ibps = std::make_shared<IndexedBoolProperty>(dv, bs);
    ib->setBoolProperty("b_m_bool", ibps);

    // Set up a real property
    std::vector<double> rv = {0.1, 42};
    boost::dynamic_bitset<>* rbs = new boost::dynamic_bitset<>(3);
    rbs->set(2);

    auto irps = std::make_shared<IndexedRealProperty>(rv, rbs);
    ib->setRealProperty("r_m_reals", irps);

    return ib;
}

BOOST_AUTO_TEST_CASE(toStringProperties)
{
    const std::string rval =
        R"(dummy {
  b_m_bool
  r_m_real
  i_m_int
  s_m_string
  :::
  0
  1.000000
  42
  "mae\"parser"
  m_atom[3] {
    # First column is Index #
    b_m_bool
    r_m_reals
    :::
    1 1 0.100000
    2 <> 42.000000
    3 1 <>
    :::
  }
}

)";
    mae::Block b("dummy");
    b.setRealProperty("r_m_real", 1.0);
    b.setBoolProperty("b_m_bool", false);
    b.setIntProperty("i_m_int", 42);
    b.setStringProperty("s_m_string", "mae\"parser");

    auto ib = getExampleIndexedBlock();

    auto ibm = std::make_shared<mae::IndexedBlockMap>();
    ibm->addIndexedBlock(ib->getName(), ib);
    b.setIndexedBlockMap(ibm);

    BOOST_REQUIRE_EQUAL(b.toString(), rval);

    // All the properties we set have the same name, but different types

    auto bool_props = b.getProperties<mae::BoolProperty>();
    BOOST_CHECK_EQUAL(bool_props.size(), 1u);
    BOOST_CHECK_EQUAL(bool_props.begin()->first, std::string("b_m_bool"));

    auto int_props = b.getProperties<int>();
    BOOST_CHECK_EQUAL(int_props.size(), 1u);
    BOOST_CHECK_EQUAL(int_props.begin()->first, std::string("i_m_int"));

    auto double_props = b.getProperties<double>();
    BOOST_CHECK_EQUAL(double_props.size(), 1u);
    BOOST_CHECK_EQUAL(double_props.begin()->first, std::string("r_m_real"));

    auto string_props = b.getProperties<std::string>();
    BOOST_CHECK_EQUAL(string_props.size(), 1u);
    BOOST_CHECK_EQUAL(string_props.begin()->first, std::string("s_m_string"));
}

BOOST_AUTO_TEST_CASE(toStringIndexedProperties)
{
    using namespace mae;
    const std::string rval =
        R"(m_atom[3] {
  # First column is Index #
  b_m_bool
  r_m_reals
  :::
  1 1 0.100000
  2 <> 42.000000
  3 1 <>
  :::
}
)";

    auto ib = getExampleIndexedBlock();

    BOOST_REQUIRE_EQUAL(ib->toString(), rval);
}

BOOST_AUTO_TEST_SUITE_END()