-
Notifications
You must be signed in to change notification settings - Fork 101
/
generate_parentheses_test.go
39 lines (34 loc) · 1.61 KB
/
generate_parentheses_test.go
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
package backtracking
import (
"slices"
"sort"
"testing"
)
/*
TestGenerateParentheses tests solution(s) with the following signature and problem description:
GenerateParentheses(n int) []string
Given an integer n produce all valid variations of arranging
n pair of parentheses. e.g. for `2` it should return `()()` and `(())`.
*/
func TestGenerateParentheses(t *testing.T) {
tests := []struct {
n int
validParentheses []string
}{
{0, []string{""}},
{1, []string{"()"}},
{2, []string{"(())", "()()"}},
{3, []string{"((()))", "(()())", "(())()", "()(())", "()()()"}},
{4, []string{"(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"}},
{5, []string{"((((()))))", "(((()())))", "(((())()))", "(((()))())", "(((())))()", "((()(())))", "((()()()))", "((()())())", "((()()))()", "((())(()))", "((())()())", "((())())()", "((()))(())", "((()))()()", "(()((())))", "(()(()()))", "(()(())())", "(()(()))()", "(()()(()))", "(()()()())", "(()()())()", "(()())(())", "(()())()()", "(())((()))", "(())(()())", "(())(())()", "(())()(())", "(())()()()", "()(((())))", "()((()()))", "()((())())", "()((()))()", "()(()(()))", "()(()()())", "()(()())()", "()(())(())", "()(())()()", "()()((()))", "()()(()())", "()()(())()", "()()()(())", "()()()()()"}},
}
for i, test := range tests {
got := GenerateParentheses(test.n)
if len(got) > 0 {
sort.Strings(got)
}
if !slices.Equal(test.validParentheses, got) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.validParentheses, got)
}
}
}