@Test public void testToArrayWithParam() { addStringsToCollection(bag); Set<String> expected = new HashSet<>(); addStringsToCollection(expected); Assert.assertEquals(expected.toArray(), bag.toArray(new Object[bag.uniqueSize()])); }
@Test public void testSingularRemoval() { bag.add(test, 5); Assert.assertTrue(bag.remove(test)); Assert.assertEquals(4, bag.size()); }
private void assignStudent(Student s) { for (int y = 0; y < dorms.size(); y++) { if (s.hasRoom()) { break; } Dorm d = (Dorm) dorms.get(y); if (d.isFull()) { continue; } if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) { continue; } int i = 0; while (i < d.getNumRooms()) { if (s.hasRoom()) { break; } try { d.getRoomByIndex(i).addResident(s); } catch (IllegalArgumentException e) { } i++; } } }
@Test public void testRemoveAllFail() { addStringsToCollection(bag); Set<String> remove = new HashSet<>(); remove.add("Not in the list"); Assert.assertFalse(bag.removeAll(remove)); Assert.assertEquals(4, bag.size()); }
@Test public void testAddAll() { Set<String> expected = new HashSet<>(); addStringsToCollection(expected); bag.addAll(expected); Assert.assertEquals(expected.size(), bag.size()); }
@Test public void testIsEmpty() { Assert.assertTrue(bag.isEmpty()); bag.add("Hello World"); Assert.assertFalse(bag.isEmpty()); }
@Test public void testGetCount() { Bag<String> bag = new Bag<>(); String test = "Hello World"; bag.add(test, 5); Assert.assertEquals(5, bag.getCount(test)); }
@Test public void testRemoveAllWithCollection() { addStringsToCollection(bag); Set<String> remove = new HashSet<>(); addStringsToCollection(remove); Assert.assertTrue(bag.removeAll(remove)); Assert.assertEquals(0, bag.size()); }
@Test public void testMultipleRemoval() { bag.add(test, 5); Assert.assertTrue(bag.remove(test, 3)); Assert.assertEquals(2, bag.size()); Assert.assertTrue(bag.remove(test)); Assert.assertEquals(1, bag.size()); }
/** * containsAll - does this ArrayBag contain all of the items in otherBag? Returns false if * otherBag is null or empty. */ public boolean containsAll(Bag otherBag) { if (otherBag == null || otherBag.numItems() == 0) return false; Object[] otherItems = otherBag.toArray(); for (int i = 0; i < otherItems.length; i++) { if (!contains(otherItems[i])) return false; } return true; }
@Test public void testSizeIncrement() { bag.add(test, 1); bag.add(test1, 1); Assert.assertEquals(2, bag.size()); bag.add(test1, 3); Assert.assertEquals(5, bag.size()); }
@Test public void testRetainAll() { addStringsToCollection(bag); List<String> retain = new ArrayList<>(2); retain.add(test); retain.add(test1); bag.retainAll(retain); Assert.assertTrue(bag.containsAll(retain)); Assert.assertEquals(2, bag.size()); }
@Test public void testToArray() { addStringsToCollection(bag); Set<String> expected = new HashSet<>(); addStringsToCollection(expected); Assert.assertEquals(expected.toArray(), bag.toArray()); }
@Test public void testUniqueSize() { addStringsToCollection(bag); List<String> expected = new ArrayList<>(); addStringsToCollection(expected); Assert.assertEquals(expected.size(), bag.uniqueSize()); }
/** * unionWith * * <p>Return a non-duplicate ArrayBag as the union of the incoming and calling object */ public Bag unionWith(Bag other) { // Check if either bag is empty if (this.numItems() == 0 && other.numItems() == 0) { return new ArrayBag(); } // Determine the size of the new ArrayBag int newCapacity = this.capacity() + other.capacity(); // Create new ArrayBag to hold unioned values ArrayBag b = new ArrayBag(newCapacity); // Convert the incoming object to an array and copy each of its items // that are not in the new ArrayBag Object[] other_arr = other.toArray(); Object[] this_arr = this.toArray(); // Determine which ArrayBag has more items int loop_count = ((other_arr.length > this_arr.length) ? other_arr.length : this_arr.length); for (int i = 0; i < loop_count; i++) { // Add 'this' item if it doesn't exist and will not IndexOutOfBounds if (i < this_arr.length) { if (!b.contains(this_arr[i])) { b.add(this_arr[i]); } } // Add 'other's item if it doesn't exist and will not IndexOutOfBounds if (i < other_arr.length) { if (!b.contains(other_arr[i])) { b.add(other_arr[i]); } } } return b; }
private UpperclassHousingSelection() { dorms = new Bag(); // actually mixed-year but for simplicity I'm keeping it // upperclassman only for now Dorm Arrington = new Dorm("Arrington", false, false, 74); Dorm Ball = new Dorm("Ball", true, false, 53); Dorm Custis = new Dorm("Custis", false, false, 21); Dorm EagleLanding = new Dorm("Eagle Landing", false, false, 312); Dorm Framar = new Dorm("Framar", false, false, 11); Dorm Madison = new Dorm("Madison", false, false, 21); Dorm Marshall = new Dorm("Marshall", false, false, 74); Dorm Mason = new Dorm("Mason", false, false, 93); Dorm Westmoreland = new Dorm("Westmoreland", false, false, 56); // Willard houses students in single rooms which is not accounted for yet Dorm Willard = new Dorm("Willard", false, false, 91); // 100 of the rooms in the aparments are singles which is not // accounted for yet Dorm Apartments = new Dorm("Apartments", false, false, 381); // for extraneous students who commute or live off campus Dorm Offcampus = new Dorm("Offcampus", false, false, 3500); dorms.add(Ball); dorms.add(Arrington); dorms.add(Custis); dorms.add(Framar); dorms.add(Madison); dorms.add(Mason); dorms.add(Westmoreland); dorms.add(Willard); dorms.add(Apartments); dorms.add(EagleLanding); dorms.add(Offcampus); }
@Test public void testUniqueItems() { addStringsToCollection(bag); Set<String> expected = new HashSet<>(); addStringsToCollection(expected); Collection<String> actual = bag.getUniqueObjects(); Assert.assertEquals(expected.size(), actual.size()); Iterator<String> expIt = expected.iterator(); Iterator<String> actIt = actual.iterator(); while (expIt.hasNext()) { Assert.assertTrue(actIt.hasNext()); Assert.assertEquals(expIt.next(), actIt.next()); } }
/** * removeItems * * <p>Remove all occurences from the calling ArrayBag of the items found in parameter other. */ public boolean removeItems(Bag other) { // Convert 'other' to an array to iterate Object[] other_arr = other.toArray(); // Create found boolean boolean found = false; // Check all items of 'other', save a found operation to variable for (int i = 0; i < other_arr.length; i++) { // While loop until other_arr[i] does not exist in this Bag while (this.contains(other_arr[i])) { this.remove(other_arr[i]); found = true; } } // Return false if go through all of 'others' and no items are removed return ((found) ? true : false); }
/** * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) passed * to their dorm rooms, in a way that does not take race into account. (compare {@link * #assignByRace}.) As a precursor (side effect) to this, any students already existing in * upperclass dorms will be removed. */ public void assign(Bag students) { emptyDorms(); // Purge all freshmen. (We're not assigning those.) Bag upperclassmen = null; try { upperclassmen = (Bag) students.clone(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } for (int x = upperclassmen.size() - 1; x >= 0; x--) { Student s = (Student) upperclassmen.get(x); if (s.getGrade() < 2) { upperclassmen.remove(s); } } for (int x = 0; x < upperclassmen.size(); x++) { Student s = (Student) upperclassmen.get(x); if (s.getGrade() < 2) { // We're only assigning upperclassmen here. continue; } s.leaveRoom(); for (int y = 0; y < dorms.size(); y++) { if (s.hasRoom()) { break; } Dorm d = (Dorm) dorms.get(y); if (d.isFull()) { continue; } if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) { continue; } int i = 0; while (i < d.getNumRooms()) { if (s.hasRoom()) { break; } try { d.getRoomByIndex(i).addResident(s); } catch (IllegalArgumentException e) { // System.out.println(e); } i++; } } } // Error testing to see if there are upperclassmen who don't have a // room/dorms that aren't full // int q=0; for (int x = 0; x < upperclassmen.size(); x++) { Student s = (Student) upperclassmen.get(x); if (!s.hasRoom()) { System.out.println(s + " has no upperclass dorm room!"); } } }
@Test public void add0Items() { Assert.assertFalse(bag.add("Zero", 0)); }
@Test public void testAddDuplicate() { bag.add(test, 5); bag.add(test, 5); Assert.assertEquals(10, bag.getCount(test)); }
/* Test the ArrayBag implementation. */ public static void main(String[] args) { // Create a Scanner object for user input. Scanner in = new Scanner(System.in); // Create an ArrayBag named bag1. System.out.print("Size of bag 1: "); int size = in.nextInt(); Bag bag1 = new ArrayBag(size); in.nextLine(); // consume the rest of the line // ****** Additions ***** // Display capacity of new bag System.out.println("Bag 1 can hold up to " + bag1.capacity() + " items."); // Determine if ArrayBag is full if (bag1.isFull()) System.out.println("Bag is full."); else System.out.println("Bag is NOT full."); // Read in strings, add them to bag1, and print out bag1. String itemStr; for (int i = 0; i < size; i++) { System.out.print("item " + i + ": "); itemStr = in.nextLine(); bag1.add(itemStr); } System.out.println("bag 1 = " + bag1); System.out.println(); // Increase capacity by 2 System.out.println("Increasing Bag 1 capacity by 2."); bag1.increaseCapacity(2); System.out.println("Bag 1 can hold up to " + bag1.capacity() + " items."); // Select a random item and print it. Object item = bag1.grab(); System.out.println("grabbed " + item); System.out.println(); // Iterate over the objects in bag1, printing them one per // line. Object[] items = bag1.toArray(); for (int i = 0; i < items.length; i++) System.out.println(items[i]); System.out.println(); // Get an item to remove from bag1, remove it, and reprint the bag. System.out.print("item to remove: "); itemStr = in.nextLine(); if (bag1.contains(itemStr)) bag1.remove(itemStr); System.out.println("bag 1 = " + bag1); System.out.println(); // Remove all items from bag1 and reprint the bag. Bag other1 = new ArrayBag(2); other1.add("ff"); other1.add("aa"); System.out.println("Removing all items {aa, ff}"); bag1.removeItems(other1); System.out.println("bag 1 = " + bag1); System.out.println(); // Create two ArrayBags and return their union (with no duplicates) // {2, 2, 3, 5, 7, 7, 7, 8} Bag b1 = new ArrayBag(8); b1.add("2"); b1.add("2"); b1.add("3"); b1.add("5"); b1.add("7"); b1.add("7"); b1.add("7"); b1.add("8"); System.out.println("Bag b1 is " + b1); // {2, 3, 4, 5, 5, 6, 7} Bag b2 = new ArrayBag(7); b2.add("2"); b2.add("3"); b2.add("4"); b2.add("5"); b2.add("5"); b2.add("6"); b2.add("7"); System.out.println("Bag b2 is " + b2); // Be sure to test case with zero items Bag b3 = new ArrayBag(); b3 = b1.unionWith(b2); System.out.println("Union of b1 & b2 is " + b3); System.out.println("b3 capacity is " + b3.capacity()); System.out.println(); }
@Test public void testRemovalOfNotAddedObject() { bag.add("Added"); Assert.assertFalse(bag.remove("Not Added")); }
// main CELL CA loop************ public boolean iterateCells() { /* // modify consumption matrix for (int i=0;i<size;i++) for (int j=0;j<size;j++) consumption[i][j] = consumptionBasal[Cells[i][j]]; */ // if (cellList == null) cellList = new Bag(size * size); for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) { if (Cells[i][j] < 4) { // All tumour cell types have Cell > 0, now 0 corresponds to 'healthy cells' that // consume at basal rate only int[] p = new int[2]; p[0] = i; p[1] = j; cellList.add(p); if (Cells[i][j] == 1) { stem_cells_this_TS++; } else if (Cells[i][j] == 2 || Cells[i][j] == 3) { non_stem_cells_this_TS++; } } } while (cellList.size() != 0) { // Select the next lattice element at random int randomElemIndex = 0; if (cellList.size() > 1) randomElemIndex = random.nextInt(cellList.size() - 1); int[] point = (int[]) cellList.get(randomElemIndex); int rI = point[0]; int rJ = point[1]; cellList.remove(randomElemIndex); // Remove it from the cell list int cell = Cells[rI][rJ]; // Cell death // if ((Oxygen[rI][rJ]<hypoxia)) { if ((random.nextFloat() < deathprob) && Cells[rI][rJ] > 0) { // x% chances of dying Age[rI][rJ] = 0; if (Cells[rI][rJ] == 1) stemDeathCounter[rI][rJ]++; if (Cells[rI][rJ] < 4) { // TACDeathCounter[rI][rJ]++; Cells[rI][rJ] = 4; // was 0, now making necrotic area (truly empty) deaths++; stemBirthCounter[rI][rJ] = 0; carriedmutation[rI][rJ] = 0; // empty space now has no mutations carriedGenome[rI][rJ] = initGenome; // empty space now has no mutations } } else if ((cell == 3) && (Age[rI][rJ] > 100 * maxMatureCellAge)) { // added * to allow for an update each celltimestep/x // ************************** Age[rI][rJ] = 0; Cells[rI][rJ] = 4; // was 0, now making necrotic area (truly empty) // TACDeathCounter[rI][rJ]++; deaths++; } else if ((radiotherapy) && (cell == 2) && (random.nextFloat() > Oxygen[rI][rJ])) { // Radiotherapy Age[rI][rJ] = 0; if (Cells[rI][rJ] == 1) stemDeathCounter[rI][rJ]++; if ((Cells[rI][rJ] == 2) || (Cells[rI][rJ] == 3)) // TACDeathCounter[rI][rJ]++; Cells[rI][rJ] = 4; // make necrotic stemBirthCounter[rI][rJ] = 0; deaths++; } // healthy division else if ((cell == 0) && (vacantSites(rI, rJ) > 0)) { if (proliferation[cell] >= random.nextFloat()) { // If tossing the coin we are to proliferate... // if (Oxygen[rI][rJ]>prolifThreshold) { // AND the oxygen concentration is enough for // division.. // consumption[rI][rJ]=consumptionDivision[Cells[rI][rJ]]; int[] daughter = findEmptySite(rI, rJ); births++; Cells[daughter[0]][daughter[1]] = 0; carriedmutation[daughter[0]][daughter[1]] = 0; carriedGenome[daughter[0]][daughter[1]] = initGenome; // resetting space to healthy cell with no mutations } } // } // cancer division else if ((vacantSitesCancer(rI, rJ) > 0) && (cell > 0)) if (proliferation[cell] >= random.nextFloat()) { // If tossing the coin we are to proliferate... if ((cell == 1) || ((cell == 2) && (Age[rI][rJ] < maxProDivisions))) { // AND the cell is stem or TAC ... // if (Oxygen[rI][rJ]>prolifThreshold) { // AND the oxygen concentration is enough for // division.. // consumption[rI][rJ]=consumptionDivision[Cells[rI][rJ]]; int[] daughter = findEmptySiteCancer(rI, rJ); // and there is space (for cancer) // if ((daughter[0]==0) || (daughter[0]==size) || // (daughter[1]==0)||(daughter[1]==size)) {simulationFinished=true;} // stop sim if a // cell hits the edge births++; if (cell == 1) { // stem cell stemBirthsTotal[rI][rJ]++; stemBirthCounter[rI][rJ]++; if (asymmetricRatio > random.nextFloat()) { Cells[daughter[0]][daughter[1]] = 1; // placing the stem daughter stemBirthCounter[daughter[0]][daughter[1]] = stemBirthCounter[rI][rJ]; // update stem birth counter carriedmutation[daughter[0]][daughter[1]] = carriedmutation[rI][rJ]; // inherit mutational status of parent carriedGenome[daughter[0]][daughter[1]] = carriedGenome[rI][rJ]; // inherit mutational status of parent if (mutfreq > random.nextFloat()) { // small chance of mutation mutationNum++; // advance mutation number System.out.println( +carriedmutation[rI][rJ] + ", " + mutationNum + ", " + stem_cells_this_TS + ", " + non_stem_cells_this_TS + ", " + timestep); // print (parent,child) pair // tree.put(carriedmutation[rI][rJ], mutationNum); // timeTree.put(mutationNum, timestep); // hash table stuff if (0.5 > random.nextFloat()) { carriedmutation[daughter[0]][daughter[1]] = mutationNum; genomeToMod = new StringBuilder(carriedGenome[daughter[0]][daughter[1]]); genomeToMod.setCharAt( mutationNum - 1, '1'); // daughter carries new carriedGenome carriedGenome[daughter[0]][daughter[1]] = genomeToMod; // System.out.println (carriedGenome[rI][rJ]); // System.out.println (carriedGenome[daughter[0]][daughter[1]]); } // 50:50 mutate new position daughter else { carriedmutation[rI][rJ] = mutationNum; // else mutate original position daughter genomeToMod = new StringBuilder(carriedGenome[rI][rJ]); // genomeToMod = carriedGenome[rI][rJ]; carriedGenome[rI][rJ] = genomeToMod; genomeToMod.setCharAt( mutationNum - 1, '1'); // original carries new carriedGenome // System.out.println (carriedGenome[rI][rJ]); // System.out.println (carriedGenome[daughter[0]][daughter[1]]); } } } else { Cells[daughter[0]][daughter[1]] = 2; // asymmetric division, daughter is TAC stemBirthCounter[daughter[0]][daughter[1]] = 0; // reset stem counter carriedmutation[daughter[0]][daughter[1]] = carriedmutation[rI][rJ]; // TAC carries parental mutation flag carriedGenome[daughter[0]][daughter[1]] = carriedGenome[rI][rJ]; } // TAC carries parental genome // } // redundant from above // else { // Only if there's hypoxia induced change of symmetric division ratio // float newASR=asymmetricRatio+0*(hypoxia-Oxygen[rI][rJ]); // currently OFF by way of 0 the ^^ multiplier. // if (newASR>random.nextFloat()) // {Cells[daughter[0]][daughter[1]]=1;stemBirthCounter[daughter[0]][daughter[1]]=stemBirthCounter[rI][rJ];} // else // {Cells[daughter[0]][daughter[1]]=2;stemBirthCounter[daughter[0]][daughter[1]]=0;} // // Otherwise differentiate // } } else if (cell == 2) { // non-stem division // TACBirthCounter[rI][rJ]++; if (Age[rI][rJ] < maxProDivisions - 1) { Cells[daughter[0]][daughter[1]] = 2; Age[rI][rJ]++; Age[daughter[0]][daughter[1]] = Age[rI][rJ]; carriedmutation[daughter[0]][daughter[1]] = carriedmutation[rI][rJ]; // TAC carries parental mutation flag carriedGenome[daughter[0]][daughter[1]] = carriedGenome[rI][rJ]; // TAC carries parental genome } else { Cells[daughter[0]][daughter[1]] = 3; Cells[rI][rJ] = 3; Age[rI][rJ] = 0; Age[daughter[0]][daughter[1]] = Age[rI][rJ]; carriedmutation[daughter[0]][daughter[1]] = carriedmutation[rI][rJ]; // TAC carries parental mutation flag carriedGenome[daughter[0]][daughter[1]] = carriedGenome[rI][rJ]; // TAC carries parental genome } } } } else if (pMotility > random.nextFloat()) { // Migration = not in use int[] daughter = findEmptySite(rI, rJ); Cells[daughter[0]][daughter[1]] = cell; Cells[rI][rJ] = 0; Age[daughter[0]][daughter[1]] = Age[rI][rJ]; Age[rI][rJ] = 0; System.err.println("moving " + rI + ", " + rJ); } // Aging for mature cells if (cell == 3) Age[rI][rJ]++; } return true; }
@Test public void testClear() { addStringsToCollection(bag); bag.clear(); Assert.assertEquals(0, bag.size()); }
private void emptyDorms() { for (int y = 0; y < dorms.size(); y++) { Dorm d = (Dorm) dorms.get(y); d.emptyAllRooms(); } }
@Test public void testSimpleAdd() { Assert.assertTrue(bag.add("Hello World")); }
/** * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) students * passed to their dorm rooms, in a way that <i>does</i> take race into account. (compare {@link * #assign}.) Minorities will have a higher probability of rooming with other minorities than they * would otherwise have had. As a precursor (side effect) to this, any students already existing * in upperclass dorms will be removed. */ public void assignByRace(Bag upperclassmen) { emptyDorms(); boolean foundRoomie; for (int x = 0; x < upperclassmen.size(); x++) { foundRoomie = false; Student s = (Student) upperclassmen.get(x); // need to leave current room so that they can get a new room s.leaveRoom(); if (s.getRace() == Student.Race.MINORITY) { double rollDice = Sim.instance().random.nextDouble(); if (rollDice < Sim.PROB_DUAL_MINORITY) { for (int m = 0; m < upperclassmen.size(); m++) { Student r = (Student) upperclassmen.get(m); // if the potential roommate is the same gender, is a // minority, and has no room if (r.getId() == s.getId()) { continue; } if (r.getRace() == Student.Race.MINORITY && r.getGender() == s.getGender() && !(r.hasRoom()) && !(s.hasRoom())) { // find a room that is completely empty foundRoomie = true; for (int y = 0; y < dorms.size(); y++) { Dorm d = (Dorm) dorms.get(y); if (d.isFull()) { continue; } if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) { continue; } int i = 0; while (i < d.getNumRooms()) { if (s.hasRoom() && r.hasRoom()) { break; } try { if (d.getRoomByIndex(i).isEmpty()) { try { d.getRoomByIndex(i).addResidents(s, r); } catch (IllegalArgumentException e) { System.out.println(e); } } } catch (IllegalArgumentException e) { } i++; } } if (!s.hasRoom()) { // no empty room has been found to put these // students in; just assign them normally assignStudent(s); } if (!r.hasRoom()) { assignStudent(r); } } } if (foundRoomie == false) { // no same-minority, same-sex, roomless roommate found assignStudent(s); } } else { // dice roll not under PROB_DUAL_MINORITY assignStudent(s); } } else { // not a minority, assign normally assignStudent(s); } } }
@Test public void testRemoveAll() { addStringsToCollection(bag); Assert.assertTrue(bag.remove(test)); }