Browse Source

added [] operator for vector; tests

develop
Robin Thoni 7 years ago
parent
commit
5925a79726
3 changed files with 20 additions and 2 deletions
  1. 3
    1
      UGameEngine/utils/vectorxd.h
  2. 6
    1
      UGameEngine/utils/vectorxd.hxx
  3. 11
    0
      tests/testvector3d.cpp

+ 3
- 1
UGameEngine/utils/vectorxd.h View File

@@ -13,7 +13,7 @@ public:
13 13
     VectorXD(const VectorXD<X>& other);
14 14
 
15 15
     VectorXD<X>& setScalar(unsigned i, double value);
16
-    double getScalar(unsigned i);
16
+    double getScalar(unsigned i) const;
17 17
 
18 18
     bool isNull() const;
19 19
 
@@ -66,6 +66,8 @@ public:
66 66
     bool operator==(const VectorXD<X>& other) const;
67 67
     bool operator!=(const VectorXD<X>& other) const;
68 68
 
69
+    double operator[](unsigned i) const;
70
+
69 71
     bool operator!() const;
70 72
     operator bool() const;
71 73
 

+ 6
- 1
UGameEngine/utils/vectorxd.hxx View File

@@ -29,7 +29,7 @@ tmpl VectorXD<X>& VectorXD<X>::setScalar(unsigned i, double value)
29 29
     return *this;
30 30
 }
31 31
 
32
-tmpl double VectorXD<X>::getScalar(unsigned i)
32
+tmpl double VectorXD<X>::getScalar(unsigned i) const
33 33
 {
34 34
     return _scalars[i];
35 35
 }
@@ -265,6 +265,11 @@ tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
265 265
     return !equal(other);
266 266
 }
267 267
 
268
+tmpl double VectorXD<X>::operator[](unsigned i) const
269
+{
270
+    return getScalar(i);
271
+}
272
+
268 273
 tmpl bool VectorXD<X>::operator!() const
269 274
 {
270 275
     return isNull();

+ 11
- 0
tests/testvector3d.cpp View File

@@ -51,6 +51,8 @@ private slots:
51 51
 
52 52
     void norm1();
53 53
     void norm2();
54
+
55
+    void getScalarOp();
54 56
 };
55 57
 
56 58
 void TestVector3D::gettersSetters1()
@@ -404,5 +406,14 @@ void TestVector3D::norm2()
404 406
     QCOMPARE(v.norm(), 50.0);
405 407
 }
406 408
 
409
+void TestVector3D::getScalarOp()
410
+{
411
+    double x = 1.42, y = 2.0, z = 3.0;
412
+    Vector3D v(x, y, z);
413
+    QCOMPARE(v[0], x);
414
+    QCOMPARE(v[1], y);
415
+    QCOMPARE(v[2], z);
416
+}
417
+
407 418
 QTEST_MAIN(TestVector3D)
408 419
 #include "testvector3d.moc"

Loading…
Cancel
Save