我目前正在为我的Java 8代码编写一些单元测试。我有一个问题。
在下面的代码中,您可以看到3个测试函数,第一个返回6是正确的。然而,第二个也会返回6,即使它应该返回16,第三个也返回6,即使它应该返回21.
如果我注释掉第一个函数,则第二个函数返回16,这是正确的。但现在第三个函数返回16而不是6,它应该返回21。
我正在测试的代码是静态类。
@Test
public void testRun1() {
Integer[][] input = {
{6},
{6, 6},
{5, 6},
{4, 6},
{3, 6},
{2, 6},
{1, 6},
};
int expResult = 6;
int result = BoardingTest.Run(input);
assertEquals(expResult, result);
}
@Test
public void testRun2() {
Integer[][] input = new Integer[][]{
{6},
{6, 6},
{6, 4},
{5, 6},
{3, 8},
{1, 9},
{2, 1},
};
int expResult = 16;
int result = instance.Run(input);
assertEquals(expResult, result);
}
@Test
public void testRun3() {
Integer[][] input = new Integer[][]{
{6},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
};
int expResult = 21;
int result = BoardingTest.Run(input);
assertEquals(expResult, result);
}
Run的代码在Class Called BoardingTest中,如下所示。
public class BoardingTest {
// N
static int N;
// Total number of passengers seated
static int Seated = 0;
// Total time
static int TotalTime = 0;
// Shortest time for a passanger who is in the act of sitting down
static int ShortestTime = 999999;
// 1.st dimension = Seat, 2.nd = Time
static int[][] Passengers;
// Current passenger
static int Passenger = 0;
// The first seat that a person is in the act of sitting down in
static int FirstOccupied = 999999;
// Refference to the passenger who is in the act of sitting in the plane
static ArrayList<Integer> Plane = new ArrayList<Integer>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
public static int Run(Integer[][] input){
GetInput(input);
// Loop untill all passengers is seated
while(Seated < N){
Tick();
}
return(TotalTime);
}
// Reads all input
private static void GetInput (Integer[][] input){
N = input[0][0];
Passengers = new int[N][2];
for(int i = 0; i < N; i++){
SplitInput(i, input);
}
}
// Splits an inputline into Seat and Time
private static void SplitInput(int i, Integer[][] input){
Passengers[i][0] = input[i+1][0];
Passengers[i][1] = input[i+1][1];
}
// This handels what happens in a Tick
private static void Tick(){
// Keep putting passengers into the plane, until we can't do that anymore
InserPassengers();
TotalTime += ShortestTime;
// Remove 1 sec from all passangers in the plane
boolean removeOne = RemoveTimeFromPassengersInPlae(ShortestTime);
// If someone was removed, find the new FirstOccupied seat
if(removeOne){
FirstOccupied = FindFirstOccupiedSeat();
}
// End Tick
}
// Will insert as many passengers as posible into the plane
private static void InserPassengers(){
while(Passenger != N && FirstOccupied > Passengers[Passenger][0]){
// If this passengers takes les time to sit, update ShortestTime
if(Passengers[Passenger][1] < ShortestTime){
ShortestTime = Passengers[Passenger][1];
}
// Add this passenger to the plane
Plane.add(Passenger);
// Set FisrstOccupied
FirstOccupied = Passengers[Passenger][0];
// Increase the Passenger number
Passenger++;
}
}
// Will find the Fist Occupied seat
private static int FindFirstOccupiedSeat(){
int result = 999999;
for(Integer i : Plane){
if(Passengers[i][0] < result){
result = Passengers[i][0];
}
}
return result;
}
// Will remove some time from everyone in the plane
// And remove everyone who has finished sitting down
// And find the shortest time for the next one to sit
private static boolean RemoveTimeFromPassengersInPlae(int time){
ShortestTime = 999999;
boolean removed = false;
for(int i = Plane.size() - 1; i >= 0; i--){
// Remove time
Passengers[Plane.get(i)][1] -= time;
// If time is 0
// Remove Passanger
if(Passengers[Plane.get(i)][1] == 0){
Plane.remove(i);
removed = true;
Seated++;
}
// Set new Shortes Time
else if(Passengers[Plane.get(i)][1] < ShortestTime){
ShortestTime = Passengers[Plane.get(i)][1];
}
}
return removed;
}
private static void Print(String s){
System.out.println(s);
}
private static void Print(int i){
System.out.println(i);
}
}
答案 0 :(得分:0)
好吧,在玩了一下后我发现了问题。
这是因为我调用的类是静态的。
如果我错了,请纠正我,但我认为静态变量在多次调用同一个静态类时不会改变。