Skip to content

Commit 6147a37

Browse files
committed
tests: move block-matrix.cpp test to core tests, split up gar test util
1 parent 7d8cdf3 commit 6147a37

File tree

7 files changed

+35
-40
lines changed

7 files changed

+35
-40
lines changed

examples/clqr.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "aligator/modelling/constraints.hpp"
88
#include <aligator/fmt-eigen.hpp>
99
#include <iostream>
10-
#include <random>
10+
#include "../tests/test_util.hpp"
1111

1212
using namespace aligator;
1313

@@ -23,20 +23,15 @@ using context::StageModel;
2323
using context::TrajOptProblem;
2424
using context::VectorXs;
2525

26-
static std::mt19937_64 urng{42};
27-
struct NormalGen {
28-
double operator()() const { return norm(urng); }
29-
mutable std::normal_distribution<double> norm;
30-
};
31-
3226
int main() {
27+
std::mt19937 rng{42};
3328
std::srand(42);
3429
const size_t nsteps = 100;
3530
const auto nx = 4;
3631
const auto nu = 2;
3732
const auto space = Space(nx);
3833

39-
NormalGen norm_gen;
34+
normal_unary_op norm_gen{rng};
4035
MatrixXs A;
4136
// clang-format off
4237
A.setIdentity(nx, nx);

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ endfunction()
3636
set(
3737
TEST_NAMES
3838
arena-matrix
39+
block-matrix
3940
continuous
4041
constraints
4142
costs
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <catch2/catch_get_random_seed.hpp>
55

66
#include "aligator/context.hpp"
7-
#include "aligator/gar/blk-matrix.hpp"
7+
#include "aligator/core/blk-matrix.hpp"
88
#include "aligator/gar/block-tridiagonal.hpp"
99
#include "aligator/core/bunchkaufman.hpp"
1010
#include <Eigen/Cholesky>
@@ -13,11 +13,10 @@
1313
#include <aligator/fmt-eigen.hpp>
1414

1515
using namespace aligator;
16-
using namespace aligator::context;
17-
18-
using MatrixXs = math_types<Scalar>::MatrixXs;
19-
using MatrixRef = Eigen::Ref<MatrixXs>;
20-
using VectorXs = math_types<Scalar>::VectorXs;
16+
using context::MatrixRef;
17+
using context::MatrixXs;
18+
using context::VectorXs;
19+
using BlkVec = BlkMatrix<VectorXs, -1, 1>;
2120

2221
TEST_CASE("blk22", "[gar]") {
2322
std::array<long, 2> dims = {4, 6};
@@ -63,7 +62,6 @@ struct BTAG_Fixture {
6362
std::vector<MatrixXs> diagonal;
6463
std::vector<MatrixXs> sup;
6564
std::vector<MatrixXs> sub;
66-
using BlkVec = BlkMatrix<VectorXs, -1, 1>;
6765
BlkVec vec;
6866
MatrixXs densemat;
6967
BlkVec rhs;

tests/gar/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(TEST_NAMES riccati block-matrix knot)
1+
set(TEST_NAMES riccati knot)
22

33
if(BUILD_WITH_OPENMP_SUPPORT)
44
list(APPEND TEST_NAMES parallel)

tests/gar/test_util.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// @copyright Copyright (C) 2023-2024 LAAS-CNRS, 2023-2025 INRIA
2-
#include "./test_util.hpp"
2+
#include "test_util.hpp"
33
#include "aligator/gar/utils.hpp"
44

55
#include <Eigen/Dense>
@@ -11,13 +11,6 @@ compute_conditioning(const Eigen::SelfAdjointView<Mat, Eigen::Lower> &view) {
1111
return eigvals(0) / eigvals(eigvals.size() - 1);
1212
}
1313

14-
/// Generate a Wishart-distributed matrix in @p n dimensions with @p p DoF
15-
MatrixXs sampleWishartDistributedMatrix(uint n, uint p) {
16-
std::mt19937 rng;
17-
MatrixXs root = MatrixXs::NullaryExpr(n, p, normal_unary_op(rng));
18-
return root * root.transpose();
19-
};
20-
2114
knot_t generateKnot(std::mt19937 rng, knot_gen_opts_t opts,
2215
const aligator::polymorphic_allocator &alloc) {
2316
normal_unary_op normal_op{rng};

tests/gar/test_util.hpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#pragma once
33

44
#include "aligator/gar/lqr-problem.hpp"
5-
#include <random>
5+
#include "../test_util.hpp"
66

77
ALIGATOR_DYNAMIC_TYPEDEFS(double);
88
using problem_t = aligator::gar::LqrProblemTpl<double>;
@@ -48,21 +48,6 @@ computeKktError(const problem_t &problem, const VectorOfVectors &xs,
4848
verbose);
4949
}
5050

51-
struct normal_unary_op {
52-
mutable std::mt19937 rng;
53-
mutable std::normal_distribution<double> gen;
54-
55-
explicit normal_unary_op(std::mt19937 rng, double stddev = 1.0)
56-
: rng(rng)
57-
, gen(0.0, stddev) {}
58-
59-
void set_seed(size_t sd) { rng.seed(sd); }
60-
61-
double operator()() const { return gen(rng); }
62-
};
63-
64-
MatrixXs sampleWishartDistributedMatrix(uint n, uint p);
65-
6651
struct knot_gen_opts_t {
6752
uint nx;
6853
uint nu;

tests/test_util.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <Eigen/Core>
2+
#include <random>
3+
4+
struct normal_unary_op {
5+
mutable std::mt19937 rng;
6+
mutable std::normal_distribution<double> gen;
7+
8+
explicit normal_unary_op(std::mt19937 rng, double stddev = 1.0)
9+
: rng(rng)
10+
, gen(0.0, stddev) {}
11+
12+
void set_seed(size_t sd) { rng.seed(sd); }
13+
14+
double operator()() const { return gen(rng); }
15+
};
16+
17+
/// Generate a Wishart-distributed matrix in @p n dimensions with @p p DoF
18+
inline Eigen::MatrixXd sampleWishartDistributedMatrix(uint n, uint p) {
19+
using Eigen::MatrixXd;
20+
std::mt19937 rng;
21+
MatrixXd root = MatrixXd::NullaryExpr(n, p, normal_unary_op(rng));
22+
return root * root.transpose();
23+
}

0 commit comments

Comments
 (0)