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
d39ce5a2
Commit
d39ce5a2
authored
3 years ago
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Add missing tests for DenseMatrix
parent
aff10f34
No related branches found
No related tags found
1 merge request
!103
Set AssertError to inherit from std::runtime_error
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/algebra/DenseMatrix.hpp
+14
-13
14 additions, 13 deletions
src/algebra/DenseMatrix.hpp
tests/test_DenseMatrix.cpp
+89
-0
89 additions, 0 deletions
tests/test_DenseMatrix.cpp
with
103 additions
and
13 deletions
src/algebra/DenseMatrix.hpp
+
14
−
13
View file @
d39ce5a2
...
...
@@ -66,7 +66,7 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_columns
==
x
.
size
());
Assert
(
m_nb_columns
==
x
.
size
()
,
"cannot compute matrix-vector product: incompatible sizes"
);
const
DenseMatrix
&
A
=
*
this
;
Vector
<
std
::
remove_const_t
<
DataType
>>
Ax
{
m_nb_rows
};
for
(
size_t
i
=
0
;
i
<
m_nb_rows
;
++
i
)
{
...
...
@@ -85,7 +85,7 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_columns
==
B
.
numberOfRows
());
Assert
(
m_nb_columns
==
B
.
numberOfRows
()
,
"cannot compute matrix product: incompatible sizes"
);
const
DenseMatrix
&
A
=
*
this
;
DenseMatrix
<
std
::
remove_const_t
<
DataType
>>
AB
{
m_nb_rows
,
B
.
numberOfColumns
()};
...
...
@@ -124,8 +124,8 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_rows
==
B
.
numberOfRows
())
;
Assert
(
m_nb_columns
==
B
.
numberOfColumns
()
);
Assert
(
(
m_nb_rows
==
B
.
numberOfRows
())
and
(
m_nb_columns
==
B
.
numberOfColumns
()),
"cannot substract matrix: incompatible sizes"
);
parallel_for
(
m_values
.
size
(),
PUGS_LAMBDA
(
index_type
i
)
{
m_values
[
i
]
-=
B
.
m_values
[
i
];
});
...
...
@@ -138,8 +138,7 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_rows
==
B
.
numberOfRows
());
Assert
(
m_nb_columns
==
B
.
numberOfColumns
());
Assert
((
m_nb_rows
==
B
.
numberOfRows
())
and
(
m_nb_columns
==
B
.
numberOfColumns
()),
"incompatible matrix sizes"
);
parallel_for
(
m_values
.
size
(),
PUGS_LAMBDA
(
index_type
i
)
{
m_values
[
i
]
+=
B
.
m_values
[
i
];
});
...
...
@@ -152,8 +151,9 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_rows
==
B
.
numberOfRows
());
Assert
(
m_nb_columns
==
B
.
numberOfColumns
());
Assert
((
m_nb_rows
==
B
.
numberOfRows
())
and
(
m_nb_columns
==
B
.
numberOfColumns
()),
"cannot compute matrix sum: incompatible sizes"
);
DenseMatrix
<
std
::
remove_const_t
<
DataType
>>
sum
{
B
.
numberOfRows
(),
B
.
numberOfColumns
()};
parallel_for
(
...
...
@@ -168,8 +168,9 @@ class DenseMatrix // LCOV_EXCL_LINE
{
static_assert
(
std
::
is_same_v
<
std
::
remove_const_t
<
DataType
>
,
std
::
remove_const_t
<
DataType2
>>
,
"incompatible data types"
);
Assert
(
m_nb_rows
==
B
.
numberOfRows
());
Assert
(
m_nb_columns
==
B
.
numberOfColumns
());
Assert
((
m_nb_rows
==
B
.
numberOfRows
())
and
(
m_nb_columns
==
B
.
numberOfColumns
()),
"cannot compute matrix difference: incompatible sizes"
);
DenseMatrix
<
std
::
remove_const_t
<
DataType
>>
difference
{
B
.
numberOfRows
(),
B
.
numberOfColumns
()};
parallel_for
(
...
...
@@ -182,7 +183,7 @@ class DenseMatrix // LCOV_EXCL_LINE
DataType
&
operator
()(
index_type
i
,
index_type
j
)
const
noexcept
(
NO_ASSERT
)
{
Assert
(
(
i
<
m_nb_rows
and
j
<
m_nb_columns
)
,
"invalid indices"
);
Assert
(
i
<
m_nb_rows
and
j
<
m_nb_columns
,
"
cannot access element:
invalid indices"
);
return
m_values
[
i
*
m_nb_columns
+
j
];
}
...
...
@@ -214,7 +215,7 @@ class DenseMatrix // LCOV_EXCL_LINE
PUGS_INLINE
DenseMatrix
&
operator
=
(
IdentityType
)
noexcept
(
NO_ASSERT
)
{
Assert
(
m_nb_rows
==
m_nb_columns
,
"
I
dentity must be a square matrix"
);
Assert
(
m_nb_rows
==
m_nb_columns
,
"
i
dentity must be a square matrix"
);
m_values
.
fill
(
0
);
parallel_for
(
...
...
@@ -286,7 +287,7 @@ class DenseMatrix // LCOV_EXCL_LINE
DenseMatrix
(
size_t
nb_rows
,
size_t
nb_columns
,
const
Array
<
DataType
>
values
)
noexcept
(
NO_ASSERT
)
:
m_nb_rows
{
nb_rows
},
m_nb_columns
{
nb_columns
},
m_values
{
values
}
{
Assert
(
m_values
.
size
()
==
m_nb_rows
*
m_nb_columns
,
"
incompatible siz
es"
);
Assert
(
m_values
.
size
()
==
m_nb_rows
*
m_nb_columns
,
"
cannot create matrix: incorrect number of valu
es"
);
}
public
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_DenseMatrix.cpp
+
89
−
0
View file @
d39ce5a2
...
...
@@ -20,6 +20,15 @@ TEST_CASE("DenseMatrix", "[algebra]")
REQUIRE
(
A
.
numberOfColumns
()
==
3
);
}
SECTION
(
"is square"
)
{
REQUIRE
(
not
DenseMatrix
<
int
>
{
2
,
3
}.
isSquare
());
REQUIRE
(
not
DenseMatrix
<
int
>
{
4
,
3
}.
isSquare
());
REQUIRE
(
DenseMatrix
<
int
>
{
1
,
1
}.
isSquare
());
REQUIRE
(
DenseMatrix
<
int
>
{
2
,
2
}.
isSquare
());
REQUIRE
(
DenseMatrix
<
int
>
{
3
,
3
}.
isSquare
());
}
SECTION
(
"write access"
)
{
DenseMatrix
<
int
>
A
{
2
,
3
};
...
...
@@ -56,6 +65,28 @@ TEST_CASE("DenseMatrix", "[algebra]")
REQUIRE
(
A
(
1
,
2
)
==
2
);
}
SECTION
(
"zero matrix"
)
{
DenseMatrix
<
int
>
A
{
2
,
3
};
A
=
zero
;
for
(
size_t
i
=
0
;
i
<
A
.
numberOfRows
();
++
i
)
{
for
(
size_t
j
=
0
;
j
<
A
.
numberOfColumns
();
++
j
)
{
REQUIRE
(
A
(
i
,
j
)
==
0
);
}
}
}
SECTION
(
"identity matrix"
)
{
DenseMatrix
<
int
>
A
{
3
,
3
};
A
=
identity
;
for
(
size_t
i
=
0
;
i
<
A
.
numberOfRows
();
++
i
)
{
for
(
size_t
j
=
0
;
j
<
A
.
numberOfColumns
();
++
j
)
{
REQUIRE
(
A
(
i
,
j
)
==
(
i
==
j
));
}
}
}
SECTION
(
"copy constructor (shallow)"
)
{
DenseMatrix
<
int
>
A
{
2
,
3
};
...
...
@@ -341,4 +372,62 @@ TEST_CASE("DenseMatrix", "[algebra]")
REQUIRE
(
C
(
1
,
0
)
==
64
);
REQUIRE
(
C
(
1
,
1
)
==
137
);
}
#ifndef NDEBUG
SECTION
(
"non square identity matrix"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
REQUIRE_THROWS_WITH
(
A
=
identity
,
"identity must be a square matrix"
);
}
SECTION
(
"invalid matrix vector product"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
Vector
<
int
>
u
(
2
);
REQUIRE_THROWS_WITH
(
A
*
u
,
"cannot compute matrix-vector product: incompatible sizes"
);
}
SECTION
(
"invalid matrix product"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
DenseMatrix
<
int
>
B
(
2
,
3
);
REQUIRE_THROWS_WITH
(
A
*
B
,
"cannot compute matrix product: incompatible sizes"
);
}
SECTION
(
"invalid matrix substract"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
DenseMatrix
<
int
>
B
(
3
,
2
);
REQUIRE_THROWS_WITH
(
A
-=
B
,
"cannot substract matrix: incompatible sizes"
);
}
SECTION
(
"invalid matrix sum"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
DenseMatrix
<
int
>
B
(
3
,
2
);
REQUIRE_THROWS_WITH
(
A
+
B
,
"cannot compute matrix sum: incompatible sizes"
);
}
SECTION
(
"invalid matrix difference"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
DenseMatrix
<
int
>
B
(
3
,
2
);
REQUIRE_THROWS_WITH
(
A
-
B
,
"cannot compute matrix difference: incompatible sizes"
);
}
SECTION
(
"invalid indices"
)
{
DenseMatrix
<
int
>
A
(
2
,
3
);
REQUIRE_THROWS_WITH
(
A
(
2
,
0
),
"cannot access element: invalid indices"
);
REQUIRE_THROWS_WITH
(
A
(
0
,
3
),
"cannot access element: invalid indices"
);
}
#endif // NDEBUG
}
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