1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package flow
16
17 import (
18 "strconv"
19 "strings"
20 "testing"
21 )
22
23
24 var MermaidGraph = mermaidGraph
25
26 func TestIsCyclic(t *testing.T) {
27 testCases := []struct {
28
29
30 tasks string
31 cycle bool
32 }{{
33 tasks: "",
34 }, {
35 tasks: "0",
36 cycle: true,
37 }, {
38 tasks: "1; 0",
39 cycle: true,
40 }, {
41 tasks: "1; 2; 3; 4;",
42 }, {
43 tasks: "1; 2; ; 4; 5; ",
44 }, {
45 tasks: "1; 2; 3; 4; 0",
46 cycle: true,
47 }, {
48 tasks: "1,2,3,4; 2,3,4; 3,4; 4;",
49 }, {
50 tasks: ";0;0,1;0,1,2;0,1,2,3;",
51 }, {
52 tasks: "1,2,3,4; 2,3,4; 2; 4;",
53 cycle: true,
54 }}
55 for _, tc := range testCases {
56 t.Run(tc.tasks, func(t *testing.T) {
57 deps := strings.Split(tc.tasks, ";")
58 tasks := make([]*Task, len(deps))
59 for i := range tasks {
60 tasks[i] = &Task{index: i}
61 }
62 for i, d := range deps {
63 if d == "" {
64 continue
65 }
66 for _, num := range strings.Split(d, ",") {
67 num = strings.TrimSpace(num)
68 if num == "" {
69 continue
70 }
71 x, err := strconv.Atoi(num)
72 if err != nil {
73 t.Fatal(err)
74 }
75 t.Logf("%d -> %d", i, x)
76 tasks[i].depTasks = append(tasks[i].depTasks, tasks[x])
77 }
78 }
79 if got := checkCycle(tasks) != nil; got != tc.cycle {
80 t.Errorf("got %v; want %v", got, tc.cycle)
81 }
82 })
83 }
84 }
85
View as plain text