Add oving 5

This commit is contained in:
Andreas Omholt Olsen
2026-02-09 15:28:09 +01:00
parent 6193e26ba1
commit 55c36a603a
33 changed files with 1586 additions and 0 deletions

View File

View File

View File

View File

View File

View File

@@ -0,0 +1,100 @@
package oving5;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class BinaryComputingIteratorTest {
private Iterator<Double> iterator1;
private Iterator<Double> iterator2;
private Iterator<Double> iteratorShort;
@BeforeEach
public void setUp() {
iterator1 = List.of(0.5, -2.0).iterator();
iterator2 = List.of(5.0, 3.0).iterator();
iteratorShort = List.of(5.0).iterator();
}
@Test
@DisplayName("Check that the constructor sets up the iterator correctly")
public void testConstructor() {
assertThrows(IllegalArgumentException.class, () -> {
new BinaryComputingIterator(null, iterator2, (a, b) -> a * b);
}, "Iterator1 cannot be null");
assertThrows(IllegalArgumentException.class, () -> {
new BinaryComputingIterator(iterator1, null, (a, b) -> a * b);
}, "Iterator2 cannot be null");
assertThrows(IllegalArgumentException.class, () -> {
new BinaryComputingIterator(iterator1, iterator2, null);
}, "Operator cannot be null");
}
@Test
@DisplayName("Check BinaryComputingIterator with multiplication")
public void testMultiplication() {
BinaryComputingIterator binaryIt =
new BinaryComputingIterator(iterator1, iterator2, (a, b) -> a * b);
assertEquals(2.5, binaryIt.next(), "The first number was incorrect");
assertTrue(binaryIt.hasNext());
assertEquals(-6.0, binaryIt.next(), "The second number was incorrect");
assertFalse(binaryIt.hasNext());
}
@Test
@DisplayName("Check BinaryComputingIterator with addition")
public void testAddition() {
BinaryComputingIterator binaryIt =
new BinaryComputingIterator(iterator1, iterator2, (a, b) -> a + b);
assertEquals(5.5, binaryIt.next(), "The first number was incorrect");
assertTrue(binaryIt.hasNext());
assertEquals(1.0, binaryIt.next(), "The second number was incorrect");
assertFalse(binaryIt.hasNext());
}
@Test
@DisplayName("Test multiplication with only one number")
public void testShortIterator() {
BinaryComputingIterator binaryIt =
new BinaryComputingIterator(iterator1, iteratorShort, (a, b) -> a * b);
assertEquals(2.5, binaryIt.next(), "The first number was incorrect");
assertFalse(binaryIt.hasNext());
}
@Test
@DisplayName("Test with default value, both a number and null")
public void testShortIteratorAndDefault() {
BinaryComputingIterator binaryIt =
new BinaryComputingIterator(iterator1, iteratorShort, null, 2.0, (a, b) -> a * b);
assertEquals(2.5, binaryIt.next(), "The first number was incorrect");
assertTrue(binaryIt.hasNext());
assertEquals(-4.0, binaryIt.next(), "The second number was incorrect");
assertFalse(binaryIt.hasNext());
}
@Test
@DisplayName("Test with an empty iterator")
public void testEmptyIterator() {
BinaryComputingIterator binaryIt = new BinaryComputingIterator(Collections.emptyIterator(),
Collections.emptyIterator(), (a, b) -> a * b);
assertFalse(binaryIt.hasNext(), "An empty iterator should not have next");
}
@Test
@DisplayName("Test an empty iterator with default value")
public void testEmptyIteratorAndDefault() {
BinaryComputingIterator binaryIt = new BinaryComputingIterator(Collections.emptyIterator(),
Collections.emptyIterator(), 1.0, 2.0, (a, b) -> a * b);
assertFalse(binaryIt.hasNext(), "An empty iterator should not have next");
}
}

View File

@@ -0,0 +1,71 @@
package oving5;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.BinaryOperator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class RPNCalcTest {
private RPNCalc calc;
@BeforeEach
public void setUp() {
calc = new RPNCalc();
}
@Test
@DisplayName("Test operation without operands")
public void testPerformOperationWithoutOperation() {
assertThrows(UnsupportedOperationException.class, () -> {
calc.performOperation('+');
});
}
@Test
@DisplayName("Test execution of a simple operation")
public void testPerformOperation() {
calc.addOperator('+', (a, b) -> a * b); // Use "incorrect" definition to filter out cheating
calc.addOperator('l', (a, b) -> a * (a + b));
calc.push(4);
calc.push(3);
calc.performOperation('+');
assertEquals(12.0, calc.pop(), "The result of the calculation was incorrect");
assertEquals(Double.NaN, calc.pop());
calc.push(4);
calc.push(3);
calc.performOperation('l');
assertEquals(28.0, calc.pop(), "The result of the calculation was incorrect");
assertEquals(Double.NaN, calc.pop());
}
@Test
@DisplayName("Test adding operators")
public void testAddOperator() {
assertTrue(calc.addOperator('+', (a, b) -> a + b), "You should be able to add operators");
assertTrue(calc.addOperator('-', (a, b) -> a - b), "You should be able to add operators");
assertFalse(calc.addOperator('+', (a, b) -> a + b),
"You should not be able to add the same operator twice");
assertFalse(calc.addOperator('-', (a, b) -> a * b),
"You should not be able to add the same operator twice");
assertFalse(calc.addOperator('.', (BinaryOperator<Double>) null),
"You should not be able to add a null operator");
}
@Test
@DisplayName("Check that you can remove operators")
public void testRemoveOperator() {
calc.addOperator('+', (a, b) -> a + b);
calc.removeOperator('+');
assertThrows(UnsupportedOperationException.class, () -> {
calc.performOperation('+');
}, "The operator should have been removed");
}
}

View File

@@ -0,0 +1,78 @@
package oving5.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardComparatorTest {
private Card s1;
private Card h1;
private Card d1;
private Card c1;
private Card s13;
private Card h13;
private Card d13;
private Card c13;
private Collection<Card> expected;
private List<Card> cards;
private static void testCards(Collection<Card> actualCards, Collection<Card> expectedCards) {
Iterator<Card> actual = actualCards.iterator();
Iterator<Card> expected = expectedCards.iterator();
while (expected.hasNext()) {
assertTrue(actual.hasNext());
Card actualCard = actual.next();
Card expectedCard = expected.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String.format(
"The card deck should have been %s, but was %s", expectedCards, actualCards));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String.format(
"The card deck should have been %s, but was %s", expectedCards, actualCards));
}
}
@BeforeEach
public void setUp() {
s1 = new Card('S', 1);
h1 = new Card('H', 1);
d1 = new Card('D', 1);
c1 = new Card('C', 1);
s13 = new Card('S', 13);
h13 = new Card('H', 13);
d13 = new Card('D', 13);
c13 = new Card('C', 13);
cards = new ArrayList<>(List.of(s1, s13, h1, h13, d1, d13, c1, c13));
}
@Test
@DisplayName("Check that the deck is sorted with aces as the lowest")
public void testNormal() {
expected = List.of(c1, c13, d1, d13, h1, h13, s1, s13);
cards.sort(new CardComparator(false, ' '));
CardComparatorTest.testCards(cards, expected);
}
@Test
@DisplayName("Check that the deck is sorted with aces as the highest")
public void testAceIsHighest() {
expected = List.of(c13, c1, d13, d1, h13, h1, s13, s1);
cards.sort(new CardComparator(true, ' '));
CardComparatorTest.testCards(cards, expected);
}
@Test
@DisplayName("Check that the deck is sorted correctly with diamonds as trump")
public void testDiamondIsTrump() {
expected = List.of(c1, c13, h1, h13, s1, s13, d1, d13);
cards.sort(new CardComparator(false, 'D'));
CardComparatorTest.testCards(cards, expected);
}
}

View File

@@ -0,0 +1,69 @@
package oving5.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardContainerIteratorTest {
private Card c1;
private Card c2;
private Card d1;
private Card d2;
private Card h1;
private Card h2;
private Card s1;
private Card s2;
private CardContainerIterator iterator;
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
while (expected.hasNext()) {
assertTrue(actual.hasNext());
Card actualCard = actual.next();
Card expectedCard = expected.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
}
}
@BeforeEach
public void setUp() {
iterator = new CardContainerIterator(new CardDeck(2));
s1 = new Card('S', 1);
s2 = new Card('S', 2);
h1 = new Card('H', 1);
h2 = new Card('H', 2);
d1 = new Card('D', 1);
d2 = new Card('D', 2);
c1 = new Card('C', 1);
c2 = new Card('C', 2);
}
@Test
@DisplayName("Check that the iterator for a new deck of cards outputs [S1, S2, H1, H2, D1, D2, C1, C2]")
public void testConstructor() {
CardContainerIteratorTest.testCards(iterator,
List.of(s1, s2, h1, h2, d1, d2, c1, c2).iterator());
}
@Test
@DisplayName("Check that the first card in the iterator is correct")
public void testNext() {
Card nextCard = iterator.next();
assertEquals(s1.toString(), nextCard.toString());
}
@Test
@DisplayName("Check that the deck contains at least one card")
public void testHasNext() {
boolean hasNext = iterator.hasNext();
assertTrue(hasNext);
}
}

View File

@@ -0,0 +1,86 @@
package oving5.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardDeckTest {
private Card s1;
private Card h1;
private Card d1;
private Card c1;
private Card s2;
private Card h2;
private Card d2;
private Card c2;
private CardDeck deck;
private Collection<Card> expected;
private static void testCards(Iterable<Card> actual, Iterator<Card> expected) {
Iterator<Card> actualIt = actual.iterator();
while (expected.hasNext()) {
assertTrue(actualIt.hasNext());
Card expectedCard = expected.next();
Card actualCard = actualIt.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
}
}
private static void testCards(CardContainer it, Collection<Card> expected) {
assertEquals(expected.size(), it.getCardCount());
Iterator<Card> expectedIt = expected.iterator();
int i = 0;
while (expectedIt.hasNext()) {
Card expectedCard = expectedIt.next();
Card actualCard = it.getCard(i);
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
i++;
}
}
@BeforeEach
public void setUp() {
deck = new CardDeck(2);
s1 = new Card('S', 1);
s2 = new Card('S', 2);
h1 = new Card('H', 1);
h2 = new Card('H', 2);
d1 = new Card('D', 1);
d2 = new Card('D', 2);
c1 = new Card('C', 1);
c2 = new Card('C', 2);
expected = new ArrayList<>(List.of(s1, s2, h1, h2, d1, d2, c1, c2));
}
@Test
@DisplayName("Checks that CardContainer works with CardDeck")
public void testCardContainer() {
CardDeckTest.testCards(deck, expected);
}
@Test
@DisplayName("Checks that the iterator works with CardDeck")
public void testDeckIterator() {
CardDeckTest.testCards(deck, expected.iterator());
}
}

View File

@@ -0,0 +1,78 @@
package oving5.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardHandTest {
private Card s1;
private Card c2;
private CardHand hand;
private Collection<Card> expected;
private static void testCards(CardContainer it, Collection<Card> expected) {
assertEquals(expected.size(), it.getCardCount());
Iterator<Card> expectedIt = expected.iterator();
int i = 0;
while (expectedIt.hasNext()) {
Card expectedCard = expectedIt.next();
Card actualCard = it.getCard(i);
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
i++;
}
}
private static void testCards(Iterable<Card> actual, Iterator<Card> expected) {
Iterator<Card> actualIt = actual.iterator();
while (expected.hasNext()) {
assertTrue(actualIt.hasNext());
Card expectedCard = expected.next();
Card actualCard = actualIt.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
}
}
@BeforeEach
public void setUp() {
s1 = new Card('S', 1);
c2 = new Card('C', 2);
hand = new CardHand();
expected = new ArrayList<>(List.of(s1, c2));
}
@Test
@DisplayName("Checks that CardContainer works with CardHand")
public void testCardContainer() {
hand.addCard(s1);
hand.addCard(c2);
CardHandTest.testCards(hand, expected);
}
@Test
@DisplayName("Checks that the iterator works with CardHand")
public void testDeckIterator() {
hand.addCard(s1);
hand.addCard(c2);
CardHandTest.testCards(hand, expected.iterator());
}
}

View File

@@ -0,0 +1,63 @@
package oving5.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardPredicateTest {
private CardDeck deck;
@BeforeEach
public void setUp() {
deck = new CardDeck(10);
}
@Test
@DisplayName("Check that hasCard() works as expected")
public void testHasCard() {
assertThrows(IllegalArgumentException.class, () -> {
deck.hasCard(null);
}, "Predicate cannot be null");
assertTrue(deck.hasCard(c -> c.getSuit() == 'S'));
assertFalse(deck.hasCard(c -> c.getFace() == 13));
assertTrue(deck.hasCard(c -> c.getSuit() == 'S' && c.getFace() == 8));
}
@Test
@DisplayName("Check that getCardCount() works as expected")
public void testGetCardCount() {
assertThrows(IllegalArgumentException.class, () -> {
deck.getCardCount(null);
}, "Predicate cannot be null");
assertEquals(10, deck.getCardCount(c -> c.getSuit() == 'S'));
assertEquals(4, deck.getCardCount(c -> c.getFace() == 4));
assertEquals(1, deck.getCardCount(c -> c.getFace() == 4 && c.getSuit() == 'H'));
}
@Test
@DisplayName("Check that getCards() works as expected")
public void testGetCards() {
assertThrows(IllegalArgumentException.class, () -> {
deck.getCards(null);
}, "Predicate cannot be null");
Card card = new Card('S', 4);
Card card2 = new Card('S', 5);
List<Card> matching = List.of(card, card2);
assertEquals(matching.size(),
deck.getCards(c -> (c.getFace() == 4 || c.getFace() == 5) && c.getSuit() == 'S')
.size(),
"getCards should have returned two cards that were spades and had the numbers 4 "
+ "or 5");
assertEquals(10, deck.getCards(c -> c.getSuit() == 'S').size(),
"getCards should have returned 10 cards of the spades suit");
}
}

View File

@@ -0,0 +1,50 @@
package oving5.named;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class NamedComparatorTest {
private NamedComparator comparator;
private Person1 p1;
private Person2 p2;
@BeforeEach
public void setUp() {
comparator = new NamedComparator();
p1 = new Person1("Aleksander", "Vestlund");
p2 = new Person2("Dan Vestlund");
}
@Test
@DisplayName("Check that people with the same name are equivalent")
public void testSameFullName() {
assertEquals(0, comparator.compare(p1, p1));
assertEquals(0, comparator.compare(p2, p2));
}
@Test
@DisplayName("Check that given names are compared when the family names are the same")
public void testSameFamilyName() {
// Return negative since first givenName is before second
assertTrue(comparator.compare(p1, p2) < 0, "Aleksander should come before Dan");
// Return positive since first givenName is after second
assertTrue(comparator.compare(p2, p1) > 0, "Dan should come after Aleksander");
}
@Test
@DisplayName("Check that family names are compared correctly")
public void testDifferentFamilyName() {
p2.setFamilyName("Bertelsen");
// Return negative since first familyName is before second
assertTrue(comparator.compare(p2, p1) < 0, "Bertelsen should come before Vestlund");
// Return positive since first familyName is after second
assertTrue(comparator.compare(p1, p2) > 0, "Vestlund should come after Bertelsen");
}
}

View File

@@ -0,0 +1,58 @@
package oving5.named;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class Person1Test {
private Person1 person;
private String given;
private String family;
private static void testName(Person1 person, String givenName, String familyName) {
assertEquals(givenName, person.getGivenName());
assertEquals(familyName, person.getFamilyName());
assertEquals(String.format("%s %s", givenName, familyName), person.getFullName());
}
@BeforeEach
public void setUp() {
given = "Hallvard";
family = "Trætteberg";
person = new Person1(given, family);
}
@Test
@DisplayName("Check that the constructor assigns the correct name to the person")
public void testConstructor() {
Person1Test.testName(person, given, family);
}
@Test
@DisplayName("Check that setGivenName() assigns the correct name")
public void testSetGivenName() {
String newGiven = "Jens";
person.setGivenName(newGiven);
Person1Test.testName(person, newGiven, family);
}
@Test
@DisplayName("Check that setFamilyName() assigns the correct name")
public void testSetFamilyName() {
String newFamily = "Olsen";
person.setFamilyName(newFamily);
Person1Test.testName(person, given, newFamily);
}
@Test
@DisplayName("Check that setFullName() assigns the correct name")
public void testSetFullName() {
String newGiven = "Lisa";
String newFamily = "Eriksen";
String newFull = String.format("%s %s", newGiven, newFamily);
person.setFullName(newFull);
Person1Test.testName(person, newGiven, newFamily);
}
}

View File

@@ -0,0 +1,58 @@
package oving5.named;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class Person2Test {
private Person2 person;
private String given;
private String family;
@BeforeEach
public void setUp() {
given = "Hallvard";
family = "Trætteberg";
person = new Person2(String.format("%s %s", given, family));
}
private static void testName(Person2 person, String givenName, String familyName) {
assertEquals(givenName, person.getGivenName());
assertEquals(familyName, person.getFamilyName());
assertEquals(String.format("%s %s", givenName, familyName), person.getFullName());
}
@Test
@DisplayName("Check that the constructor assigns the correct name to the person")
public void testConstructor() {
Person2Test.testName(person, given, family);
}
@Test
@DisplayName("Check that setGivenName() assigns the correct name")
public void testSetGivenName() {
String newGiven = "Jens";
person.setGivenName(newGiven);
Person2Test.testName(person, newGiven, family);
}
@Test
@DisplayName("Check that setFamilyName() assigns the correct name")
public void testSetFamilyName() {
String newFamily = "Olsen";
person.setFamilyName(newFamily);
Person2Test.testName(person, given, newFamily);
}
@Test
@DisplayName("Check that setFullName() assigns the correct name")
public void testSetFullName() {
String newGiven = "Lisa";
String newFamily = "Eriksen";
String newFull = String.format("%s %s", newGiven, newFamily);
person.setFullName(newFull);
Person2Test.testName(person, newGiven, newFamily);
}
}

View File

@@ -0,0 +1,111 @@
package oving5.stringgrid;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class StringGridTest {
private StringGrid grid;
@BeforeEach
public void setUp() {
grid = new StringGridImpl(2, 3);
}
@Test
@DisplayName("Test the size of the grid")
public void testSize() {
assertEquals(2, grid.getRowCount(), "The number of rows was incorrect");
assertEquals(3, grid.getColumnCount(), "The number of columns was incorrect");
}
@Test
@DisplayName("Test that setElement sets the correct grid element")
public void testGrid() {
grid.setElement(0, 0, "0, 0");
grid.setElement(0, 1, "0, 1");
grid.setElement(0, 2, "0, 2");
grid.setElement(1, 0, "1, 0");
grid.setElement(1, 1, "1, 1");
grid.setElement(1, 2, "1, 2");
assertEquals("0, 0", grid.getElement(0, 0));
assertEquals("0, 1", grid.getElement(0, 1));
assertEquals("0, 2", grid.getElement(0, 2));
assertEquals("1, 0", grid.getElement(1, 0));
assertEquals("1, 1", grid.getElement(1, 1));
assertEquals("1, 2", grid.getElement(1, 2));
}
@Test
@DisplayName("Check the elements in the grid with row-first order")
public void testGridIteratorRowMajor() {
StringGridIterator iterator = new StringGridIterator(grid, true);
grid.setElement(0, 0, "0, 0");
grid.setElement(0, 1, "0, 1");
grid.setElement(0, 2, "0, 2");
grid.setElement(1, 0, "1, 0");
grid.setElement(1, 1, "1, 1");
grid.setElement(1, 2, "1, 2");
assertTrue(iterator.hasNext());
assertEquals("0, 0", iterator.next());
assertTrue(iterator.hasNext());
assertEquals("0, 1", iterator.next());
assertTrue(iterator.hasNext());
assertEquals("0, 2", iterator.next());
assertTrue(iterator.hasNext());
assertEquals("1, 0", iterator.next());
assertTrue(iterator.hasNext());
assertEquals("1, 1", iterator.next());
assertTrue(iterator.hasNext());
// False after going through the last one
assertEquals("1, 2", iterator.next());
assertFalse(iterator.hasNext());
}
@Test
@DisplayName("Check the elements in the grid with column-first order")
public void testGridIteratorColumnMajor() {
StringGridIterator iterator = new StringGridIterator(grid, false);
grid.setElement(0, 0, "0, 0");
grid.setElement(0, 1, "0, 1");
grid.setElement(0, 2, "0, 2");
grid.setElement(1, 0, "1, 0");
grid.setElement(1, 1, "1, 1");
grid.setElement(1, 2, "1, 2");
assertTrue(iterator.hasNext());
assertEquals("0, 0", iterator.next(), "A cell was incorrect");
assertTrue(iterator.hasNext(), "Incorrect number of cells in the grid");
assertEquals("1, 0", iterator.next(), "A cell was incorrect");
assertTrue(iterator.hasNext(), "Incorrect number of cells in the grid");
assertEquals("0, 1", iterator.next(), "A cell was incorrect");
assertTrue(iterator.hasNext(), "Incorrect number of cells in the grid");
assertEquals("1, 1", iterator.next(), "A cell was incorrect");
assertTrue(iterator.hasNext(), "Incorrect number of cells in the grid");
assertEquals("0, 2", iterator.next(), "A cell was incorrect");
assertTrue(iterator.hasNext(), "Incorrect number of cells in the grid");
// False after going through the last one
assertEquals("1, 2", iterator.next());
assertFalse(iterator.hasNext(), "Incorrect number of cells in the grid");
}
}

View File

@@ -0,0 +1,51 @@
package oving5.ticket;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class PeriodTicketTest {
@Test
@DisplayName("Check that the ticket is valid within the period")
public void testIsValidWhenWithinPeriod() {
LocalDateTime start = LocalDateTime.now().minusDays(1);
LocalDateTime end = LocalDateTime.now().plusDays(1);
PeriodTicket ticket = new PeriodTicket(start, end);
assertTrue(ticket.scan(), "The ticket should be valid within the period");
}
@Test
@DisplayName("Check that the ticket is not valid before the period")
public void testIsNotValidWhenBeforePeriod() {
LocalDateTime start = LocalDateTime.now().plusDays(1);
LocalDateTime end = LocalDateTime.now().plusDays(2);
PeriodTicket ticket = new PeriodTicket(start, end);
assertFalse(ticket.scan(), "The ticket should not be valid before the period");
}
@Test
@DisplayName("Check that the ticket is not valid after the period")
public void testIsNotValidWhenAfterPeriod() {
LocalDateTime start = LocalDateTime.now().minusDays(2);
LocalDateTime end = LocalDateTime.now().minusDays(1);
PeriodTicket ticket = new PeriodTicket(start, end);
assertFalse(ticket.scan(), "The ticket should not be valid after the period");
}
@Test
@DisplayName("Check that the ticket throws IllegalArgumentException when the start date is "
+ "after the end date")
public void testThrowsExceptionWhenStartIsAfterEnd() {
LocalDateTime start = LocalDateTime.now().plusDays(1);
LocalDateTime end = LocalDateTime.now().minusDays(1);
assertThrows(IllegalArgumentException.class, () -> {
new PeriodTicket(start, end);
}, "The ticket should throw IllegalArgumentException when the start date is after the "
+ "end date");
}
}

View File

@@ -0,0 +1,41 @@
package oving5.ticket;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class SingleTicketTest {
private Ticket ticket;
@BeforeEach
public void setUp() {
ticket = new SingleTicket();
}
@Test
@DisplayName("Check that the ticket returns true when scanned for the first time")
public void testReturnsTrueWhenScanned() {
assertTrue(this.ticket.scan(),
"The ticket should return true when scanned for the first time");
}
@Test
@DisplayName("Check that the ticket returns false when scanned for the second time")
public void testReturnsFalseWhenScannedTwice() {
ticket.scan();
assertFalse(ticket.scan(),
"The ticket should return false when scanned for the second time");
}
@Test
@DisplayName("Check that the ticket returns true when scanned for the first time after reset")
public void testReturnsFalseWhenScannedTwiceAfterReset() {
ticket.scan();
ticket = new SingleTicket();
assertTrue(ticket.scan(),
"The ticket should return true when scanned for the first time after reset");
}
}

View File

@@ -0,0 +1,35 @@
package oving5.twitter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class FollowersCountComparatorTest {
private TwitterAccount aaron;
private TwitterAccount ben;
private TwitterAccount charlie;
private FollowersCountComparator comparator;
@BeforeEach
public void SetUp() {
aaron = new TwitterAccount("Aaron");
ben = new TwitterAccount("Ben");
charlie = new TwitterAccount("Charlie");
comparator = new FollowersCountComparator();
}
@Test
@DisplayName("Check that the comparison is based on followers")
public void testCompare() {
aaron.follow(ben);
ben.follow(aaron);
assertEquals(0, comparator.compare(aaron, ben), "Aaron and Ben should be equal");
charlie.follow(ben);
assertTrue(comparator.compare(aaron, ben) > 0, "Aaron should come after Ben");
assertTrue(comparator.compare(ben, aaron) < 0, "Ben should come before Aaron");
}
}

View File

@@ -0,0 +1,38 @@
package oving5.twitter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TweetsCountComparatorTest {
private TwitterAccount mostTweet;
private TwitterAccount lessTweet1;
private TwitterAccount lessTweet2;
private TweetsCountComparator comparator;
@BeforeEach
public void SetUp() {
mostTweet = new TwitterAccount("Aaron");
lessTweet1 = new TwitterAccount("Ben");
lessTweet2 = new TwitterAccount("Charlie");
comparator = new TweetsCountComparator();
}
@Test
@DisplayName("Check comparison based on tweets")
public void testCompare() {
mostTweet.tweet("Tweet");
mostTweet.tweet("Tweet");
lessTweet1.tweet("Tweet");
lessTweet2.tweet("Tweet");
assertTrue(comparator.compare(mostTweet, lessTweet1) < 0,
"The account with the most tweets should come first");
assertTrue(comparator.compare(lessTweet1, mostTweet) > 0,
"The account with the fewest tweets should come last");
assertEquals(0, comparator.compare(lessTweet1, lessTweet2),
"Two accounts with the same number of tweets should be equal");
}
}

View File

@@ -0,0 +1,146 @@
package oving5.twitter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TwitterAccountTest {
private TwitterAccount nils;
private TwitterAccount ole;
private static void checkFollow(TwitterAccount accountA, TwitterAccount accountB,
boolean AfollowsB, boolean BfollowsA) {
if (AfollowsB) {
assertTrue(accountA.isFollowing(accountB), String.format("%s should be following %s",
accountA.getUserName(), accountB.getUserName()));
assertTrue(accountB.isFollowedBy(accountA), String.format("%s should be followed by %s",
accountB.getUserName(), accountA.getUserName()));
} else {
assertFalse(accountA.isFollowing(accountB),
String.format("%s should not be following %s", accountA.getUserName(),
accountB.getUserName()));
assertFalse(accountB.isFollowedBy(accountA),
String.format("%s should not be followed by %s", accountB.getUserName(),
accountA.getUserName()));
}
if (BfollowsA) {
assertTrue(accountB.isFollowing(accountA), String.format("%s should be following %s",
accountB.getUserName(), accountA.getUserName()));
assertTrue(accountA.isFollowedBy(accountB), String.format("%s should be followed by %s",
accountA.getUserName(), accountB.getUserName()));
} else {
assertFalse(accountB.isFollowing(accountA),
String.format("%s should not be following %s", accountB.getUserName(),
accountA.getUserName()));
assertFalse(accountA.isFollowedBy(accountB),
String.format("%s should not be followed by %s", accountA.getUserName(),
accountB.getUserName()));
}
}
@BeforeEach
public void setUp() {
nils = new TwitterAccount("Nils");
ole = new TwitterAccount("Ole");
}
@Test
@DisplayName("Check that the constructor sets up the account correctly")
public void testConstructor() {
assertEquals("Nils", nils.getUserName());
assertEquals(0, nils.getTweetCount());
assertEquals("Ole", ole.getUserName());
assertEquals(0, ole.getTweetCount());
}
@Test
@DisplayName("Follow")
public void testFollow() {
nils.follow(ole);
TwitterAccountTest.checkFollow(nils, ole, true, false);
ole.follow(nils);
TwitterAccountTest.checkFollow(nils, ole, true, true);
}
@Test
@DisplayName("Unfollow")
public void testUnfollow() {
TwitterAccountTest.checkFollow(nils, ole, false, false);
nils.follow(ole);
TwitterAccountTest.checkFollow(nils, ole, true, false);
nils.unfollow(ole);
TwitterAccountTest.checkFollow(nils, ole, false, false);
}
@Test
@DisplayName("Tests that a new tweet is correct")
public void testNewTweet() {
nils.tweet("Kvitre!");
assertEquals(1, nils.getTweetCount(), "Nils' tweet count should be 1");
assertEquals("Kvitre!", nils.getTweet(1).getText(), "The text should be 'Tweet'");
nils.tweet("Kvitre igjen!");
assertEquals(2, nils.getTweetCount());
assertEquals("Kvitre igjen!", nils.getTweet(1).getText());
assertEquals("Kvitre!", nils.getTweet(2).getText());
}
@Test
@DisplayName("Tests exceptions for illegal tweets")
public void testIllegalTweet() {
assertThrows(RuntimeException.class, () -> {
nils.getTweet(1);
}, "Should not be able to retrieve a tweet that does not exist");
assertThrows(RuntimeException.class, () -> {
nils.getTweet(-1);
}, "Should not be able to retrieve a tweet that does not exist");
nils.tweet("Tweet!");
assertThrows(RuntimeException.class, () -> {
nils.getTweet(2);
}, "Should not be able to retrieve a tweet that does not exist");
assertThrows(RuntimeException.class, () -> {
nils.getTweet(-1);
}, "Should not be able to retrieve a tweet that does not exist");
}
@Test
@DisplayName("Check that retweet works, including retweeting a retweet")
public void testRetweet() {
TwitterAccount kari = new TwitterAccount("Kari");
nils.tweet("Kvitre!");
assertEquals(1, nils.getTweetCount());
assertEquals("Kvitre!", nils.getTweet(1).getText());
ole.retweet(nils.getTweet(1));
assertEquals(1, nils.getTweetCount());
assertEquals(1, nils.getRetweetCount());
assertEquals(1, ole.getTweetCount());
assertEquals(0, ole.getRetweetCount());
assertEquals("Kvitre!", ole.getTweet(1).getText());
assertEquals(nils.getTweet(1), ole.getTweet(1).getOriginalTweet());
kari.retweet(ole.getTweet(1));
assertEquals(1, nils.getTweetCount());
assertEquals(2, nils.getRetweetCount());
assertEquals(1, ole.getTweetCount());
assertEquals(0, ole.getRetweetCount());
assertEquals(1, kari.getTweetCount());
assertEquals(0, kari.getRetweetCount());
assertEquals("Kvitre!", kari.getTweet(1).getText());
assertEquals(nils.getTweet(1), kari.getTweet(1).getOriginalTweet());
}
}

View File

@@ -0,0 +1,32 @@
package oving5.twitter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class UserNameComparatorTest {
private TwitterAccount aaron1;
private TwitterAccount aaron2;
private TwitterAccount ben;
private UserNameComparator comparator;
@BeforeEach
public void setUp() {
aaron1 = new TwitterAccount("Aaron");
aaron2 = new TwitterAccount("Aaron");
ben = new TwitterAccount("Ben");
comparator = new UserNameComparator();
}
@Test
@DisplayName("Check comparison based on username")
public void testCompare() {
assertTrue(comparator.compare(aaron1, ben) < 0, "Aaron should be sorted before Ben");
assertTrue(comparator.compare(ben, aaron1) > 0, "Ben should be sorted after Aaron");
assertEquals(comparator.compare(aaron1, aaron2), 0,
"Two people with the same name should be equal");
}
}