Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayList add test does not work properly with this example #159

Open
obaibula opened this issue Feb 22, 2023 · 0 comments
Open

ArrayList add test does not work properly with this example #159

obaibula opened this issue Feb 22, 2023 · 0 comments

Comments

@obaibula
Copy link

obaibula commented Feb 22, 2023

Hello. The test does not fail with this corrupted code:

 @Override
    public void add(T element) {
        Objects.requireNonNull(element);

        if (elements.length != size) {
            elements[size] = element;
        } else {
            Object[] copy = new Object[size * 2];
            System.arraycopy(elements, 0, copy, 0, size);
            elements = copy;
        }
        size++;
    }
We should check at least 5 elements (if InitialCapaciti is 5):
  void add() {
       arrayList.add(10);
       arrayList.add(15);
       arrayList.add(20);
       arrayList.add(21);
       arrayList.add(24);
       arrayList.add(845);

       Object[] internalArray = getTestArray();

       assertThat(internalArray[0]).isEqualTo(10);
       assertThat(internalArray[1]).isEqualTo(15);
       assertThat(internalArray[2]).isEqualTo(20);
       assertThat(internalArray[3]).isEqualTo(21);
       assertThat(internalArray[4]).isEqualTo(24);
       assertThat(internalArray[5]).isEqualTo(845);
   }
@shryhus shryhus added this to To do in Java Fundamentals Feb 22, 2023
@obaibula obaibula changed the title Expanding ArrayList add test ArrayList add test does not work properly with this example Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant