Bird Watcher in Java - Should we add a test case?

Mentoring solution I found that this passes the tests:

    public boolean hasDayWithoutBirds() {
        for(int i=0; i<birdsPerDay.length-1; i++){
            if(birdsPerDay[i] == 0){
                return true;
            }
        }
        return false;
    }

What test case are we adding here? What’s the rationale?

It looks like an off-by-one on the last element.

2 Likes

Yes, it is off-by-one on the last element.

So something like this?

    @Test
    @Tag("task:4")
    @DisplayName("The hasDayWithoutBirds method returns true if the last day has zero visits")
    public void itHasLastDayWithoutBirds() {
        birdWatcher = new BirdWatcher(new int[]{1, 2, 5, 3, 7, 8, 0});
        assertThat(birdWatcher.hasDayWithoutBirds()).isTrue();
    }

I explained to the students that this was an error in the code logic. The solution passes all the test cases. Perhaps we could add a test case to the problem specifications?

It may be in other tracks as well

It’s a concept exercise, not a practice exercise, so the test cases are specific to the Java track implementation. I’m not a Java track maintainer, but they’re fairly active so I’m sure you’ll get feedback soon enough.

Perhaps, but that’d be a separate thread. :slight_smile:

I think this is a concept exercise, at least it is for JavaScript, so no problem-spec. We don’t have hasDayWithoutBirds in JS.

Ref: On Excercism

2 Likes

I’m hesitant to add this one at the moment. There are already 11,976 completed solutions for this one that would we would need to retest if we add this test case.

Since this cover the arrays concept, learning to iterate over the entire array seems reasonable to me. However, tasks 5 and 6 also cover iterating over the array correctly. I don’t think we need to cover this off by one case on all of the three tasks (assuming they used the same way to iterate for all three tasks).

Although it hasn’t been implemented yet, I think the analyzer could check for this. Happy to add it the list of things that analyzer could look for in its design.md.

2 Likes

You could disable the tests from rerunning by adding that no-retest string to the commit message.

1 Like

Community solutions for Bird Watcher in Java on Exercism has two pages of solutions that reference birdsPerDay.length but that might be used elsewhere in the solution. So we’re looking at a theoretical max of 2.6k solutions (48 search results times 56 grouped solutions for the most submitted one), but the number affected is likely much lower. The first ten I checked don’t use a for loop inside this method so the proposed tests should almost certainly pass. The grouped solutions counts drop into the tens of solutions after this point so I’m thinking this might not be a common issue.

2 Likes

The test fails only on solutions that has a logical errors. This help our students to find their error and learn Java.

I belive that our goal is to help our students.

2 Likes