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
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/
#include "eckit/filesystem/PathName.h"
#include "eckit/io/AutoCloser.h"
#include "eckit/serialisation/FileStream.h"
#include "util.h"
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
template <typename T>
void test(const T& v, const T& r) {
EXPECT(v.rows() == r.rows());
EXPECT(v.cols() == r.cols());
EXPECT(v.size() == r.size());
EXPECT(testing::make_view(v.begin(), v.end()) == testing::make_view(r.begin(), r.end()));
}
void test(const linalg::SparseMatrix& v, const linalg::SparseMatrix& r) {
EXPECT(v.rows() == r.rows());
EXPECT(v.cols() == r.cols());
EXPECT(v.nonZeros() == r.nonZeros());
const auto nnz = v.nonZeros();
EXPECT(testing::make_view(v.outer(), v.outer() + v.rows() + 1) ==
testing::make_view(r.outer(), r.outer() + r.rows() + 1));
EXPECT(testing::make_view(v.inner(), v.inner() + nnz) == testing::make_view(r.inner(), r.inner() + nnz));
EXPECT(testing::make_view(v.data(), v.data() + nnz) == testing::make_view(r.data(), r.data() + nnz));
}
template <typename T>
void stream_test(const T& t) {
PathName filename = PathName::unique("data");
{
FileStream sout(filename, "w");
auto c = closer(sout);
sout << t;
}
{
FileStream sin(filename, "r");
auto c = closer(sin);
T out(sin);
test(t, out);
}
if (filename.exists()) {
filename.unlink();
}
}
//----------------------------------------------------------------------------------------------------------------------
/// Test linear algebra interface
CASE("test_stream_vector") {
stream_test(V(5, 1., 2., 3., 4., 5.));
}
CASE("test_stream_matrix") {
stream_test(M(3, 3, 1., 2., 3., 4., 5., 6., 7., 8., 9.));
}
CASE("test_stream_sparsematrix") {
std::vector<eckit::linalg::Triplet> triplets;
triplets.emplace_back(0, 0, 2.);
triplets.emplace_back(0, 2, -3.);
triplets.emplace_back(1, 1, 2.);
triplets.emplace_back(2, 2, 2.);
eckit::linalg::SparseMatrix smat(3, 3, triplets);
stream_test(smat);
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return eckit::testing::run_tests(argc, argv);
}
|