4. Unit Tests

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

4.1. Before Unit Testing

This lab focuses on writing unit tests for the Course and CourseList classes described in the Objects Lab and Collections Lab respectively. These classes are to be completed before writing the unit tests. If these classes are not complete, complete them before continuing.

4.2. Unit Testing

It is always important to ensure code correctness. For this reason, unit tests are written as they help programmers test their code to demonstrate correctness in a systematic way. While working on this lab, it is strongly recommended to refer to the relevant topic on unit testing.

4.2.1. Testing the Course Class

Note

Give the test methods descriptive names while still being brief. The recommended convention to follow is method_condition_expected(), for example:

  • getFirstName_generalCase_returnsFirstName

  • size_empty_returnsZero()

  • indexOf_singleton_returnsCorrectIndex()

  1. Create the test class for Course called CourseTest

  2. Write a unit test for getProgramCode

  3. Write a unit test for getCourseCode

  4. Write a unit test for getCourseTitle

  5. Write a unit test for toString

  6. Write a unit test for equals

    • Do not write tests for hashCode

4.2.2. Testing the CourseList Class

Warning

Writing unit tests for collections is quite difficult for a variety of reasons. For example, consider that collections can have various different states — empty, single element, many elements, duplicate elements.

Consider remove:

  • What should happen when remove is called on an empty CourseList?

  • What should happen when remove is called on a CourseList with one element?

  • What should happen when calling it on a CourseList with many elements?

  • What should happen when trying to remove an element that does not exist in the CourseList?

  • What should happen when there are multiple equal Course objects to be removed?

Try to test each method as thoroughly as possible.

  1. Write unit tests for add

  2. Write unit tests for contains

  3. Write unit tests for indexOf

  4. Write unit tests for remove

  5. Write unit tests for get

  6. Write unit tests for size

  7. Write unit tests for toString

  8. Try to write unit tests for equals

    • Testing equality on collections is very challenging

4.3. Kattis Problems

  1. https://open.kattis.com/problems/quadrant

  2. https://open.kattis.com/problems/judgingmoose

  3. https://open.kattis.com/problems/timeloop

  4. https://open.kattis.com/problems/oddities

  5. https://open.kattis.com/problems/fizzbuzz

  6. https://open.kattis.com/problems/twostones

  7. https://open.kattis.com/problems/spavanac

  8. https://open.kattis.com/problems/cetvrta

  9. https://open.kattis.com/problems/bus

  10. https://open.kattis.com/problems/timeloop

  11. https://open.kattis.com/problems/oddities

  12. https://open.kattis.com/problems/fizzbuzz

  13. https://open.kattis.com/problems/sibice

  14. https://open.kattis.com/problems/datum

  15. https://open.kattis.com/problems/dicecup

  16. https://open.kattis.com/problems/autori

  17. https://open.kattis.com/problems/apaxiaaans

  18. https://open.kattis.com/problems/hissingmicrophone

  19. https://open.kattis.com/problems/trik

  20. https://open.kattis.com/problems/pot

  21. https://open.kattis.com/problems/filip

  22. https://open.kattis.com/problems/reversebinary

  23. https://open.kattis.com/problems/sevenwonders

  24. https://open.kattis.com/problems/zamka

  25. https://open.kattis.com/problems/bijele

  26. https://open.kattis.com/problems/cold

  27. https://open.kattis.com/problems/nastyhacks

  28. https://open.kattis.com/problems/grassseed

  29. https://open.kattis.com/problems/pet

  30. https://open.kattis.com/problems/batterup

  31. https://open.kattis.com/problems/aboveaverage

  32. https://open.kattis.com/problems/icpcawards

  33. https://open.kattis.com/problems/quickbrownfox

  34. https://open.kattis.com/problems/nodup

  35. https://open.kattis.com/problems/conundrum

  36. https://open.kattis.com/problems/bela

  37. https://open.kattis.com/problems/kornislav