Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.68 KB

README.md

File metadata and controls

64 lines (47 loc) · 2.68 KB

Testing SumPrimes in Java

Reminder: If there's been a correction to this exercise posted, update your local copy via these instructions before proceeding.

Goal: Gain familiarity with a typical test framework

First, set up the local environment:

   cd TestSumPrimesJava
   source setup.sh

You'll find our sum-of-primes example in the file SumPrimes.java. It's been recast as a Java class. To compile and run it, hopefully printing the sum of primes in the 1st 7 integers, do:

   ./build
   java SumPrimes 7

(As we discussed in class, the original code doesn't work correctly.)

If you want to see the actual compilation commands used, look inside the "build" file. Note that this is just recompiling everything and leaving the resulting machine-readable files in the current directory. This is a fine approach in a simple project like this exercise; larger projects will use some form of "make" tool to do this smarter and more efficiently.

The file TestSumPrimes.java contains the code to setup JUnit, plus a single test. Take a look at it, and see if you understand what it contains. You can run it with

  java TestSumPrimes

In its initial form, the test just makes sure that you can create a SumPrimes object, and passes. That gives the "OK" response.

There are commented-out tests that will check specific test cases, and therefore fail in the manner we discussed in lecture. Remove the leading // that comments out those lines, and watch the test fail when you run it:

  (edit the TestSumPrimes.java file)
  ./build
  java TestSumPrimes

Isn't that "FAILURES!!!" message ugly? Now fix the bug(s) (See below for hints), and see the lovely "OK" again. Finally, add a couple more tests to check other ranges. (The sum of primes from 1 to 20 inclusive is 77, for example)

Hints:

  • It might be simpler to separate development and test of the "is N prime?" code from the "check the sum over a range" code. That way you can test the two parts separately. In the "refactored" subdirectory we've provided a version of the SumPrimes.java and TestSumPrimes.java files where that's been started. Copy those to the exercise directory if you'd like to see if that's an easier way to work. You might want to add additional tests to TestSumPrimes.java.

  • It's OK for a prime number to be divisible by 1.

  • If you try to divide a prime number by itself, you'll find that the remainder is zero.

  • For more information on Java math routines like floor(...), round(...), sqrt(...), etc please see the Math class doc page.