Skip to content

Commit 6fe7464

Browse files
committed
Added JUnit tests fo BankersAlgorithmn
1 parent 7f10968 commit 6fe7464

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/test/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithm.java renamed to src/main/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithm.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.operatingsystemconcepts;
22

3+
34
import java.util.Arrays;
45
import java.util.Scanner;
56

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.operatingsystemconcepts;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.PrintStream;
8+
import java.io.FileOutputStream;
9+
import java.io.FileDescriptor;
10+
11+
public class BankersAlgorithmTest {
12+
13+
private BankersAlgorithm bankersAlgorithm;
14+
15+
@BeforeEach
16+
void setUp() {
17+
// Example setup with 5 processes and 3 resources
18+
bankersAlgorithm = new BankersAlgorithm(5, 3);
19+
20+
// Setting up maximum resources
21+
bankersAlgorithm.max = new int[][] {
22+
{7, 5, 3},
23+
{3, 2, 2},
24+
{9, 0, 2},
25+
{2, 2, 2},
26+
{4, 3, 3}
27+
};
28+
29+
// Setting up allocation resources
30+
bankersAlgorithm.allot = new int[][] {
31+
{0, 1, 0},
32+
{2, 0, 0},
33+
{3, 0, 2},
34+
{2, 1, 1},
35+
{0, 0, 2}
36+
};
37+
38+
// Setting up available resources
39+
bankersAlgorithm.available = new int[]{3, 3, 2};
40+
41+
// Calculating need matrix
42+
bankersAlgorithm.calculateNeed();
43+
}
44+
45+
@Test
46+
void testSystemIsInSafeState() {
47+
assertTrue(bankersAlgorithm.isSafe(), "The system should be in a safe state.");
48+
}
49+
50+
@Test
51+
void testRequestResourcesSuccessfully() {
52+
int[] request = {1, 0, 2};
53+
assertDoesNotThrow(() -> bankersAlgorithm.requestResources(0, request), "Request should not throw an exception.");
54+
}
55+
56+
@Test
57+
void testRequestExceedsMaximumClaim() {
58+
int[] request = {8, 0, 0};
59+
assertEquals("Error: Process has exceeded its maximum claim.",
60+
getOutput(() -> bankersAlgorithm.requestResources(0, request)),
61+
"Expected error message for exceeding maximum claim.");
62+
}
63+
64+
@Test
65+
void testRequestExceedsAvailableResources() {
66+
int[] request = {1, 0, 3};
67+
assertEquals("Process is waiting.",
68+
getOutput(() -> bankersAlgorithm.requestResources(0, request)),
69+
"Expected message when requesting more resources than available.");
70+
}
71+
72+
@Test
73+
void testRequestResultsInUnsafeState() {
74+
// Requesting resources that lead to an unsafe state
75+
int[] request = {3, 3, 0};
76+
assertEquals("Resources allocation leads to unsafe state, request denied.",
77+
getOutput(() -> bankersAlgorithm.requestResources(1, request)),
78+
"Expected message when allocation leads to unsafe state.");
79+
}
80+
81+
// Helper method to capture console output
82+
private String getOutput(Runnable runnable) {
83+
// Code to capture System.out output
84+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
85+
PrintStream originalOut = System.out; // Save original System.out
86+
System.setOut(new PrintStream(outContent));
87+
88+
runnable.run();
89+
90+
System.setOut(originalOut); // Reset System.out
91+
return outContent.toString().trim();
92+
}
93+
}

0 commit comments

Comments
 (0)