Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
5907c128
Commit
5907c128
authored
4 years ago
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for CastArray
parent
53c3ad8b
No related branches found
No related tags found
1 merge request
!63
Feature/utils coverage
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/utils/CastArray.hpp
+9
-13
9 additions, 13 deletions
src/utils/CastArray.hpp
tests/CMakeLists.txt
+1
-0
1 addition, 0 deletions
tests/CMakeLists.txt
tests/test_CastArray.cpp
+93
-0
93 additions, 0 deletions
tests/test_CastArray.cpp
with
103 additions
and
13 deletions
src/utils/CastArray.hpp
+
9
−
13
View file @
5907c128
...
...
@@ -8,7 +8,7 @@
#include
<iostream>
template
<
typename
DataType
,
typename
CastDataType
>
class
CastArray
class
[[
nodiscard
]]
CastArray
{
public:
using
data_type
=
CastDataType
;
...
...
@@ -20,15 +20,13 @@ class CastArray
public:
PUGS_INLINE
const
size_t
&
size
()
const
const
size_t
&
size
()
const
{
return
m_size
;
}
PUGS_INLINE
CastDataType
&
operator
[](
size_t
i
)
const
CastDataType
&
operator
[](
size_t
i
)
const
{
Assert
(
i
<
m_size
);
return
m_values
[
i
];
...
...
@@ -40,11 +38,10 @@ class CastArray
PUGS_INLINE
CastArray
&
operator
=
(
CastArray
&&
)
=
default
;
PUGS_INLINE
CastArray
(
const
Array
<
DataType
>&
array
)
explicit
CastArray
(
const
Array
<
DataType
>&
array
)
:
m_array
(
array
),
m_size
(
sizeof
(
DataType
)
*
array
.
size
()
/
sizeof
(
CastDataType
)),
m_values
((
array
.
size
()
==
0
)
?
nullptr
:
reinterpret_cast
<
CastDataType
*>
(
&
(
array
[
0
])))
m_values
((
array
.
size
()
==
0
)
?
nullptr
:
reinterpret_cast
<
CastDataType
*>
(
&
(
static_cast
<
DataType
&>
(
array
[
0
])))
)
{
static_assert
((
std
::
is_const_v
<
CastDataType
>
and
std
::
is_const_v
<
DataType
>
)
or
(
not
std
::
is_const_v
<
DataType
>
),
"CastArray cannot remove const attribute"
);
...
...
@@ -54,9 +51,8 @@ class CastArray
}
}
PUGS_INLINE
CastArray
(
DataType
&
value
)
:
m_size
(
sizeof
(
DataType
)
/
sizeof
(
CastDataType
)),
m_values
(
reinterpret_cast
<
CastDataType
*>
(
&
(
value
)))
explicit
CastArray
(
DataType
&
value
)
:
m_size
(
sizeof
(
DataType
)
/
sizeof
(
CastDataType
)),
m_values
(
reinterpret_cast
<
CastDataType
*>
(
&
value
))
{
static_assert
((
std
::
is_const_v
<
CastDataType
>
and
std
::
is_const_v
<
DataType
>
)
or
(
not
std
::
is_const_v
<
DataType
>
),
"CastArray cannot remove const attribute"
);
...
...
This diff is collapsed.
Click to expand it.
tests/CMakeLists.txt
+
1
−
0
View file @
5907c128
...
...
@@ -49,6 +49,7 @@ add_executable (unit_tests
test_BuiltinFunctionEmbedder.cpp
test_BuiltinFunctionEmbedderTable.cpp
test_BuiltinFunctionProcessor.cpp
test_CastArray.cpp
test_CG.cpp
test_ContinueProcessor.cpp
test_ConcatExpressionProcessor.cpp
...
...
This diff is collapsed.
Click to expand it.
tests/test_CastArray.cpp
0 → 100644
+
93
−
0
View file @
5907c128
#ifndef TEST_CAST_ARRAY_HPP
#define TEST_CAST_ARRAY_HPP
#include
<catch2/catch.hpp>
#include
<utils/ArrayUtils.hpp>
#include
<utils/CastArray.hpp>
// clazy:excludeall=non-pod-global-static
TEST_CASE
(
"CastArray"
,
"[utils]"
)
{
SECTION
(
"explicit cast Array -> CastArray"
)
{
Array
<
double
>
x_double
{
5
};
x_double
[
0
]
=
1
;
x_double
[
1
]
=
2
;
x_double
[
2
]
=
3
;
x_double
[
3
]
=
4
;
x_double
[
4
]
=
5
;
CastArray
<
double
,
char
>
x_char
{
x_double
};
REQUIRE
(
x_char
.
size
()
*
sizeof
(
char
)
==
x_double
.
size
()
*
sizeof
(
double
));
Array
<
char
>
y_char
{
x_char
.
size
()};
for
(
size_t
i
=
0
;
i
<
x_char
.
size
();
++
i
)
{
y_char
[
i
]
=
x_char
[
i
];
}
CastArray
<
char
,
double
>
y_double
{
y_char
};
REQUIRE
(
y_char
.
size
()
*
sizeof
(
char
)
==
y_double
.
size
()
*
sizeof
(
double
));
REQUIRE
(
&
(
y_double
[
0
])
!=
&
(
x_double
[
0
]));
for
(
size_t
i
=
0
;
i
<
y_double
.
size
();
++
i
)
{
REQUIRE
(
y_double
[
i
]
==
x_double
[
i
]);
}
}
SECTION
(
"explicit cast value -> CastArray"
)
{
double
x
=
3
;
CastArray
<
double
,
char
>
x_char
(
x
);
REQUIRE
(
x_char
.
size
()
*
sizeof
(
char
)
==
sizeof
(
double
));
}
SECTION
(
"invalid cast array"
)
{
Array
<
char
>
x_char
{
13
};
REQUIRE_THROWS_WITH
((
CastArray
<
char
,
double
>
{
x_char
}),
"unexpected error: cannot cast array to the chosen data type"
);
}
SECTION
(
"cast array utilities"
)
{
SECTION
(
"Array -> CastArray"
)
{
Array
<
double
>
x_double
{
5
};
x_double
[
0
]
=
1.3
;
x_double
[
1
]
=
3.2
;
x_double
[
2
]
=
-
4
;
x_double
[
3
]
=
6.2
;
x_double
[
4
]
=
-
1.6
;
CastArray
<
double
,
short
>
x_short
{
x_double
};
auto
x_short_from
=
cast_array_to
<
short
>::
from
(
x_double
);
REQUIRE
(
x_short_from
.
size
()
==
x_short
.
size
());
for
(
size_t
i
=
0
;
i
<
x_short_from
.
size
();
++
i
)
{
REQUIRE
(
x_short_from
[
i
]
==
x_short
[
i
]);
}
}
SECTION
(
"Value -> CastArray"
)
{
double
x
=
3.14
;
CastArray
<
double
,
short
>
x_short
{
x
};
auto
x_short_from
=
cast_value_to
<
short
>::
from
(
x
);
REQUIRE
(
x_short_from
.
size
()
==
x_short
.
size
());
for
(
size_t
i
=
0
;
i
<
x_short_from
.
size
();
++
i
)
{
REQUIRE
(
x_short_from
[
i
]
==
x_short
[
i
]);
}
}
}
}
#endif // TEST_CAST_ARRAY_HPP
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment