0

I am working on a lab where we needed to locate an error in this method sumOfPositives. My response is that the method does not account for double types. We are then to write an assertion at the end of the test method that conforms to the initial change made. I can't seem to figure this out. My answer was to write this assertion:

assertEquals(1.5, Util.sumOfPositives(new double[] {0,1,0.5}));

yet it is not working correctly. What am I doing wrong?

Initial code:

public static int sumOfPositives(int[] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
        sum += arr[i];
    }

    return sum;
}

@Test
public void testSumOfPositives() {
    assertEquals(3, Util.sumOfPositives(new int[] { 1, 1, 1 }));
    assertEquals(7, Util.sumOfPositives(new int[] { 1, 2, 4 }));
    assertEquals(9, Util.sumOfPositives(new int[] { 0, 0, 3, 6 }));
}

My changes:

public static double sumOfPositives(double[] arr) {
    double sum =  0;
    for (double i : arr) {
         sum += i;
    }

    return sum;
}

@Test
public void testSumOfPositives() {
    assertEquals(3, Util.sumOfPositives(new int[] { 1, 1, 1 }));
    assertEquals(7, Util.sumOfPositives(new int[] { 1, 2, 4 }));
    assertEquals(9, Util.sumOfPositives(new int[] { 0, 0, 3, 6 }));
    assertEquals(1.5, Util.sumOfPositives(new double[] {0,1,0.5}));
}

4 Answers4

1

Why would you care about doubles if you're only ever expecting integers?

I'd actually conjecture you have a bigger bug inside of your summation. Remember: ints (and for that matter, longs) can only hold so much data before...something very interesting happens to the number.

Additionally, "positive" numbers is a big thing - the initial code makes no effort to check if the value it's trying to add is positive or not.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

double is not a problem.
Methods takes int as arguments and you should consider it as its specification.
I identify 2 cases not considered by the implementation :

  • the no nullity of the array parameter is not tested. If the array is null, a NullPointerException is raised.
  • int overflow if the sum of int arrays exceeds Integer.MAX_VALUE.

Defining a method by scenario is generally better to makes tests more readable and to make explicit the case as a test fails.

For the first case, you could throw IllegalArgumentException if the array is null and test it in this way :

@Test(exepected = IllegalArgumentException.class)
public void sumOfPositives_with_null_array_throws_IllegalArgumentException() {
    Util.sumOfPositives(null);
}

For the second case, either throw an exception, either do nothing in the implementation and in the unit test assert that the sum doesn't equals to the expected numerical sum because of the overflow.
For example :

@Test
public void sumOfPositives_with_overflow_sum_returns_unexpected_sum() {
    assertTrue(sumOfPositives(new int[] {Integer.MAX_VALUE + 1}) < 0);      
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
-1

You can use assertEquals(expected,actual,delta) as follows

public static double sumOfPositives(double[] arr) {
double sum =  0;
for (double i : arr) {
     sum += i;
}

return sum;
}

@Test
public void testSumOfPositives() {
assertEquals(3, Util.sumOfPositives(new double[] { 1, 1, 1 }),0);
assertEquals(7, Util.sumOfPositives(new double[] { 1, 2, 4 }),0);
assertEquals(9, Util.sumOfPositives(new double[] { 0, 0, 3, 6 }),0);
assertEquals(1.5, Util.sumOfPositives(new double[] {0,1,0.5}),0);
}
-3

When you changed the for loop you introduced a bug.

for (int i = 0; i < arr.length; i++) {
    sum+=arr[i];
}

Int in here is just for the loop counter. Not the data in the array. Put that line back to how it was and it should work. Also the contents of the loop should be restored to the original. As stated the i here is just the index of the array. Not the data within

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21