Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ public void removeFromEnd() {
LinkedListNode previous = this.last.getPrevious();
previous.setNext(null);
this.last = previous;
this.totalElements--;
}

this.totalElements--;
}

public void remove(final int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private boolean positionOccupied(final int position) {
}

public void remove(final int position) {
for(int i = position; i < this.totalElements; i++) {
for(int i = position; i < this.totalElements - 1; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Add a bounds check before shifting and decrementing the element count, rejecting positions that are negative or not occupied.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

this.elements[i] = this.elements[i+1];
}
totalElements--;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.diegourban.algorithms.datastructure;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class ElementTest {

@Test
void shouldEqualAnElementWithTheSameName() {
assertThat(new Element("diego")).isEqualTo(new Element("diego"));
assertThat(new Element("diego")).isNotEqualTo(new Element("paulo"));
}

@Test
void shouldNotEqualNull() {
assertThat(new Element("diego")).isNotEqualTo(null);
}

@Test
void shouldPrintItsName() {
assertThat(new Element("diego")).hasToString("diego");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.diegourban.algorithms.datastructure.linkedlist;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

import io.github.diegourban.algorithms.datastructure.Element;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class LinkedListTest {

@Test
Expand Down Expand Up @@ -121,4 +123,76 @@ void shouldPrint() {

assertThat(linkedList).hasToString("[diego,paulo,josé,]");
}

@Test
void shouldAddAtPositionOfEmptyList() {
LinkedList linkedList = new LinkedList();
linkedList.add(0, new Element("diego"));

assertThat(linkedList.size()).isEqualTo(1);
assertThat(linkedList.get(0)).isEqualTo(new Element("diego"));
}

@Test
void shouldAddAtPositionEqualToSizeAsLast() {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd(new Element("diego"));
linkedList.add(1, new Element("paulo"));

assertThat(linkedList).hasToString("[diego,paulo,]");
}

@Test
void shouldRejectGettingAMissingPosition() {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd(new Element("diego"));

assertThatIllegalArgumentException().isThrownBy(() -> linkedList.get(1));
assertThatIllegalArgumentException().isThrownBy(() -> linkedList.get(-1));
}

@Test
void shouldRejectRemovingFromStartOfEmptyList() {
assertThatIllegalStateException().isThrownBy(() -> new LinkedList().removeFromStart());
}

@Test
void shouldBecomeEmptyAfterRemovingTheOnlyElementFromStart() {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd(new Element("diego"));
linkedList.removeFromStart();

assertThat(linkedList.size()).isZero();
assertThat(linkedList).hasToString("[]");
}

@Test
void shouldBecomeEmptyAfterRemovingTheOnlyElementFromEnd() {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd(new Element("diego"));
linkedList.removeFromEnd();

assertThat(linkedList.size()).isZero();
assertThat(linkedList).hasToString("[]");
}

@Test
void shouldRemoveFirstAndLastByPosition() {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd(new Element("diego"));
linkedList.addAtEnd(new Element("paulo"));
linkedList.addAtEnd(new Element("josé"));

linkedList.remove(0);
assertThat(linkedList).hasToString("[paulo,josé,]");

linkedList.remove(1);
assertThat(linkedList).hasToString("[paulo,]");
assertThat(linkedList.size()).isEqualTo(1);
}

@Test
void shouldPrintEmptyList() {
assertThat(new LinkedList()).hasToString("[]");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.diegourban.algorithms.datastructure.set;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class SetTest {

@Test
Expand All @@ -12,7 +12,9 @@ void shouldAdd() {
set.add("Diego");
set.add("André");
set.add("Zuleika");
assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]");
assertThat(set)
.hasToString(
"[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]");
}

@Test
Expand All @@ -21,8 +23,34 @@ void shouldRemove() {
set.add("Diego");
set.add("André");
set.add("Zuleika");
assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]");
assertThat(set)
.hasToString(
"[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]");
set.remove("André");
assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [], [], [], [Diego], [], [], []]");
assertThat(set)
.hasToString(
"[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [], [], [], [Diego], [], [], []]");
}

@Test
void shouldIgnoreDuplicates() {
Set set = new Set();
set.add("Diego");
set.add("Diego");

assertThat(set)
.hasToString(
"[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Diego], [], [], []]");
}

@Test
void shouldIgnoreRemovingAMissingWord() {
Set set = new Set();
set.add("Diego");
set.remove("Daniel");

assertThat(set)
.hasToString(
"[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Diego], [], [], []]");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.github.diegourban.algorithms.datastructure.vector;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import io.github.diegourban.algorithms.datastructure.Element;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class VectorTest {

@Test
Expand All @@ -26,10 +27,50 @@ void test() {

vector.remove(1);

for(int i = 0; i < 300; i++) {
for (int i = 0; i < 300; i++) {
Element e = new Element("Element " + i);
vector.add(e);
}
System.out.println(vector);
}

@Test
void shouldRejectAddingAtAnInvalidPosition() {
Vector vector = new Vector();
vector.add(new Element("Joao"));

assertThatIllegalArgumentException().isThrownBy(() -> vector.add(-1, new Element("Jose")));
assertThatIllegalArgumentException().isThrownBy(() -> vector.add(5, new Element("Jose")));
}

@Test
void shouldRejectGettingAnInvalidPosition() {
Vector vector = new Vector();
vector.add(new Element("Joao"));

assertThat(vector.get(0)).isEqualTo(new Element("Joao"));
assertThatIllegalArgumentException().isThrownBy(() -> vector.get(1));
assertThatIllegalArgumentException().isThrownBy(() -> vector.get(-1));
}

@Test
void shouldNotContainAMissingElement() {
Vector vector = new Vector();
vector.add(new Element("Joao"));

assertThat(vector.contains(new Element("Mario"))).isFalse();
}

@Test
void shouldRemoveFromAFullVector() {
Vector vector = new Vector();
for (int i = 0; i < 100; i++) {
vector.add(new Element("Element " + i));
}

vector.remove(0);

assertThat(vector.size()).isEqualTo(99);
assertThat(vector.get(0)).isEqualTo(new Element("Element 1"));
}
}
Loading