From eaaf3abe472d6b8bc65ab97e71603ad0b14f9035 Mon Sep 17 00:00:00 2001 From: "U-DAMIEN\\DamienN" Date: Sat, 6 Mar 2010 23:24:27 +0100 Subject: [PATCH] classe 'Vector' : fin de la phase de test. Correction de quelques bug mineurs et mise a jour du fichier de test. Declare la classe 'Vector' prete a l'utilisation. --- testVector.cc | 391 +++++++++++++++++++++++++++++++++++++++------------------- vector.cc | 8 +- vector.h | 6 +- 3 files changed, 271 insertions(+), 134 deletions(-) rewrite testVector.cc (91%) diff --git a/testVector.cc b/testVector.cc dissimilarity index 91% index b5eeed1..9620510 100644 --- a/testVector.cc +++ b/testVector.cc @@ -1,127 +1,264 @@ -#include "vector.h" -#include - -using namespace std; - -int main () -{ - Vector vec1; - Vector vec2; - Vector vec3; - Vector vec4; - - vec1.addComponent(0.0); - vec1.addComponent(0.0); - vec1.addComponent(0.0); - - vec2.addComponent(1.0); - vec2.addComponent(2.0); - vec2.addComponent(3.0); - vec2.addComponent(4.0); - - vec3.addComponent(2.7); - vec3.addComponent(5.6); - vec3.addComponent(45.2); - - vec4.addComponent(0.58); - vec4.addComponent(-23.5); - vec4.addComponent(12.7); - - cout << "Test 1 : showing vectors" << endl; - cout << "vector 1, must be (0,0,0)" << endl; - vec1.show(); - - cout << "vector 2, must be (1,2,3,4)" << endl; - vec2.show(); - - cout << "vector 3, must be (2.7,5.6,45.2)" << endl; - vec3.show(); - - cout << "vector 4, must be (0.58, -23.5, 12.7)" << endl; - vec4.show(); - - cout << "Test 2 : equality" << endl; - - Vector vec2b; - vec2b.addComponent(1.0); - vec2b.addComponent(2.0); - vec2b.addComponent(3.0); - vec2b.addComponent(4.0); - - cout << "Must return true : " << boolalpha << vec2.equals(vec2b) << endl; - cout << "Must return false : " << boolalpha << vec3.equals(vec4) << endl << endl; - - cout << "Test 3 : adding / substracting vectors" << endl; - - cout << "vector 1 + vector 3 = (2.7, 5.6, 45.2)" << endl; - vec1.add(vec3).show(); - - cout << "vector 3 - vector 2 => std::logic_error" << endl; - try { - vec3.substract(vec2).show(); - } - catch (std::logic_error e) { - cout << e.what() << endl; - } - - cout << "vector 4 + vector 3 = (3.28, -17.9, 57.9)" << endl; - vec4.add(vec3).show(); - - cout << "Test 4 : external product" << endl; - cout << "2 * vector 1 = (0,0,0)" << endl; - vec1.externalProduct(2).show(); - - cout << "1.5 * vector 2 = (1.5, 3.0, 4.5, 6.0)" << endl; - vec2.externalProduct(1.5).show(); - - cout << "Test 5 : dot product" << endl; - cout << "vector 1 dot vector 2 => std::logic_error" << endl; - try { - vec1.dotProduct(vec2); - } - catch (std::logic_error e) { - cout << e.what() << endl; - } - - cout << "vector 1 dot vector 3 = 0" << endl; - cout << vec1.dotProduct(vec3) << endl; - - cout << "vector 2 dot vector 2 = 30" << endl; - cout << vec2.dotProduct(vec2) << endl << endl; - - cout << "Test 6 : cross product" << endl; - cout << "vector 2 cross vector 2 => std::logic_error" << endl; - try { - vec2.crossProduct(vec2).show(); - } - catch (std::logic_error e) { - cout << e.what() << endl; - } - - cout << "vector 3 cross vector 4 = (1133.32, -8.074, -6.6698)" << endl; - vec3.crossProduct(vec4).show(); - - cout << "Test 7 : magnitudes" << endl; - cout << "mag(vec1) = 0" << endl; - cout << vec1.magnitude() << endl; - - cout << "mag(vec2) = 5.47723" << endl; - cout << vec2.magnitude() << endl; - cout << "mag2(vec2) = 30" << endl; - cout << vec2.magnitude2() << endl; - cout << endl; - - cout << "Test 8 : dimensions" << endl; - cout << "dim(vec2) = 4" << endl; - cout << vec2.dimension() << endl; - - cout << "dim(vec3) = 3" << endl; - cout << vec3.dimension() << endl << endl; - - cout << "End" << endl; - - - return 0; -} - - +#include "vector.h" +#include + +using namespace std; + +bool test (const Vector &testVal, const Vector &refVal) {return (testVal == refVal);} +bool test (const double testVal, const double refVal) {return (testVal == refVal);} + +ostream& operator << (ostream& os, const Vector &aVector) +{ + const unsigned int SIZE (aVector.dimension()); + + for (unsigned int i(0) ; i < SIZE ; ++i) { + os << aVector.at(i) << ' '; + } + return os; +} + +int main () +{ + Vector vec1 (vector (5,1)); + Vector vec2 (vector (5, -2.25)); + Vector vec3 (4); + Vector vec4 (vec3); + Vector e3 (8, Vector::ei, 3); + + + cout << "**** Beginnning of preliminary testing ****" << endl << endl; + + cout << "Testing constructors. Output should be (1 1 1 1 1), \n(-2.25 -2.25 -2.25 -2.25 -2.25), 2x(0 0 0 0) and e3 in R8" << endl; + vec1.show(); + vec2.show(); + vec3.show(); + vec4.show(); + e3.show(); + + cout << "Testing overload of operator << (implementation only for testing purpose)" << endl << "Output should be (0 0 0 0)" << endl; + cout << vec4 << endl; + + cout << "Testing operator = : Output should be (-2.25 -2.25 -2.25 -2.25-2.25)" << endl; + vec3 = vec2; + cout << vec3 << endl; + vec3 = vec4; + + cout << "Testing operator == : output should be FALSE TRUE" << endl; + cout << boolalpha << (vec1 == vec2) << '\t' << (vec3 == vec2) << endl << endl; + + cout << "**** End of preliminary testing ****" << endl + << "/!\\ If any of the above tests failed, discard rest of tests /!\\" << endl << endl; + + cout << "**** Beginning of methods testing ****" << endl << endl; + + cout << "at() const test : "; + cout << boolalpha << ( test(vec2.at(2), -2.25) and test(e3.at(3), 0) ) << endl; + + cout << "at() non const test : "; { + Vector e4 (4, Vector::ei, 1); + e4.at(0) = 1; + e4.at(3) = 3.1415; + cout << boolalpha << ( test(e4.at(0), 1) and test(e4.at(3), 3.1415) ) << endl; + } + + cout << "'addComponent()' test : "; { + vec3.addComponent(4); + Vector refVal (5); + refVal.at(4) = 4; + cout << boolalpha << test (vec3, refVal) << endl; + } + + + cout << "magnitude() test : "; + Vector vec5 (vector(25, 5)); + cout << boolalpha << test(vec5.magnitude(), 25) << endl; + cout << "magnitude2() test : "; + cout << boolalpha << test (vec1.magnitude2(), 5) << endl; + + cout << "show() test : Output should be (0 0 1 0 0 0 0 0)" << endl; + e3.show(); + + cout << "**** End of methods testing ****" << endl << endl; + + cout << "**** Beginning of operators testing ****" << endl << endl; + + cout << "Operator + test : "; { + vec3 = vec1 + vec2; + Vector refVal (vector (5,-1.25)); + cout << boolalpha << test (vec3, refVal) << endl; + } + + cout << "Operator += test : "; { + vec1 += vec3; + Vector refVal (vector (5,-0.25)); + cout << boolalpha << test (vec1, refVal) << endl; + } + + cout << "Operator - test : "; { + vec3 = vec1 - vec2; + Vector refVal (vector (5,2)); + cout << boolalpha << test (vec3, refVal) << endl; + } + + cout << "Operator -= test : "; { + vec2 -= vec3; + Vector refVal (vector (5,-4.25)); + cout << boolalpha << test (vec2, refVal) << endl; + } + + cout << "Operator * (scalar) test : "; { + vec3 = vec1 * -3; + Vector refVal (vector (5,0.75)); + cout << boolalpha << test (vec3, refVal) << endl; + } + + cout << "Operator *= (scalar) test : "; { + vec1 *= 4; + Vector refVal (vector (5,-1)); + cout << boolalpha << test (vec1, refVal) << endl; + } + + cout << "Operator * (dotProduct) test : "; { + cout << boolalpha << test ((vec2 * vec3), -15.9375) << endl; + } + + cout << "Operator / test : "; { + Vector a (vector (5, -6)); + vec3 = a / -3; + Vector refVal (vector (5,2)); + cout << boolalpha << test (vec3, refVal) << endl; + } + + cout << "Operator /= test : "; { + Vector a (vector (5, 1.75)); + a /= 1.25; + Vector refVal (vector (5,1.4)); + cout << boolalpha << test (a, refVal) << endl; + } + + cout << "Operator ^ test : "; { + Vector e2 (3, Vector::ei, 2); + Vector a; + a.addComponent(5); + a.addComponent(3); + a.addComponent(2.27); + Vector refVal; + refVal.addComponent(2.27); + refVal.addComponent(0); + refVal.addComponent(-5.0); + cout << boolalpha << test ((e2^a), refVal) << endl; + } + + cout << "Operator != test : "; + cout << boolalpha << ((vec1 != vec2) == true) << endl; + + cout << "Operator [] test : "; { + cout << boolalpha << ( test(vec1[5], -1) and test(vec3[2], 2) ) << endl; + } + + cout << endl; + cout << "**** End of testing operators ****" << endl << endl; + + cout << "**** Beginning of exception handling testing ****" << endl << endl; + + try { + cout << "Operator +" << endl; + vec1 = e3 + vec2; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator +=" << endl; + vec1 += e3; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator -" << endl; + vec1 = e3 - vec2; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator -=" << endl; + vec1 -= e3; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator *" << endl; + vec1 = e3 * vec2; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator /" << endl; + vec1 = e3 / 0; + } + catch (domain_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator /=" << endl; + vec1 /= 0; + } + catch (domain_error &e) { + cout << e.what() << endl; + } + + try { + cout << "Operator ^" << endl; + vec3 = vec1 ^ vec2; + } + catch (logic_error &e) { + cout << e.what() << endl; + } + + try { + cout << "at() const" << endl; + vec1.at(100); + } + catch (out_of_range &e) { + cout << e.what() << endl; + } + + try { + cout << "at() non-const" << endl; + vec1.at(6) = 2; + } + catch (out_of_range &e) { + cout << e.what() << endl; + } + + try { + cout << "[] const" << endl; + vec1[100]; + } + catch (out_of_range &e) { + cout << e.what() << endl; + } + + try { + cout << "[] non-const" << endl; + vec1[7] = 2; + } + catch (out_of_range &e) { + cout << e.what() << endl; + } + + cout << endl << endl << "**** End of testing ****" << endl << endl; + + return 0; +} + + diff --git a/vector.cc b/vector.cc index b41ffbd..7d3e0e8 100644 --- a/vector.cc +++ b/vector.cc @@ -135,7 +135,7 @@ Vector Vector::operator ^ (const Vector &otherVector) const if ( m_components.size() != 3 ) { throw logic_error ("Cross product only defined in dimension 3"); } - else if ( otherVector.components().size() != 3 ) { + else if ( otherVector.dimension() != 3 ) { throw logic_error ("Cross product only defined in dimension 3"); } @@ -149,12 +149,12 @@ Vector Vector::operator ^ (const Vector &otherVector) const } //Equality operators ==, != -Vector Vector::operator == (const Vector &otherVector) const +bool Vector::operator == (const Vector &otherVector) const { return (m_components == otherVector.m_components); } -Vector Vector::operator != (const Vector &otherVector) const +bool Vector::operator != (const Vector &otherVector) const { return not(m_components == otherVector.m_components); } @@ -209,7 +209,7 @@ double Vector::dotProduct (const Vector &otherVector) const const unsigned int SIZE(m_components.size()); - if ( SIZE == otherVector.components().size() ) { + if ( SIZE == otherVector.components().size()) { for (unsigned int i(0) ; i < SIZE ; ++i) { result += ( m_components.at(i) * otherVector.components().at(i) ); } diff --git a/vector.h b/vector.h index 096badf..352a877 100644 --- a/vector.h +++ b/vector.h @@ -48,8 +48,8 @@ class Vector Vector operator ^ (const Vector &otherVector) const; - Vector operator == (const Vector &otherVector) const; - Vector operator != (const Vector &otherVector) const; + bool operator == (const Vector &otherVector) const; + bool operator != (const Vector &otherVector) const; /*Can throw out_of_range exceptions /!\ index is understood as a mathematical index, not as a general C++ index /!\ @@ -65,7 +65,7 @@ class Vector double dotProduct (const Vector &otherVector) const; //Calculate Euclidea standard product //Other methods - void addComponent (double value); + void addComponent (double value = 0); double magnitude () const; double magnitude2 () const; void show () const; -- 2.11.4.GIT