001 package ps2.test; 002 003 import ps2.*; 004 005 import org.junit.Test; 006 import org.junit.Before; 007 import static org.junit.Assert.assertTrue; 008 import static org.junit.Assert.assertFalse; 009 import static org.junit.Assert.assertEquals; 010 011 import java.io.IOException; 012 013 014 /** 015 * Unit tests for the DrivingRouteFormatter class. 016 **/ 017 public class DrivingRouteFormatterTest { 018 019 private DrivingRouteFormatter mDirections; 020 private Route mShortRoute; 021 022 // Some constants for easy reference 023 private String mName = "Penny Lane"; 024 private int oneUnit = 100000; 025 026 027 // JUnit calls @Before before each @Test. 028 @Before 029 public void setupBeforeTests() { 030 mDirections = new DrivingRouteFormatter(); 031 mShortRoute = new Route(new GeoSegment(mName, 032 new GeoPoint(0,0), 033 new GeoPoint(0,100000))); 034 } 035 036 037 /** 038 * Test simple directions with one distance and one origHeading. 039 **/ 040 @Test 041 public void testShortDirections() throws IOException { 042 assertEquals(new Double(90.0), 043 new Double(mShortRoute.getStartHeading())); 044 045 assertEquals("Turn left onto Penny Lane and go 4.7 miles.\n", 046 mDirections.computeDirections(mShortRoute, 180)); 047 assertEquals("Turn right onto Penny Lane and go 4.7 miles.\n", 048 mDirections.computeDirections(mShortRoute, 0)); 049 050 assertEquals("Turn slight left onto Penny Lane and go 4.7 miles.\n", 051 mDirections.computeDirections(mShortRoute, 100)); 052 assertEquals("Turn slight right onto Penny Lane and go 4.7 miles.\n", 053 mDirections.computeDirections(mShortRoute, 80)); 054 055 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 056 mDirections.computeDirections(mShortRoute, 99)); 057 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 058 mDirections.computeDirections(mShortRoute, 81)); 059 060 assertEquals("Turn sharp right onto Penny Lane and go 4.7 miles.\n", 061 mDirections.computeDirections(mShortRoute, 330)); 062 assertEquals("Turn sharp left onto Penny Lane and go 4.7 miles.\n", 063 mDirections.computeDirections(mShortRoute, 231)); 064 065 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 066 mDirections.computeDirections(mShortRoute, 271)); 067 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 068 mDirections.computeDirections(mShortRoute, 269)); 069 } 070 071 072 /** 073 * Test turning directions with different origHeadings. 074 **/ 075 @Test 076 public void testTurning() { 077 078 // Left turn 079 mShortRoute = new Route(new GeoSegment(mName, 080 new GeoPoint(0,0), 081 new GeoPoint(oneUnit,0))); 082 assertEquals("Turn left onto Penny Lane and go 6.9 miles.\n", 083 mDirections.computeDirections(mShortRoute, 90)); 084 085 // Right turn 086 mShortRoute = new Route(new GeoSegment(mName, 087 new GeoPoint(0,0), 088 new GeoPoint(-oneUnit,0))); 089 assertEquals("Turn right onto Penny Lane and go 6.9 miles.\n", 090 mDirections.computeDirections(mShortRoute, 90)); 091 092 // U-turn 093 mShortRoute = new Route(new GeoSegment(mName, 094 new GeoPoint(0,0), 095 new GeoPoint(0,-oneUnit))); 096 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 097 mDirections.computeDirections(mShortRoute, 90)); 098 099 // Continue 100 mShortRoute = new Route(new GeoSegment(mName, 101 new GeoPoint(0,0), 102 new GeoPoint(0,oneUnit))); 103 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 104 mDirections.computeDirections(mShortRoute, 90)); 105 106 // Slight left (15 degrees) 107 mShortRoute = new Route(new GeoSegment(mName, 108 new GeoPoint(0,0), 109 new GeoPoint(-oneUnit,39510))); 110 assertEquals("Turn slight left onto Penny Lane and go 7.1 miles.\n", 111 mDirections.computeDirections(mShortRoute, 180)); 112 113 // Slight right (15 degrees) 114 mShortRoute = new Route(new GeoSegment(mName, 115 new GeoPoint(0,0), 116 new GeoPoint(-oneUnit,-39510))); 117 assertEquals("Turn slight right onto Penny Lane and go 7.1 miles.\n", 118 mDirections.computeDirections(mShortRoute, 180)); 119 120 // Sharp left (165 degrees) 121 mShortRoute = new Route(new GeoSegment(mName, 122 new GeoPoint(0,0), 123 new GeoPoint(oneUnit,39510))); 124 assertEquals("Turn sharp left onto Penny Lane and go 7.1 miles.\n", 125 mDirections.computeDirections(mShortRoute, 180)); 126 127 // Sharp right (165 degrees) 128 mShortRoute = new Route(new GeoSegment(mName, 129 new GeoPoint(0,0), 130 new GeoPoint(oneUnit,-39510))); 131 assertEquals("Turn sharp right onto Penny Lane and go 7.1 miles.\n", 132 mDirections.computeDirections(mShortRoute, 180)); 133 134 // U-turn (on the right side, 179.5 degree) 135 mShortRoute = new Route(new GeoSegment(mName, 136 new GeoPoint(0,0), 137 new GeoPoint(-oneUnit,1293))); 138 assertEquals("U-turn onto Penny Lane and go 6.9 miles.\n", 139 mDirections.computeDirections(mShortRoute, 0)); 140 141 // U-turn (on the left side, 179.5 degree) 142 mShortRoute = new Route(new GeoSegment(mName, 143 new GeoPoint(0,0), 144 new GeoPoint(-oneUnit,-1293))); 145 assertEquals("U-turn onto Penny Lane and go 6.9 miles.\n", 146 mDirections.computeDirections(mShortRoute, 0)); 147 } 148 149 150 /** 151 * Test rounding distance, especially if rounded up to 0.1 and 152 * rounded down to 0.0. Should compute time before rounding. 153 **/ 154 @Test 155 public void testDistance() { 156 157 // 0.08 miles 158 mShortRoute = new Route(new GeoSegment(mName, 159 new GeoPoint(0,0), 160 new GeoPoint(0,1566))); 161 assertEquals("Continue onto Penny Lane and go 0.1 miles.\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 go 0.0 miles.\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 go 9.3 miles.\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 202 route = route.addSegment(new GeoSegment("Strawberry Fields", 203 new GeoPoint(oneUnit*2,oneUnit*2), 204 new GeoPoint(oneUnit*4,oneUnit*2))); 205 206 route = route.addSegment(new GeoSegment("Octopus's Garden", 207 new GeoPoint(oneUnit*4,oneUnit*2), 208 new GeoPoint(oneUnit*4,oneUnit*4))); 209 210 route = route.addSegment(new GeoSegment("Norwegian Wood", 211 new GeoPoint(oneUnit*4,oneUnit*4), 212 new GeoPoint(oneUnit*10,oneUnit*10))); 213 214 route = route.addSegment(new GeoSegment("Yellow Submarine", 215 new GeoPoint(oneUnit*10,oneUnit*10), 216 new GeoPoint(0,0))); 217 218 String directions = 219 "Turn left onto Penny Lane and go 9.3 miles." + "\n" + 220 "Turn left onto Abby Road and go 13.8 miles." + "\n" + 221 "Continue onto Strawberry Fields and go 13.8 miles." + "\n" + 222 "Turn right onto Octopus's Garden and go 9.3 miles." + "\n" + 223 "Turn slight left onto Norwegian Wood and go 50.0 miles." + "\n" + 224 "U-turn onto Yellow Submarine and go 83.3 miles." + "\n" 225 ; 226 227 assertEquals(directions, 228 mDirections.computeDirections(route, 180)); 229 } 230 231 232 /** 233 * Just like long route, but different makeup of geosegements. 234 **/ 235 @Test 236 public void testRepeatedRoute() { 237 Route route = new Route(new GeoSegment("Penny Lane", 238 new GeoPoint(0,0), 239 new GeoPoint(0,oneUnit*2))); 240 241 route = route.addSegment(new GeoSegment("Abby Road", 242 new GeoPoint(0,oneUnit*2), 243 new GeoPoint(oneUnit,oneUnit*2))); 244 route = route.addSegment(new GeoSegment("Abby Road", 245 new GeoPoint(oneUnit,oneUnit*2), 246 new GeoPoint(oneUnit*2,oneUnit*2))); 247 route = route.addSegment(new GeoSegment("Strawberry Fields", 248 new GeoPoint(oneUnit*2,oneUnit*2), 249 new GeoPoint(oneUnit*3,oneUnit*2))); 250 route = route.addSegment(new GeoSegment("Strawberry Fields", 251 new GeoPoint(oneUnit*3,oneUnit*2), 252 new GeoPoint(oneUnit*4,oneUnit*2))); 253 route = route.addSegment(new GeoSegment("Octopus's Garden", 254 new GeoPoint(oneUnit*4,oneUnit*2), 255 new GeoPoint(oneUnit*3,oneUnit*3))); 256 route = route.addSegment(new GeoSegment("Octopus's Garden", 257 new GeoPoint(oneUnit*3,oneUnit*3), 258 new GeoPoint(oneUnit*4,oneUnit*3))); 259 route = route.addSegment(new GeoSegment("Octopus's Garden", 260 new GeoPoint(oneUnit*4,oneUnit*3), 261 new GeoPoint(oneUnit*4,oneUnit*4))); 262 route = route.addSegment(new GeoSegment("Norwegian Wood", 263 new GeoPoint(oneUnit*4,oneUnit*4), 264 new GeoPoint(oneUnit*10,oneUnit*10))); 265 route = route.addSegment(new GeoSegment("Yellow Submarine", 266 new GeoPoint(oneUnit*10,oneUnit*10), 267 new GeoPoint(0,0))); 268 269 String directions = 270 "Turn left onto Penny Lane and go 9.3 miles." + "\n" + 271 "Turn left onto Abby Road and go 13.8 miles." + "\n" + 272 "Continue onto Strawberry Fields and go 13.8 miles." + "\n" + 273 "Turn sharp right onto Octopus's Garden and go 19.9 miles."+ "\n" + 274 "Turn slight left onto Norwegian Wood and go 50.0 miles." + "\n" + 275 "U-turn onto Yellow Submarine and go 83.3 miles." + "\n" 276 ; 277 278 assertEquals(directions, 279 mDirections.computeDirections(route, 180)); 280 } 281 }