Skip to content

Commit 9cebf3f

Browse files
committed
feat: manual branch coverage: issue TheAlgorithms#3
1 parent a127416 commit 9cebf3f

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/main/java/com/thealgorithms/strings/ValidParentheses.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,43 @@
66
// Every close bracket has a corresponding open bracket of the same type.
77

88

9+
import java.util.*;
10+
911
public class ValidParentheses {
12+
public static HashSet<String> branchesReached = new HashSet<>();
1013
public static boolean isValid(String s) {
1114
char[] stack = new char[s.length()];
1215
int head = 0;
1316
for(char c : s.toCharArray()) {
17+
branchesReached.add("1.1");
1418
switch(c) {
1519
case '{':
20+
branchesReached.add("1.2");
1621
case '[':
22+
branchesReached.add("1.3");
1723
case '(':
1824
stack[head++] = c;
1925
break;
2026
case '}':
21-
if(head == 0 || stack[--head] != '{') return false;
27+
branchesReached.add("1.4.1");
28+
if(head == 0 || stack[--head] != '{') {
29+
branchesReached.add("1.4.2");
30+
return false;
31+
}
2232
break;
2333
case ')':
24-
if(head == 0 || stack[--head] != '(') return false;
34+
branchesReached.add("1.5.1");
35+
if(head == 0 || stack[--head] != '(') {
36+
branchesReached.add("1.5.2");
37+
return false;
38+
}
2539
break;
2640
case ']':
27-
if(head == 0 || stack[--head] != '[') return false;
41+
branchesReached.add("1.6.1");
42+
if(head == 0 || stack[--head] != '[') {
43+
branchesReached.add("1.6.2");
44+
return false;
45+
}
2846
break;
2947
}
3048
}

src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.strings;
22

3+
import com.thealgorithms.datastructures.hashmap.hashing.HashMap;
4+
import org.junit.jupiter.api.AfterAll;
35
import org.junit.jupiter.api.Test;
46
import static org.junit.jupiter.api.Assertions.*;
57

@@ -20,5 +22,7 @@ void testTwo() {
2022
void testThree() {
2123
assertEquals(false, ValidParentheses.isValid("(]"));
2224
}
23-
25+
26+
@AfterAll
27+
static void printCoverage(){System.out.println(ValidParentheses.branchesReached);}
2428
}

0 commit comments

Comments
 (0)