Konstantin
Konstantin

Reputation: 3

Testing if an Array is empty JUNIT

I wanna write a Method displaying a deck from a Game. I initialized an Array with 98 Elements

private int [] cards = new int[98];

I also created a get and a set method

public int[] getCards() {
    return cards;
}

and

public void setCards(final int... cards) {
    this.cards = cards;
}

The Method I wrote is called drawCard. If I use this method it should remove the first element out of the Array and return it.

public int drawCard() throws IndexOutOfBoundsException {
    if (getCards().length == 0) {
        throw new IndexOutOfBoundsException("No Cards Left!");
    }
    setCards(ArrayUtils.removeElement(getCards(), 0));
    return getCards()[0];
}

And then I wrote the JUnit Test. The test should remove all the 98 elements and then the array should be empty == 0. But the test always stops at 1.

@Test
public void testDrawCard() {
    Deck deck = new Deck();
    assertThat(deck.getCards().length).isEqualTo(98);

    for(int x = 98; x >= 0; x--){
        deck.drawCard();
    }
    assertThat(deck.getCards().length).isEqualTo(0);
}

Error message:

java.lang.ArrayIndexOutOfBoundsException: 0
at edu.hm.hafner.java2.thegame.Deck.drawCard(Deck.java:35) 

35=Return Line

at edu.hm.hafner.java2.thegame.DeckTest.testDrawCard(DeckTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Does somebody know what's wrong?

Upvotes: 0

Views: 6178

Answers (2)

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11950

@ajb is right, but when you want to normally iterate backwards, you start with length - 1 because indices start with 0. So in your style, the loop should be

for(int x = 98 - 1; x >= 0; x--)

But however, you aren't using the x variable inside the loop, so the order should not matter as long as you are drawing exactly 98 elements.

Upvotes: 0

ajb
ajb

Reputation: 31699

for(int x = 98; x >= 0; x--)

This executes the loop while x >= 0. That is, if we decrement x and it gets the value 0, we execute the loop again because 0 >= 0 is true. Then, we decrement x and find that it's -1, so the loop stops. This means that the loop will execute for x = 98, 97, 96, ..., 2, 1, 0. There are 99 numbers in this list, so the loop executes 99 times.

Since you're not using x at all, there's no reason to have it start at the top and go downward. If the only purpose is to make sure your loop is executed exactly 98 times, then don't get clever--just use the standard for loop idiom:

for (int i = 0; i < 98; i++)

It doesn't make a difference to the program's execution. But by sticking to the standard idioms except when necessary, you'll save some brain cells and end up with a lot fewer bugs to figure out.

Upvotes: 1

Related Questions