From e14cf50b0f1cf86dc78beff9be3475fe41b703ea Mon Sep 17 00:00:00 2001 From: Steven Palmer <palmes4@mcmaster.ca> Date: Sun, 8 Apr 2018 18:40:00 -0400 Subject: [PATCH] Updates to a3 soln --- .../A3/A3Soln/implementation/bin/.gitkeep | Bin 0 -> 6 bytes .../A3Soln/implementation/include/PathADT.h | 7 +- .../A3/A3Soln/implementation/include/Seq2D.h | 10 +- .../A3/A3Soln/implementation/src/PathADT.cpp | 2 + .../A3/A3Soln/implementation/src/Seq2D.cpp | 2 +- .../A3/A3Soln/implementation/test/testDEM.cpp | 184 ++++++++++++++++-- .../A3Soln/implementation/test/testLineT.cpp | 112 +++++++++++ .../A3Soln/implementation/test/testPathT.cpp | 165 +++++++++++++++- .../A3Soln/implementation/test/testPointT.cpp | 38 ++-- 9 files changed, 473 insertions(+), 47 deletions(-) create mode 100644 Assignments/A3/A3Soln/implementation/bin/.gitkeep create mode 100644 Assignments/A3/A3Soln/implementation/test/testLineT.cpp diff --git a/Assignments/A3/A3Soln/implementation/bin/.gitkeep b/Assignments/A3/A3Soln/implementation/bin/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..49cc8ef0e116cef009fe0bd72473a964bbd07f9b GIT binary patch literal 6 NcmezWkC%aq0RRg=0u=xN literal 0 HcmV?d00001 diff --git a/Assignments/A3/A3Soln/implementation/include/PathADT.h b/Assignments/A3/A3Soln/implementation/include/PathADT.h index e3c2c263..ebe687d9 100644 --- a/Assignments/A3/A3Soln/implementation/include/PathADT.h +++ b/Assignments/A3/A3Soln/implementation/include/PathADT.h @@ -7,14 +7,13 @@ #include <vector> -using std::vector; - class PathT { private: - vector<LineT> s; + std::vector<LineT> s; + void translatePriv(int dx, int dy); - vector<PointT> pointsInLine(LineT ln) const; + std::vector<PointT> pointsInLine(LineT ln) const; PointT adjPt(CompassT o) const; bool intersects(LineT a, LineT b) const; diff --git a/Assignments/A3/A3Soln/implementation/include/Seq2D.h b/Assignments/A3/A3Soln/implementation/include/Seq2D.h index 0f60e2f5..c6d45fdb 100644 --- a/Assignments/A3/A3Soln/implementation/include/Seq2D.h +++ b/Assignments/A3/A3Soln/implementation/include/Seq2D.h @@ -8,13 +8,11 @@ #include <vector> -using std::vector; - template <class T> class Seq2D { private: - vector<vector<T>> s; + std::vector<std::vector<T>> s; double scale; nat nRow; nat nCol; @@ -24,11 +22,11 @@ class Seq2D bool validPoint(PointT p) const; bool validLine(LineT l) const; bool validPath(PathT pth) const; - vector<PointT> pointsInLine(LineT l) const; - vector<PointT> pointsInPath(PathT pth) const; + std::vector<PointT> pointsInLine(LineT l) const; + std::vector<PointT> pointsInPath(PathT pth) const; public: - Seq2D(vector<vector<T>> s, double scale); + Seq2D(std::vector<std::vector<T>> s, double scale); void set(PointT p, T v); T get(PointT p) const; nat getNumRow() const; diff --git a/Assignments/A3/A3Soln/implementation/src/PathADT.cpp b/Assignments/A3/A3Soln/implementation/src/PathADT.cpp index fc14a5e0..dab085fb 100644 --- a/Assignments/A3/A3Soln/implementation/src/PathADT.cpp +++ b/Assignments/A3/A3Soln/implementation/src/PathADT.cpp @@ -38,6 +38,8 @@ PointT PathT::end() const LineT PathT::line(nat i) const { + if(i >= s.size()) + throw outside_bounds(); return s[i]; } diff --git a/Assignments/A3/A3Soln/implementation/src/Seq2D.cpp b/Assignments/A3/A3Soln/implementation/src/Seq2D.cpp index ec73972d..9d027950 100644 --- a/Assignments/A3/A3Soln/implementation/src/Seq2D.cpp +++ b/Assignments/A3/A3Soln/implementation/src/Seq2D.cpp @@ -180,7 +180,7 @@ bool Seq2D<T>::validCol(nat j) const template <class T> bool Seq2D<T>::validPoint(PointT p) const { - return validRow(p.x()) && validCol(p.y()); + return validRow(p.y()) && validCol(p.x()); } template <class T> diff --git a/Assignments/A3/A3Soln/implementation/test/testDEM.cpp b/Assignments/A3/A3Soln/implementation/test/testDEM.cpp index 46bc8e50..a3c110e4 100644 --- a/Assignments/A3/A3Soln/implementation/test/testDEM.cpp +++ b/Assignments/A3/A3Soln/implementation/test/testDEM.cpp @@ -1,38 +1,196 @@ #include "catch.h" +#include "PointADT.h" +#include "LineADT.h" +#include "PathADT.h" #include "DEM.h" +#include "Exceptions.h" #include <vector> +using std::vector; + TEST_CASE( "tests for DEMT" , "[DEMT]" ) { vector<vector<int>> v; /* TEST GRID 'v': * - * 3 3 3 0 - * 0 1 3 1 - * 1 1 3 1 - * 1 2 3 3 + * 3 3 3 0 6 + * 0 1 3 1 6 + * 1 1 3 1 5 + * 1 2 3 3 5 */ - v.push_back( vector<int> {1,2,3,3} ); - v.push_back( vector<int> {1,1,3,1} ); - v.push_back( vector<int> {0,1,3,1} ); - v.push_back( vector<int> {3,3,3,0} ); + v.push_back( vector<int> {1,2,3,3,5} ); + v.push_back( vector<int> {1,1,3,1,5} ); + v.push_back( vector<int> {0,1,3,1,6} ); + v.push_back( vector<int> {3,3,3,0,6} ); DEMT dem(v, 3.0); - - SECTION( "get" ){ - REQUIRE( dem.get(PointT(2,2)) == 3 ); + SECTION( "constructor exception for negative scale" ){ + REQUIRE_THROWS_AS( DEMT(v, -1.0), ::invalid_argument ); + } + + SECTION( "constructor exception for zero scale" ){ + REQUIRE_THROWS_AS( DEMT(v, 0.0), ::invalid_argument ); + } + + SECTION( "constructor exception for empty grid" ){ + REQUIRE_THROWS_AS( DEMT(vector<vector<int>>(), 1.0), ::invalid_argument ); + } + + SECTION( "constructor exception for grid with empty row" ){ + vector<vector<int>> grid; + grid.push_back( vector<int>() ); + + REQUIRE_THROWS_AS( DEMT(grid, 1.0), ::invalid_argument ); + } + + SECTION( "constructor exception for grid with unequal row lengths" ){ + vector<vector<int>> grid; + grid.push_back( vector<int> {0,0,0,0} ); + grid.push_back( vector<int> {0,0,0,0} ); + grid.push_back( vector<int> {0,0,0} ); + grid.push_back( vector<int> {0,0,0,0} ); + + REQUIRE_THROWS_AS( DEMT(grid, 1.0), ::invalid_argument ); + } + + SECTION( "general tests for get" ){ + REQUIRE( dem.get(PointT(1,2)) == 1 ); + } + + SECTION( "get on upper right corner" ){ + REQUIRE( dem.get(PointT(4,3)) == 6 ); + } + + SECTION( "get exception on invalid point" ){ + REQUIRE_THROWS_AS( dem.get(PointT(3,4)), outside_bounds ); + } + + SECTION( "general tests for set" ){ + dem.set(PointT(1,3), 50); + REQUIRE( dem.get(PointT(1,3)) == 50 ); + } + + SECTION( "set exception on invalid point" ){ + REQUIRE_THROWS_AS( dem.set(PointT(3,4), 0), outside_bounds ); + } + + SECTION( "general tests for getNumRow" ){ + REQUIRE( dem.getNumRow() == 4 ); + } + + SECTION( "general tests for getNumCol" ){ + REQUIRE( dem.getNumCol() == 5 ); + } + + SECTION( "general tests for getScale" ){ + // don't need approx here since no arithmetic has been done. + REQUIRE( dem.getScale() == 3.0 ); } - SECTION( "connected"){ + SECTION( "general tests for count(T)" ){ + REQUIRE + (( dem.count(0) == 2 + && dem.count(1) == 6 + && dem.count(2) == 1 + && dem.count(3) == 7 + && dem.count(4) == 0 + && dem.count(5) == 2 + && dem.count(6) == 2 + )); + } + + SECTION( "general tests for count(LineT, T)" ){ + LineT l(PointT(4,2), W, 4); + + REQUIRE + (( dem.count(l, 0) == 0 + && dem.count(l, 1) == 2 + && dem.count(l, 2) == 0 + && dem.count(l, 3) == 1 + && dem.count(l, 4) == 0 + && dem.count(l, 5) == 0 + && dem.count(l, 6) == 1 + )); + } + + SECTION( "count(LineT, T) exception for line extending outside of grid" ){ + LineT l(PointT(2,2), N, 3); + + REQUIRE_THROWS_AS( dem.count(l, 0), ::invalid_argument ); + } + + SECTION( "general tests for count(PathT, T)" ){ + PathT p(PointT(0,0), N, 3); + p.append(E, 3); + p.append(S, 1); + + REQUIRE + (( dem.count(p, 0) == 1 + && dem.count(p, 1) == 5 + && dem.count(p, 2) == 0 + && dem.count(p, 3) == 1 + && dem.count(p, 4) == 0 + && dem.count(p, 5) == 0 + && dem.count(p, 6) == 0 + )); + } + + SECTION( "count(PathT, T) exception for path extending outside of grid" ){ + PathT p(PointT(0,0), N, 3); + p.append(E, 3); + p.append(S, 1); + p.append(E, 2); + + REQUIRE_THROWS_AS( dem.count(p, 0), ::invalid_argument ); + } + + SECTION( "general tests for length" ){ + PathT p(PointT(0,0), N, 3); + p.append(E, 3); + p.append(S, 1); + + REQUIRE( dem.length(p) == Approx(21.0) ); + } + + SECTION( "length exception for path extending outside of grid" ){ + PathT p(PointT(0,0), N, 3); + p.append(E, 3); + p.append(S, 1); + p.append(E, 2); + + REQUIRE_THROWS_AS( dem.length(p), ::invalid_argument ); + } + + SECTION( "(0,0) -> (1,2) is connected" ){ REQUIRE( dem.connected(PointT(0,0), PointT(1,2)) ); + } + + SECTION( "(0,0) -> (3,2) not connected" ){ REQUIRE( !dem.connected(PointT(0,0), PointT(3,2)) ); + } + + SECTION( "(3,0) -> (0,3) is connected" ){ REQUIRE( dem.connected(PointT(3,0), PointT(0,3)) ); + } + + SECTION( "(0,3) -> (3,0) is connected" ){ + REQUIRE( dem.connected(PointT(0,3), PointT(3,0)) ); + } + + SECTION( "(0,2) -> (3,3) not connected" ){ REQUIRE( !dem.connected(PointT(0,2), PointT(3,3)) ); + } + + SECTION( "(2,1) -> (0,0) not connected" ){ REQUIRE( !dem.connected(PointT(2,1), PointT(0,0)) ); } -} \ No newline at end of file + SECTION( "connected exception for point outside grid" ){ + REQUIRE_THROWS_AS( dem.connected(PointT(0,0), PointT(5,5)), ::invalid_argument ); + } + +} diff --git a/Assignments/A3/A3Soln/implementation/test/testLineT.cpp b/Assignments/A3/A3Soln/implementation/test/testLineT.cpp new file mode 100644 index 00000000..4b87cf1f --- /dev/null +++ b/Assignments/A3/A3Soln/implementation/test/testLineT.cpp @@ -0,0 +1,112 @@ +#include "catch.h" +#include "PointADT.h" +#include "LineADT.h" +#include "Exceptions.h" + +TEST_CASE( "tests for LineT" , "[LineT]" ) { + + LineT lN(PointT(5, 10), N, 5); + LineT lE(PointT(5, 10), E, 5); + LineT lS(PointT(5, 10), S, 5); + LineT lW(PointT(5, 10), W, 5); + + SECTION( "constructor exception for LineT with length 0" ){ + REQUIRE_THROWS_AS( LineT(PointT(0,0), N, 0), ::invalid_argument ); + } + + SECTION( "general tests for strt" ){ + REQUIRE + (( lN.strt().x() == 5 + && lN.strt().y() == 10 + )); + } + + SECTION( "general tests for end" ){ + REQUIRE + (( lN.end().x() == 5 + && lN.end().y() == 14 + && lE.end().x() == 9 + && lE.end().y() == 10 + && lS.end().x() == 5 + && lS.end().y() == 6 + && lW.end().x() == 1 + && lW.end().y() == 10 + )); + } + + SECTION( "general tests for orient" ){ + REQUIRE + (( lN.orient() == N + && lE.orient() == E + && lS.orient() == S + && lW.orient() == W + )); + } + + SECTION( "general tests for len" ){ + REQUIRE( lN.len() == 5 ); + } + + SECTION( "flip reverses direction" ){ + REQUIRE + (( lN.flip().orient() == S + && lE.flip().orient() == W + && lS.flip().orient() == N + && lW.flip().orient() == E + )); + } + + SECTION( "flip maintains other parameters" ){ + REQUIRE + (( lN.flip().strt().x() == 5 + && lN.flip().strt().y() == 10 + && lN.flip().len() == 5 + )); + } + + SECTION( "rotate changes direction properly" ){ + REQUIRE + (( lN.rotate(CW).orient() == E + && lE.rotate(CW).orient() == S + && lS.rotate(CW).orient() == W + && lW.rotate(CW).orient() == N + && lN.rotate(CCW).orient() == W + && lE.rotate(CCW).orient() == N + && lS.rotate(CCW).orient() == E + && lW.rotate(CCW).orient() == S + )); + } + + SECTION( "rotate maintains other parameters" ){ + REQUIRE + (( lN.rotate(CW).strt().x() == 5 + && lN.rotate(CW).strt().y() == 10 + && lN.rotate(CW).len() == 5 + && lN.rotate(CCW).strt().x() == 5 + && lN.rotate(CCW).strt().y() == 10 + && lN.rotate(CCW).len() == 5 + )); + } + + SECTION( "general tests for translate" ){ + LineT testLn = lN.translate(5, 5); + + REQUIRE + (( testLn.strt().x() == 10 + && testLn.strt().y() == 15 + && testLn.orient() == N + && testLn.len() == 5 + )); + } + + SECTION( "translate does not change original line" ){ + lN.translate(5, 5); + + REQUIRE + (( lN.strt().x() == 5 + && lN.strt().y() == 10 + && lN.orient() == N + && lN.len() == 5 + )); + } +} diff --git a/Assignments/A3/A3Soln/implementation/test/testPathT.cpp b/Assignments/A3/A3Soln/implementation/test/testPathT.cpp index fd1a6506..183b6cdf 100644 --- a/Assignments/A3/A3Soln/implementation/test/testPathT.cpp +++ b/Assignments/A3/A3Soln/implementation/test/testPathT.cpp @@ -1,20 +1,165 @@ #include "catch.h" #include "PointADT.h" +#include "LineADT.h" #include "PathADT.h" #include "MapTypes.h" +#include "Exceptions.h" -TEST_CASE( "tests for PathT" , "[PathT]" ) { +bool pt_eql(PointT p1, PointT p2) { + return p1.x() == p2.x() + && p1.y() == p2.y(); +} + +bool line_eql(LineT l1, LineT l2) { + return pt_eql(l1.strt(), l2.strt()) + && l1.orient() == l2.orient() + && l1.len() == l2.len(); +} + + + +TEST_CASE( "general tests for PathT" , "[PathT]" ) { + + SECTION( "general tests for append" ){ + PathT p_appendTest(PointT(0,0), N, 1); + + for(int i = 2; i <= 100; ++i){ + CompassT dir; + + switch(i%4) { + case 0: + dir = W; + break; + case 1: + dir = N; + break; + case 2: + dir = E; + break; + case 3: + dir = S; + break; + } + + p_appendTest.append(dir, i); + } + + REQUIRE + (( p_appendTest.len() == 5050 + && p_appendTest.size() == 100 + )); + } + + SECTION( "translate does not change original path" ){ + PathT p(PointT(0,0), N, 5); + p.append(E, 6); + p.append(S, 3); + + p.translate(5,5); + + REQUIRE + (( line_eql(p.line(0), LineT(PointT(0, 0), N, 5)) + && line_eql(p.line(1), LineT(PointT(1, 4), E, 6)) + && line_eql(p.line(2), LineT(PointT(6, 3), S, 3)) + )); + } + +} + + + +TEST_CASE( "tests for PathT on a simple path" , "[PathT]" ) { + + PathT p_simple(PointT(5, 10), N, 5); + + SECTION( "general tests for strt (simple)" ){ + REQUIRE( pt_eql(p_simple.strt(), PointT(5, 10)) ); + } + + SECTION( "general tests for end (simple)" ){ + REQUIRE( pt_eql(p_simple.end(), PointT(5, 14)) ); + } + + SECTION( "general tests for size (simple)" ){ + REQUIRE( p_simple.size() == 1 ); + } + + SECTION( "general tests for len (simple)" ){ + REQUIRE( p_simple.len() == 5 ); + } + + SECTION( "general tests for line (simple)" ){ + REQUIRE( line_eql(p_simple.line(0), LineT(PointT(5, 10), N, 5)) ); + } + + SECTION( "general tests for translate (simple)" ){ + PathT trans = p_simple.translate(3, 3); + + REQUIRE( line_eql(trans.line(0), LineT(PointT(8, 13), N, 5)) ); + } + + SECTION( "line exception for outside bounds (simple)" ){ + REQUIRE_THROWS_AS( p_simple.line(1), outside_bounds ); + } + + SECTION( "append exception for self-crossing path (simple)" ){ + REQUIRE_THROWS_AS( p_simple.append(S, 1), ::invalid_argument ); + } + +} + + + +TEST_CASE( "tests for PathT on a complex path" , "[PathT]" ) { + + PathT p_complex(PointT(10, 5), N, 5); + p_complex.append(E, 3); + p_complex.append(S, 3); + p_complex.append(E, 6); + p_complex.append(S, 3); + p_complex.append(W, 14); + p_complex.append(N, 5); - PathT p1(PointT(0, 0), N, 4); + SECTION( "general tests for strt (complex)" ){ + REQUIRE( pt_eql(p_complex.strt(), PointT(10, 5)) ); + } + SECTION( "general tests for end (complex)" ){ + REQUIRE( pt_eql(p_complex.end(), PointT(5, 8)) ); + } + + SECTION( "general tests for size (complex)" ){ + REQUIRE( p_complex.size() == 7 ); + } + + SECTION( "general tests for len (complex)" ){ + REQUIRE( p_complex.len() == 39 ); + } + + SECTION( "general tests for line (complex)" ){ + REQUIRE( line_eql(p_complex.line(4), LineT(PointT(19, 5), S, 3)) ); + } + + SECTION( "general tests for translate (complex)" ){ + PathT trans = p_complex.translate(3, 3); + + REQUIRE + (( line_eql(trans.line(0), LineT(PointT(13, 8), N, 5)) + && line_eql(trans.line(1), LineT(PointT(14, 12), E, 3)) + && line_eql(trans.line(2), LineT(PointT(16, 11), S, 3)) + && line_eql(trans.line(3), LineT(PointT(17, 9), E, 6)) + && line_eql(trans.line(4), LineT(PointT(22, 8), S, 3)) + && line_eql(trans.line(5), LineT(PointT(21, 6), W, 14)) + && line_eql(trans.line(6), LineT(PointT(8, 7), N, 5)) + )); + } - SECTION( "Self-crossing path" ){ - p1.append(E, 3); - p1.append(S, 3); - p1.append(W, 2); - p1.append(N, 2); - p1.append(E, 1); - p1.append(S, 1); + SECTION( "line exception for outside bounds (complex)" ){ + REQUIRE_THROWS_AS( p_complex.line(7), outside_bounds ); + } + + SECTION( "append exception for self-crossing path (complex)" ){ + REQUIRE_THROWS_AS( p_complex.append(E, 15), ::invalid_argument ); } -} \ No newline at end of file +} diff --git a/Assignments/A3/A3Soln/implementation/test/testPointT.cpp b/Assignments/A3/A3Soln/implementation/test/testPointT.cpp index dce93a1d..fd6b4bf9 100644 --- a/Assignments/A3/A3Soln/implementation/test/testPointT.cpp +++ b/Assignments/A3/A3Soln/implementation/test/testPointT.cpp @@ -3,20 +3,32 @@ TEST_CASE( "tests for PointT" , "[PointT]" ) { - PointT p1(0, 0); - PointT p2(0, 1); - PointT p3(1, 0); - PointT p4(1, 1); + PointT p(5, 10); - - // the test below aren't necessary for the assignment - SECTION( "PointT equality" ){ - REQUIRE( p1 == PointT(0, 0) ); - REQUIRE( !(p1 == p2) ); + SECTION( "general tests for x" ){ + REQUIRE( p.x() == 5 ); + } + + SECTION( "general tests for y" ){ + REQUIRE( p.y() == 10 ); } - SECTION( "PointT inequality" ){ - REQUIRE( !(p1 != PointT(0, 0)) ); - REQUIRE( p1 != p2 ); + SECTION( "general tests for translate" ){ + PointT testPt = p.translate(-20, 30); + + REQUIRE + (( testPt.x() == -15 + && testPt.y() == 40 + )); } -} \ No newline at end of file + + SECTION( "translate does not change original point" ){ + p.translate(5, 5); + + REQUIRE + (( p.x() == 5 + && p.y() == 10 + )); + } + +} -- GitLab