Archived
0

Implement path find

This commit is contained in:
iMoHax
2014-08-15 16:36:37 +04:00
parent dc506b2920
commit 5fcf43b0a0
25 changed files with 1355 additions and 45 deletions

View File

@@ -3,9 +3,13 @@ package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ItemStatTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(ItemStatTest.class);
private final static Item ITEM1 = new Item("Item1");
@@ -29,12 +33,14 @@ public class ItemStatTest extends Assert {
@Test
public void testSell(){
LOG.info("Start sell test");
assertEquals(itemSellStat.getAvg(), (10+20+30+40)/4, 0);
assertEquals(itemSellStat.getBest().getPrice(), 10d, 0);
}
@Test
public void testBuy(){
LOG.info("Start buy test");
assertEquals(itemBuyStat.getAvg(), (100+200+300+400)/4, 0);
assertEquals(itemBuyStat.getBest().getPrice(), 400d, 0);
}

View File

@@ -0,0 +1,192 @@
package ru.trader.core;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtil;
import ru.trader.graph.Path;
import java.util.Collection;
public class MarketAnalyzerTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzerTest.class);
private static Market market = new SimpleMarket();
private static Vendor v1;
private static Vendor v2;
private static Vendor v3;
private static Vendor v4;
private static Vendor v5;
private static Vendor v6;
private static Vendor v7;
private static Vendor v8;
private static Vendor v9;
private static Vendor v10;
private static Vendor v11;
private static Item ITEM1 = new Item("ITEM1");
private static Item ITEM2 = new Item("ITEM2");
private static Item ITEM3 = new Item("ITEM3");
@Before
public void setUp() throws Exception {
v1 = new SimpleVendor("v1_x0y0z0");
v2 = new SimpleVendor("v2_x1y0z0");
v3 = new SimpleVendor("v3_x0y1z0");
v4 = new SimpleVendor("v4_x0y0z1");
v5 = new SimpleVendor("v5_x1y1z0");
v6 = new SimpleVendor("v6_x110y100z100");
v7 = new SimpleVendor("v7_x115y100z100");
v8 = new SimpleVendor("v8_x105y105z100");
v9 = new SimpleVendor("v9_x100y115z100");
v10 = new SimpleVendor("v10_x100y100z100");
v11 = new SimpleVendor("v11_x105y105z105");
v1.setX(0); v1.setY(0); v1.setZ(0);
v2.setX(1); v2.setY(0); v2.setZ(0);
v3.setX(0); v3.setY(1); v3.setZ(0);
v4.setX(0); v4.setY(0); v4.setZ(1);
v5.setX(1); v5.setY(1); v5.setZ(0);
v6.setX(110); v6.setY(100); v6.setZ(100);
v7.setX(115); v7.setY(100); v7.setZ(100);
v8.setX(105); v8.setY(105); v8.setZ(100);
v9.setX(100); v9.setY(115); v9.setZ(100);
v10.setX(100); v10.setY(100); v10.setZ(100);
v11.setX(105); v11.setY(105); v11.setZ(105);
market.add(v1);
market.add(v2);
market.add(v3);
market.add(v4);
market.add(v5);
market.add(v6);
market.add(v7);
market.add(v8);
market.add(v9);
market.add(v10);
market.add(v11);
market.add(v6, new Offer(OFFER_TYPE.SELL, ITEM1, 100));
market.add(v7, new Offer(OFFER_TYPE.SELL, ITEM1, 100));
market.add(v9, new Offer(OFFER_TYPE.SELL, ITEM1, 100));
market.add(v10, new Offer(OFFER_TYPE.SELL, ITEM1, 100));
market.add(v6, new Offer(OFFER_TYPE.BUY, ITEM1, 50));
market.add(v7, new Offer(OFFER_TYPE.BUY, ITEM1, 120));
market.add(v9, new Offer(OFFER_TYPE.BUY, ITEM1, 200));
market.add(v10, new Offer(OFFER_TYPE.BUY, ITEM1, 150));
market.add(v9, new Offer(OFFER_TYPE.SELL, ITEM2, 100));
market.add(v6, new Offer(OFFER_TYPE.BUY, ITEM2, 140));
market.add(v7, new Offer(OFFER_TYPE.SELL, ITEM3, 154));
market.add(v10, new Offer(OFFER_TYPE.BUY, ITEM3, 140));
market.add(v11, new Offer(OFFER_TYPE.BUY, ITEM3, 500));
}
@Test
public void testPaths() throws Exception {
LOG.info("Start paths test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(5);analyzer.setMaxDistance(1);analyzer.setStock(1);
Collection<Path<Vendor>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v2));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v3));
paths = analyzer.getPaths(v1, v4);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v4));
paths = analyzer.getPaths(v1, v5);
assertTrue(paths.isEmpty());
paths = analyzer.getPaths(v2, v5);
TestUtil.assertCollectionEquals(paths, Path.toPath(v2, v5));
paths = analyzer.getPaths(v4, v3);
assertTrue(paths.isEmpty());
}
@Test
public void testPathsWithStock() throws Exception {
LOG.info("Start paths with stock test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(5);analyzer.setMaxDistance(1);analyzer.setStock(2);
Collection<Path<Vendor>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v2));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v3));
paths = analyzer.getPaths(v1, v4);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v4));
paths = analyzer.getPaths(v1, v5);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v2, v5), Path.toPath(v1, v3, v5));
paths = analyzer.getPaths(v2, v5);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v2, v5));
paths = analyzer.getPaths(v4, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v4, v1, v3));
}
@Test
public void testPathsWithStockAndRefill() throws Exception {
LOG.info("Start paths with stock and refill test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(2);analyzer.setMaxDistance(10);analyzer.setStock(15);
Collection<Path<Vendor>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6), Path.toPath(v10, v11, v6),
Path.toPath(v10, v8, v6));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v3), Path.toPath(v1, v2, v3),
Path.toPath(v1, v4, v3), Path.toPath(v1, v5, v3)
);
}
@Test
public void testPathsWithStockAndRefill2() throws Exception {
LOG.info("Start paths with stock and refill test 2");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(3);analyzer.setMaxDistance(10);analyzer.setStock(15);
Collection<Path<Vendor>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6), Path.toPath(v10, v11, v6),
Path.toPath(v10, v8, v6), Path.toPath(v10, v8, v11, v6));
paths = analyzer.getPaths(v10, v7);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6, v7), Path.toPath(v10, v11, v6, v7),
Path.toPath(v10, v8, v6, v7)
);
paths = analyzer.getPaths(v10, v8);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v8), Path.toPath(v10, v11, v8),
Path.toPath(v10, v11, v6, v8), Path.toPath(v10, v6, v8), Path.toPath(v10, v6, v11, v8));
paths = analyzer.getPaths(v10, v9);
assertTrue(paths.isEmpty());
paths = analyzer.getPaths(v10, v10);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v11, v10), Path.toPath(v10, v6, v10),
Path.toPath(v10, v11, v6, v10), Path.toPath(v10, v6, v11, v10),
Path.toPath(v10, v8, v10), Path.toPath(v10, v8, v11, v10),
Path.toPath(v10, v8, v6, v10));
}
@After
public void tearDown() throws Exception {
market = new SimpleMarket();
}
}

View File

@@ -3,8 +3,12 @@ package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MarketTest1 extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(MarketTest1.class);
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Item ITEM3 = new Item("Item3");
@@ -64,6 +68,7 @@ public class MarketTest1 extends Assert {
@Test
public void testBestSell(){
LOG.info("Start best sell test");
Offer test = market.getStatSell(ITEM1).getBest();
assertEquals(test, bestSellOffer1);
test = market.getStatSell(ITEM2).getBest();
@@ -74,6 +79,7 @@ public class MarketTest1 extends Assert {
@Test
public void testBestBuy(){
LOG.info("Start best buy test");
Offer test = market.getStatBuy(ITEM1).getBest();
assertEquals(test, bestBuyOffer1);
test = market.getStatBuy(ITEM2).getBest();

View File

@@ -3,9 +3,13 @@ package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VendorTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(VendorTest.class);
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Offer SELL_OFFER = new Offer(OFFER_TYPE.SELL, ITEM1, 10);
@@ -24,6 +28,7 @@ public class VendorTest extends Assert {
@Test
public void testAddSellOffer(){
LOG.info("Start add sell offer test");
final Iterable<Offer> offers = sellVendor.getAllSellOffers();
Offer test = offers.iterator().next();
assertEquals(test, SELL_OFFER);
@@ -31,18 +36,21 @@ public class VendorTest extends Assert {
@Test
public void testAddSellOffer2(){
LOG.info("Start add sell offer test2");
final Iterable<Offer> offers = sellVendor.getAllBuyOffers();
assertFalse(offers.iterator().hasNext());
}
@Test
public void testAddBuyOffer(){
LOG.info("Start add buy offer test");
final Iterable<Offer> offers = buyVendor.getAllSellOffers();
assertFalse(offers.iterator().hasNext());
}
@Test
public void testAddBuyOffer2(){
LOG.info("Start add buy offer test2");
final Iterable<Offer> offers = buyVendor.getAllBuyOffers();
Offer test = offers.iterator().next();
assertEquals(test, BUY_OFFER);
@@ -50,36 +58,42 @@ public class VendorTest extends Assert {
@Test
public void testGetSellOfferOnSellVendor(){
LOG.info("Start get sell offer from sell vendor test");
Offer test = sellVendor.getSell(ITEM1);
assertEquals(test, SELL_OFFER);
}
@Test
public void testGetSellOfferOnBuyVendor(){
LOG.info("Start get sell offer from buy vendor test");
Offer test = buyVendor.getSell(ITEM1);
assertNull(test);
}
@Test
public void testGetWrongItemSellOfferOnSellVendor(){
LOG.info("Start get wrong item from sell vendor test");
Offer test = buyVendor.getSell(ITEM2);
assertNull(test);
}
@Test
public void testGetBuyOfferOnSellVendor(){
LOG.info("Start get buy offer from sell vendor test");
Offer test = sellVendor.getBuy(ITEM1);
assertNull(test);
}
@Test
public void testGetBuyOffersOnBuyVendor(){
LOG.info("Start get buy offer from buy vendor test");
Offer test = buyVendor.getBuy(ITEM1);
assertEquals(test, BUY_OFFER);
}
@Test
public void testGetWrongItemBuyOfferOnBuyVendor(){
LOG.info("Start get wrong item from buy vendor test");
Offer test = sellVendor.getBuy(ITEM2);
assertNull(test);
}

View File

@@ -3,11 +3,15 @@ package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtil;
import java.util.Collection;
public class VendorTest2 extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(VendorTest2.class);
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Item ITEM3 = new Item("Item3");
@@ -42,24 +46,28 @@ public class VendorTest2 extends Assert {
@Test
public void testGetSellOffer(){
LOG.info("Start get sell offer test");
final Offer test = sellVendor.getSell(ITEM1);
assertEquals(test, DUBLE_SELL_OFFER1);
}
@Test
public void testGetBuyOffer(){
LOG.info("Start get buy offer test");
final Offer test = buyVendor.getBuy(ITEM1);
assertEquals(test, DUBLE_BUY_OFFER1);
}
@Test
public void testGetAllSellOffer(){
LOG.info("Start get all sell offer test");
final Collection<Offer> test = sellVendor.getAllSellOffers();
TestUtil.assertCollectionContainAll(test, DUBLE_SELL_OFFER1, SELL_OFFER2, SELL_OFFER3);
}
@Test
public void testGetAllBuyOffer(){
LOG.info("Start get all buy offer test");
final Collection<Offer> test = buyVendor.getAllBuyOffers();
TestUtil.assertCollectionContainAll(test, DUBLE_BUY_OFFER1, BUY_OFFER3, BUY_OFFER2);
}

View File

@@ -0,0 +1,314 @@
package ru.trader.graph;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtil;
import java.util.ArrayList;
import java.util.Collection;
public class GraphTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(GraphTest.class);
private final static ArrayList<Point> entrys = new ArrayList<>();
private final static Point x1 = new Point("x1",-40);
private final static Point x2 = new Point("x2",-20);
private final static Point x3 = new Point("x3",-10, true);
private final static Point x4 = new Point("x4",-5, true);
private final static Point x5 = new Point("x5",0);
private final static Point x6 = new Point("x6",5);
private final static Point x7 = new Point("x7",20);
private final static Point x8 = new Point("x8",30);
private final static Point x9 = new Point("x9",40);
private final static Point x10 = new Point("x10",50);
@Before
public void setUp() throws Exception {
entrys.add(x1);
entrys.add(x2);
entrys.add(x3);
entrys.add(x4);
entrys.add(x5);
entrys.add(x6);
entrys.add(x7);
entrys.add(x8);
entrys.add(x9);
entrys.add(x10);
}
@Test
public void testBuild0() throws Exception {
LOG.info("Start graph build test0");
Graph<Point> graph = new Graph<>(x5, entrys, 4.9, 10);
// x5
assertFalse(graph.isAccessible(x1));
assertFalse(graph.isAccessible(x2));
assertFalse(graph.isAccessible(x3));
assertFalse(graph.isAccessible(x4));
assertTrue(graph.isAccessible(x5));
assertFalse(graph.isAccessible(x6));
assertFalse(graph.isAccessible(x7));
assertFalse(graph.isAccessible(x8));
assertFalse(graph.isAccessible(x9));
assertFalse(graph.isAccessible(x10));
}
@Test
public void testBuild1() throws Exception {
LOG.info("Start graph build test1");
Graph<Point> graph = new Graph<>(x5, entrys, 5.1, true, 2);
// x5 <-> x4 <-refill-> x3, x5 -> x6
assertFalse(graph.isAccessible(x1));
assertFalse(graph.isAccessible(x2));
assertTrue(graph.isAccessible(x3));
assertTrue(graph.isAccessible(x4));
assertTrue(graph.isAccessible(x5));
assertTrue(graph.isAccessible(x6));
assertFalse(graph.isAccessible(x7));
assertFalse(graph.isAccessible(x8));
assertFalse(graph.isAccessible(x9));
assertFalse(graph.isAccessible(x10));
Vertex<Point> x = graph.getVertex(x5);
// x5 -> x4, x5 -> x6
checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10});
// x4 -> x5
x = graph.getVertex(x4);
checkEdges(x, new Point[]{x5, x3}, new Point[]{x1, x2, x6, x7, x8, x9, x10});
// x6 <- x5
x = graph.getVertex(x6);
checkEdges(x, new Point[]{}, new Point[]{x1, x2, x3, x4, x5, x7, x8, x9, x10});
}
private void checkEdges(Vertex<Point> vertex, Point[] trueEdge, Point[] falseEdge){
for (Point point : trueEdge) {
assertTrue(String.format("%s must have edge to %s", vertex, point), vertex.isConnected(point));
}
for (Point point : falseEdge) {
assertFalse(String.format("%s must not have edge to %s", vertex, point), vertex.isConnected(point));
}
}
@Test
public void testBuild2() throws Exception {
LOG.info("Start graph build test2");
Graph<Point> graph = new Graph<>(x5, entrys, 5.1, 3);
// x5 <-> x4 <-> x3, x5 <-> x6
assertFalse(graph.isAccessible(x1));
assertFalse(graph.isAccessible(x2));
assertTrue(graph.isAccessible(x3));
assertTrue(graph.isAccessible(x4));
assertTrue(graph.isAccessible(x5));
assertTrue(graph.isAccessible(x6));
assertFalse(graph.isAccessible(x7));
assertFalse(graph.isAccessible(x8));
assertFalse(graph.isAccessible(x9));
assertFalse(graph.isAccessible(x10));
Vertex<Point> x = graph.getVertex(x5);
// x5 -> x4, x5 -> x6
checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10});
// x3 -> x4
x = graph.getVertex(x3);
checkEdges(x, new Point[]{x4}, new Point[]{x1, x2, x5, x6, x7, x8, x9, x10});
// x4 -> x5, x4 -> x3
x = graph.getVertex(x4);
checkEdges(x, new Point[]{x3, x5}, new Point[]{x1, x2, x6, x7, x8, x9, x10});
// x6 -> x5
x = graph.getVertex(x6);
checkEdges(x, new Point[]{x5}, new Point[]{x1, x2, x3, x4, x7, x8, x9, x10});
}
@Test
public void testBuild3() throws Exception {
LOG.info("Start graph build test3");
Graph<Point> graph = new Graph<>(x5, entrys, 5.1, 3);
// x5 <-> x4 <-> x3, x5 <-> x6
assertFalse(graph.isAccessible(x1));
assertFalse(graph.isAccessible(x2));
assertTrue(graph.isAccessible(x3));
assertTrue(graph.isAccessible(x4));
assertTrue(graph.isAccessible(x5));
assertTrue(graph.isAccessible(x6));
assertFalse(graph.isAccessible(x7));
assertFalse(graph.isAccessible(x8));
assertFalse(graph.isAccessible(x9));
assertFalse(graph.isAccessible(x10));
Vertex<Point> x = graph.getVertex(x5);
// x5 -> x4, x5 -> x6
checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10});
// x3 -> x4
x = graph.getVertex(x3);
checkEdges(x, new Point[]{x4}, new Point[]{x1, x2, x5, x6, x7, x8, x9, x10});
// x4 -> x5, x4 -> x3
x = graph.getVertex(x4);
checkEdges(x, new Point[]{x3, x5}, new Point[]{x1, x2, x6, x7, x8, x9, x10});
// x6 -> x5
x = graph.getVertex(x6);
checkEdges(x, new Point[]{x5}, new Point[]{x1, x2, x3, x4, x7, x8, x9, x10});
}
@Test
public void testBuild4() throws Exception {
LOG.info("Start graph build test4");
Graph<Point> graph = new Graph<>(x5, entrys, 15.1, 3);
// x5 <-> x4 <-> x3 -> x2, x5 <-> x6 <-> x7 -> x8
// x5 <-> x3, x5 <-> x4 <-> x2, x3 <-> x6, x4 <-> x6
assertFalse(graph.isAccessible(x1));
assertTrue(graph.isAccessible(x2));
assertTrue(graph.isAccessible(x3));
assertTrue(graph.isAccessible(x4));
assertTrue(graph.isAccessible(x5));
assertTrue(graph.isAccessible(x6));
assertTrue(graph.isAccessible(x7));
assertTrue(graph.isAccessible(x8));
assertFalse(graph.isAccessible(x9));
assertFalse(graph.isAccessible(x10));
Vertex<Point> x = graph.getVertex(x5);
// x5 -> x4, x5 -> x3, x5 -> x6
checkEdges(x, new Point[]{x3, x4, x6}, new Point[]{x1, x2, x7, x8, x9, x10});
// x2 -> x3, x2 -> x4
x = graph.getVertex(x2);
checkEdges(x, new Point[]{x3, x4}, new Point[]{x1, x5, x6, x7, x8, x9, x10});
// x3 -> x4, x3 -> x2, x3 -> x5, x3 -> x6
x = graph.getVertex(x3);
checkEdges(x, new Point[]{x2, x4, x5, x6}, new Point[]{x1, x7, x8, x9, x10});
// x4 -> x5, x4 -> x3, x4 -> x2, x4 -> x6
x = graph.getVertex(x4);
checkEdges(x, new Point[]{x2, x3, x5, x6}, new Point[]{x1, x7, x8, x9, x10});
// x6 -> x5, x6 -> x7, x6 -> x3, x6 -> x4
x = graph.getVertex(x6);
checkEdges(x, new Point[]{x5, x7, x3, x4}, new Point[]{x1, x2, x8, x9, x10});
// x7 -> x6, x7 -> x8
x = graph.getVertex(x7);
checkEdges(x, new Point[]{x6, x8}, new Point[]{x1, x2, x3, x4, x5, x9, x10});
// x8 <- x7
x = graph.getVertex(x8);
checkEdges(x, new Point[]{}, new Point[]{x1, x2, x3, x4, x5, x6, x7, x9, x10});
}
@Test
public void testGetPaths() throws Exception {
LOG.info("Start get paths test");
Graph<Point> graph = new Graph<>(x5, entrys, 5.1, 2);
// x5 <-> x4, x5 <-> x6
Collection<Path<Point>> paths = graph.getPathsTo(x4, true);
TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x4));
paths = graph.getPathsTo(x6, true);
TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x6));
paths = graph.getPathsTo(x7, true);
assertEquals(paths.size(), 0);
}
@Test
public void testGetPaths2() throws Exception {
LOG.info("Start get paths test2");
Graph<Point> graph = new Graph<>(x5, entrys, 15.1, 3);
// x5 <-> x4 <-> x3 <-> x2, x5 <-> x6 <-> x7 <-> x8
// x5 <-> x3, x4 <-> x2, x3 <-> x6, x4 <-> x6
Collection<Path<Point>> paths = graph.getPathsTo(x8, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6, x7, x8));
paths = graph.getPathsTo(x7, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6, x7), Path.toPath(x5, x4, x6, x7), Path.toPath(x5, x3, x6, x7));
paths = graph.getPathsTo(x7, false);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x3, x6, x7));
paths = graph.getPathsTo(x4, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4), Path.toPath(x5, x6, x4), Path.toPath(x5, x3, x4),
Path.toPath(x5, x6, x3, x4), Path.toPath(x5, x3, x2, x4), Path.toPath(x5, x3, x6, x4));
paths = graph.getPathsTo(x5, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4, x5), Path.toPath(x5, x4, x6, x5), Path.toPath(x5, x4, x3, x5),
Path.toPath(x5, x6, x5), Path.toPath(x5, x6, x4, x5), Path.toPath(x5, x6, x3, x5), Path.toPath(x5, x3, x5),
Path.toPath(x5, x3, x4, x5), Path.toPath(x5, x3, x6, x5));
Path<Point> fast = graph.getFastPathTo(x8);
assertEquals(fast, Path.toPath(x5, x6, x7, x8));
fast = graph.getFastPathTo(x7);
assertEquals(fast, Path.toPath(x5, x6, x7));
fast = graph.getFastPathTo(x4);
assertEquals(fast, Path.toPath(x5, x4));
}
@Test
public void testGetRefillPaths() throws Exception {
LOG.info("Start get refill paths");
Graph<Point> graph = new Graph<>(x5, entrys, 10.1, 3);
// x5 <-> x4 <- refill -> x3 <- refill -> x2, x5 <-> x6
// x5 <-> x3 <- refill -> x2, x5 <-> x4 <- refill -> x6
Collection<Path<Point>> paths = graph.getPathsTo(x1, true);
assertTrue(paths.isEmpty());
paths = graph.getPathsTo(x2, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4, x3, x2), Path.toPath(x5, x3, x2));
paths = graph.getPathsTo(x6, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6), Path.toPath(x5, x4, x6), Path.toPath(x5, x3, x4, x6));
Path<Point> fast = graph.getFastPathTo(x2);
assertEquals(fast, Path.toPath(x5, x3, x2));
}
@Test
public void testGetRefillPaths2() throws Exception {
LOG.info("Start get refill paths 2 ");
Graph<Point> graph = new Graph<>(x5, entrys, 15.1, true, 4);
// x5 <-> x4 <-> x3 - refill -> x2,
// x5 <-> x6 <-> x4 <-refill -> x2
// x5 <-> x3 <- refill -> x2
// x5 <-> x4 <- refill -> x6
Collection<Path<Point>> paths = graph.getPathsTo(x1, true);
assertTrue(paths.isEmpty());
paths = graph.getPathsTo(x2, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x3, x4, x2), Path.toPath(x5, x3, x2),
Path.toPath(x5, x4, x3, x2), Path.toPath(x5, x4, x2),
Path.toPath(x5, x6, x4, x2), Path.toPath(x5, x6, x4, x3, x2));
paths = graph.getPathsTo(x6, true);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6), Path.toPath(x5, x4, x6),
Path.toPath(x5, x3, x4, x6), Path.toPath(x5, x3, x6), Path.toPath(x5, x4, x3, x6));
paths = graph.getPathsTo(x7, true);
assertTrue(paths.isEmpty());
Path<Point> fast = graph.getFastPathTo(x2);
assertEquals(fast, Path.toPath(x5, x3, x2));
}
@After
public void tearDown() throws Exception {
entrys.clear();
}
}

View File

@@ -0,0 +1,45 @@
package ru.trader.graph;
public class Point implements Connectable<Point> {
private int x;
private String name;
private boolean refill;
public Point(String name, int x) {
this(name, x, false);
}
public Point(String name, int x, boolean refill) {
this.x = x;
this.name = name;
this.refill = refill;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x;
}
@Override
public int hashCode() {
return x;
}
@Override
public double getDistance(Point other) {
return Math.abs(x-other.x);
}
@Override
public boolean canRefill() {
return refill;
}
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,30 @@
package ru.trader.graph;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VertexTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(VertexTest.class);
private final static Point x1 = new Point("x1",1);
private final static Point x2 = new Point("x2",4);
private final static Point x3 = new Point("x3",-2);
private final static Point x4 = new Point("x4",5);
@Test
public void testContains() throws Exception {
LOG.info("Start vertex contains test");
Vertex<Point> vertex = new Vertex<>(x1);
vertex.addEdge(new Edge<>(vertex, new Vertex<>(x2)));
vertex.addEdge(new Edge<>(vertex, x3));
assertFalse("Vertex must contains entry",vertex.isConnected(x1));
assertTrue("Vertex must contains entry",vertex.isConnected(new Vertex<>(x3)));
assertTrue("Vertex must contains entry",vertex.isConnected(x2));
assertFalse("Vertex not must contains entry", vertex.isConnected(new Vertex<>(x4)));
}
}

View File

@@ -2,6 +2,8 @@ package ru.trader.store;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import ru.trader.core.Market;
@@ -10,9 +12,11 @@ import java.io.IOException;
import java.io.InputStream;
public class LoadTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(LoadTest.class);
@Test
public void testLoad(){
LOG.info("Start world load test");
InputStream is = getClass().getResourceAsStream("/test.xml");
Market world;
try {

View File

@@ -0,0 +1,6 @@
log4j.rootLogger = INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p: %d{dd.MM.yyyy HH:mm:ss} (%F:%L) - %m%n