[idlescreen] r209 committed - Added unit tests for misc_funcs & Vector2D, but the still need a lot o...

1 view
Skip to first unread message

codesite...@google.com

unread,
Feb 7, 2010, 9:41:36 PM2/7/10
to dev-idl...@googlegroups.com
Revision: 209
Author: jeff.backus
Date: Sun Feb 7 18:40:30 2010
Log: Added unit tests for misc_funcs & Vector2D, but the still need a lot
of work.
Added a function to improve comparing floats/doubles with a variable
precision.
Rewrote the unit test main function to iterate through a QList of test
classes.

http://code.google.com/p/idlescreen/source/detail?r=209

Added:
/branches/backus_dev/src/common/utility/unit_tests/Test_MiscFuncs.cpp
/branches/backus_dev/src/common/utility/unit_tests/Test_MiscFuncs.h
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h
Modified:
/branches/backus_dev/src/common/unit_tests/unit_tests.pro
/branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp
/branches/backus_dev/src/common/utility/Point2D.cpp
/branches/backus_dev/src/common/utility/Point2D.h
/branches/backus_dev/src/common/utility/Vector2D.cpp
/branches/backus_dev/src/common/utility/Vector2D.h
/branches/backus_dev/src/common/utility/misc_funcs.cpp
/branches/backus_dev/src/common/utility/misc_funcs.h
/branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.cpp
/branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.h

=======================================
--- /dev/null
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_MiscFuncs.cpp
Sun Feb 7 18:40:30 2010
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+//#include <iostream>
+#include <sstream>
+#include <string>
+using namespace std;
+
+#include "Test_MiscFuncs.h"
+
+void Test_MiscFuncs::Test_Jrand() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_BoolToString() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_StringToBool() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_DotProduct() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_RandStr() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_BoolToCheckState() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_RoundFToI() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_RoundDToI() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_RelativeCompareF() {
+ QFAIL("Not implemented yet!");
+}
+void Test_MiscFuncs::Test_RelativeCompareD() {
+ QFAIL("Not implemented yet!");
+}
+
+/*
+void Test_MiscFuncs::Test_GetSetX() {
+ MiscFuncs testPoint;
+
+ // set to a positive number
+ testPoint.setX(5.1);
+ QVERIFY(testPoint.getX() == 5.1);
+ QVERIFY(testPoint.getY() == 0.0);
+
+ // set to default
+ testPoint.setX();
+ QVERIFY(testPoint.getX() == 0.0);
+ QVERIFY(testPoint.getY() == 0.0);
+
+ // set to a negative number
+ testPoint.setX(-1000.4);
+ QVERIFY(testPoint.getX() == -1000.4);
+ QVERIFY(testPoint.getY() == 0.0);
+}
+*/
=======================================
--- /dev/null
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_MiscFuncs.h Sun
Feb 7 18:40:30 2010
@@ -0,0 +1,53 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+
+/**
+ * This set of classes is for unit testing the functions in misc_funcs.
+ */
+
+#ifndef __TEST_MISC_FUNCS_H__
+#define __TEST_MISC_FUNCS_H__
+
+#include <QtTest/QtTest>
+#include "../misc_funcs.h"
+
+class Test_MiscFuncs: public QObject {
+ Q_OBJECT
+
+ private slots:
+ void Test_Jrand();
+ void Test_BoolToString();
+ void Test_StringToBool();
+ void Test_DotProduct();
+ void Test_RandStr();
+ void Test_BoolToCheckState();
+ void Test_RoundFToI();
+ void Test_RoundDToI();
+ void Test_RelativeCompareF();
+ void Test_RelativeCompareD();
+
+ private:
+};
+
+#endif
=======================================
--- /dev/null
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
Sun Feb 7 18:40:30 2010
@@ -0,0 +1,646 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+//#include <iostream>
+#include <sstream>
+#include <string>
+using namespace std;
+
+#include "Test_Vector2D.h"
+
+void Test_Vector2D::Test_GetSetX() {
+ Vector2D testVector;
+
+ // set to a positive number
+ testVector.setX(5.1);
+ QVERIFY(testVector.getX() == 5.1);
+ QVERIFY(testVector.getY() == 0.0);
+
+ // set to default
+ testVector.setX();
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == 0.0);
+
+ // set to a negative number
+ testVector.setX(-1000.4);
+ QVERIFY(testVector.getX() == -1000.4);
+ QVERIFY(testVector.getY() == 0.0);
+}
+
+void Test_Vector2D::Test_GetSetY() {
+ Vector2D testVector;
+
+ // set to a positive number
+ testVector.setY(7.2);
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == 7.2);
+
+ // set to default
+ testVector.setY();
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == 0.0);
+
+ // set to a negative number
+ testVector.setY(-1413.9);
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == -1413.9);
+}
+
+void Test_Vector2D::Test_SetValue() {
+ Vector2D testVector;
+ double xVal;
+ double yVal;
+
+ // set to positive numbers
+ xVal = 12.19;
+ yVal = 0.194;
+ testVector.setValue(xVal,yVal);
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // set to default
+ testVector.setValue();
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == 0.0);
+
+ // set to a negative number
+ xVal = -0.294;
+ yVal = -194.109;
+ testVector.setValue(xVal,yVal);
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+}
+
+void Test_Vector2D::Test_Constructor() {
+ // set to positive numbers
+ Vector2D testVectorA(5.4,7.2);
+ QVERIFY(testVectorA.getX() == 5.4);
+ QVERIFY(testVectorA.getY() == 7.2);
+
+ // set to default
+ Vector2D testVectorB;
+ QVERIFY(testVectorB.getX() == 0.0);
+ QVERIFY(testVectorB.getY() == 0.0);
+
+ // set to a negative number
+ Vector2D testVectorC(-5923.1,-21734.1);
+ QVERIFY(testVectorC.getX() == -5923.1);
+ QVERIFY(testVectorC.getY() == -21734.1);
+}
+void Test_Vector2D::Test_Magnitude() {
+ double xVal;
+ double yVal;
+
+ // both positive numbers
+ xVal = 1.92;
+ yVal = 0.32;
+ Vector2D testVectorA(xVal, yVal);
+ // we are assuming that the math function sqrt is ok...
+ QVERIFY(testVectorA.magnitude() == sqrt(xVal*xVal+yVal*yVal));
+
+ // x pos, y neg numbers
+ xVal = 274.13;
+ yVal = -192.68;
+ Vector2D testVectorB(xVal, yVal);
+ // we are assuming that the math function sqrt is ok...
+ QVERIFY(testVectorB.magnitude() == sqrt(xVal*xVal+yVal*yVal));
+
+ // x neg, y pos numbers
+ xVal = -0.719;
+ yVal = 1.768;
+ Vector2D testVectorC(xVal, yVal);
+ // we are assuming that the math function sqrt is ok...
+ QVERIFY(testVectorC.magnitude() == sqrt(xVal*xVal+yVal*yVal));
+
+ // both negative numbers
+ xVal = -173.43;
+ yVal = -297.67;
+ Vector2D testVectorD(xVal, yVal);
+ // we are assuming that the math function sqrt is ok...
+ QVERIFY(testVectorD.magnitude() == sqrt(xVal*xVal+yVal*yVal));
+
+ // set to default
+ Vector2D testVectorE;
+ QVERIFY(testVectorE.magnitude() == sqrt(0.0));
+}
+void Test_Vector2D::Test_Normalize() {
+ double xVal;
+ double yVal;
+
+ // both positive numbers
+ xVal = 1.92;
+ yVal = 0.32;
+ Vector2D testVectorA(xVal, yVal);
+ testVectorA.normalize();
+ QVERIFY(testVectorA.magnitude() == 1.0);
+
+ // x pos, y neg numbers
+ xVal = 274.13;
+ yVal = -192.68;
+ Vector2D testVectorB(xVal, yVal);
+ testVectorB.normalize();
+ QVERIFY(testVectorB.magnitude() == 1.0);
+
+ // x neg, y pos numbers
+ xVal = -0.719;
+ yVal = 1.768;
+ Vector2D testVectorC(xVal, yVal);
+ testVectorC.normalize();
+ QVERIFY(testVectorC.magnitude() == 1.0);
+
+ // both negative numbers
+ xVal = -173.43;
+ yVal = -297.67;
+ Vector2D testVectorD(xVal, yVal);
+ testVectorD.normalize();
+ QVERIFY(testVectorD.magnitude() == 1.0);
+
+ // set to default
+ Vector2D testVectorE;
+ testVectorE.normalize();
+ QVERIFY(testVectorE.magnitude() == 0.0);
+}
+void Test_Vector2D::Test_GetNormal() {
+ double xVal;
+ double yVal;
+ double mag;
+ Vector2D testVector;
+
+ // both positive numbers
+ xVal = 1.92;
+ yVal = 0.32;
+ mag = sqrt(xVal*xVal+yVal*yVal);
+ Vector2D testVectorA(xVal, yVal);
+ testVector = testVectorA.getNormal();
+ QVERIFY(testVector.getX() == yVal * -1.0/mag);
+ QVERIFY(testVector.getY() == xVal/mag);
+
+ // x pos, y neg numbers
+ xVal = 274.13;
+ yVal = -192.68;
+ mag = sqrt(xVal*xVal+yVal*yVal);
+ Vector2D testVectorB(xVal, yVal);
+ testVector = testVectorB.getNormal();
+ QVERIFY(testVector.getX() == yVal * -1.0/mag);
+ QVERIFY(testVector.getY() == xVal/mag);
+
+ // x neg, y pos numbers
+ xVal = -0.719;
+ yVal = 1.768;
+ mag = sqrt(xVal*xVal+yVal*yVal);
+ Vector2D testVectorC(xVal, yVal);
+ testVector = testVectorC.getNormal();
+ QVERIFY(testVector.getX() == yVal * -1.0/mag);
+ QVERIFY(testVector.getY() == xVal/mag);
+
+ // both negative numbers
+ xVal = -173.43;
+ yVal = -297.67;
+ mag = sqrt(xVal*xVal+yVal*yVal);
+ Vector2D testVectorD(xVal, yVal);
+ testVector = testVectorD.getNormal();
+ QVERIFY(testVector.getX() == yVal * -1.0/mag);
+ QVERIFY(testVector.getY() == xVal/mag);
+
+ // set to default
+ Vector2D testVectorE;
+ testVector = testVectorE.getNormal();
+ QVERIFY(testVector.getX() == 0.0);
+ QVERIFY(testVector.getY() == 0.0);
+}
+void Test_Vector2D::Test_Dot() {
+ double xValA, xValB;
+ double yValA, yValB;
+ double dot;
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+
+ // all positive numbers
+ xValA = 1.92;
+ yValA = 0.32;
+ xValB = 272.1;
+ yValB = 173.9;
+ dot = xValA*xValB+yValA*yValB;
+ testVectorA.setValue(xValA, yValA);
+ testVectorB.setValue(xValB, yValB);
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+
+ // x pos, one y neg numbers
+ xValA = 731.4;
+ yValA = 173.61;
+ xValB = 14.93;
+ yValB = -56.173;
+ dot = xValA*xValB+yValA*yValB;
+ testVectorA.setValue(xValA, yValA);
+ testVectorB.setValue(xValB, yValB);
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+
+ // one x neg, y pos numbers
+ xValA = -0.7193;
+ yValA = 0.361;
+ xValB = 1.934;
+ yValB = 1.431;
+ dot = xValA*xValB+yValA*yValB;
+ testVectorA.setValue(xValA, yValA);
+ testVectorB.setValue(xValB, yValB);
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+
+ // all negative numbers
+ xValA = -17.31;
+ yValA = -16.21;
+ xValB = -0.932;
+ yValB = -0.871;
+ dot = xValA*xValB+yValA*yValB;
+ testVectorA.setValue(xValA, yValA);
+ testVectorB.setValue(xValB, yValB);
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+
+ // one set to default
+ xValA = 1.92;
+ yValA = 0.32;
+ xValB = 0.0;
+ yValB = 0.0;
+ dot = xValA*xValB+yValA*yValB;
+ testVectorA.setValue(xValA, yValA);
+ testVectorB.setValue();
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+
+ // both set to default
+ dot = 0.0;
+ testVectorA.setValue();
+ testVectorB.setValue();
+ QVERIFY(testVectorA.dot(testVectorB) == dot);
+}
+
+void Test_Vector2D::Test_IsParallelTo() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+
+ // both positive slopes
+ testVectorA.setValue(3.3,5.7);
+ testVectorB.setValue(3.3*9.7,5.7*9.7);
+ QVERIFY(testVectorA.isParallelTo(testVectorB));
+ QVERIFY(testVectorB.isParallelTo(testVectorA));
+
+ // one positive, one negative
+ testVectorA.setValue(-4.7,6.9);
+ testVectorB.setValue(4.7*191.6,6.9*191.6);
+ QVERIFY(testVectorA.isParallelTo(testVectorB));
+ QVERIFY(testVectorB.isParallelTo(testVectorA));
+
+ // both negative
+ testVectorA.setValue(-12.1,17.73);
+ testVectorB.setValue(-12.1*4.6,17.73*4.6);
+ QVERIFY(testVectorA.isParallelTo(testVectorB));
+ QVERIFY(testVectorB.isParallelTo(testVectorA));
+
+ // not parallel
+ testVectorA.setValue(6.7,9.1);
+ testVectorB.setValue(6.7*13.4,9.1*8.3);
+ QVERIFY(!testVectorA.isParallelTo(testVectorB));
+ QVERIFY(!testVectorB.isParallelTo(testVectorA));
+
+ // one set to default
+ testVectorA.setValue(17.1,4.3);
+ testVectorB.setValue();
+ QVERIFY(!testVectorA.isParallelTo(testVectorB));
+ QVERIFY(!testVectorB.isParallelTo(testVectorA));
+
+ // both set to default
+ testVectorA.setValue();
+ testVectorB.setValue();
+ QVERIFY(!testVectorA.isParallelTo(testVectorB));
+ QVERIFY(!testVectorB.isParallelTo(testVectorA));
+}
+
+void Test_Vector2D::Test_IsNormalTo() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+
+ // direct normal
+ testVectorA.setValue(3.3,5.7);
+ testVectorB.setValue(5.7,-3.3);
+ QVERIFY(testVectorA.isNormalTo(testVectorB));
+ QVERIFY(testVectorB.isNormalTo(testVectorA));
+
+ // with one scaled
+ testVectorA.setValue(3.3,5.7);
+ testVectorB.setValue(5.7*9.7,-3.3*9.7);
+ QVERIFY(testVectorA.isNormalTo(testVectorB));
+ QVERIFY(testVectorB.isNormalTo(testVectorA));
+
+ // direct normal, the other direction
+ testVectorA.setValue(4.7,6.9);
+ testVectorB.setValue(-6.9,4.7);
+ QVERIFY(testVectorA.isNormalTo(testVectorB));
+ QVERIFY(testVectorB.isNormalTo(testVectorA));
+
+ // normal with multiplier, the other direction
+ testVectorA.setValue(4.7,6.9);
+ testVectorB.setValue(-6.9*119.31,4.7*119.31);
+ QVERIFY(testVectorA.isNormalTo(testVectorB));
+ QVERIFY(testVectorB.isNormalTo(testVectorA));
+
+ // try a set of parallel vectors...
+ testVectorA.setValue(12.1,17.73);
+ testVectorB.setValue(12.1*4.6,17.73*4.6);
+ QVERIFY(!testVectorA.isNormalTo(testVectorB));
+ QVERIFY(!testVectorB.isNormalTo(testVectorA));
+
+ // not normal
+ testVectorA.setValue(6.7,9.1);
+ testVectorB.setValue(-9.1*13.4,6.7*8.3);
+ QVERIFY(!testVectorA.isNormalTo(testVectorB));
+ QVERIFY(!testVectorB.isNormalTo(testVectorA));
+
+ // one set to default
+ testVectorA.setValue(17.1,4.3);
+ testVectorB.setValue();
+ QVERIFY(!testVectorA.isNormalTo(testVectorB));
+ QVERIFY(!testVectorB.isNormalTo(testVectorA));
+
+ // both set to default
+ testVectorA.setValue();
+ testVectorB.setValue();
+ QVERIFY(!testVectorA.isNormalTo(testVectorB));
+ QVERIFY(!testVectorB.isNormalTo(testVectorA));
+}
+
+void Test_Vector2D::Test_OperatorSet() {
+ Vector2D testVector;
+
+ // set to positive numbers
+ Vector2D testVectorA(5.4,7.2);
+ testVector = testVectorA;
+ QVERIFY(testVector.getX() == testVectorA.getX());
+ QVERIFY(testVector.getY() == testVectorA.getY());
+
+ // set to default
+ Vector2D testVectorB;
+ testVector = testVectorB;
+ QVERIFY(testVector.getX() == testVectorB.getX());
+ QVERIFY(testVector.getY() == testVectorB.getY());
+
+ // set to a negative number
+ Vector2D testVectorC(-5923.1,-21734.1);
+ testVector = testVectorC;
+ QVERIFY(testVector.getX() == testVectorC.getX());
+ QVERIFY(testVector.getY() == testVectorC.getY());
+}
+
+void Test_Vector2D::Test_OperatorEqual() {
+ Vector2D testVectorA(5.4,7.2);
+ Vector2D testVectorB;
+ Vector2D testVectorC(-5923.1,-21734.1);
+
+ Vector2D testVector = testVectorA;
+ QVERIFY(testVector == testVectorA);
+ QVERIFY(!(testVector == testVectorB));
+ QVERIFY(!(testVector == testVectorC));
+
+ testVector = testVectorB;
+ QVERIFY(!(testVector == testVectorA));
+ QVERIFY(testVector == testVectorB);
+ QVERIFY(!(testVector == testVectorC));
+
+ testVector = testVectorC;
+ QVERIFY(!(testVector == testVectorA));
+ QVERIFY(!(testVector == testVectorB));
+ QVERIFY(testVector == testVectorC);
+}
+
+void Test_Vector2D::Test_OperatorNotEqual() {
+ Vector2D testVectorA(5.4,7.2);
+ Vector2D testVectorB;
+ Vector2D testVectorC(-5923.1,-21734.1);
+
+ Vector2D testVector = testVectorA;
+ QVERIFY(!(testVector != testVectorA));
+ QVERIFY(testVector != testVectorB);
+ QVERIFY(testVector != testVectorC);
+
+ testVector = testVectorB;
+ QVERIFY(testVector != testVectorA);
+ QVERIFY(!(testVector != testVectorB));
+ QVERIFY(testVector != testVectorC);
+
+ testVector = testVectorC;
+ QVERIFY(testVector != testVectorA);
+ QVERIFY(testVector != testVectorB);
+ QVERIFY(!(testVector != testVectorC));
+}
+
+void Test_Vector2D::Test_OperatorOstream() {
+ double xVal;
+ double yVal;
+
+ // set to positive numbers
+ xVal = 56.4;
+ yVal = 17.2;
+
+ Vector2D testVector(xVal, yVal);
+ ostringstream testOSSA(ostringstream::out);
+ testOSSA.precision(15);
+ testOSSA << testVector;
+ string testStrA = testOSSA.str();
+
+ ostringstream controlOSSA(ostringstream::out);
+ controlOSSA.precision(15);
+ controlOSSA << "("<<xVal<<","<<yVal<<")";
+ string controlStrA = controlOSSA.str();
+
+ QVERIFY(testStrA == controlStrA);
+
+ // set to negative numbers
+ xVal = -324151.1;
+ yVal = -0.1932414;
+
+ testVector.setValue(xVal, yVal);
+ ostringstream testOSSB(ostringstream::out);
+ testOSSB.precision(15);
+ testOSSB << testVector;
+ string testStrB = testOSSB.str();
+
+ ostringstream controlOSSB(ostringstream::out);
+ controlOSSB.precision(15);
+ controlOSSB << "("<<xVal<<","<<yVal<<")";
+ string controlStrB = controlOSSB.str();
+
+ QVERIFY(testStrB == controlStrB);
+}
+
+#ifdef __NEEDS_DEBUGGING__
+
+void Test_Vector2D::Test_OperatorMultiply() {
+ double xVal = 5.4;
+ double yVal = 7.2;
+ double mul;
+ Vector2D testVector(xVal, yVal);
+
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Positive number
+ mul = 4.91;
+ testVector = testVector * mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Negative number
+ mul = -0.15;
+ testVector = testVector * mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Zero
+ mul = 0.0;
+ testVector = testVector * mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+}
+
+void Test_Vector2D::Test_OperatorDivide() {
+ double xVal = 32.149;
+ double yVal = 91.43;
+ double div;
+ Vector2D testVector(xVal, yVal);
+
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Positive number
+ div = 0.7291;
+ testVector = testVector / div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Negative number
+ div = -15.941;
+ testVector = testVector / div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // One
+ div = 1.0;
+ testVector = testVector / div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Zero
+ div = 0.0;
+ testVector = testVector / div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+}
+
+void Test_Vector2D::Test_OperatorMultiplyEqual() {
+ double xVal = 1.19;
+ double yVal = 910.14;
+ double mul;
+ Vector2D testVector(xVal, yVal);
+
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Positive number
+ mul = 0.1234613;
+ testVector *= mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Negative number
+ mul = -7234.19;
+ testVector *= mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Zero
+ mul = 0.0;
+ testVector *= mul;
+ xVal *= mul;
+ yVal *= mul;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+}
+
+void Test_Vector2D::Test_OperatorDivideEqual() {
+ double xVal = 12.1;
+ double yVal = 0.7143;
+ double div;
+ Vector2D testVector(xVal, yVal);
+
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Positive number
+ div = 71.45;
+ testVector /= div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Negative number
+ div = -0.941613;
+ testVector /= div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // One
+ div = 1.0;
+ testVector /= div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+
+ // Zero
+ div = 0.0;
+ testVector /= div;
+ xVal /= div;
+ yVal /= div;
+ QVERIFY(testVector.getX() == xVal);
+ QVERIFY(testVector.getY() == yVal);
+}
+#endif
+
=======================================
--- /dev/null
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h Sun
Feb 7 18:40:30 2010
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+
+/**
+ * This set of classes is for unit testing Point2D.
+ */
+
+#ifndef __TEST_VECTOR2D_H__
+#define __TEST_VECTOR2D_H__
+
+#include <QtTest/QtTest>
+#include "../Vector2D.h"
+
+class Test_Vector2D: public QObject {
+ Q_OBJECT
+
+ private slots:
+ void Test_GetSetX();
+ void Test_GetSetY();
+ void Test_SetValue();
+ void Test_Constructor();
+ void Test_Magnitude();
+ void Test_Normalize();
+ void Test_GetNormal();
+ void Test_Dot();
+ void Test_IsParallelTo();
+ void Test_IsNormalTo();
+
+ void Test_OperatorSet();
+ void Test_OperatorEqual();
+ void Test_OperatorNotEqual();
+ /*
+ void Test_OperatorMultiply();
+ void Test_OperatorDivide();
+ void Test_OperatorMultiplyEqual();
+ void Test_OperatorDivideEqual();
+ */
+ void Test_OperatorOstream();
+
+ private:
+};
+
+#endif
=======================================
--- /branches/backus_dev/src/common/unit_tests/unit_tests.pro Sun Feb 7
14:05:58 2010
+++ /branches/backus_dev/src/common/unit_tests/unit_tests.pro Sun Feb 7
18:40:30 2010
@@ -20,15 +20,19 @@

# Input
SOURCES = unit_tests_main.cpp
+SOURCES += $$COMMON_PATH/utility/misc_funcs.cpp
+SOURCES += $$COMMON_PATH/utility/unit_tests/Test_MiscFuncs.cpp
SOURCES += $$COMMON_PATH/utility/Point2D.cpp
SOURCES += $$COMMON_PATH/utility/unit_tests/Test_Point2D.cpp
-#SOURCES += $$COMMON_PATH/utility/Vector2D.cpp
-#SOURCES += $$COMMON_PATH/utility/unit_tests/Test_Vector2D.cpp
-
-HEADERS = $$COMMON_PATH/utility/Point2D.h
+SOURCES += $$COMMON_PATH/utility/Vector2D.cpp
+SOURCES += $$COMMON_PATH/utility/unit_tests/Test_Vector2D.cpp
+
+HEADERS = $$COMMON_PATH/utility/misc_funcs.h
+HEADERS += $$COMMON_PATH/utility/unit_tests/Test_MiscFuncs.h
+HEADERS += $$COMMON_PATH/utility/Point2D.h
HEADERS += $$COMMON_PATH/utility/unit_tests/Test_Point2D.h
-#HEADERS += $$COMMON_PATH/utility/Vector2D.h
-#HEADERS += $$COMMON_PATH/utility/unit_tests/Test_Vector2D.h
+HEADERS += $$COMMON_PATH/utility/Vector2D.h
+HEADERS += $$COMMON_PATH/utility/unit_tests/Test_Vector2D.h

TARGET = common_unit_tests
#RC_FILE = $$RESOURCE_PATH/win_resource.rc
=======================================
--- /branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp Sun Feb
7 14:05:58 2010
+++ /branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp Sun Feb
7 18:40:30 2010
@@ -1,4 +1,35 @@
#include <QtTest/QtTest>
+#include <QList>
+#include "utility/unit_tests/Test_MiscFuncs.h"
#include "utility/unit_tests/Test_Point2D.h"
-
-QTEST_MAIN(Test_Point2D)
+#include "utility/unit_tests/Test_Vector2D.h"
+
+int main(int argc, char** argv) {
+ QObject* tmpObj;
+ QList<QObject*> testObjs;
+
+ // Add test objects to the list.
+ tmpObj = new Test_MiscFuncs();
+ if(tmpObj != NULL)
+ testObjs.append(tmpObj);
+
+ tmpObj = new Test_Point2D();
+ if(tmpObj != NULL)
+ testObjs.append(tmpObj);
+
+ tmpObj = new Test_Vector2D();
+ if(tmpObj != NULL)
+ testObjs.append(tmpObj);
+
+ // Iterate through tests, incrementing
+ // retVal for every failure
+ int retVal = 0;
+ for(int i=0; i<testObjs.size(); i++) {
+ tmpObj = testObjs.at(i);
+ if(tmpObj != NULL) {
+ retVal += QTest::qExec(tmpObj, argc, argv);
+ }
+ }
+
+ return retVal;
+}
=======================================
--- /branches/backus_dev/src/common/utility/Point2D.cpp Sun Feb 7 14:05:58
2010
+++ /branches/backus_dev/src/common/utility/Point2D.cpp Sun Feb 7 18:40:30
2010
@@ -21,8 +21,40 @@
* http://trolltech.com/
*
*/
+#include "misc_funcs.h"
#include "Point2D.h"

+// Set default value of the global epsilon
+double Point2D::DEFAULT_EPSILON = 0.0000001;
+
+/**
+ * Sets the default epsilon value.
+ */
+void Point2D::setDefaultEpsilon(const double& epsilon) {
+ DEFAULT_EPSILON = epsilon;
+}
+
+/**
+ * Gets the default epsilon value.
+ */
+double Point2D::getDefaultEpsilon() {
+ return DEFAULT_EPSILON;
+}
+
+/**
+ * Sets the epsilon value for this object.
+ */
+void Point2D::setEpsilon(const double &epsilon) {
+ _epsilon = epsilon;
+}
+
+/**
+ * Gets the epsilon value for this object.
+ */
+double Point2D::getEpsilon() {
+ return _epsilon;
+}
+
/**
* Constructor.
*
@@ -32,6 +64,7 @@
Point2D::Point2D(const double x, const double y) {
_x = x;
_y = y;
+ _epsilon = DEFAULT_EPSILON;
}

/**
@@ -84,7 +117,8 @@
}

bool Point2D::operator==(const Point2D& other) {
- return (_x == other._x && _y == other._y);
+ return (relativeCompare(1.0+_x, 1.0+other._x, _epsilon) &&
+ relativeCompare(1.0+_y,1.0+other._y, _epsilon));
}
bool Point2D::operator!=(const Point2D& other) {
return !(*this == other);
=======================================
--- /branches/backus_dev/src/common/utility/Point2D.h Sun Feb 7 14:05:58
2010
+++ /branches/backus_dev/src/common/utility/Point2D.h Sun Feb 7 18:40:30
2010
@@ -77,6 +77,26 @@
*/
void setValue(const double x=0.0, const double y=0.0);

+ /**
+ * Sets the default epsilon value.
+ */
+ static void setDefaultEpsilon(const double& epsilon);
+
+ /**
+ * Gets the default epsilon value.
+ */
+ static double getDefaultEpsilon();
+
+ /**
+ * Sets the epsilon value for this object.
+ */
+ void setEpsilon(const double &epsilon);
+
+ /**
+ * Gets the epsilon value for this object.
+ */
+ double getEpsilon();
+
Point2D& operator=(const Point2D& other);
bool operator==(const Point2D& other);
bool operator!=(const Point2D& other);
@@ -91,6 +111,9 @@
private:
double _x;
double _y;
+
+ double _epsilon;
+ static double DEFAULT_EPSILON;
};

#endif
=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.cpp Mon Apr 27
14:19:22 2009
+++ /branches/backus_dev/src/common/utility/Vector2D.cpp Sun Feb 7
18:40:30 2010
@@ -1,16 +1,48 @@
-
+#include "misc_funcs.h"
#include "Vector2D.h"

+// Set default value of the global epsilon
+double Vector2D::DEFAULT_EPSILON = 0.0000001;
+
+/**
+ * Sets the default epsilon value.
+ */
+void Vector2D::setDefaultEpsilon(const double& epsilon) {
+ DEFAULT_EPSILON = epsilon;
+}
+
+/**
+ * Gets the default epsilon value.
+ */
+double Vector2D::getDefaultEpsilon() {
+ return DEFAULT_EPSILON;
+}
+
+/**
+ * Sets the epsilon value for this object.
+ */
+void Vector2D::setEpsilon(const double &epsilon) {
+ _epsilon = epsilon;
+}
+
+/**
+ * Gets the epsilon value for this object.
+ */
+double Vector2D::getEpsilon() {
+ return _epsilon;
+}
+
/**
* Constructor.
*
* @param x The initial x value of the vector.
* @param y The initial y value of the vector.
*/
-Vector2D::Vector2D(const double x=0.0, const double y=0.0) {
+Vector2D::Vector2D(const double x, const double y) {
_x = x;
_y = y;
_bIsNormalized = false;
+ _epsilon = DEFAULT_EPSILON;
}

/**
@@ -28,7 +60,7 @@
/**
* Sets the x value.
*/
-void Vector2D::setX(const double x=0.0) {
+void Vector2D::setX(const double x) {
_x = x;
_bIsNormalized = false;
}
@@ -42,10 +74,21 @@
/**
* Sets the y value.
*/
-void Vector2D::setY(const double y=0.0) {
+void Vector2D::setY(const double y) {
_y = y;
_bIsNormalized = false;
}
+
+/**
+ * Sets the x & y values.
+ *
+ * @param x The x value of the vector.
+ * @param y The y value of the vector.
+ */
+void Vector2D::setValue(const double x, const double y) {
+ _x = x;
+ _y = y;
+}

/**
* Gets the magnitude of the vector.
@@ -78,11 +121,21 @@
void Vector2D::normalize() {
if(!_bIsNormalized) {
double mag = magnitude();
- _x = _x/mag;
- _y = _y/mag;
- _bIsNormalized = true;
+ if(!relativeCompare(mag, 0.0, _epsilon)) {
+ _x = _x/mag;
+ _y = _y/mag;
+ _bIsNormalized = true;
+ }
}
}
+
+/**
+ * Returns the dot product of the specified vector with this vector.
+ */
+double Vector2D::dot(const Vector2D other) {
+ return (_x*other._x+_y*other._y);
+}
+

/**
* Returns true if the specified vector is parallel to this vector.
@@ -94,26 +147,42 @@
otherUnitV.normalize();

return
- ( (thisUnitV._x == otherUnitV._x || thisUnitV._x ==
-1.0*otherUnitV._x) &&
- (thisUnitV._y == otherUnitV._y || thisUnitV._y ==
-1.0*otherUnitV._y) );
+ ( relativeCompare(thisUnitV.magnitude(), 1.0, _epsilon) &&
+ relativeCompare(otherUnitV.magnitude(), 1.0, _epsilon) &&
+ (relativeCompare(thisUnitV._x, otherUnitV._x, _epsilon) ||
+ relativeCompare(thisUnitV._x, 1.0*otherUnitV._x, _epsilon)) &&
+ (relativeCompare(thisUnitV._y, otherUnitV._y, _epsilon) ||
+ relativeCompare(thisUnitV._y, 1.0*otherUnitV._y, _epsilon)) );
}

/**
* Returns true if the specified vector is normal to this vector.
*/
bool Vector2D::isNormalTo(const Vector2D other) {
- Vector2D otherNormV = other.getNormal();
-
- return isParallel(otherNormV);
+ Vector2D thisV = *this;
+ Vector2D otherNormV = other;
+
+ return thisV.isParallelTo(otherNormV.getNormal());
}

-/**
- * Returns the dot product of the specified vector with this vector.
- */
-double Vector2D::dot(const Vector2D other) {
- return (_x*other._x+_y*other._y);
-}
-
+Vector2D& Vector2D::operator=(const Vector2D& other) {
+ // no need to check for self-assignment, since we're not
+ // allocating/deallocating memory.
+ _x = other._x;
+ _y = other._y;
+ _bIsNormalized = other._bIsNormalized;
+
+ return *this;
+}
+bool Vector2D::operator==(const Vector2D& other) {
+ return (relativeCompare(_x, other._x, _epsilon) &&
+ relativeCompare(_y, other._y, _epsilon));
+}
+bool Vector2D::operator!=(const Vector2D& other) {
+ return !(*this == other);
+
+}
+#ifdef __NEEDS_DEBUGING__
/**
* Calculates the angle between the specified vector and this vector.
* Note: returns a value between [0, pi] or NaN if values are invalid.
@@ -173,67 +242,78 @@
return false;
}

-Vector2D& Vector2D::operator=(const Vector2D& other) {
- // no need to check for self-assignment, since we're not
- // allocating/deallocating memory.
- _x = other._x;
- _y = other._y;
- _bIsNormalized = other._bIsNormalized;
-
+Vector2D& Vector2D::operator+(const Vector2D& other) {
+ *this += other;
return *this;
}
-Vector2D& Vector2D::operator+(const Vector2D& other) {
- Vector2D retVal = *this;
- retVal += other;
- return retVal;
-}
Vector2D& Vector2D::operator*(const double val) {
- Vector2D retVal = *this;
- retVal *= val;
- return retVal;
+ *this *= val;
+ return *this;
}
Vector2D& Vector2D::operator*(const double val, Vector2D& v) {
return v*val;
}
Vector2D& Vector2D::operator/(const double val) {
- Vector2D retVal = *this;
- retVal /= val;
- return retVal;
+ *this /= val;
+ return *this;
}
Vector2D& Vector2D::operator+=(const Vector2D& other) {
- Vector2D retVal = *this;
- retVal._x += other._x;
- retVal._y += other._y;
- retVal._bIsNormalized = false;
- return retVal;
+ _x += other._x;
+ _y += other._y;
+ _bIsNormalized = false;
+ return *this;
}
Vector2D& Vector2D::operator*=(const double val) {
- Vector2D retVal = *this;
- retVal._x *= val;
- retVal._y *= val;
- retVal._bIsNormalized = false;
- return retVal;
+ _x *= val;
+ _y *= val;
+ _bIsNormalized = false;
+ return *this;
}
Vector2D& Vector2D::operator/=(const double val) {
- Vector2D retVal = *this;
- retVal *= (1.0/val);
- return retVal;
-}
-Vector2D& Vector2D::operator==(const Vector2D& other) {
- return (_x == other._x && _y == other._y);
-}
-Vector2D& Vector2D::operator!=(const Vector2D& other) {
- return !(*this == other);
-
+ *this *= (1.0/val);
+ return *this;
}
Vector2D& Vector2D::operator-=(const Vector2D& other) {
Vector2D otherInv = other * -1.0;
- Vector2D retVal = *this;
- retVal += otherInv;
- return retVal;
+ *this += otherInv;
+ return *this;
}
Vector2D& Vector2D::operator-(const Vector2D& other) {
- Vector2D retVal = *this;
- retVal -= other;
+ *this -= other;
+ return *this;
+}
+
+// **** Begin Friend definitions ****
+
+/**
+ * point subtraction results in a vector
+ */
+Vector2D& operator-(const Point2D& initial, const Point2D& final) {
+ double x = final.getX() - initial.getX();
+ double y = final.getY() - initial.getY();
+ Vector2D retVal(x, y);
return retVal;
}
+
+/**
+ * A point plus a vector results in a new point.
+ */
+Point2D& operator+(const Point2D& p, const Vector2D& v) {
+ Point2D retVal;
+ retVal.setX(p.getX()+v.getX());
+ retVal.setY(p.getY()+v.getY());
+}
+Point2D& operator+(const Vector2D& v, const Point2D& p) {
+ return p+v;
+}
+#endif
+
+/**
+ * friend vector to dump to an ostream
+ */
+ostream& operator<<(ostream& os, const Vector2D& vector) {
+ Vector2D tmp = vector;
+ os<<"("<<tmp.getX()<<","<<tmp.getY()<<")";
+ return os;
+}
+
=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.h Mon Apr 27 14:19:22
2009
+++ /branches/backus_dev/src/common/utility/Vector2D.h Sun Feb 7 18:40:30
2010
@@ -59,21 +59,30 @@
* Gets the x value.
*/
double getX();
+
/**
* Sets the x value.
*/
void setX(const double x=0.0);
+
/**
* Gets the y value.
*/
double getY();

-
/**
* Sets the y value.
*/
void setY(const double y=0.0);

+ /**
+ * Sets the x & y values.
+ *
+ * @param x The x value of the vector.
+ * @param y The y value of the vector.
+ */
+ void setValue(const double x=0.0, const double y=0.0);
+
/**
* Gets the magnitude of the vector.
*/
@@ -89,6 +98,11 @@
*/
void normalize();

+ /**
+ * Returns the dot product of the specified vector with this vector.
+ */
+ double dot(const Vector2D other);
+
/**
* Returns true if the specified vector is parallel to this vector.
*/
@@ -100,10 +114,30 @@
bool isNormalTo(const Vector2D other);

/**
- * Returns the dot product of the specified vector with this vector.
+ * Sets the default epsilon value.
*/
- double dot(const Vector2D other);
-
+ static void setDefaultEpsilon(const double& epsilon);
+
+ /**
+ * Gets the default epsilon value.
+ */
+ static double getDefaultEpsilon();
+
+ /**
+ * Sets the epsilon value for this object.
+ */
+ void setEpsilon(const double &epsilon);
+
+ /**
+ * Gets the epsilon value for this object.
+ */
+ double getEpsilon();
+
+ Vector2D& operator=(const Vector2D& other);
+ bool operator==(const Vector2D& other);
+ bool operator!=(const Vector2D& other);
+
+#ifdef __NEEDS_DEBUGGING__
/**
* Calculates the angle between the specified vector and this vector.
*/
@@ -118,6 +152,15 @@
*/
bool isOnLine(const Point2D a, const point2D b);

+ /**
+ * Checks to see if point B is on the line specified
+ * by this vector and point A.
+ *
+ * @param a A point known to be on the line.
+ * @param b The point to check.
+ */
+ Point2D getPoint(const Point2D a, const double distance=1.0);
+
/**
* Finds the point of intersection of the line specified
* by this vector and point A and the line specified by
@@ -133,56 +176,30 @@
bool getIntersectingPt(const Point2D pA, const Vector2D vB,
const Point2D pB, Vector2D* result);

- Vector2D& operator=(const Vector2D& other);
Vector2D& operator+(const Vector2D& other);
+ Vector2D& operator-(const Vector2D& other);
Vector2D& operator*(const double val);
Vector2D& operator*(const double val, Vector2D& v);
Vector2D& operator/(const double val);
Vector2D& operator+=(const Vector2D& other);
Vector2D& operator*=(const double val);
Vector2D& operator/=(const double val);
- Vector2D& operator==(const Vector2D& other);
- Vector2D& operator!=(const Vector2D& other);
Vector2D& operator-=(const Vector2D& other);
- Vector2D& operator-(const Vector2D& other);
+
+ friend Vector2D& operator-(const Point2D& initial, const Point2D& final);
+ friend Point2D& operator+(const Point2D& p, const Vector2D& v);
+ friend Point2D& operator+(const Vector2D& v, const Point2D& p);
+#endif
+ friend ostream& operator<<(ostream& os, const Vector2D& vector);

private:
double _x;
double _y;

bool _bIsNormalized; //!< cuts down on excess calculations.
-};
-
-// **** Begin Friend definitions ****
-
-/**
- * friend vector to dump to an ostream
- */
-friend ostream& operator<<(ostream& os, const Vector2D& vector) {
- os<<"("<<vector.getX()<<","<<vector.getY()<<")";
- return os;
-}
-
-/**
- * point subtraction results in a vector
- */
-friend Vector2D& operator-(const Point2D& initial, const Point2D& final) {
- double x = final.getX() - initial.getX();
- double y = final.getY() - initial.getY();
- Vector2D retVal(x, y);
- return retVal;
-}
-
-/**
- * A point plus a vector results in a new point.
- */
-friend Point2D& operator+(const Point2D& p, const Vector2D& v) {
- Point2D retVal;
- retVal.setX(p.getX()+v.getX());
- retVal.setY(p.getY()+v.getY());
-}
-friend Point2D& operator+(const Vector2D& v, const Point2D& p) {
- return p+v;
-}
+
+ double _epsilon;
+ static double DEFAULT_EPSILON;
+};

#endif
=======================================
--- /branches/backus_dev/src/common/utility/misc_funcs.cpp Sun Apr 26
17:56:58 2009
+++ /branches/backus_dev/src/common/utility/misc_funcs.cpp Sun Feb 7
18:40:30 2010
@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <ctime>

+//#include <iostream>
using namespace std;

#include <QString>
@@ -116,3 +117,25 @@
int roundDtoI(const double& val) {
return (val < 0.0) ? int(val-0.5) : int(val+0.5);
}
+
+/**
+ * Compares to doubles using the specified precision.
+ */
+bool relativeCompare(const double& A, const double& B,
+ const double& epsilon) {
+ double tmpA = (A > 0.0) ? A : -1.0 * A;
+ double tmpB = (B > 0.0) ? B : -1.0 * B;
+ double delta = (tmpA > tmpB) ? tmpA - tmpB : tmpB - tmpA;
+ return (delta <= epsilon);
+}
+
+/**
+ * Compares to floats using the specified precision.
+ */
+bool relativeCompare(const float& A, const float& B,
+ const float& epsilon) {
+ float tmpA = (A > 0.0) ? A : -1.0 * A;
+ float tmpB = (B > 0.0) ? B : -1.0 * B;
+ float delta = (tmpA > tmpB) ? tmpA - tmpB : tmpB - tmpA;
+ return (delta <= epsilon);
+}
=======================================
--- /branches/backus_dev/src/common/utility/misc_funcs.h Sun Apr 26
17:56:58 2009
+++ /branches/backus_dev/src/common/utility/misc_funcs.h Sun Feb 7
18:40:30 2010
@@ -72,4 +72,16 @@
return (val < 0.0) ? int(val-0.5) : int(val+0.5);
}*/

+/**
+ * Compares to doubles using the specified precision.
+ */
+extern bool relativeCompare(const double& A, const double& B,
+ const double& epsilon);
+
+/**
+ * Compares to floats using the specified precision.
+ */
+extern bool relativeCompare(const float& A, const float& B,
+ const float& epsilon);
+
//#endif
=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.cpp Sun
Feb 7 14:05:58 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.cpp Sun
Feb 7 18:40:30 2010
@@ -108,6 +108,23 @@
QVERIFY(testPointC.getY() == -21734.1);
}

+void Test_Point2D::Test_Epsilon() {
+ Point2D testPointA;
+
+ // get/set Defaults
+ double defVal = 0.0000001;
+ QVERIFY(testPointA.getDefaultEpsilon() == defVal);
+ testPointA.setDefaultEpsilon(0.001);
+ QVERIFY(testPointA.getDefaultEpsilon() == 0.001);
+
+ Point2D testPointB;
+ QVERIFY(testPointB.getEpsilon() == 0.001);
+ testPointB.setEpsilon(0.0007);
+ QVERIFY(testPointB.getEpsilon() == 0.0007);
+
+ // return to default
+ testPointA.setDefaultEpsilon(defVal);
+}
void Test_Point2D::Test_OperatorSet() {
Point2D testPoint;

=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.h Sun
Feb 7 14:05:58 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Point2D.h Sun
Feb 7 18:40:30 2010
@@ -40,6 +40,7 @@
void Test_GetSetY();
void Test_SetValue();
void Test_Constructor();
+ void Test_Epsilon();
void Test_OperatorSet();
void Test_OperatorEqual();
void Test_OperatorNotEqual();

Reply all
Reply to author
Forward
0 new messages