Browse Source

vector; tests

develop
Robin Thoni 7 years ago
parent
commit
be7fb238e6
3 changed files with 346 additions and 4 deletions
  1. 76
    2
      UGameEngine/vector3d.cpp
  2. 24
    2
      UGameEngine/vector3d.h
  3. 246
    0
      tests/testvector3d.cpp

+ 76
- 2
UGameEngine/vector3d.cpp View File

@@ -51,6 +51,16 @@ bool Vector3D::isNull() const
51 51
     return _x == 0 && _y == 0 && _z == 0;
52 52
 }
53 53
 
54
+bool Vector3D::equal(const Vector3D &other) const
55
+{
56
+    return _x == other._x && _y == other._y && _z == other._z;
57
+}
58
+
59
+Vector3D &Vector3D::add(double k)
60
+{
61
+    return add(Vector3D(k, k, k));
62
+}
63
+
54 64
 Vector3D &Vector3D::add(double x, double y, double z)
55 65
 {
56 66
     return add(Vector3D(x, y, z));
@@ -64,6 +74,11 @@ Vector3D& Vector3D::add(const Vector3D &other)
64 74
     return *this;
65 75
 }
66 76
 
77
+Vector3D &Vector3D::sub(double k)
78
+{
79
+    return sub(Vector3D(k, k, k));
80
+}
81
+
67 82
 Vector3D &Vector3D::sub(double x, double y, double z)
68 83
 {
69 84
     return sub(Vector3D(x, y, z));
@@ -108,8 +123,67 @@ double Vector3D::norm() const
108 123
     return sqrt((_x * _x) + (_y * _y) + (_z * _z));
109 124
 }
110 125
 
126
+Vector3D Vector3D::operator+()
127
+{
128
+    return *this;
129
+}
130
+
131
+Vector3D Vector3D::operator+(const double &k)
132
+{
133
+    return Vector3D(*this).add(k);
134
+}
135
+
136
+Vector3D Vector3D::operator+(const Vector3D &v2)
137
+{
138
+    return Vector3D(*this).add(v2);
139
+}
140
+
141
+Vector3D Vector3D::operator-()
142
+{
143
+    return Vector3D(-_x, -_y, -_z);
144
+}
145
+
146
+Vector3D Vector3D::operator-(const double &k)
147
+{
148
+    return Vector3D(*this).sub(k);
149
+}
150
+
151
+Vector3D Vector3D::operator-(const Vector3D &v2)
152
+{
153
+    return Vector3D(*this).sub(v2);
154
+}
155
+
156
+Vector3D Vector3D::operator*(const double &k)
157
+{
158
+    return Vector3D(*this).mult(k);
159
+}
160
+
161
+double Vector3D::operator*(const Vector3D &v2)
162
+{
163
+    return Vector3D(*this).dotProduct(v2);
164
+}
165
+
166
+Vector3D Vector3D::operator/(const double &k)
167
+{
168
+    return Vector3D(*this).div(k);
169
+}
170
+
171
+bool Vector3D::operator==(const Vector3D &other)
172
+{
173
+    return equal(other);
174
+}
175
+
176
+bool Vector3D::operator!=(const Vector3D &other)
177
+{
178
+    return !equal(other);
179
+}
180
+
181
+bool Vector3D::operator!()
182
+{
183
+    return isNull();
184
+}
111 185
 
112
-Vector3D &operator+(const Vector3D &v1, const Vector3D &v2)
186
+Vector3D::operator bool()
113 187
 {
114
-    return Vector3D(v1).add(v2);
188
+    return !isNull();
115 189
 }

+ 24
- 2
UGameEngine/vector3d.h View File

@@ -4,7 +4,7 @@
4 4
 class Vector3D
5 5
 {
6 6
 public:
7
-    Vector3D(double x = 0.0, double y = 0.0, double z = 0.0);
7
+    explicit Vector3D(double x = 0.0, double y = 0.0, double z = 0.0);
8 8
     Vector3D(const Vector3D& other);
9 9
 
10 10
     double getX() const;
@@ -18,9 +18,13 @@ public:
18 18
 
19 19
     bool isNull() const;
20 20
 
21
+    bool equal(const Vector3D& other) const;
22
+
23
+    Vector3D& add(double k);
21 24
     Vector3D& add(double x, double y, double z);
22 25
     Vector3D& add(const Vector3D& other);
23 26
 
27
+    Vector3D& sub(double k);
24 28
     Vector3D& sub(double x, double y, double z);
25 29
     Vector3D& sub(const Vector3D& other);
26 30
 
@@ -33,12 +37,30 @@ public:
33 37
 
34 38
     double norm() const;
35 39
 
40
+    Vector3D operator+();
41
+    Vector3D operator+(const double& k);
42
+    Vector3D operator+(const Vector3D& v2);
43
+
44
+    Vector3D operator-();
45
+    Vector3D operator-(const double& k);
46
+    Vector3D operator-(const Vector3D& v2);
47
+
48
+    Vector3D operator*(const double& k);
49
+    double operator*(const Vector3D& v2);
50
+
51
+    Vector3D operator/(const double& k);
52
+
53
+    bool operator==(const Vector3D& other);
54
+    bool operator!=(const Vector3D& other);
55
+
56
+    bool operator!();
57
+    operator bool();
58
+
36 59
 private:
37 60
     double _x;
38 61
     double _y;
39 62
     double _z;
40 63
 };
41 64
 
42
-Vector3D& operator+(const Vector3D& v1, const Vector3D& v2);
43 65
 
44 66
 #endif // VECTOR3D_H

+ 246
- 0
tests/testvector3d.cpp View File

@@ -9,7 +9,35 @@ private slots:
9 9
     void gettersSetters1();
10 10
     void gettersSetters2();
11 11
     void gettersSetters3();
12
+
13
+    void isNull1();
14
+    void isNull1Op();
15
+    void isNull2();
16
+    void isNull2Op();
17
+
18
+    void equal1();
19
+    void equal2();
20
+    void equal3();
21
+    void equal4();
22
+    void equal5();
23
+    void equal6();
24
+
25
+    void add1();
26
+    void add1Op();
27
+    void add2();
28
+    void add2Op();
29
+    void add3Op();
30
+
31
+    void sub1();
32
+    void sub1Op();
33
+    void sub2();
34
+    void sub2Op();
35
+    void sub3Op();
36
+
12 37
     void mult();
38
+    void multOp();
39
+
40
+
13 41
     void div();
14 42
 };
15 43
 
@@ -40,6 +68,214 @@ void TestVector3D::gettersSetters3()
40 68
     QCOMPARE(v.getZ(), z);
41 69
 }
42 70
 
71
+void TestVector3D::isNull1()
72
+{
73
+    double x = 1.42, y = 2.0, z = 3.0;
74
+    Vector3D v(x, y, z);
75
+    QCOMPARE(v.isNull(), false);
76
+}
77
+
78
+void TestVector3D::isNull1Op()
79
+{
80
+    double x = 1.42, y = 2.0, z = 3.0;
81
+    Vector3D v(x, y, z);
82
+    QCOMPARE((bool)v, true);
83
+}
84
+
85
+void TestVector3D::isNull2()
86
+{
87
+    Vector3D v;
88
+    QCOMPARE(v.isNull(), true);
89
+}
90
+
91
+void TestVector3D::isNull2Op()
92
+{
93
+    Vector3D v;
94
+    QCOMPARE(!v, true);
95
+}
96
+
97
+void TestVector3D::equal1()
98
+{
99
+    Vector3D v1(1.42, 2.0, 3.0);
100
+    Vector3D v2(1.42, 2.0, 3.0);
101
+    QCOMPARE(v1.equal(v2), true);
102
+}
103
+
104
+void TestVector3D::equal2()
105
+{
106
+    Vector3D v1(1.42, 2.0, 3.0);
107
+    Vector3D v2(1.40, 2.0, 3.0);
108
+    QCOMPARE(v1.equal(v2), false);
109
+}
110
+
111
+void TestVector3D::equal3()
112
+{
113
+    Vector3D v1(1.42, 2.0, 3.0);
114
+    Vector3D v2(1.42, 2.1, 3.0);
115
+    QCOMPARE(v1.equal(v2), false);
116
+}
117
+
118
+void TestVector3D::equal4()
119
+{
120
+    Vector3D v1(1.42, 2.0, 3.0);
121
+    Vector3D v2(1.42, 2.0, 3.1);
122
+    QCOMPARE(v1.equal(v2), false);
123
+}
124
+
125
+void TestVector3D::equal5()
126
+{
127
+    Vector3D v1(1.42, 2.0, 3.0);
128
+    Vector3D v2(1.42, 2.0, 3.1);
129
+    QCOMPARE(v1 == v2, false);
130
+}
131
+
132
+void TestVector3D::equal6()
133
+{
134
+    Vector3D v1(1.42, 2.0, 3.0);
135
+    Vector3D v2(1.42, 2.0, 3.0);
136
+    QCOMPARE(v1 == v2, true);
137
+}
138
+
139
+void TestVector3D::add1()
140
+{
141
+    double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
142
+    Vector3D v(x, y, z);
143
+    v.add(x2, y2, z2);
144
+    QCOMPARE(v.getX(), x + x2);
145
+    QCOMPARE(v.getY(), y + y2);
146
+    QCOMPARE(v.getZ(), z + z2);
147
+}
148
+
149
+void TestVector3D::add1Op()
150
+{
151
+    double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
152
+    Vector3D v1(x, y, z);
153
+    Vector3D v2(x2, y2, z2);
154
+    Vector3D v = v1 + v2;
155
+
156
+    QCOMPARE(v.getX(), x + x2);
157
+    QCOMPARE(v.getY(), y + y2);
158
+    QCOMPARE(v.getZ(), z + z2);
159
+
160
+    QCOMPARE(v1.getX(), x);
161
+    QCOMPARE(v1.getY(), y);
162
+    QCOMPARE(v1.getZ(), z);
163
+
164
+    QCOMPARE(v2.getX(), x2);
165
+    QCOMPARE(v2.getY(), y2);
166
+    QCOMPARE(v2.getZ(), z2);
167
+}
168
+
169
+void TestVector3D::add2()
170
+{
171
+    double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
172
+    Vector3D v(x, y, z);
173
+    v.add(m);
174
+    QCOMPARE(v.getX(), x + m);
175
+    QCOMPARE(v.getY(), y + m);
176
+    QCOMPARE(v.getZ(), z + m);
177
+}
178
+
179
+void TestVector3D::add2Op()
180
+{
181
+    double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
182
+    Vector3D v1(x, y, z);
183
+    Vector3D v = v1 + m;
184
+
185
+    QCOMPARE(v.getX(), x + m);
186
+    QCOMPARE(v.getY(), y + m);
187
+    QCOMPARE(v.getZ(), z + m);
188
+
189
+    QCOMPARE(v1.getX(), x);
190
+    QCOMPARE(v1.getY(), y);
191
+    QCOMPARE(v1.getZ(), z);
192
+}
193
+
194
+void TestVector3D::add3Op()
195
+{
196
+    double x = 1.42, y = 2.0, z = 3.0;
197
+    Vector3D v1(x, y, z);
198
+    Vector3D v = +v1;
199
+
200
+    QCOMPARE(v.getX(), x);
201
+    QCOMPARE(v.getY(), y);
202
+    QCOMPARE(v.getZ(), z);
203
+
204
+    QCOMPARE(v1.getX(), x);
205
+    QCOMPARE(v1.getY(), y);
206
+    QCOMPARE(v1.getZ(), z);
207
+}
208
+
209
+void TestVector3D::sub1()
210
+{
211
+    double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
212
+    Vector3D v(x, y, z);
213
+    v.sub(x2, y2, z2);
214
+    QCOMPARE(v.getX(), x - x2);
215
+    QCOMPARE(v.getY(), y - y2);
216
+    QCOMPARE(v.getZ(), z - z2);
217
+}
218
+
219
+void TestVector3D::sub1Op()
220
+{
221
+    double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
222
+    Vector3D v1(x, y, z);
223
+    Vector3D v2(x2, y2, z2);
224
+    Vector3D v = v1 - v2;
225
+
226
+    QCOMPARE(v.getX(), x - x2);
227
+    QCOMPARE(v.getY(), y - y2);
228
+    QCOMPARE(v.getZ(), z - z2);
229
+
230
+    QCOMPARE(v1.getX(), x);
231
+    QCOMPARE(v1.getY(), y);
232
+    QCOMPARE(v1.getZ(), z);
233
+
234
+    QCOMPARE(v2.getX(), x2);
235
+    QCOMPARE(v2.getY(), y2);
236
+    QCOMPARE(v2.getZ(), z2);
237
+}
238
+
239
+void TestVector3D::sub2()
240
+{
241
+    double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
242
+    Vector3D v(x, y, z);
243
+    v.sub(m);
244
+    QCOMPARE(v.getX(), x - m);
245
+    QCOMPARE(v.getY(), y - m);
246
+    QCOMPARE(v.getZ(), z - m);
247
+}
248
+
249
+void TestVector3D::sub2Op()
250
+{
251
+    double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
252
+    Vector3D v1(x, y, z);
253
+    Vector3D v = v1 - m;
254
+
255
+    QCOMPARE(v.getX(), x - m);
256
+    QCOMPARE(v.getY(), y - m);
257
+    QCOMPARE(v.getZ(), z - m);
258
+
259
+    QCOMPARE(v1.getX(), x);
260
+    QCOMPARE(v1.getY(), y);
261
+    QCOMPARE(v1.getZ(), z);
262
+}
263
+
264
+void TestVector3D::sub3Op()
265
+{
266
+    double x = 1.42, y = 2.0, z = 3.0;
267
+    Vector3D v1(x, y, z);
268
+    Vector3D v = -v1;
269
+
270
+    QCOMPARE(v.getX(), -x);
271
+    QCOMPARE(v.getY(), -y);
272
+    QCOMPARE(v.getZ(), -z);
273
+
274
+    QCOMPARE(v1.getX(), x);
275
+    QCOMPARE(v1.getY(), y);
276
+    QCOMPARE(v1.getZ(), z);
277
+}
278
+
43 279
 void TestVector3D::mult()
44 280
 {
45 281
     double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
@@ -50,6 +286,16 @@ void TestVector3D::mult()
50 286
     QCOMPARE(v.getZ(), z * m);
51 287
 }
52 288
 
289
+void TestVector3D::multOp()
290
+{
291
+    double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
292
+    Vector3D v1(x, y, z);
293
+    Vector3D v = v1 * m;
294
+    QCOMPARE(v.getX(), x * m);
295
+    QCOMPARE(v.getY(), y * m);
296
+    QCOMPARE(v.getZ(), z * m);
297
+}
298
+
53 299
 void TestVector3D::div()
54 300
 {
55 301
     double x = 1.42, y = 2.0, z = 3.0, m = 2.0;

Loading…
Cancel
Save