diff options
author | Jakob Odersky <jodersky@gmail.com> | 2011-03-08 20:36:26 +0000 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2011-03-08 20:36:26 +0000 |
commit | 36ccf9c5518272554557ab4b4e865a9f66cf74cf (patch) | |
tree | 858ab48d4fd3019de351a9731307df530984715c /src | |
parent | 9d1ee82cc15bff4912414af0efab56ccfa889f5e (diff) | |
download | vhc-36ccf9c5518272554557ab4b4e865a9f66cf74cf.tar.gz vhc-36ccf9c5518272554557ab4b4e865a9f66cf74cf.tar.bz2 vhc-36ccf9c5518272554557ab4b4e865a9f66cf74cf.zip |
Corrige une erreur dans la methode `getUnit()' de `Vector3D'.
Diffstat (limited to 'src')
-rw-r--r-- | src/vhc/Vector3D.h | 4 | ||||
-rw-r--r-- | src/vhctest/Vector3DTest.cc | 42 |
2 files changed, 15 insertions, 31 deletions
diff --git a/src/vhc/Vector3D.h b/src/vhc/Vector3D.h index e5e2ffe..f15b4c1 100644 --- a/src/vhc/Vector3D.h +++ b/src/vhc/Vector3D.h @@ -87,10 +87,12 @@ public: /** Vecteur unitaire de ce vecteur. */ Vector3D operator~() const { - if (getLength() != 0.0) return (*this) / getLength(); + if (getNorm() != 0.0) return (*this) / getNorm(); else throw std::domain_error("Unit vector."); }; + Vector3D getUnit() const {return ~(*this);} + /** Retourne la norme du vecteur. */ double getNorm() const {return sqrt(dot(*this));}; diff --git a/src/vhctest/Vector3DTest.cc b/src/vhctest/Vector3DTest.cc index 2afca4b..eda1e3f 100644 --- a/src/vhctest/Vector3DTest.cc +++ b/src/vhctest/Vector3DTest.cc @@ -8,6 +8,8 @@ #include <iostream> #include <assert.h> +#include <iomanip> +#include <limits> //TODO change relative include #include "../vhc/Vector3D.h" @@ -25,20 +27,9 @@ using namespace vhc; bool printTest(); int main() { - - Vector3D u = Vector3D(1,2,3); - Vector3D v = u; - cout << "1) u:" << u << endl; - cout << "1) v:" << v << endl; - u = Vector3D(1,2,5); - cout << "2) u:" << u << endl; - cout << "2) v:" << v << endl; - - cout << "Vector3D: running tests..." << endl; - cout << flush; - //equality test assert(Vector3D(1, 2, 3) == Vector3D(1, 2, 3)); + assert(Vector3D(1, 2.4, 3) != Vector3D(1, 2, 3)); //addition test assert(Vector3D(1, -0.9, 57683) + Vector3D(-1, 0.9, -57683) == Vector3D::Null); @@ -46,25 +37,16 @@ int main() { //addition, mutliplication test assert(Vector3D(4, 0, 16) / 4 == -Vector3D(0.25, 0, 1) * -4); - //print test - //assert(printTest()); - //length test - assert(Vector3D(0,3,4).getLength() == 5); - assert(Vector3D(0.0, 3.0, 4.0).getUnit().getLength() == 1); - - //unit test - Vector3D a = Vector3D(0, 3, 4).getUnit(); - Vector3D b = Vector3D(0, 0.6, 0.8); - - cout << "unit: " << a << " std: " << b << endl; - cout << "x: " << (a.getX() == b.getX()) << endl; - cout << "y: " << (a.getY() == b.getY()) << endl; - cout << "z: " << (a.getZ() == b.getZ()) << endl; - - - assert((Vector3D(0, 3.0, 4.0).getUnit()) == Vector3D(0, 0.6, 0.8)); - + assert(Vector3D(0,3,4).getNorm() == 5); + + bool caught = false; + try { + ~Vector3D::Null; + } catch (domain_error& ex) { + caught = true; + }; + assert(caught == true); cout << "Vector3D: tests completed successfully" << endl; |