/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * geometry.cpp - Geometry classes tests */ #include #include #include "test.h" using namespace std; using namespace libcamera; class GeometryTest : public Test { protected: template bool compare(const T &lhs, const T &rhs, bool (*op)(const T &lhs, const T &rhs), const char *opName, bool expect) { bool result = op(lhs, rhs); if (result != expect) { cout << lhs.toString() << opName << " " << rhs.toString() << "test failed" << std::endl; return false; } return true; } int run() { /* * Point tests */ /* Equality */ if (!compare(Point(50, 100), Point(50, 100), &operator==, "==", true)) return TestFail; if (!compare(Point(-50, 100), Point(-50, 100), &operator==, "==", true)) return TestFail; if (!compare(Point(50, -100), Point(50, -100), &operator==, "==", true)) return TestFail; if (!compare(Point(-50, -100), Point(-50, -100), &operator==, "==", true)) return TestFail; /* Inequality */ if (!compare(Point(50, 100), Point(50, 100), &operator!=, "!=", false)) return TestFail; if (!compare(Point(-50, 100), Point(-50, 100), &operator!=, "!=", false)) return TestFail; if (!compare(Point(50, -100), Point(50, -100), &operator!=, "!=", false)) return TestFail; if (!compare(Point(-50, -100), Point(-50, -100), &operator!=, "!=", false)) return TestFail; if (!compare(Point(-50, 100), Point(50, 100), &operator!=, "!=", true)) return TestFail; if (!compare(Point(50, -100), Point(50, 100), &operator!=, "!=", true)) return TestFail; if (!compare(Point(-50, -100), Point(50, 100), &operator!=, "!=", true)) return TestFail; /* Negation */ if (Point(50, 100) != -Point(-50, -100) || Point(50, 100) == -Point(50, -100) || Point(50, 100) == -Point(-50, 100)) { cout << "Point negation test failed" << endl; return TestFail; } /* Default constructor */ if (Point() != Point(0, 0)) { cout << "Default constructor test failed" << endl; return TestFail; } /* * Size tests */ if (!Size().isNull() || !Size(0, 0).isNull()) { cout << "Null size incorrectly reported as not null" << endl; return TestFail; } if (Size(0, 100).isNull() || Size(100, 0).isNull() || Size(100, 100).isNull()) { cout << "Non-null size incorrectly reported as null" << endl; return TestFail; } /* Test alignDownTo(), alignUpTo(), boundTo() and expandTo() */ Size s(50, 50); s.alignDownTo(16, 16); if (s != Size(48, 48)) { cout << "Size::alignDownTo() test failed" << endl; return TestFail; } s.alignUpTo(32, 32); if (s != Size(64, 64)) { cout << "Size::alignUpTo() test failed" << endl; return TestFail; } s.boundTo({ 40, 40 }); if (s != Size(40, 40)) { cout << "Size::boundTo() test failed" << endl; return TestFail; } s.expandTo({ 50, 50 }); if (s != Size(50, 50)) { cout << "Size::expandTo() test failed" << endl; return TestFail; } s.alignDownTo(16, 16).alignUpTo(32, 32) .boundTo({ 40, 80 }).expandTo({ 16, 80 }); if (s != Size(40, 80)) { cout << "Size chained in-place modifiers test failed" << endl; return TestFail; } /* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */ if (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) || Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) || Size(16, 8).alignedDownTo(16, 8) != Size(16, 8)) { cout << "Size::alignedDownTo() test failed" << endl; return TestFail; } if (Size(0, 0).alignedUpTo(16, 8) != Size(0, 0) || Size(1, 1).alignedUpTo(16, 8) != Size(16, 8) || Size(16, 8).alignedUpTo(16, 8) != Size(16, 8)) { cout << "Size::alignedUpTo() test failed" << endl; return TestFail; } if (Size(0, 0).boundedTo({ 100, 100 }) != Size(0, 0) || Size(200, 50).boundedTo({ 100, 100 }) != Size(100, 50) || Size(50, 200).boundedTo({ 100, 100 }) != Size(50, 100)) { cout << "Size::boundedTo() test failed" << endl; return TestFail; } if (Size(0, 0).expandedTo({ 100, 100 }) != Size(100, 100) || Size(200, 50).expandedTo({ 100, 100 }) != Size(200, 100) || Size(50, 200).expandedTo({ 100, 100 }) != Size(100, 200)) { cout << "Size::expandedTo() test failed" << endl; return TestFail; } /* Aspect ratio tests */ if (Size(0, 0).boundedToAspectRatio(Size(4, 3)) != Size(0, 0) || Size(1920, 1440).boundedToAspectRatio(Size(16, 9)) != Size(1920, 1080) || Size(1920, 1440).boundedToAspectRatio(Size(65536, 36864)) != Size(1920, 1080) || Size(1440, 1920).boundedToAspectRatio(Size(9, 16)) != Size(1080, 1920) || Size(1920, 1080).boundedToAspectRatio(Size(4, 3)) != Size(1440, 1080) || Size(1920, 1080).boundedToAspectRatio(Size(65536, 49152)) != Size(1440, 1080) || Size(1024, 1024).boundedToAspectRatio(Size(1, 1)) != Size(1024, 1024) || Size(1920, 1080).boundedToAspectRatio(Size(16, 9)) != Size(1920, 1080) || Size(200, 100).boundedToAspectRatio(Size(16, 9)) != Size(177, 100) || Size(300, 200).boundedToAspectRatio(Size(16, 9)) != Size(300, 168)) { cout << "Size::boundedToAspectRatio() test failed" << endl; return TestFail; } if (Size(0, 0).expandedToAspectRatio(Size(4, 3)) != Size(0, 0) || Size(1920, 1440).expandedToAspectRatio(Size(16, 9)) != Size(2560, 1440) || Size(1920, 1440).expandedToAspectRatio(Size(65536, 36864)) != Size(2560, 1440) || Size(1440, 1920).expandedToAspectRatio(Size(9, 16)) != Size(1440, 2560) || Size(1920, 1080).expandedToAspectRatio(Size(4, 3)) != Size(1920, 1440) || Size(1920, 1080).expandedToAspectRatio(Size(65536, 49152)) != Size(1920, 1440) || Size(1024, 1024).expandedToAspectRatio(Size(1, 1)) != Size(1024, 1024) || Size(1920, 1080).expandedToAspectRatio(Size(16, 9)) != Size(1920, 1080) || Size(200, 100).expandedToAspectRatio(Size(16, 9)) != Size(200, 112) || Size(300, 200).expandedToAspectRatio(Size(16, 9)) != Size(355, 200)) { cout << "Size::expandedToAspectRatio() test failed" << endl; return TestFail; } /* Size::centeredTo() tests */ if (Size(0, 0).centeredTo(Point(50, 100)) != Rectangle(50, 100, 0, 0) || Size(0, 0).centeredTo(Point(-50, -100)) != Rectangle(-50, -100, 0, 0) || Size(100, 200).centeredTo(Point(50, 100)) != Rectangle(0, 0, 100, 200) || Size(100, 200).centeredTo(Point(-50, -100)) != Rectangle(-100, -200, 100, 200) || /* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * stream_formats.cpp - StreamFormats test */ #include <iostream> #include <libcamera/geometry.h> #include <libcamera/stream.h> #include "test.h" using namespace std; using namespace libcamera; class StreamFormatsTest : public Test { protected: int testSizes(std::string name, std::vector<Size> test, std::vector<Size> valid) { bool pass = false; for (Size &size : test) { pass = false; for (Size &validSize : valid) { if (size == validSize) { pass = true; break; } } if (!pass) break; } if (!pass) { cout << "Failed " << name << endl; cout << "Sizes to test:" << endl; for (Size &size : test) cout << size << endl; cout << "Valid sizes:" << endl; for (Size &size : valid) cout << size << endl; return TestFail; } return TestPass; } int run() { /* Test discrete sizes */ StreamFormats discrete({ { PixelFormat(1), { SizeRange({ 100, 100 }), SizeRange({ 200, 200 }) } }, { PixelFormat(2), { SizeRange({ 300, 300 }), SizeRange({ 400, 400 }) } }, }); if (testSizes("discrete 1", discrete.sizes(PixelFormat(1)), { Size(100, 100), Size(200, 200) })) return TestFail; if (testSizes("discrete 2", discrete.sizes(PixelFormat(2)), { Size(300, 300), Size(400, 400) })) return TestFail; /* Test range sizes */ StreamFormats range({ { PixelFormat(1), { SizeRange({ 640, 480 }, { 640, 480 }) } }, { PixelFormat(2), { SizeRange({ 640, 480 }, { 800, 600 }, 8, 8) } }, { PixelFormat(3), { SizeRange({ 640, 480 }, { 800, 600 }, 16, 16) } }, { PixelFormat(4), { SizeRange({ 128, 128 }, { 4096, 4096 }, 128, 128) } }, }); if (testSizes("range 1", range.sizes(PixelFormat(1)), { Size(640, 480) })) return TestFail; if (testSizes("range 2", range.sizes(PixelFormat(2)), { Size(640, 480), Size(720, 480), Size(720, 576), Size(768, 480), Size(800, 600) })) return TestFail; if (testSizes("range 3", range.sizes(PixelFormat(3)), { Size(640, 480), Size(720, 480), Size(720, 576), Size(768, 480) })) return TestFail; if (testSizes("range 4", range.sizes(PixelFormat(4)), { Size(1024, 768), Size(1280, 1024), Size(2048, 1152), Size(2048, 1536), Size(2560, 2048), Size(3200, 2048), })) return TestFail; return TestPass; } }; TEST_REGISTER(StreamFormatsTest)