Added tests

Deliverable_3
asdfasdf 6 years ago
parent f7137a818b
commit d7ab73eb1c

@ -5,15 +5,19 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import data.StockType; import data.StockType;
//import data.Nyse; import data.Nyse;
//import data.Nasdaq; //import data.Nasdaq;
//import data.Jpx; //import data.Jpx;
//import data.Sse; //import data.Sse;
//import data.Hkse; //import data.Hkse;
//Contains one instance of all supported
//StockType
public class StypeMap { public class StypeMap {
private HashMap<String, StockType> smap; private HashMap<String, StockType> smap;
// Effects: smap is initialized
// with supported StockType
public StypeMap() { public StypeMap() {
smap = new HashMap<String, StockType>(); smap = new HashMap<String, StockType>();
smap.put("Nyse", new Nyse()); smap.put("Nyse", new Nyse());
@ -23,6 +27,8 @@ public class StypeMap {
//smap.put("Hkse", new Hkse()); //smap.put("Hkse", new Hkse());
} }
// Effects: Returns the StockType
// Require: Valid typeString
public StockType getStype(String typeString) { public StockType getStype(String typeString) {
return smap.get(typeString); return smap.get(typeString);
} }

@ -6,21 +6,36 @@ import data.StockEntry;
public class WatchList { public class WatchList {
private LinkedHashMap<String, StockEntry> listdata; private LinkedHashMap<String, StockEntry> listdata;
// Effects: List is empty
public WatchList() { public WatchList() {
this.listdata = new LinkedHashMap<String, StockEntry>(); this.listdata = new LinkedHashMap<String, StockEntry>();
} }
// Effects: Add an entry with key==target
// (XXX For now, only Nyse)
// Modifies: this.listdata
// Requires: target doesn't already exists
public void addStock(String target, StockType stype) { public void addStock(String target, StockType stype) {
//The only implementation yet //The only implementation yet
this.listdata.put(target, new StockEntry(stype, target)); this.listdata.put(target, new StockEntry(stype, target));
} }
// Effects: Delete an entry with key==target
// Modifies: this.listdata
// Requires: target exists in list
public void delStock(String target) { public void delStock(String target) {
this.listdata.remove(target); this.listdata.remove(target);
} }
// Effects: Return an iterator of the list
// Warning: not thread-safe, fail-fast
public Iterator iterator() { public Iterator iterator() {
Set entryset = listdata.entrySet(); Set entryset = listdata.entrySet();
return entryset.iterator(); return entryset.iterator();
} }
// Effects: Return the size of list
public int size() {
return listdata.size();
}
} }

@ -0,0 +1,23 @@
import data.StypeMap;
import data.StockType;
import data.Nyse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StypeMapTest {
private StypeMap stypemap;
@BeforeEach
public void runBefore() {
stypemap = new StypeMap();
}
@Test
public void testGetStype() {
StockType stype = stypemap.getStype("Nyse");
assertEquals(stype.getName(), "NYSE");
}
}

@ -0,0 +1,50 @@
import data.WatchList;
import data.StypeMap;
import data.StockType;
import data.Nyse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class WatchListTest {
private WatchList watchlist;
private StypeMap smap;
@BeforeEach
public void runBefore() {
watchlist = new WatchList();
smap = new StypeMap();
}
@Test
public void testAddStock() {
StockType nyyyse = smap.getStype("Nyse");
for (int i = 0; i < 100; i++) {
watchlist.addStock(Integer.toString(i), nyyyse);
}
}
@Test
public void testSize() {
assertEquals(watchlist.size(), 0);
StockType nyyyse = smap.getStype("Nyse");
for (int i = 0; i < 100; i++) {
watchlist.addStock(Integer.toString(i), nyyyse);
}
assertEquals(watchlist.size(), 100);
}
@Test
public void testDelStock() {
StockType nyyyse = smap.getStype("Nyse");
for (int i = 0; i < 100; i++) {
watchlist.addStock(Integer.toString(i), nyyyse);
}
for (int i = 0; i < 50; i++) {
watchlist.delStock(Integer.toString(i));
}
assertEquals(watchlist.size(), 50);
}
}
Loading…
Cancel
Save