Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import static org.junit.Assert.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import org.junit.Test;
- import controller.MyPair;
- import javafx.scene.layout.Pane;
- import setup.Organism;
- import view.RectangleCoord;
- /**
- * Test the algorithms organism uses in the simulation these tests changed many
- * times throughout the project as algorithms and the simulation changed
- *
- * These tests imitate the algorithms in a way that does not requre the Main GUI
- * to be open to run them
- *
- * @author Alex
- *
- */
- public class OrganismTest {
- Organism o;
- ArrayList<String> food = new ArrayList<>();
- ArrayList<String> pollinate = new ArrayList<>();
- ArrayList<String> terrain = new ArrayList<>();
- RectangleCoord rect = new RectangleCoord(0, 0, 0, 0);
- Pane ip = new Pane();
- HashMap<Integer, RectangleCoord> map_intrectangle = new HashMap<>();
- public void t_setUp() {
- food.add("Name");
- terrain.add("");
- // define new rectangle grid for simulation
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- RectangleCoord temp = new RectangleCoord(0, 0, 0, 0);
- MyPair p = new MyPair(i, j);
- temp.setPair(p);
- int hash = i + (j * 10);
- System.out.println(hash);
- map_intrectangle.put(hash, temp);
- // defind new organism in this rectangle
- Organism temp_org = new Organism("Type", "Name", true, food, pollinate, terrain, 0, 0, 0, 0, 0, 0, 0, 0,
- 0,0, "Colour", temp, ip, 0, 0);
- temp.addSpecies(temp_org);
- }
- }
- // every rectangle now has one organism in
- }
- /**
- * initiate organism in a central location test find food with hunting
- * distance 1
- */
- @Test
- public void findFoodTest() {
- t_setUp();
- RectangleCoord rect2 = map_intrectangle.get(45); // central rectangle
- System.out.println(rect2.getName());
- o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0, 0, 0,0, "Colour",
- rect2, ip, 0, 0); // new organism in central rectangle w/
- // hunting distance 1
- ArrayList<RectangleCoord> neighbours = new ArrayList<>(); // set up
- // local
- // rectangles
- for (int i = 20; i < 30; i++) {
- RectangleCoord r = map_intrectangle.get(i);
- neighbours.add(r);
- }
- ArrayList<Organism> local_species = new ArrayList<>();
- for (RectangleCoord r : neighbours) {
- for (Organism o : r.speciesContained()) {
- local_species.add(o);
- }
- }
- assertEquals(10, local_species.size()); // test that all 10 local
- // species have been added to
- // lacal_species
- }
- /**
- * initiate organism and a local food list test to see if it correctly
- * identifies food in the list
- */
- @Test
- public void checkFodTest() {
- ArrayList<String> food_temp = new ArrayList<>(); // food organism can
- // eat
- String[] strs = { "Name", "Not Name" };
- food_temp.addAll(Arrays.asList(strs));
- ArrayList<Organism> localList = new ArrayList<>();// make a list of
- // local organisms
- for (int i = 0; i < 10; i++) {
- Organism o = new Organism("Type", "Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
- "Colour", rect, ip, 0, 0);
- localList.add(o);
- }
- Organism o1 = new Organism("Type", "Not Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0,0, 0, 0, 0, 0,
- "Colour", rect, ip, 0, 0);
- Organism o2 = new Organism("Type", "Not Not Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0,0, 0, 0, 0,
- "Colour", rect, ip, 0, 0);
- localList.add(o1);
- localList.add(o2);
- Organism test_o = new Organism("Type", "Different Name", true, food_temp, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0,
- 0, 0, 0, "Colour", rect, ip, 0, 0); // define organism to test
- // which can eat the other
- // organisms
- assertEquals(11, test_o.checkFood(localList)); // should be able to eat
- // 12 of the 13
- // organisms
- }
- /**
- * check to see if new competition detection in grid system is working
- */
- @Test
- public void checkCompetitionTest() {
- t_setUp();
- ArrayList<RectangleCoord> neighbours = new ArrayList<>(); // set up
- // local
- // rectangles
- for (int i = 20; i < 30; i++) {
- RectangleCoord r = map_intrectangle.get(i);
- neighbours.add(r);
- }
- Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0,0, 0, 0, 0, 0,
- 0, "Colour", rect, ip, 0, 0);// set up test organism
- ArrayList<Organism> local_competition = new ArrayList<>();
- for (RectangleCoord r : neighbours) {
- ArrayList<Organism> speciesContained = r.speciesContained();
- for (Organism o : speciesContained) {
- if (test_o.name.equals(o.getName())) {
- local_competition.add(o);
- } else {
- ArrayList<String> food_temp = o.getFood();
- search: for (String f : food_temp) {
- if (this.food.contains(f)) {
- local_competition.add(o);
- break search;
- }
- }
- }
- }
- }
- assertEquals(10, local_competition.size()); //test if all 10 are being
- //seen as competition
- }
- /**
- * Test growth rate algorithm with heavy competition
- */
- @Test
- public void calculateGrowthRateTest1() {
- Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0,0, 0, 1, 0, 0, 0,
- 5, 2, 0, "Colour", rect, ip, 0, 0);
- assertEquals(0, test_o.calculateGrowthRate(10, 10));
- }
- /**
- * Test growth rate algorithm with an abundance of food
- */
- @Test
- public void calculateGrowthRateTest2() {
- Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0,0, 1, 0, 0, 0,
- 5, 2, 0, "Colour", rect, ip, 0, 0);
- assertEquals(2, test_o.calculateGrowthRate(0, 10));
- }
- /**
- * Test growth rate algorithm with medium competition
- */
- @Test
- public void calculateGrowthRateTest3() {
- Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0,
- 5, 2, 0,0 , "Colour", rect, ip, 0, 0);
- assertEquals(1, test_o.calculateGrowthRate(6, 10));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement