Test for Account class

This commit is contained in:
7u83 2018-12-08 15:00:35 +01:00
parent 8dbe6cfe6d
commit 571b8be7ee
9 changed files with 221 additions and 28 deletions

View File

@ -1,4 +1,4 @@
#Sat, 08 Dec 2018 12:17:24 +0100
#Sat, 08 Dec 2018 14:53:59 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset;
import opensesim.world.GodWorld;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class DummyAsset extends AbstractAsset {
@Override
public String getTypeName() {
return "DummyAsset";
}
DummyAsset(GodWorld gw, JSONObject cfg) {
super(gw, cfg);
}
public DummyAsset(String symbol, String name, int decimals) {
setSymbol(symbol);
setDecimals(decimals);
setName(name);
}
}

View File

@ -37,16 +37,17 @@ import java.util.HashSet;
*/
public class Scollection<KEY, OBJ> {
HashSet<OBJ> byObj = new HashSet<>();
// HashSet<OBJ> byObj = new HashSet<>();
HashMap<KEY, OBJ> byKey = new HashMap<>();
public void add(KEY key, OBJ obj) {
byObj.add(obj);
// byObj.add(obj);
byKey.put(key, obj);
}
public Collection<OBJ> getCollection(){
return Collections.unmodifiableCollection(byObj);
return Collections.unmodifiableCollection(byKey.values());
}
public OBJ get(KEY key){

View File

@ -66,24 +66,41 @@ public abstract class AbstractAsset implements GetJson {
this.world = world;
}
public AbstractAsset(){
}
public abstract String getTypeName();
public int getDecimals() {
public final int getDecimals() {
return decimals;
}
protected final void setDecimals(int decimals){
this.decimals=decimals;
}
protected void setDescription(String description) {
this.description = description;
}
public String getSymbol() {
public final String getSymbol() {
return symbol;
}
protected final void setSymbol(String symbol){
this.symbol=symbol;
}
public String getName() {
public final String getName() {
return name;
}
protected final void setName(String name){
this.name=name;
}
public String getDescription() {
return description;

View File

@ -25,13 +25,9 @@
*/
package opensesim.world;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import opensesim.world.Exchange;
import java.util.concurrent.ConcurrentHashMap;
import static jdk.vm.ci.code.CodeUtil.K;
import opensesim.sesim.interfaces.Asset;
import opensesim.sesim.interfaces.Trader;
/**
@ -39,27 +35,33 @@ import opensesim.sesim.interfaces.Trader;
* @author 7u83 <7u83@mail.ru>
*/
public class Account {
ConcurrentHashMap <Asset,Double> assets;
ConcurrentHashMap <AbstractAsset,Double> assets = new ConcurrentHashMap<>();
Trader owner;
Exchange exchange;
public Map<Asset,Double> getAssets() {
return Collections.unmodifiableMap(assets);
public Map<AbstractAsset,Double> getAssets() {
return Collections.unmodifiableMap(assets);
}
public Trader getOwner() {
return owner;
}
public Exchange getExchange() {
return exchange;
}
Account(Exchange exchange){
assets = new ConcurrentHashMap<>();
// assets = new ConcurrentHashMap<>();
}
Account(){
}
void add(AssetPack pack){
assets.put( pack.asset, get(pack.asset)+pack.volume);
}
public double get(AbstractAsset asset){
return assets.getOrDefault(asset, 0.0);
}
}

View File

@ -25,18 +25,20 @@
*/
package opensesim.world;
import opensesim.world.AbstractAsset;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AssetVol {
public class AssetPack {
public AbstractAsset asset;
public double volume;
public AssetVol(AbstractAsset asset, double volume){
public AssetPack(AbstractAsset asset, double volume){
this.asset=asset;
this.volume=volume;
}
public AssetPack(World world, String symbol,double volume){
this(world.getAssetBySymbol(symbol),volume);
}
}

View File

@ -34,9 +34,9 @@ import opensesim.sesim.interfaces.FeeModel;
public class DefaultFeeModel implements FeeModel{
public AssetVol xgetTakerFee(AssetPair pair) {
public AssetPack xgetTakerFee(AssetPair pair) {
double vol = 5;
return new AssetVol(pair.getCurrency(),vol);
return new AssetPack(pair.getCurrency(),vol);
}

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.world;
import java.util.Map;
import opensesim.sesim.Assets.CurrencyAsset;
import opensesim.sesim.Assets.DummyAsset;
import opensesim.sesim.interfaces.Trader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AccountTest {
public AccountTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getAssets method, of class Account.
*/
/* @Test
public void testGetAssets() {
System.out.println("getAssets");
Account instance = new Account();
Map<AbstractAsset, Double> expResult = null;
Map<AbstractAsset, Double> result = instance.getAssets();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}
*/
/**
* Test of getOwner method, of class Account.
*/
@Test
public void testGetOwner() {
System.out.println("getOwner");
Account instance = new Account();
Trader expResult = null;
Trader result = instance.getOwner();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}
/**
* Test of add method, of class Account.
*/
@Test
public void testAdd() {
System.out.println("add");
AbstractAsset c = new DummyAsset("EUR", "Euro", 2);
Double expResult = 123.0;
AssetPack pack = new AssetPack(c, expResult);
Account account = new Account();
account.add(pack);
Double result;
result = account.get(c);
assertEquals(expResult, result);
account.add(pack);
result = account.get(c);
expResult *= 2;
assertEquals(expResult, result);
}
}

View File

@ -94,9 +94,9 @@ public class ExchangeTest {
Account result = instance.createAccount();
assertEquals(expResult, result);
// assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
// fail("The test case is a prototype.");
}
}