Exercise 7

info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions. Aim to complete as much as possible during our meeting, but you need not hand it in.

Learning Objectives#

Objectives

This exercise should help you practice with:

  • Property-based Testing
  • Jqwik

Task 1#

Consider the following two functions (which you have already seen in exercise 1):

/**
* Count positive elements
* *
* @param x array to search
* @return count of positive elements in x
* @throws NullPointerException if x is null
*/
public static int countPositive (int[] x) {
int count = 0;
for (int i=0; i < x.length; i++) {
if (x[i] >= 0) {
count++;
}
}
return count;
}
public static int countOutsideRange(int arr[], int r1, int r2) {
int count = 0;
for (int i = 0; i < arr.length; ++i) {
// faults here
if (arr[i] <= r1 || arr[i] >= r2) {
count++;
}
}
return count;
}
  1. Write three properties for each of these functions.
  2. Both of these functions have fault(s). Does any of the properties you wrote in previous step helps with detecting any of the faults. If not, can you come up with a property that would do?
  3. Implement all the properties you came up with in Jqwik and run them against the above functions.
  4. Fix all the fualts so that all your property-based tests pass!

Task 2#

Using arbitraries and @Provide annotation, write "provider" functions to generate:

  1. integer values 3, 5, 7, 13, 17, 23, 41
  2. odd integer values
  3. a guassian distribution of integer values between 0 and 100 with a mean of 50
  4. string values only comprised of digits of min length 3 and max length 10
  5. edge cases for doubles
  6. string values of variable length with character set of letters a through z with a 0.5 probability of generating duplicate values