nrtb-core team mailing list archive
-
nrtb-core team
-
Mailing list archive
-
Message #00141
[Branch ~fpstovall/nrtb/cpp_common] Rev 29: Automated unit test process for nrtb::triad. All methods are tested, and the test will cause the ...
------------------------------------------------------------
revno: 29
committer: fpstovall@xxxxxxxxx
branch nick: dev
timestamp: Tue 2011-07-19 22:12:28 -0400
message:
Automated unit test process for nrtb::triad. All methods are tested, and the test will cause the build to fail and not place the header file if any test fails.
modified:
common/point/Makefile
common/point/common_test.cpp
--
lp:~fpstovall/nrtb/cpp_common
https://code.launchpad.net/~fpstovall/nrtb/cpp_common
Your team NRTB Core is subscribed to branch lp:~fpstovall/nrtb/cpp_common.
To unsubscribe from this branch go to https://code.launchpad.net/~fpstovall/nrtb/cpp_common/+edit-subscription
=== modified file 'common/point/Makefile'
--- common/point/Makefile 2010-12-29 14:47:26 +0000
+++ common/point/Makefile 2011-07-20 02:12:28 +0000
@@ -17,8 +17,9 @@
#***********************************************
lib: common_test Makefile
+ @echo "(2,3.5,7)" | ./common_test
@cp -v triad.h ../include
- @echo build complete
+ @echo nrtb::triad build complete
../include/common.h:
@cd ../common_rl; make lib
=== modified file 'common/point/common_test.cpp'
--- common/point/common_test.cpp 2010-12-25 22:44:22 +0000
+++ common/point/common_test.cpp 2011-07-20 02:12:28 +0000
@@ -27,54 +27,87 @@
typedef nrtb::triad<long double> ld_triad;
+int test_triad(const std::string prompt, ld_triad val, ld_triad right, int ec)
+{
+ cout << "\t" << prompt << " = " << val << endl;
+ if (val != right)
+ {
+ ec++;
+ cerr << "\t\tTest Failed: Answer should be " << val << endl;
+ }
+ return ec;
+};
+
+int test_ld(const std::string prompt, long double val, long double right, int ec)
+{
+ cout << "\t" << prompt << " = " << val << endl;
+ if (val != right)
+ {
+ ec++;
+ cerr << "\t\tTest Failed: Answer should be " << val << endl;
+ }
+ return ec;
+};
+
int main()
{
ld_triad a(1,2,3);
ld_triad b(3,2,1);
+ int returnme = 0;
cout << setprecision(10);
- cout << "=== Triad Test ===" << endl;
- cout << "\ta = " << a << "; b = " << b << "\n" << endl;
-
- cout << "\ta + b = " << a + b << endl;
- cout << "\ta - b = " << a - b << endl;
- cout << "\ta * b = " << a * b << endl;
- cout << "\ta / b = " << a / b << endl;
- cout << "\ta += b; a = " << (a += b) << endl;
- cout << "\ta -= b; a = " << (a -= b) << endl;
- cout << "\ta *= b; a = " << (a *= b) << endl;
- cout << "\ta /= b; a = " << (a /= b) << endl;
- cout << "\n\ta.pow(b) = " << a.pow(b) << endl;
- cout << "\ta.range(b) = " << a.range(b) << endl;
- cout << "\ta.magnatude() = " << a.magnatude() << endl;
- cout << "\n\ta == a = " << (a == a) << endl;
- cout << "\ta == b = " << (a== b) << endl;
- cout << "\ta != b = " << (a != b) << endl;
- cout << "\ta != a = " << (a != a) << endl;
-
- cout << endl;
-
- cout << "\ta + 2 = " << a + 2 << endl;
- cout << "\ta - 2 = " << a - 2 << endl;
- cout << "\ta * 2 = " << a * 2 << endl;
- cout << "\ta / 2 = " << a / 2 << endl;
- cout << "\ta += 2; a = " << (a += 2) << endl;
- cout << "\ta -= 2; a = " << (a -= 2) << endl;
- cout << "\ta *= 2; a = " << (a *= 2) << endl;
- cout << "\ta /= 2; a = " << (a /= 2) << endl;
- cout << "\n\ta.pow(2) = " << a.pow(2) << endl;
- cout << "\ta.pow(0.5) = " << a.pow(0.5) << endl;
+ cout << "=== nrtb::triad Unit Test ===" << endl;
+ cout << "\ta = " << a << "; b = " << b << endl;
+ // basic operations tests
+ returnme = test_triad("a + b",a + b,ld_triad(4,4,4),returnme);
+ returnme = test_triad("a - b",a - b,ld_triad(-2,0,2),returnme);
+ returnme = test_triad("a * b",a * b,ld_triad(3,4,3),returnme);
+ returnme = test_triad("a / b",a / b,ld_triad(1.0d/(long double) 3.0,1,3),returnme);
+ returnme = test_triad("a += b; a",a += b,ld_triad(4,4,4),returnme);
+ returnme = test_triad("a -= b; a",a -= b,ld_triad(1,2,3),returnme);
+ returnme = test_triad("a *= b; a",a *= b,ld_triad(3,4,3),returnme);
+ returnme = test_triad("a /= b; a",a /= b,ld_triad(1,2,3),returnme);
+ // power test
+ returnme = test_triad("a.pow(b)",a.pow(b),ld_triad(1,4,3),returnme);
+ // range test
+ ld_triad t = b - a;
+ t *= t;
+ long double r = sqrt(t.x + t.y + t.z);
+ returnme = test_ld("a.range(b)",a.range(b),r,returnme);
+ // magnatude test
+ returnme = test_ld("a.magnatude()",a.magnatude(),a.range(0),returnme);
+ // boolean tests
+ returnme = test_ld("a == a",a == a,1,returnme);
+ returnme = test_ld("a == b",a == b,0,returnme);
+ returnme = test_ld("a != b",a != b,1,returnme);
+ returnme = test_ld("a != a",a != a,0,returnme);
+ // point/scalar operations
+ returnme = test_triad("a + 2",a + 2,ld_triad(3,4,5),returnme);
+ returnme = test_triad("a - 2",a - 2,ld_triad(-1,0,1),returnme);
+ returnme = test_triad("a * 2",a * 2,ld_triad(2,4,6),returnme);
+ returnme = test_triad("a / 2",a / 2,ld_triad(0.5,1,1.5),returnme);
+ returnme = test_triad("a += 2",a += 2,ld_triad(3,4,5),returnme);
+ returnme = test_triad("a -= 2",a -= 2,ld_triad(1,2,3),returnme);
+ returnme = test_triad("a *= 2",a *= 2,ld_triad(2,4,6),returnme);
+ returnme = test_triad("a /= 2",a /= 2,ld_triad(1,2,3),returnme);
+ returnme = test_triad("a.pow(2)",a.pow(2),ld_triad(1,4,9),returnme);
+ // normalization test
cout << "\ta.normalize() = " << a.normalize() << endl;
- cout << "\ta.normalize().magnatude() = "<< a.normalize().magnatude() << endl;
-
- cout << "\n\a\tInput a new value for b (x,y,z): " << flush;
- cin >> b;
- cout << "\t\tb = " << b << endl;
- cout << "\tb.to_str() =\"" << b.to_str() << "\"" << endl;
- cout << "\tb.from_str(b.to_str(10)) = " << b.from_str(b.to_str(10)) << endl;
-
-
- cout << "\n=== Test Complete ===" << endl;
- return 0;
+ returnme = test_ld("a.normalize().magnatude()",a.normalize().magnatude(),1.0,returnme);
+ // string i/o tests, assumes "2,3.5,7) is input.
+ cout << "\tInput a new value for b \"(2,3.5,7)\": " << flush;
+ cin >> b; cout << endl;
+ returnme = test_triad("b",b,ld_triad(2,3.5,7),returnme);
+ returnme = test_triad("b.from_str(b.to_str(10))",
+ b.from_str(b.to_str(10)),ld_triad(2,3.5,7),returnme);
+ // report errors, if any
+ if (returnme)
+ {
+ cerr << "There were " << returnme
+ << " error(s) found." << endl;
+ }
+ cout << "=== nrtb::triad Unit Test Complete ===" << endl;
+ // return the error count as the exit code
+ return returnme;
};