292 public static void main (String args[]) { 293 Trace.drawStepsOfMethod ("main"); 294 Trace.run (); 295 296 // Here are some tests to get you started. 297 // You can edit this all you like. 298 YDeque d1, d2, d3; 299 double k; 300 301 //////////////////////////////////////////////////////////////////// 302 // push/pop tests 303 //////////////////////////////////////////////////////////////////// 304 d1 = new YDeque (); 305 d1.pushLeft (11); 306 check ("left", d1, "[ 11 ]"); 307 d1.pushLeft (12); 308 check ("left", d1, "[ 12 11 ]"); 309 d1.pushLeft (13); 310 check ("left", d1, "[ 13 12 11 ]"); 311 k = d1.popLeft (); 312 check ("left", d1, "[ 12 11 ]", k, 13); 313 k = d1.popLeft (); 314 check ("left", d1, "[ 11 ]", k, 12); 315 k = d1.popLeft (); 316 check ("left", d1, "[ ]", k, 11); 317 318 d1 = new YDeque (); 319 d1.pushRight (11); 320 check ("right", d1, "[ 11 ]"); 321 d1.pushRight (12); 322 check ("right", d1, "[ 11 12 ]"); 323 d1.pushRight (13); 324 check ("right", d1, "[ 11 12 13 ]"); 325 k = d1.popRight (); 326 check ("right", d1, "[ 11 12 ]", k, 13); 327 k = d1.popRight (); 328 check ("right", d1, "[ 11 ]", k, 12); 329 k = d1.popRight (); 330 check ("right", d1, "[ ]", k, 11); 331 332 d1 = new YDeque (); 333 d1.pushLeft (11); 334 check ("left/right", d1, "[ 11 ]"); 335 d1.pushRight (21); 336 check ("left/right", d1, "[ 11 21 ]"); 337 d1.pushLeft (12); 338 check ("left/right", d1, "[ 12 11 21 ]"); 339 d1.pushRight (22); 340 check ("left/right", d1, "[ 12 11 21 22 ]"); 341 k = d1.popLeft (); 342 check ("left/right", d1, "[ 11 21 22 ]", k, 12); 343 k = d1.popLeft (); 344 check ("left/right", d1, "[ 21 22 ]", k, 11); 345 k = d1.popLeft (); 346 check ("left/right", d1, "[ 22 ]", k, 21); 347 k = d1.popLeft (); 348 check ("left/right", d1, "[ ]", k, 22); 349 350 d1 = new YDeque (); 351 d1.pushLeft (11); 352 check ("left/right", d1, "[ 11 ]"); 353 d1.pushRight (21); 354 check ("left/right", d1, "[ 11 21 ]"); 355 d1.pushLeft (12); 356 check ("left/right", d1, "[ 12 11 21 ]"); 357 d1.pushRight (22); 358 check ("left/right", d1, "[ 12 11 21 22 ]"); 359 k = d1.popRight (); 360 check ("left/right", d1, "[ 12 11 21 ]", k, 22); 361 k = d1.popRight (); 362 check ("left/right", d1, "[ 12 11 ]", k, 21); 363 k = d1.popRight (); 364 check ("left/right", d1, "[ 12 ]", k, 11); 365 k = d1.popRight (); 366 check ("left/right", d1, "[ ]", k, 12); 367 368 //////////////////////////////////////////////////////////////////// 369 // test exceptions 370 //////////////////////////////////////////////////////////////////// 371 try { 372 d1.popLeft (); 373 showError ("Expected exception"); 374 } catch (NoSuchElementException e) {} 375 try { 376 d1.popRight (); 377 showError ("Expected exception"); 378 } catch (NoSuchElementException e) {} 379 380 //////////////////////////////////////////////////////////////////// 381 // concat tests (and more push/pop tests) 382 //////////////////////////////////////////////////////////////////// 383 d1 = new YDeque (); 384 d1.concat (new YDeque ()); 385 check ("concat", d1, "[ ]"); 386 d1.pushLeft (11); 387 d1.concat (new YDeque ()); 388 check ("concat", d1, "[ 11 ]"); 389 390 d1 = new YDeque (); 391 d2 = new YDeque (); 392 d2.pushLeft (11); 393 d1.concat (d2); 394 check ("concat", d1, "[ 11 ]"); 395 396 d1 = new YDeque (); 397 d2 = new YDeque (); 398 d1.pushLeft (11); 399 d1.pushLeft (12); 400 d2.pushLeft (21); 401 d2.pushLeft (22); 402 d1.concat (d2); 403 check ("concat", d1, "[ 12 11 22 21 ]"); 404 check ("concat", d2, "[ ]"); 405 406 d1 = new YDeque (); 407 for (int i = 10; i < 13; i++) { d1.pushLeft (i); checkInvariants ("left", d1); } 408 for (int i = 20; i < 23; i++) { d1.pushRight (i); checkInvariants ("right", d1); } 409 check ("concat", d1, "[ 12 11 10 20 21 22 ]"); 410 d2 = new YDeque (); 411 d1.concat (d2); 412 check ("concat", d1, "[ 12 11 10 20 21 22 ]"); 413 check ("concat", d2, "[ ]"); 414 415 for (int i = 30; i < 33; i++) { d2.pushLeft (i); checkInvariants ("left", d2); } 416 for (int i = 40; i < 43; i++) { d2.pushRight (i); checkInvariants ("right", d2); } 417 check ("concat", d2, "[ 32 31 30 40 41 42 ]"); 418 419 d3 = new YDeque (); 420 d2.concat (d3); 421 check ("concat", d2, "[ 32 31 30 40 41 42 ]"); 422 check ("concat", d3, "[ ]"); 423 424 d1.concat (d2); 425 check ("concat", d1, "[ 12 11 10 20 21 22 32 31 30 40 41 42 ]"); 426 check ("concat", d2, "[ ]"); 427 for (int i = 0; i < 12; i++) { d1.popLeft (); checkInvariants ("left", d1); } 428 //////////////////////////////////////////////////////////////////// 429 // delete tests 430 //////////////////////////////////////////////////////////////////// 431 d1 = new YDeque (); 432 d1.pushLeft (11); 433 k = d1.delete (0); 434 check ("delete", d1, "[ ]", k, 11); 435 for (int i = 10; i < 20; i++) { d1.pushRight (i); checkInvariants ("right", d1); } 436 k = d1.delete (0); 437 check ("delete", d1, "[ 11 12 13 14 15 16 17 18 19 ]", k, 10); 438 k = d1.delete (8); 439 check ("delete", d1, "[ 11 12 13 14 15 16 17 18 ]", k, 19); 440 k = d1.delete (4); 441 check ("delete", d1, "[ 11 12 13 14 16 17 18 ]", k, 15); 442 StdOut.println ("Finished tests"); 443 }