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 DrivingRouteFormatter class. 012 **/ 013 public class DrivingRouteFormatterTest extends TestCase { 014 015 private DrivingRouteFormatter 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 DrivingRouteFormatter(); 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 go 4.7 miles.\n", 039 mDirections.computeDirections(mShortRoute, 180)); 040 assertEquals("Turn right onto Penny Lane and go 4.7 miles.\n", 041 mDirections.computeDirections(mShortRoute, 0)); 042 043 assertEquals("Turn slight left onto Penny Lane and go 4.7 miles.\n", 044 mDirections.computeDirections(mShortRoute, 100)); 045 assertEquals("Turn slight right onto Penny Lane and go 4.7 miles.\n", 046 mDirections.computeDirections(mShortRoute, 80)); 047 048 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 049 mDirections.computeDirections(mShortRoute, 99)); 050 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 051 mDirections.computeDirections(mShortRoute, 81)); 052 053 assertEquals("Turn sharp right onto Penny Lane and go 4.7 miles.\n", 054 mDirections.computeDirections(mShortRoute, 330)); 055 assertEquals("Turn sharp left onto Penny Lane and go 4.7 miles.\n", 056 mDirections.computeDirections(mShortRoute, 231)); 057 058 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 059 mDirections.computeDirections(mShortRoute, 271)); 060 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 061 mDirections.computeDirections(mShortRoute, 269)); 062 } 063 064 065 /** 066 * Test turning directions with different origHeadings. 067 **/ 068 public void testTurning() { 069 070 // Left turn 071 mShortRoute = new Route(new GeoSegment(mName, 072 new GeoPoint(0,0), 073 new GeoPoint(oneUnit,0))); 074 assertEquals("Turn left onto Penny Lane and go 6.9 miles.\n", 075 mDirections.computeDirections(mShortRoute, 90)); 076 077 // Right turn 078 mShortRoute = new Route(new GeoSegment(mName, 079 new GeoPoint(0,0), 080 new GeoPoint(-oneUnit,0))); 081 assertEquals("Turn right onto Penny Lane and go 6.9 miles.\n", 082 mDirections.computeDirections(mShortRoute, 90)); 083 084 // U-turn 085 mShortRoute = new Route(new GeoSegment(mName, 086 new GeoPoint(0,0), 087 new GeoPoint(0,-oneUnit))); 088 assertEquals("U-turn onto Penny Lane and go 4.7 miles.\n", 089 mDirections.computeDirections(mShortRoute, 90)); 090 091 // Continue 092 mShortRoute = new Route(new GeoSegment(mName, 093 new GeoPoint(0,0), 094 new GeoPoint(0,oneUnit))); 095 assertEquals("Continue onto Penny Lane and go 4.7 miles.\n", 096 mDirections.computeDirections(mShortRoute, 90)); 097 098 // Slight left (15 degrees) 099 mShortRoute = new Route(new GeoSegment(mName, 100 new GeoPoint(0,0), 101 new GeoPoint(-oneUnit,39510))); 102 assertEquals("Turn slight left onto Penny Lane and go 7.1 miles.\n", 103 mDirections.computeDirections(mShortRoute, 180)); 104 105 // Slight right (15 degrees) 106 mShortRoute = new Route(new GeoSegment(mName, 107 new GeoPoint(0,0), 108 new GeoPoint(-oneUnit,-39510))); 109 assertEquals("Turn slight right onto Penny Lane and go 7.1 miles.\n", 110 mDirections.computeDirections(mShortRoute, 180)); 111 112 // Sharp left (165 degrees) 113 mShortRoute = new Route(new GeoSegment(mName, 114 new GeoPoint(0,0), 115 new GeoPoint(oneUnit,39510))); 116 assertEquals("Turn sharp left onto Penny Lane and go 7.1 miles.\n", 117 mDirections.computeDirections(mShortRoute, 180)); 118 119 // Sharp right (165 degrees) 120 mShortRoute = new Route(new GeoSegment(mName, 121 new GeoPoint(0,0), 122 new GeoPoint(oneUnit,-39510))); 123 assertEquals("Turn sharp right onto Penny Lane and go 7.1 miles.\n", 124 mDirections.computeDirections(mShortRoute, 180)); 125 126 // U-turn (on the right side, 179.5 degree) 127 mShortRoute = new Route(new GeoSegment(mName, 128 new GeoPoint(0,0), 129 new GeoPoint(-oneUnit,1293))); 130 assertEquals("U-turn onto Penny Lane and go 6.9 miles.\n", 131 mDirections.computeDirections(mShortRoute, 0)); 132 133 // U-turn (on the left side, 179.5 degree) 134 mShortRoute = new Route(new GeoSegment(mName, 135 new GeoPoint(0,0), 136 new GeoPoint(-oneUnit,-1293))); 137 assertEquals("U-turn onto Penny Lane and go 6.9 miles.\n", 138 mDirections.computeDirections(mShortRoute, 0)); 139 } 140 141 142 /** 143 * Test rounding distance, especially if rounded up to 0.1 and 144 * rounded down to 0.0. Should compute time before rounding. 145 **/ 146 public void testDistance() { 147 148 // 0.08 miles 149 mShortRoute = new Route(new GeoSegment(mName, 150 new GeoPoint(0,0), 151 new GeoPoint(0,1566))); 152 assertEquals("Continue onto Penny Lane and go 0.1 miles.\n", 153 mDirections.computeDirections(mShortRoute, 90)); 154 155 // 0.02 miles 156 mShortRoute = new Route(new GeoSegment(mName, 157 new GeoPoint(0,0), 158 new GeoPoint(0,392))); 159 assertEquals("Continue onto Penny Lane and go 0.0 miles.\n", 160 mDirections.computeDirections(mShortRoute, 90)); 161 } 162 163 164 /** 165 * Two step route with one geo feature. 166 **/ 167 public void testRepeatedSegment() { 168 Route route = new Route(new GeoSegment(mName, 169 new GeoPoint(0,0), 170 new GeoPoint(0,oneUnit))); 171 route = route.addSegment(new GeoSegment(mName, 172 new GeoPoint(0,oneUnit), 173 new GeoPoint(0,oneUnit*2))); 174 175 assertEquals("Turn left onto Penny Lane and go 9.3 miles.\n", 176 mDirections.computeDirections(route, 180)); 177 } 178 179 180 /** 181 * Long route with no repeats. 182 **/ 183 public void testLongRoute() { 184 Route route = new Route(new GeoSegment("Penny Lane", 185 new GeoPoint(0,0), 186 new GeoPoint(0,oneUnit*2))); 187 route = route.addSegment(new GeoSegment("Abby Road", 188 new GeoPoint(0,oneUnit*2), 189 new GeoPoint(oneUnit*2,oneUnit*2))); 190 191 route = route.addSegment(new GeoSegment("Strawberry Fields", 192 new GeoPoint(oneUnit*2,oneUnit*2), 193 new GeoPoint(oneUnit*4,oneUnit*2))); 194 195 route = route.addSegment(new GeoSegment("Octopus's Garden", 196 new GeoPoint(oneUnit*4,oneUnit*2), 197 new GeoPoint(oneUnit*4,oneUnit*4))); 198 199 route = route.addSegment(new GeoSegment("Norwegian Wood", 200 new GeoPoint(oneUnit*4,oneUnit*4), 201 new GeoPoint(oneUnit*10,oneUnit*10))); 202 203 route = route.addSegment(new GeoSegment("Yellow Submarine", 204 new GeoPoint(oneUnit*10,oneUnit*10), 205 new GeoPoint(0,0))); 206 207 String directions = 208 "Turn left onto Penny Lane and go 9.3 miles." + "\n" + 209 "Turn left onto Abby Road and go 13.8 miles." + "\n" + 210 "Continue onto Strawberry Fields and go 13.8 miles." + "\n" + 211 "Turn right onto Octopus's Garden and go 9.3 miles." + "\n" + 212 "Turn slight left onto Norwegian Wood and go 50.0 miles." + "\n" + 213 "U-turn onto Yellow Submarine and go 83.3 miles." + "\n" 214 ; 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 229 route = route.addSegment(new GeoSegment("Abby Road", 230 new GeoPoint(0,oneUnit*2), 231 new GeoPoint(oneUnit,oneUnit*2))); 232 route = route.addSegment(new GeoSegment("Abby Road", 233 new GeoPoint(oneUnit,oneUnit*2), 234 new GeoPoint(oneUnit*2,oneUnit*2))); 235 route = route.addSegment(new GeoSegment("Strawberry Fields", 236 new GeoPoint(oneUnit*2,oneUnit*2), 237 new GeoPoint(oneUnit*3,oneUnit*2))); 238 route = route.addSegment(new GeoSegment("Strawberry Fields", 239 new GeoPoint(oneUnit*3,oneUnit*2), 240 new GeoPoint(oneUnit*4,oneUnit*2))); 241 route = route.addSegment(new GeoSegment("Octopus's Garden", 242 new GeoPoint(oneUnit*4,oneUnit*2), 243 new GeoPoint(oneUnit*3,oneUnit*3))); 244 route = route.addSegment(new GeoSegment("Octopus's Garden", 245 new GeoPoint(oneUnit*3,oneUnit*3), 246 new GeoPoint(oneUnit*4,oneUnit*3))); 247 route = route.addSegment(new GeoSegment("Octopus's Garden", 248 new GeoPoint(oneUnit*4,oneUnit*3), 249 new GeoPoint(oneUnit*4,oneUnit*4))); 250 route = route.addSegment(new GeoSegment("Norwegian Wood", 251 new GeoPoint(oneUnit*4,oneUnit*4), 252 new GeoPoint(oneUnit*10,oneUnit*10))); 253 route = route.addSegment(new GeoSegment("Yellow Submarine", 254 new GeoPoint(oneUnit*10,oneUnit*10), 255 new GeoPoint(0,0))); 256 257 String directions = 258 "Turn left onto Penny Lane and go 9.3 miles." + "\n" + 259 "Turn left onto Abby Road and go 13.8 miles." + "\n" + 260 "Continue onto Strawberry Fields and go 13.8 miles." + "\n" + 261 "Turn sharp right onto Octopus's Garden and go 19.9 miles."+ "\n" + 262 "Turn slight left onto Norwegian Wood and go 50.0 miles." + "\n" + 263 "U-turn onto Yellow Submarine and go 83.3 miles." + "\n" 264 ; 265 266 assertEquals(directions, 267 mDirections.computeDirections(route, 180)); 268 } 269 }