summaryrefslogtreecommitdiff
path: root/test/ipa/libipa/vector.cpp
blob: 8e4ec77d7820fcd990e7fa79b8308f3af4c6b7c8 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2024, Ideas on Board Oy
 *
 * Vector tests
 */

#include "../src/ipa/libipa/vector.h"

#include <cmath>
#include <iostream>

#include "test.h"

using namespace libcamera::ipa;

#define ASSERT_EQ(a, b)							\
if ((a) != (b)) {							\
	std::cout << #a " != " #b << " (line " << __LINE__ << ")"	\
		  << std::endl;						\
	return TestFail;						\
}

class VectorTest : public Test
{
protected:
	int run()
	{
		Vector<double, 3> v1{ 0.0 };

		ASSERT_EQ(v1[0], 0.0);
		ASSERT_EQ(v1[1], 0.0);
		ASSERT_EQ(v1[2], 0.0);

		ASSERT_EQ(v1.length(), 0.0);
		ASSERT_EQ(v1.length2(), 0.0);

		Vector<double, 3> v2{{ 1.0, 4.0, 8.0 }};

		ASSERT_EQ(v2[0], 1.0);
		ASSERT_EQ(v2[1], 4.0);
		ASSERT_EQ(v2[2], 8.0);

		ASSERT_EQ(v2.x(), 1.0);
		ASSERT_EQ(v2.y(), 4.0);
		ASSERT_EQ(v2.z(), 8.0);

		ASSERT_EQ(v2.r(), 1.0);
		ASSERT_EQ(v2.g(), 4.0);
		ASSERT_EQ(v2.b(), 8.0);

		ASSERT_EQ(v2.length2(), 81.0);
		ASSERT_EQ(v2.length(), 9.0);
		ASSERT_EQ(v2.sum(), 13.0);

		Vector<double, 3> v3{ v2 };

		ASSERT_EQ(v2, v3);

		v3 = Vector<double, 3>{{ 4.0, 4.0, 4.0 }};

		ASSERT_EQ(v2 + v3, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));
		ASSERT_EQ(v2 + 4.0, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));
		ASSERT_EQ(v2 - v3, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));
		ASSERT_EQ(v2 - 4.0, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));
		ASSERT_EQ(v2 * v3, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));
		ASSERT_EQ(v2 * 4.0, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));
		ASSERT_EQ(v2 / v3, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));
		ASSERT_EQ(v2 / 4.0, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));

		ASSERT_EQ(v2.min(v3), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));
		ASSERT_EQ(v2.min(4.0), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));
		ASSERT_EQ(v2.max(v3), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));
		ASSERT_EQ(v2.max(4.0), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));

		ASSERT_EQ(v2.dot(v3), 52.0);

		v2 += v3;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));
		v2 -= v3;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));
		v2 *= v3;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));
		v2 /= v3;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));

		v2 += 4.0;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));
		v2 -= 4.0;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));
		v2 *= 4.0;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));
		v2 /= 4.0;
		ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));

		return TestPass;
	}
};

TEST_REGISTER(VectorTest)