Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. package test;
  2. import static org.junit.Assert.*;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import org.junit.Test;
  7. import controller.MyPair;
  8. import javafx.scene.layout.Pane;
  9. import setup.Organism;
  10. import view.RectangleCoord;
  11. /**
  12. * Test the algorithms organism uses in the simulation these tests changed many
  13. * times throughout the project as algorithms and the simulation changed
  14. *
  15. * These tests imitate the algorithms in a way that does not requre the Main GUI
  16. * to be open to run them
  17. *
  18. * @author Alex
  19. *
  20. */
  21. public class OrganismTest {
  22. Organism o;
  23. ArrayList<String> food = new ArrayList<>();
  24. ArrayList<String> pollinate = new ArrayList<>();
  25. ArrayList<String> terrain = new ArrayList<>();
  26. RectangleCoord rect = new RectangleCoord(0, 0, 0, 0);
  27. Pane ip = new Pane();
  28. HashMap<Integer, RectangleCoord> map_intrectangle = new HashMap<>();
  29. public void t_setUp() {
  30. food.add("Name");
  31. terrain.add("");
  32. // define new rectangle grid for simulation
  33. for (int i = 0; i < 10; i++) {
  34. for (int j = 0; j < 10; j++) {
  35. RectangleCoord temp = new RectangleCoord(0, 0, 0, 0);
  36. MyPair p = new MyPair(i, j);
  37. temp.setPair(p);
  38. int hash = i + (j * 10);
  39. System.out.println(hash);
  40. map_intrectangle.put(hash, temp);
  41. // defind new organism in this rectangle
  42. Organism temp_org = new Organism("Type", "Name", true, food, pollinate, terrain, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0,0, "Colour", temp, ip, 0, 0);
  44. temp.addSpecies(temp_org);
  45. }
  46. }
  47. // every rectangle now has one organism in
  48. }
  49. /**
  50. * initiate organism in a central location test find food with hunting
  51. * distance 1
  52. */
  53. @Test
  54. public void findFoodTest() {
  55. t_setUp();
  56. RectangleCoord rect2 = map_intrectangle.get(45); // central rectangle
  57. System.out.println(rect2.getName());
  58. o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0, 0, 0,0, "Colour",
  59. rect2, ip, 0, 0); // new organism in central rectangle w/
  60. // hunting distance 1
  61. ArrayList<RectangleCoord> neighbours = new ArrayList<>(); // set up
  62. // local
  63. // rectangles
  64. for (int i = 20; i < 30; i++) {
  65. RectangleCoord r = map_intrectangle.get(i);
  66. neighbours.add(r);
  67. }
  68. ArrayList<Organism> local_species = new ArrayList<>();
  69. for (RectangleCoord r : neighbours) {
  70. for (Organism o : r.speciesContained()) {
  71. local_species.add(o);
  72. }
  73. }
  74. assertEquals(10, local_species.size()); // test that all 10 local
  75. // species have been added to
  76. // lacal_species
  77. }
  78. /**
  79. * initiate organism and a local food list test to see if it correctly
  80. * identifies food in the list
  81. */
  82. @Test
  83. public void checkFodTest() {
  84. ArrayList<String> food_temp = new ArrayList<>(); // food organism can
  85. // eat
  86. String[] strs = { "Name", "Not Name" };
  87. food_temp.addAll(Arrays.asList(strs));
  88. ArrayList<Organism> localList = new ArrayList<>();// make a list of
  89. // local organisms
  90. for (int i = 0; i < 10; i++) {
  91. Organism o = new Organism("Type", "Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
  92. "Colour", rect, ip, 0, 0);
  93. localList.add(o);
  94. }
  95. Organism o1 = new Organism("Type", "Not Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0,0, 0, 0, 0, 0,
  96. "Colour", rect, ip, 0, 0);
  97. Organism o2 = new Organism("Type", "Not Not Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0,0, 0, 0, 0,
  98. "Colour", rect, ip, 0, 0);
  99. localList.add(o1);
  100. localList.add(o2);
  101. Organism test_o = new Organism("Type", "Different Name", true, food_temp, pollinate, terrain, 0, 0, 1, 0, 0, 0, 0,
  102. 0, 0, 0, "Colour", rect, ip, 0, 0); // define organism to test
  103. // which can eat the other
  104. // organisms
  105. assertEquals(11, test_o.checkFood(localList)); // should be able to eat
  106. // 12 of the 13
  107. // organisms
  108. }
  109. /**
  110. * check to see if new competition detection in grid system is working
  111. */
  112. @Test
  113. public void checkCompetitionTest() {
  114. t_setUp();
  115. ArrayList<RectangleCoord> neighbours = new ArrayList<>(); // set up
  116. // local
  117. // rectangles
  118. for (int i = 20; i < 30; i++) {
  119. RectangleCoord r = map_intrectangle.get(i);
  120. neighbours.add(r);
  121. }
  122. Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0,0, 0, 0, 0, 0,
  123. 0, "Colour", rect, ip, 0, 0);// set up test organism
  124. ArrayList<Organism> local_competition = new ArrayList<>();
  125. for (RectangleCoord r : neighbours) {
  126. ArrayList<Organism> speciesContained = r.speciesContained();
  127. for (Organism o : speciesContained) {
  128. if (test_o.name.equals(o.getName())) {
  129. local_competition.add(o);
  130. } else {
  131. ArrayList<String> food_temp = o.getFood();
  132. search: for (String f : food_temp) {
  133. if (this.food.contains(f)) {
  134. local_competition.add(o);
  135. break search;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. assertEquals(10, local_competition.size()); //test if all 10 are being
  142. //seen as competition
  143. }
  144. /**
  145. * Test growth rate algorithm with heavy competition
  146. */
  147. @Test
  148. public void calculateGrowthRateTest1() {
  149. Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0,0, 0, 1, 0, 0, 0,
  150. 5, 2, 0, "Colour", rect, ip, 0, 0);
  151.  
  152. assertEquals(0, test_o.calculateGrowthRate(10, 10));
  153. }
  154.  
  155. /**
  156. * Test growth rate algorithm with an abundance of food
  157. */
  158. @Test
  159. public void calculateGrowthRateTest2() {
  160. Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0,0, 1, 0, 0, 0,
  161. 5, 2, 0, "Colour", rect, ip, 0, 0);
  162.  
  163. assertEquals(2, test_o.calculateGrowthRate(0, 10));
  164. }
  165.  
  166. /**
  167. * Test growth rate algorithm with medium competition
  168. */
  169. @Test
  170. public void calculateGrowthRateTest3() {
  171. Organism test_o = new Organism("Type", "Different Name", true, food, pollinate, terrain, 0, 0, 1, 0, 0, 0,
  172. 5, 2, 0,0 , "Colour", rect, ip, 0, 0);
  173.  
  174. assertEquals(1, test_o.calculateGrowthRate(6, 10));
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement