-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent infinite loop within serialization methods (#287)
- Loading branch information
1 parent
d17f3fa
commit 4db71be
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package goutils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInfiniteLoopDetect(t *testing.T) { | ||
|
||
linkedList := &ListNode{Val: 1} | ||
linkedList.Next = &ListNode{Val: 2, Next: linkedList} | ||
|
||
tree := &TreeNode{Val: 1} | ||
tree.Left = &TreeNode{Val: 2, Right: tree} | ||
|
||
naryTree := &NaryTreeNode{Val: 1} | ||
naryTree.Children = []*NaryTreeNode{{Val: 2, Children: []*NaryTreeNode{naryTree}}} | ||
|
||
type toStringer interface { | ||
ToString() string | ||
} | ||
|
||
tests := []toStringer{ | ||
linkedList, | ||
tree, | ||
naryTree, | ||
} | ||
|
||
for _, tc := range tests { | ||
assert.PanicsWithValue(t, ErrInfiniteLoop, func() { tc.ToString() }) | ||
} | ||
} |