-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathA4sampTest.java
155 lines (117 loc) · 3.71 KB
/
A4sampTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package assignment4;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import assignment4.Critter.TestCritter;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import static org.hamcrest.CoreMatchers.containsString;
public class A4sampTest{
private static String TESTSRCDIR = "test_sample/";
private static ByteArrayOutputStream outContent;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
TestCritter.clearWorld();
outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
// System.setOut(new PrintStream(Main.old));
}
@After
public void tearDown() throws Exception {
}
/*
* 4. KillCritters
* Test: killCritters
* Test for make critter and stats, and step
* Creates large number of make critters and compare stats after 500 steps
* Expects all Critters to be dead
*/
@Test
public void KillCritters(){
//Uncomment Following Codeblock to test
//Remove final keyword from Params.java
Params.world_width = 20;
Params.world_height = 20;
Params.walk_energy_cost = 2;
Params.run_energy_cost = 5;
Params.rest_energy_cost = 1;
Params.min_reproduce_energy = 20;
Params.refresh_algae_count = (int)Math.max(1, Params.world_width*Params.world_height/1000);
Params.photosynthesis_energy_amount = 1;
Params.start_energy = 5;
String fileFolder = "kill_all_critter";
String[] inputs = {TESTSRCDIR + fileFolder + "/input.txt" ,"test"};
try{
Main.main(inputs);
}catch(Exception e){
System.out.println("exception caught on line 78 of A4sampTest");
}
outContent = Main.testOutputString;
Scanner scanner = null;
try {
scanner = new Scanner( new File(TESTSRCDIR + fileFolder + "/expected_output.txt") );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String text = scanner.useDelimiter("\\A").next().trim();
String output =outContent.toString().replace("critter>","").trim();
scanner.close();
assertEquals(text,output);
}
/*
* 6. ParseErrors
*
* Test: ParseErrors
* Test for errors within valid inputs
* Expects errors to be printed
*/
@Test
public void ParseErrors(){
//Uncomment following codeblock to test with parameters
//Remove final keyword in Params.java
/*
Params.world_width = 20;
Params.world_width = 20;
Params.walk_energy_cost = 2;
Params.run_energy_cost = 5;
Params.rest_energy_cost = 1;
Params.min_reproduce_energy = 20;
Params.refresh_algae_count = (int)Math.max(1, Params.world_width*Params.world_height/1000);
Params.photosynthesis_energy_amount = 1;
Params.start_energy = 100;
*/
String fileFolder = "error_processing";
String[] inputs = {TESTSRCDIR + fileFolder + "/input.txt" ,"test"};
try{
Main.main(inputs);
}catch(Exception e){
System.out.println("Exception, line 132, A4sampTest");
}
outContent = Main.testOutputString;
Scanner scanner = null;
try {
scanner = new Scanner( new File(TESTSRCDIR + fileFolder + "/expected_output.txt") );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String text = scanner.useDelimiter("\\A").next().trim();
//String output = outContent.toString().replace("critter>","").trim();
String output = outContent.toString();
output = output.replace("critters>", "");
scanner.close();
//assertThat(text, containsString(output));
assertThat(output, containsString(text));
}
}