0

I don't know why the tests fail even if the lists are the same [13] [, abc, Abjh45, ch1, 5662, ch2, ch3, ch4, ch5], abc ✘ expected: listes.PureListString@5b94b04d<[, abc, Abjh45, ch1, 5662, ch2, ch3, ch4, ch5, abc]> but was: listes.PureListString@8c3b9d<[, abc, Abjh45, ch1, 5662, ch2, ch3, ch4, ch5, abc]>

enter image description here

Here is my code of addLast() method

    public PureListString addLast(String elt) {
        PureListString newCell = new PureListString(elt);
        PureListString head = new PureListString(this.first);
        PureListString tmp = this;
        if (tmp == EMPTY_LIST)
        {
            return newCell;
        }
        newCell = tmp.tail.addLast(elt);
        head.tail = newCell;
        head.size = tmp.size + 1;
        return head;
    }

and this is the constructor


    public PureListString(String elt) {
        this.first = elt;
        this.tail = EMPTY_LIST;
        this.size = 1;
    }

this is my equals method

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof PureListString)) {
            return false;
        }
        PureListString p = (PureListString) obj;
        if (size() != p.size()) {
            return false;
        }
        if (!isEmpty() && !p.isEmpty()){
            if (getFirst() != p.getFirst())
            {
                return false;
            }
            return removeFirst() == p.removeFirst();
        }
        for (int i = 0; i < size(); i++) {
            if (get(i) != p.get(i))
            {
                return false;
            }
        }
        return true;
    }

The test method

    public final void testAddLast(PureListString self, String elt) {
        assumeTrue(self != null);
        // Invariant
        assertInvariant(self);

        // préconditions
        assumeTrue(elt != null);

        // Purity
        saveState(self);

        // Exécution
        PureListString result = self.addLast(elt);

        // Post conditions
        assertNotNull(result);
        assertNotSame(result, self);
        assertEquals(result.getLast(), elt);
        assertTrue(result.size() == (self.size() + 1));
        for (int i = 0; i < self.size(); i++) {
            assertEquals(self.get(i), result.get(i));
        }
        if (self.isEmpty()) {
            assertEquals(result.getFirst(), elt);
            assertTrue(result.removeFirst().isEmpty());
        } else {
            assertEquals(result.getFirst(), self.getFirst());
            assertEquals(result, self.removeFirst().addLast(elt).addFirst(self.getFirst()));
        }

        // Purity
        assertPurity(self);

        // Invariant
        assertInvariant(self);
    }

Please Note that I cannot change the test methods.

idaoudi07
  • 11
  • 5
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Add the full `PureListString` class as well as the JUnit tests. – Progman Dec 31 '20 at 17:05
  • 2
    Shwo us the test also, so we can tell you what exactly is wrong. My first guess is that you didnt implement "equals" properly or test for identity `==` somewhere, instead of checking equality – JayC667 Dec 31 '20 at 17:05
  • I added the methods this I will check my code again to see if I tested equality using == – idaoudi07 Dec 31 '20 at 17:12
  • @idaoudi07 Please [edit] your question to include the **FULL** source code of the `PureListString` class and the JUnit test class. – Progman Dec 31 '20 at 17:14
  • @idaoudi07 What are you trying to check with the `if (!isEmpty() && !p.isEmpty()){ ... }` block inside your `equals()` method? And having `removeFirst()` inside the `equals()` method doesn't looks correct, as it would change the object when you just want to compare it with another instance with `equals()`. This method should only check, not change the object. – Progman Dec 31 '20 at 17:18
  • @Progman I can't add the full source Code because it is too big – idaoudi07 Dec 31 '20 at 17:23
  • @idaoudi07 What is the return type of your `get()` method. – Progman Dec 31 '20 at 17:35
  • @Progman String is the return type of get – idaoudi07 Dec 31 '20 at 17:40
  • @idaoudi07 Then check the duplicates, they explain how to compare strings (and other objects in general). – Progman Dec 31 '20 at 17:41

0 Answers0