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 @@ -134,7 +134,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
if (n != 0 || currentPrice >= order.getPrice()) {
// Traverse all matching orders.
for (int i = 0; i >= 0; i++) {
final Order buyOrder = this.buyOrders.get(i);
final Order buyOrder = this.buyOrders.get(0);

// Fill entire order.
if (buyOrder.getAmount() >= order.getAmount()) {
Expand All @@ -143,7 +143,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
buyOrder.setAmount(
buyOrder.getAmount() - order.getAmount());
if (buyOrder.getAmount() == 0) {
this.removeBuyOrder(i);
this.removeBuyOrder(0);
}
this.setLastSalePrice(buyOrder.getPrice());
return trades;
Expand All @@ -154,7 +154,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
trades.add(new Trade(order.getId(), buyOrder.getId(),
buyOrder.getAmount(), buyOrder.getPrice()));
order.setAmount(order.getAmount() - buyOrder.getAmount());
this.removeBuyOrder(i);
this.removeBuyOrder(0);
this.setLastSalePrice(buyOrder.getPrice());
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.Resource;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void setUp() {
* Get orders test.
*/
@Test
@DisplayName("Get the orders")
public void getOrders() {
final Map<String, List<Order>> result = this.controller.getOrders();

Expand All @@ -69,6 +71,7 @@ public void getOrders() {
* Get order by ID test.
*/
@Test
@DisplayName("Get an order by ID")
public void getOrderById() {
final Order found = Mockito.mock(Order.class);
final String id = "testId";
Expand All @@ -83,6 +86,7 @@ public void getOrderById() {
* Add order test.
*/
@Test
@DisplayName("Add an order")
public void addOrder() {
final Side side = Side.BUY;
final double amt = 10;
Expand All @@ -103,6 +107,7 @@ public void addOrder() {
* Delete an order.
*/
@Test
@DisplayName("Delete an order")
public void deleteOrder() {
final String id = "id";
final Side side = Side.BUY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Resource;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;

Expand All @@ -27,6 +28,7 @@ public class CancelOrderTests extends MatchingEngineTest {
*
*/
@Test
@DisplayName("Attempt to cancel an order that doesn't exist")
public void cancelNoCancellation() {
assertEquals(this.orderBook.cancelOrder("test", Side.BUY), false);
assertEquals(this.orderBook.cancelOrder("test", Side.SELL), false);
Expand All @@ -38,6 +40,7 @@ public void cancelNoCancellation() {
* @throws InterruptedException
*/
@Test
@DisplayName("Cancel a buy order with the side not given")
public void cancelBuyOrder() throws InterruptedException {

final int amt = 1;
Expand All @@ -62,6 +65,7 @@ public void cancelBuyOrder() throws InterruptedException {
*
*/
@Test
@DisplayName("Cancel a buy order")
public void cancelBuyOrderSideGiven() {

final int amt = 1;
Expand All @@ -87,6 +91,7 @@ public void cancelBuyOrderSideGiven() {
* @throws InterruptedException
*/
@Test
@DisplayName("Cancel a sell order with the side not given")
public void cancelSellOrder() throws InterruptedException {
final int amt = 1;
final double price = 1;
Expand All @@ -110,6 +115,7 @@ public void cancelSellOrder() throws InterruptedException {
*
*/
@Test
@DisplayName("Cancel a sell order")
public void cancelSellOrderSideGiven() {
final int amt = 1;
final double price = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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 java.util.List;

import javax.annotation.Resource;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;

Expand All @@ -31,19 +31,12 @@ public class OrderBookTests extends MatchingEngineTest {
@Resource
private OrderBook orderBook;

// /**
// * Test setup.
// */
// @BeforeEach
// public void setUp() {
// this.orderBook = new OrderBook(new ArrayList<Order>(),
// new ArrayList<Order>());
// }

/**
* Add a buy order, then add a matching sell order.
*/
@Test
@DisplayName("Add a buy order, then add a matching sell order")
public void buyThenSell() {

// Add buy order
Expand Down Expand Up @@ -78,6 +71,7 @@ public void buyThenSell() {
* Add a sell order, then add a buy order.
*/
@Test
@DisplayName("Add a sell order, then add a matching buy order")
public void sellThenBuy() {

// Add sell order
Expand Down Expand Up @@ -112,6 +106,7 @@ public void sellThenBuy() {
* Find order tests.
*/
@Test
@DisplayName("Find orders")
public void findOrder() {

// Can't find an order that hasn't been added to the book.
Expand Down Expand Up @@ -139,11 +134,76 @@ public void findOrder() {
* Test that no orders are cancelled when an invalid side is provided.
*/
@Test
@DisplayName("No orders are cancelled when an invalid side is provided")
public void cancelTestInvalidSide() {

final String orderId = "";

final boolean result = this.orderBook.cancelOrder(orderId, null);
assertFalse(result);
}

/**
* A buy order that fills two sell orders.
*/
@Test
@DisplayName("A buy order that fills two sell orders")
public void buyPartialFill() {

// Add two sell orders.
this.orderBook.process(this.orders[1]);
this.orderBook.process(this.orders[1]);

// Modify buy order to be twice the amount.
final Order buyOrder = this.orders[0];
buyOrder.setAmount(buyOrder.getAmount() * 2);

// Add the buy order.
final List<Trade> trades = this.orderBook.process(buyOrder);

// Two trades should have taken place.
assertTrue(trades.size() == 2);

// Order book should be empty.
assertTrue(this.orderBook.getBuyOrders().isEmpty());
assertTrue(this.orderBook.getSellOrders().isEmpty());

// The trades match the expected amt and price.
for (final Trade trade : trades) {
assertTrue(trade.getAmount() == buyOrder.getAmount());
assertTrue(trade.getPrice() == buyOrder.getPrice());
}
}

/**
* A buy order that fills two sell orders.
*/
@Test
@DisplayName("A sell order that fills two buy orders")
public void sellPartialFill() {

// Add two buy orders.
this.orderBook.process(this.orders[0]);
this.orderBook.process(this.orders[0]);

// Modify sell order to be twice the amount.
final Order sellOrder = this.orders[1];
sellOrder.setAmount(sellOrder.getAmount() * 2);

// Add the buy order.
final List<Trade> trades = this.orderBook.process(sellOrder);

// Two trades should have taken place.
assertTrue(trades.size() == 2);

// Order book should be empty.
assertTrue(this.orderBook.getBuyOrders().isEmpty());
assertTrue(this.orderBook.getSellOrders().isEmpty());

// The trades match the expected amt and price.
for (final Trade trade : trades) {
assertTrue(trade.getAmount() == sellOrder.getAmount());
assertTrue(trade.getPrice() == sellOrder.getPrice());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.laffyco.javamatchingengine.engine;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Date;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -20,6 +19,7 @@ public class OrderTests extends MatchingEngineTest {
* Compare two orders with different prices.
*/
@Test
@DisplayName("Compare two orders with different prices")
public void comparePrice() {
final double price2 = 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Date;

import javax.annotation.Resource;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;

Expand All @@ -24,6 +24,7 @@ public class SpreadTests extends MatchingEngineTest {
private OrderBook orderBook;

@Test
@DisplayName("Get the spread when there are two orders")
void spreadTwoOrders() {
final Order[] orders = {
new Order(2, 2.50, "sellOrder", Side.SELL, new Date()),
Expand All @@ -36,6 +37,7 @@ void spreadTwoOrders() {
}

@Test
@DisplayName("Get the spread when there are multiple orders")
void spreadMultipleOrders() {
final Order[] orders = {
new Order(2, 2.50, "sellOrder", Side.SELL, new Date()),
Expand All @@ -50,6 +52,7 @@ void spreadMultipleOrders() {
}

@Test
@DisplayName("Attempt to get the spread when there are no orders")
void spreadNoOrders() {
assertEquals(this.orderBook.getSpread(), 0);
}
Expand Down