Message ID | 20250403154925.382973-7-stefan.klug@ideasonboard.com |
---|---|
State | Accepted |
Headers | show |
Series |
|
Related | show |
Quoting Stefan Klug (2025-04-03 16:49:11) > Add a few tests for the Matrix class. This is not full fledged but at > least a starter. > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > > --- > > Changes in v2: > - Added this patch > > Changes in v3: > - Fixed test class name > --- > test/matrix.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ > test/meson.build | 1 + > 2 files changed, 54 insertions(+) > create mode 100644 test/matrix.cpp > > diff --git a/test/matrix.cpp b/test/matrix.cpp > new file mode 100644 > index 000000000000..3a1c8b4c88a2 > --- /dev/null > +++ b/test/matrix.cpp > @@ -0,0 +1,53 @@ > +/* SPDX-License-Identifier: GPL-2.0-or-later */ > +/* > + * Copyright (C) 2024, Ideas on Board Oy > + * > + * Vector tests > + */ > + > +#include "libcamera/internal/matrix.h" > + > +#include <cmath> > +#include <iostream> > + > +#include "test.h" > + > +using namespace libcamera; > + > +#define ASSERT_EQ(a, b) \ > + if ((a) != (b)) { \ > + std::cout << #a " != " #b << " (line " << __LINE__ << ")" \ > + << std::endl; \ > + return TestFail; \ > + } > + > +class MatrixTest : public Test > +{ > +protected: > + int run() > + { > + Matrix<double, 3, 3> m1; > + > + ASSERT_EQ(m1[0][0], 0.0); > + ASSERT_EQ(m1[0][1], 0.0); > + > + constexpr Matrix<float, 2, 2> m2 = Matrix<float, 2, 2>().identity(); > + ASSERT_EQ(m2[0][0], 1.0); > + ASSERT_EQ(m2[0][1], 0.0); > + ASSERT_EQ(m2[1][0], 0.0); > + ASSERT_EQ(m2[1][1], 1.0); > + > + Matrix<float, 2, 2> m3{ { 2.0, 0.0, 0.0, 2.0 } }; > + Matrix<float, 2, 2> m4 = m3.inverse(); Are there any corner cases we should test on the inverse()? Anyway, this gets it in place! Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > + > + Matrix<float, 2, 2> m5 = m3 * m4; > + ASSERT_EQ(m5[0][0], 1.0); > + ASSERT_EQ(m5[0][1], 0.0); > + ASSERT_EQ(m5[1][0], 0.0); > + ASSERT_EQ(m5[1][1], 1.0); > + > + return TestPass; > + } > +}; > + > +TEST_REGISTER(MatrixTest) > diff --git a/test/meson.build b/test/meson.build > index 4095664994fd..52f04364e4fc 100644 > --- a/test/meson.build > +++ b/test/meson.build > @@ -60,6 +60,7 @@ internal_tests = [ > {'name': 'file', 'sources': ['file.cpp']}, > {'name': 'flags', 'sources': ['flags.cpp']}, > {'name': 'hotplug-cameras', 'sources': ['hotplug-cameras.cpp']}, > + {'name': 'matrix', 'sources': ['matrix.cpp']}, > {'name': 'message', 'sources': ['message.cpp']}, > {'name': 'object', 'sources': ['object.cpp']}, > {'name': 'object-delete', 'sources': ['object-delete.cpp']}, > -- > 2.43.0 >
On Thu, Apr 03, 2025 at 05:49:11PM +0200, Stefan Klug wrote: > Add a few tests for the Matrix class. This is not full fledged but at > least a starter. > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > > --- > > Changes in v2: > - Added this patch > > Changes in v3: > - Fixed test class name > --- > test/matrix.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ > test/meson.build | 1 + > 2 files changed, 54 insertions(+) > create mode 100644 test/matrix.cpp > > diff --git a/test/matrix.cpp b/test/matrix.cpp > new file mode 100644 > index 000000000000..3a1c8b4c88a2 > --- /dev/null > +++ b/test/matrix.cpp > @@ -0,0 +1,53 @@ > +/* SPDX-License-Identifier: GPL-2.0-or-later */ > +/* > + * Copyright (C) 2024, Ideas on Board Oy > + * > + * Vector tests s/Vector/Matrix/ ...? Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> > + */ > + > +#include "libcamera/internal/matrix.h" > + > +#include <cmath> > +#include <iostream> > + > +#include "test.h" > + > +using namespace libcamera; > + > +#define ASSERT_EQ(a, b) \ > + if ((a) != (b)) { \ > + std::cout << #a " != " #b << " (line " << __LINE__ << ")" \ > + << std::endl; \ > + return TestFail; \ > + } > + > +class MatrixTest : public Test > +{ > +protected: > + int run() > + { > + Matrix<double, 3, 3> m1; > + > + ASSERT_EQ(m1[0][0], 0.0); > + ASSERT_EQ(m1[0][1], 0.0); > + > + constexpr Matrix<float, 2, 2> m2 = Matrix<float, 2, 2>().identity(); > + ASSERT_EQ(m2[0][0], 1.0); > + ASSERT_EQ(m2[0][1], 0.0); > + ASSERT_EQ(m2[1][0], 0.0); > + ASSERT_EQ(m2[1][1], 1.0); > + > + Matrix<float, 2, 2> m3{ { 2.0, 0.0, 0.0, 2.0 } }; > + Matrix<float, 2, 2> m4 = m3.inverse(); > + > + Matrix<float, 2, 2> m5 = m3 * m4; > + ASSERT_EQ(m5[0][0], 1.0); > + ASSERT_EQ(m5[0][1], 0.0); > + ASSERT_EQ(m5[1][0], 0.0); > + ASSERT_EQ(m5[1][1], 1.0); > + > + return TestPass; > + } > +}; > + > +TEST_REGISTER(MatrixTest) > diff --git a/test/meson.build b/test/meson.build > index 4095664994fd..52f04364e4fc 100644 > --- a/test/meson.build > +++ b/test/meson.build > @@ -60,6 +60,7 @@ internal_tests = [ > {'name': 'file', 'sources': ['file.cpp']}, > {'name': 'flags', 'sources': ['flags.cpp']}, > {'name': 'hotplug-cameras', 'sources': ['hotplug-cameras.cpp']}, > + {'name': 'matrix', 'sources': ['matrix.cpp']}, > {'name': 'message', 'sources': ['message.cpp']}, > {'name': 'object', 'sources': ['object.cpp']}, > {'name': 'object-delete', 'sources': ['object-delete.cpp']}, > -- > 2.43.0 >
diff --git a/test/matrix.cpp b/test/matrix.cpp new file mode 100644 index 000000000000..3a1c8b4c88a2 --- /dev/null +++ b/test/matrix.cpp @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2024, Ideas on Board Oy + * + * Vector tests + */ + +#include "libcamera/internal/matrix.h" + +#include <cmath> +#include <iostream> + +#include "test.h" + +using namespace libcamera; + +#define ASSERT_EQ(a, b) \ + if ((a) != (b)) { \ + std::cout << #a " != " #b << " (line " << __LINE__ << ")" \ + << std::endl; \ + return TestFail; \ + } + +class MatrixTest : public Test +{ +protected: + int run() + { + Matrix<double, 3, 3> m1; + + ASSERT_EQ(m1[0][0], 0.0); + ASSERT_EQ(m1[0][1], 0.0); + + constexpr Matrix<float, 2, 2> m2 = Matrix<float, 2, 2>().identity(); + ASSERT_EQ(m2[0][0], 1.0); + ASSERT_EQ(m2[0][1], 0.0); + ASSERT_EQ(m2[1][0], 0.0); + ASSERT_EQ(m2[1][1], 1.0); + + Matrix<float, 2, 2> m3{ { 2.0, 0.0, 0.0, 2.0 } }; + Matrix<float, 2, 2> m4 = m3.inverse(); + + Matrix<float, 2, 2> m5 = m3 * m4; + ASSERT_EQ(m5[0][0], 1.0); + ASSERT_EQ(m5[0][1], 0.0); + ASSERT_EQ(m5[1][0], 0.0); + ASSERT_EQ(m5[1][1], 1.0); + + return TestPass; + } +}; + +TEST_REGISTER(MatrixTest) diff --git a/test/meson.build b/test/meson.build index 4095664994fd..52f04364e4fc 100644 --- a/test/meson.build +++ b/test/meson.build @@ -60,6 +60,7 @@ internal_tests = [ {'name': 'file', 'sources': ['file.cpp']}, {'name': 'flags', 'sources': ['flags.cpp']}, {'name': 'hotplug-cameras', 'sources': ['hotplug-cameras.cpp']}, + {'name': 'matrix', 'sources': ['matrix.cpp']}, {'name': 'message', 'sources': ['message.cpp']}, {'name': 'object', 'sources': ['object.cpp']}, {'name': 'object-delete', 'sources': ['object-delete.cpp']},