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