001    package ps2.test;
002    
003    import ps2.*;
004    
005    import junit.framework.TestCase;
006    
007    import java.io.IOException;
008    
009    
010    /**
011     * Unit tests for the WalkingRouteFormatter class.
012     **/
013    public class WalkingRouteFormatterTest extends TestCase {
014      
015        private WalkingRouteFormatter mDirections;
016        private Route mShortRoute;
017      
018        // Some constants for easy reference
019        private String mName = "Penny Lane";
020        private int oneUnit = 100000;
021      
022        // JUnit calls setUp() before each test__ method is run
023        protected void setUp() {
024            mDirections = new WalkingRouteFormatter();
025            mShortRoute = new Route(new GeoSegment(mName,
026                                                   new GeoPoint(0,0),
027                                                   new GeoPoint(0,100000)));
028        }
029    
030    
031        /**
032         * Test simple directions with one distance and one origHeading.
033         **/
034        public void testShortDirections() throws IOException {
035            assertEquals(new Double(90.0),
036                         new Double(mShortRoute.getStartHeading()));
037    
038            assertEquals("Turn left onto Penny Lane and walk for 93 minutes.\n", 
039                         mDirections.computeDirections(mShortRoute, 180));
040            assertEquals("Turn right onto Penny Lane and walk for 93 minutes.\n", 
041                         mDirections.computeDirections(mShortRoute, 0));
042            
043            assertEquals("Turn slight left onto Penny Lane and walk for 93 minutes.\n", 
044                         mDirections.computeDirections(mShortRoute, 100));
045            assertEquals("Turn slight right onto Penny Lane and walk for 93 minutes.\n", 
046                         mDirections.computeDirections(mShortRoute, 80));
047    
048            assertEquals("Continue onto Penny Lane and walk for 93 minutes.\n", 
049                         mDirections.computeDirections(mShortRoute, 99));
050            assertEquals("Continue onto Penny Lane and walk for 93 minutes.\n", 
051                         mDirections.computeDirections(mShortRoute, 81));
052    
053            assertEquals("Turn left onto Penny Lane and walk for 93 minutes.\n",
054                         mDirections.computeDirections(mShortRoute, 181));
055            assertEquals("Turn right onto Penny Lane and walk for 93 minutes.\n", 
056                         mDirections.computeDirections(mShortRoute, 359));
057    
058            assertEquals("Turn sharp right onto Penny Lane and walk for 93 minutes.\n",
059                         mDirections.computeDirections(mShortRoute, 300));
060            assertEquals("Turn sharp left onto Penny Lane and walk for 93 minutes.\n", 
061                         mDirections.computeDirections(mShortRoute, 231));
062    
063            assertEquals("U-turn onto Penny Lane and walk for 93 minutes.\n",
064                         mDirections.computeDirections(mShortRoute, 269));
065            assertEquals("U-turn onto Penny Lane and walk for 93 minutes.\n",
066                         mDirections.computeDirections(mShortRoute, 271));
067        }
068    
069      
070        /**
071         * Test turning directions, with different origHeadings.
072         **/
073        public void testTurning() {
074        
075            // Left turn
076            mShortRoute = new Route(new GeoSegment(mName,
077                                                   new GeoPoint(0,0),
078                                                   new GeoPoint(oneUnit,0)));
079            assertEquals("Turn left onto Penny Lane and walk for 138 minutes.\n",
080                         mDirections.computeDirections(mShortRoute, 90));
081        
082            // Right turn
083            mShortRoute = new Route(new GeoSegment(mName,
084                                                   new GeoPoint(0,0),
085                                                   new GeoPoint(-oneUnit,0)));
086            assertEquals("Turn right onto Penny Lane and walk for 138 minutes.\n",
087                         mDirections.computeDirections(mShortRoute, 90));
088    
089            // U-turn
090            mShortRoute = new Route(new GeoSegment(mName,
091                                                   new GeoPoint(0,0),
092                                                   new GeoPoint(0,-oneUnit)));
093            assertEquals("U-turn onto Penny Lane and walk for 93 minutes.\n",
094                         mDirections.computeDirections(mShortRoute, 90));
095        
096            // Continue
097            mShortRoute = new Route(new GeoSegment(mName,
098                                                   new GeoPoint(0,0),
099                                                   new GeoPoint(0,oneUnit)));
100            assertEquals("Continue onto Penny Lane and walk for 93 minutes.\n",
101                         mDirections.computeDirections(mShortRoute, 90));
102        
103            // Slight left (13.74 degrees)
104            mShortRoute = new Route(new GeoSegment(mName,
105                                                   new GeoPoint(0,0),
106                                                   new GeoPoint(-27988,10134)));
107            assertEquals("Turn slight left onto Penny Lane and walk for 40 minutes.\n",
108                         mDirections.computeDirections(mShortRoute, 180));
109    
110            // Slight right (13.74 degrees)
111            mShortRoute = new Route(new GeoSegment(mName,
112                                                   new GeoPoint(0,0),
113                                                   new GeoPoint(-27988,-10134)));
114            assertEquals("Turn slight right onto Penny Lane and walk for 40 minutes.\n",
115                         mDirections.computeDirections(mShortRoute, 180));
116    
117            // Sharp left (166.26 degrees)
118            mShortRoute = new Route(new GeoSegment(mName,
119                                                   new GeoPoint(0,0),
120                                                   new GeoPoint(27988,10134)));
121            assertEquals("Turn sharp left onto Penny Lane and walk for 40 minutes.\n",
122                         mDirections.computeDirections(mShortRoute, 180));
123    
124            // Sharp right (166.26 degrees)
125            mShortRoute = new Route(new GeoSegment(mName,
126                                                   new GeoPoint(0,0),
127                                                   new GeoPoint(27988,-10134)));
128            assertEquals("Turn sharp right onto Penny Lane and walk for 40 minutes.\n",
129                         mDirections.computeDirections(mShortRoute, 180));
130    
131            // U-turn (on the left side, 179.5 degree)
132            mShortRoute = new Route(new GeoSegment(mName,
133                                                   new GeoPoint(0,0),
134                                                   new GeoPoint(-724368,8542)));
135            assertEquals("U-turn onto Penny Lane and walk for 1000 minutes.\n",
136                         mDirections.computeDirections(mShortRoute, 0));
137    
138            // U-turn (on the right side, 179.5 degree)
139            mShortRoute = new Route(new GeoSegment(mName,
140                                                   new GeoPoint(0,0),
141                                                   new GeoPoint(-724368,-8542)));
142            assertEquals("U-turn onto Penny Lane and walk for 1000 minutes.\n",
143                         mDirections.computeDirections(mShortRoute, 0));
144        }
145    
146    
147        /**
148         * Test rounding distance, specifically if it rounds up to 0.1 and
149         * rounds down to 0.0.  Should compute time before rounding.
150         **/
151        public void testDistance() {    
152            // 0.08 miles
153            mShortRoute = new Route(new GeoSegment(mName,
154                                                   new GeoPoint(0,0),
155                                                   new GeoPoint(0,1718)));
156            assertEquals("Continue onto Penny Lane and walk for 2 minutes.\n",
157                         mDirections.computeDirections(mShortRoute, 90));
158        
159            // 0.02 miles
160            mShortRoute = new Route(new GeoSegment(mName,
161                                                   new GeoPoint(0,0),
162                                                   new GeoPoint(0,392)));
163            assertEquals("Continue onto Penny Lane and walk for 0 minutes.\n",
164                         mDirections.computeDirections(mShortRoute, 90));
165        }
166    
167    
168        /**
169         * Two step route with one geo feature.
170         **/
171        public void testRepeatedSegment() {
172            Route route = new Route(new GeoSegment(mName,
173                                                   new GeoPoint(0,0),
174                                                   new GeoPoint(0,oneUnit)));
175            route = route.addSegment(new GeoSegment(mName,
176                                                    new GeoPoint(0,oneUnit),
177                                                    new GeoPoint(0,oneUnit*2)));
178    
179            assertEquals("Turn left onto Penny Lane and walk for 186 minutes.\n",
180                         mDirections.computeDirections(route, 180));
181        }
182    
183      
184        /**
185         * Long route with no repeats.
186         **/ 
187        public void testLongRoute() {
188            Route route = new Route(new GeoSegment("Penny Lane",
189                                                   new GeoPoint(0,0),
190                                                   new GeoPoint(0,oneUnit*2)));
191            route = route.addSegment(new GeoSegment("Abby Road",
192                                                    new GeoPoint(0,oneUnit*2),
193                                                    new GeoPoint(oneUnit*2,oneUnit*2)));
194            route = route.addSegment(new GeoSegment("Strawberry Fields",
195                                                    new GeoPoint(oneUnit*2,oneUnit*2),
196                                                    new GeoPoint(oneUnit*4,oneUnit*2)));
197            route = route.addSegment(new GeoSegment("Octopus's Garden",
198                                                    new GeoPoint(oneUnit*4,oneUnit*2),
199                                                    new GeoPoint(oneUnit*4,oneUnit*4)));
200    
201            route = route.addSegment(new GeoSegment("Norwegian Wood",
202                                                    new GeoPoint(oneUnit*4,oneUnit*4),
203                                                    new GeoPoint(oneUnit*10,oneUnit*10)));
204    
205            route = route.addSegment(new GeoSegment("Yellow Submarine",
206                                                    new GeoPoint(oneUnit*10,oneUnit*10),
207                                                    new GeoPoint(0,0)));
208            String directions =
209                "Turn left onto Penny Lane and walk for 186 minutes." + "\n" + 
210                "Turn left onto Abby Road and walk for 276 minutes." + "\n" + 
211                "Continue onto Strawberry Fields and walk for 276 minutes." + "\n" + 
212                "Turn right onto Octopus's Garden and walk for 186 minutes." + "\n" + 
213                "Turn slight left onto Norwegian Wood and walk for 999 minutes." + "\n" + 
214                "U-turn onto Yellow Submarine and walk for 1666 minutes." + "\n"
215                ;
216            assertEquals(directions,
217                         mDirections.computeDirections(route, 180));
218        }
219      
220    
221        /**
222         * Just like long route, but different makeup of geosegements.
223         **/
224        public void testRepeatedRoute() {
225            Route route = new Route(new GeoSegment("Penny Lane",
226                                                   new GeoPoint(0,0),
227                                                   new GeoPoint(0,oneUnit*2)));
228            route = route.addSegment(new GeoSegment("Abby Road",
229                                                    new GeoPoint(0,oneUnit*2),
230                                                    new GeoPoint(oneUnit,oneUnit*2)));
231            route = route.addSegment(new GeoSegment("Abby Road",
232                                                    new GeoPoint(oneUnit,oneUnit*2),
233                                                    new GeoPoint(oneUnit*2,oneUnit*2)));
234            route = route.addSegment(new GeoSegment("Strawberry Fields",
235                                                    new GeoPoint(oneUnit*2,oneUnit*2),
236                                                    new GeoPoint(oneUnit*3,oneUnit*2)));
237            route = route.addSegment(new GeoSegment("Strawberry Fields",
238                                                    new GeoPoint(oneUnit*3,oneUnit*2),
239                                                    new GeoPoint(oneUnit*4,oneUnit*2)));
240            route = route.addSegment(new GeoSegment("Octopus's Garden",
241                                                    new GeoPoint(oneUnit*4,oneUnit*2),
242                                                    new GeoPoint(oneUnit*3,oneUnit*3)));
243            route = route.addSegment(new GeoSegment("Octopus's Garden",
244                                                    new GeoPoint(oneUnit*3,oneUnit*3),
245                                                    new GeoPoint(oneUnit*4,oneUnit*3)));
246            route = route.addSegment(new GeoSegment("Octopus's Garden",
247                                                    new GeoPoint(oneUnit*4,oneUnit*3),
248                                                    new GeoPoint(oneUnit*4,oneUnit*4)));
249            route = route.addSegment(new GeoSegment("Norwegian Wood",
250                                                    new GeoPoint(oneUnit*4,oneUnit*4),
251                                                    new GeoPoint(oneUnit*10,oneUnit*10)));
252            route = route.addSegment(new GeoSegment("Yellow Submarine",
253                                                    new GeoPoint(oneUnit*10,oneUnit*10),
254                                                    new GeoPoint(0,0)));
255        
256            String directions =
257                "Turn left onto Penny Lane and walk for 186 minutes." + "\n" +
258                "Turn left onto Abby Road and walk for 276 minutes." + "\n" +
259                "Continue onto Strawberry Fields and walk for 276 minutes." + "\n" +
260                "Turn sharp right onto Octopus's Garden and walk for 398 minutes." + "\n" +
261                "Turn slight left onto Norwegian Wood and walk for 999 minutes." + "\n" +
262                "U-turn onto Yellow Submarine and walk for 1666 minutes." + "\n"
263                ;
264          
265            assertEquals(directions,
266                         mDirections.computeDirections(route, 180));
267    
268        }
269    }
270