Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
libcifpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
libcifpp
Commits
96a26eae
Commit
96a26eae
authored
Sep 28, 2020
by
Maarten L. Hekkelman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a unit test
parent
71a46cd1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
211 additions
and
58 deletions
+211
-58
src/CifParser.cpp
+0
-2
test/unit-test.cpp
+211
-56
No files found.
src/CifParser.cpp
View file @
96a26eae
...
...
@@ -889,8 +889,6 @@ void DictParser::linkItems()
if
(
not
mDataBlock
)
error
(
"no datablock"
);
auto
&
dict
=
*
mDataBlock
;
std
::
map
<
std
::
tuple
<
std
::
string
,
std
::
string
,
int
>
,
size_t
>
linkIndex
;
std
::
vector
<
std
::
tuple
<
std
::
vector
<
std
::
string
>
,
std
::
vector
<
std
::
string
>>>
linkKeys
;
...
...
test/unit-test.cpp
View file @
96a26eae
...
...
@@ -34,25 +34,25 @@
cif
::
File
operator
""
_cf
(
const
char
*
text
,
size_t
length
)
{
struct
membuf
:
public
std
::
streambuf
{
membuf
(
char
*
text
,
size_t
length
)
{
this
->
setg
(
text
,
text
,
text
+
length
);
}
}
buffer
(
const_cast
<
char
*>
(
text
),
length
);
std
::
istream
is
(
&
buffer
);
return
cif
::
File
(
is
);
struct
membuf
:
public
std
::
streambuf
{
membuf
(
char
*
text
,
size_t
length
)
{
this
->
setg
(
text
,
text
,
text
+
length
);
}
}
buffer
(
const_cast
<
char
*>
(
text
),
length
);
std
::
istream
is
(
&
buffer
);
return
cif
::
File
(
is
);
}
// --------------------------------------------------------------------
BOOST_AUTO_TEST_CASE
(
ut1
)
{
// using namespace mmcif;
// using namespace mmcif;
auto
f
=
R"(data_TEST
auto
f
=
R"(data_TEST
#
loop_
_test.id
...
...
@@ -60,38 +60,38 @@ _test.name
1 aap
2 noot
3 mies
)"
_cf
;
)"
_cf
;
auto
&
db
=
f
.
firstDatablock
();
auto
&
db
=
f
.
firstDatablock
();
BOOST_CHECK
(
db
.
getName
()
==
"TEST"
);
auto
&
test
=
db
[
"test"
];
BOOST_CHECK
(
test
.
size
()
==
3
);
BOOST_CHECK
(
db
.
getName
()
==
"TEST"
);
auto
&
test
=
db
[
"test"
];
BOOST_CHECK
(
test
.
size
()
==
3
);
// wrong! the next lines will crash. And that's OK, don't do that
// for (auto r: test)
// test.erase(r);
// BOOST_CHECK(test.empty());
// wrong! the next lines will crash. And that's OK, don't do that
// for (auto r: test)
// test.erase(r);
// BOOST_CHECK(test.empty());
// test.purge();
// test.purge();
auto
n
=
test
.
erase
(
cif
::
Key
(
"id"
)
==
1
,
[](
const
cif
::
Row
&
r
)
{
BOOST_CHECK_EQUAL
(
r
[
"id"
].
as
<
int
>
(),
1
);
BOOST_CHECK_EQUAL
(
r
[
"name"
].
as
<
std
::
string
>
(),
"aap"
);
});
auto
n
=
test
.
erase
(
cif
::
Key
(
"id"
)
==
1
,
[](
const
cif
::
Row
&
r
)
{
BOOST_CHECK_EQUAL
(
r
[
"id"
].
as
<
int
>
(),
1
);
BOOST_CHECK_EQUAL
(
r
[
"name"
].
as
<
std
::
string
>
(),
"aap"
);
});
BOOST_CHECK_EQUAL
(
n
,
1
);
BOOST_CHECK_EQUAL
(
n
,
1
);
}
// --------------------------------------------------------------------
BOOST_AUTO_TEST_CASE
(
ut2
)
{
// using namespace mmcif;
// using namespace mmcif;
auto
f
=
R"(data_TEST
auto
f
=
R"(data_TEST
#
loop_
_test.id
...
...
@@ -100,29 +100,183 @@ _test.value
1 aap 1.0
2 noot 1.1
3 mies 1.2
)"
_cf
;
auto
&
db
=
f
.
firstDatablock
();
BOOST_CHECK
(
db
.
getName
()
==
"TEST"
);
auto
&
test
=
db
[
"test"
];
BOOST_CHECK
(
test
.
size
()
==
3
);
int
n
=
0
;
for
(
auto
r
:
test
.
find
(
cif
::
Key
(
"name"
)
==
"aap"
))
{
BOOST_CHECK
(
++
n
==
1
);
BOOST_CHECK
(
r
[
"id"
].
as
<
int
>
()
==
1
);
BOOST_CHECK
(
r
[
"name"
].
as
<
std
::
string
>
()
==
"aap"
);
BOOST_CHECK
(
r
[
"value"
].
as
<
float
>
()
==
1.0
);
}
auto
t
=
test
.
find
(
cif
::
Key
(
"id"
)
==
1
);
BOOST_CHECK
(
not
t
.
empty
());
BOOST_CHECK
(
t
.
front
()[
"name"
].
as
<
std
::
string
>
()
==
"aap"
);
auto
t2
=
test
.
find
(
cif
::
Key
(
"value"
)
==
1.2
);
BOOST_CHECK
(
not
t2
.
empty
());
BOOST_CHECK
(
t2
.
front
()[
"name"
].
as
<
std
::
string
>
()
==
"mies"
);
)"
_cf
;
auto
&
db
=
f
.
firstDatablock
();
BOOST_CHECK
(
db
.
getName
()
==
"TEST"
);
auto
&
test
=
db
[
"test"
];
BOOST_CHECK
(
test
.
size
()
==
3
);
int
n
=
0
;
for
(
auto
r
:
test
.
find
(
cif
::
Key
(
"name"
)
==
"aap"
))
{
BOOST_CHECK
(
++
n
==
1
);
BOOST_CHECK
(
r
[
"id"
].
as
<
int
>
()
==
1
);
BOOST_CHECK
(
r
[
"name"
].
as
<
std
::
string
>
()
==
"aap"
);
BOOST_CHECK
(
r
[
"value"
].
as
<
float
>
()
==
1.0
);
}
auto
t
=
test
.
find
(
cif
::
Key
(
"id"
)
==
1
);
BOOST_CHECK
(
not
t
.
empty
());
BOOST_CHECK
(
t
.
front
()[
"name"
].
as
<
std
::
string
>
()
==
"aap"
);
auto
t2
=
test
.
find
(
cif
::
Key
(
"value"
)
==
1.2
);
BOOST_CHECK
(
not
t2
.
empty
());
BOOST_CHECK
(
t2
.
front
()[
"name"
].
as
<
std
::
string
>
()
==
"mies"
);
}
// --------------------------------------------------------------------
BOOST_AUTO_TEST_CASE
(
d1
)
{
const
char
dict
[]
=
R"(
data_test_dict.dic
_datablock.id test_dict.dic
_datablock.description
;
A test dictionary
;
_dictionary.title test_dict.dic
_dictionary.datablock_id test_dict.dic
_dictionary.version 1.0
loop_
_item_type_list.code
_item_type_list.primitive_code
_item_type_list.construct
_item_type_list.detail
code char
'[][_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
; code item types/single words ...
;
text char
'[][ \n\t()_,.;:"&<>/\{}'`~!@#$%?+=*A-Za-z0-9|^-]*'
; text item types / multi-line text ...
;
int numb
'[+-]?[0-9]+'
; int item types are the subset of numbers that are the negative
or positive integers.
;
save_cat_1
_category.description 'A simple test category'
_category.id cat_1
_category.mandatory_code no
_category_key.name '_cat_1.id'
save_
save__cat_1.id
_item.name '_cat_1.id'
_item.category_id cat_1
_item.mandatory_code yes
_item_aliases.dictionary cif_core.dic
_item_aliases.version 2.0.1
_item_linked.child_name '_cat_2.parent_id'
_item_linked.parent_name '_cat_1.id'
_item_type.code code
save_
save__cat_1.name
_item.name '_cat_1.name'
_item.category_id cat_1
_item.mandatory_code yes
_item_aliases.dictionary cif_core.dic
_item_aliases.version 2.0.1
_item_type.code text
save_
save_cat_2
_category.description 'A second simple test category'
_category.id cat_2
_category.mandatory_code no
_category_key.name '_cat_2.id'
save_
save__cat_2.id
_item.name '_cat_2.id'
_item.category_id cat_2
_item.mandatory_code yes
_item_aliases.dictionary cif_core.dic
_item_aliases.version 2.0.1
_item_type.code code
save_
save__cat_2.parent_id
_item.name '_cat_2.parent_id'
_item.category_id cat_2
_item.mandatory_code yes
_item_aliases.dictionary cif_core.dic
_item_aliases.version 2.0.1
_item_type.code code
save_
save__cat_2.desc
_item.name '_cat_2.desc'
_item.category_id cat_2
_item.mandatory_code yes
_item_aliases.dictionary cif_core.dic
_item_aliases.version 2.0.1
_item_type.code text
save_
)"
;
struct
membuf
:
public
std
::
streambuf
{
membuf
(
char
*
text
,
size_t
length
)
{
this
->
setg
(
text
,
text
,
text
+
length
);
}
}
buffer
(
const_cast
<
char
*>
(
dict
),
sizeof
(
dict
)
-
1
);
std
::
istream
is_dict
(
&
buffer
);
cif
::
File
f
;
f
.
loadDictionary
(
is_dict
);
// --------------------------------------------------------------------
const
char
data
[]
=
R"(
data_test
loop_
_cat_1.id
_cat_1.name
1 Aap
2 Noot
3 Mies
loop_
_cat_2.id
_cat_2.parent_id
_cat_2.desc
1 1 'Een dier'
2 1 'Een andere aap'
3 2 'walnoot bijvoorbeeld'
)"
;
struct
data_membuf
:
public
std
::
streambuf
{
data_membuf
(
char
*
text
,
size_t
length
)
{
this
->
setg
(
text
,
text
,
text
+
length
);
}
}
data_buffer
(
const_cast
<
char
*>
(
data
),
sizeof
(
data
)
-
1
);
std
::
istream
is_data
(
&
data_buffer
);
f
.
load
(
is_data
);
auto
&
cat1
=
f
.
firstDatablock
()[
"cat_1"
];
auto
&
cat2
=
f
.
firstDatablock
()[
"cat_2"
];
BOOST_CHECK
(
cat1
.
size
()
==
3
);
BOOST_CHECK
(
cat2
.
size
()
==
3
);
cat1
.
erase
(
cif
::
Key
(
"id"
)
==
1
);
BOOST_CHECK
(
cat1
.
size
()
==
2
);
BOOST_CHECK
(
cat2
.
size
()
==
1
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment