diff --git a/fundamentals/exrecises/union_find/test_count.go b/fundamentals/exrecises/union_find/test_count.go deleted file mode 100644 index e1532fd..0000000 --- a/fundamentals/exrecises/union_find/test_count.go +++ /dev/null @@ -1,11 +0,0 @@ -package unionfind - -import "log" - -func testCount(i implementation) { - qf := i.create(10) - - if qf.Count() != 10 { - log.Fatalf("%v: Before any union number of components should be equal to number of sites", i.name) - } -} diff --git a/fundamentals/exrecises/union_find/test_file.go b/fundamentals/exrecises/union_find/test_file.go deleted file mode 100644 index 97e236b..0000000 --- a/fundamentals/exrecises/union_find/test_file.go +++ /dev/null @@ -1,53 +0,0 @@ -package unionfind - -import ( - "bufio" - "fmt" - "log" - "os" - "strconv" - "strings" -) - -func testFile(fileName string, components int, i implementation) { - lines := readByLine(fileName) - count, _ := strconv.Atoi(<-lines) - qf := i.create(count) - - for line := range lines { - first, second := pair(line) - if qf.Connected(first, second) { - continue - } - qf.Union(first, second) - } - - if components != qf.Count() { - log.Fatalf("%v: Expected components count: %v, got: %v", i.name, components, qf.Count()) - } - fmt.Printf("%v: Components: %v\n", i.name, qf.Count()) -} - -func readByLine(fileName string) <-chan string { - file, _ := os.Open(fileName) - scanner := bufio.NewScanner(file) - line := make(chan string) - - go func() { - defer close(line) - for scanner.Scan() { - line <- scanner.Text() - } - }() - - return line -} - -func pair(str string) (int, int) { - numbers := strings.Split(str, " ") - - a, _ := strconv.Atoi(numbers[0]) - b, _ := strconv.Atoi(numbers[1]) - - return a, b -} diff --git a/fundamentals/exrecises/union_find/test_find.go b/fundamentals/exrecises/union_find/test_find.go deleted file mode 100644 index 0f86a83..0000000 --- a/fundamentals/exrecises/union_find/test_find.go +++ /dev/null @@ -1,11 +0,0 @@ -package unionfind - -import "log" - -func testFind(i implementation) { - qf := i.create(2) - - if qf.Find(0) != 0 || qf.Find(1) != 1 { - log.Fatalf("%v Before union all sites belongs to component with same number", i.name) - } -} diff --git a/fundamentals/exrecises/union_find/test_union.go b/fundamentals/exrecises/union_find/test_union.go deleted file mode 100644 index d53fa90..0000000 --- a/fundamentals/exrecises/union_find/test_union.go +++ /dev/null @@ -1,33 +0,0 @@ -package unionfind - -import "log" - -func testUnion(i implementation) { - qf := i.create(4) - - qf.Union(0, 1) - - if !qf.Connected(0, 1) { - log.Fatalf("%v: sites should be connected after union", i.name) - } - - if qf.Find(0) != qf.Find(1) { - log.Fatalf("%v after union sites should be in the same component", i.name) - } - - qf.Union(2, 3) - - if !qf.Connected(2, 3) { - log.Fatalf("%v: sites should be connected after union", i.name) - } - - if qf.Find(2) != qf.Find(3) { - log.Fatalf("%v after union sites should be in the same component", i.name) - } - - qf.Union(1, 2) - - if qf.Count() != 1 { - log.Fatalf("%v after union count should be decreased", i.name) - } -} diff --git a/fundamentals/exrecises/union_find/union_find_test.go b/fundamentals/exrecises/union_find/union_find_test.go index 472fb8a..1bf8051 100644 --- a/fundamentals/exrecises/union_find/union_find_test.go +++ b/fundamentals/exrecises/union_find/union_find_test.go @@ -1,6 +1,12 @@ package unionfind import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" "testing" ) @@ -24,6 +30,95 @@ var implementations = []implementation{ }, } +func testUnion(i implementation) { + qf := i.create(4) + + qf.Union(0, 1) + + if !qf.Connected(0, 1) { + log.Fatalf("%v: sites should be connected after union", i.name) + } + + if qf.Find(0) != qf.Find(1) { + log.Fatalf("%v after union sites should be in the same component", i.name) + } + + qf.Union(2, 3) + + if !qf.Connected(2, 3) { + log.Fatalf("%v: sites should be connected after union", i.name) + } + + if qf.Find(2) != qf.Find(3) { + log.Fatalf("%v after union sites should be in the same component", i.name) + } + + qf.Union(1, 2) + + if qf.Count() != 1 { + log.Fatalf("%v after union count should be decreased", i.name) + } +} + +func testFind(i implementation) { + qf := i.create(2) + + if qf.Find(0) != 0 || qf.Find(1) != 1 { + log.Fatalf("%v Before union all sites belongs to component with same number", i.name) + } +} + +func testFile(fileName string, components int, i implementation) { + lines := readByLine(fileName) + count, _ := strconv.Atoi(<-lines) + qf := i.create(count) + + for line := range lines { + first, second := pair(line) + if qf.Connected(first, second) { + continue + } + qf.Union(first, second) + } + + if components != qf.Count() { + log.Fatalf("%v: Expected components count: %v, got: %v", i.name, components, qf.Count()) + } + fmt.Printf("%v: Components: %v\n", i.name, qf.Count()) +} + +func readByLine(fileName string) <-chan string { + file, _ := os.Open(fileName) + scanner := bufio.NewScanner(file) + line := make(chan string) + + go func() { + defer close(line) + for scanner.Scan() { + line <- scanner.Text() + } + }() + + return line +} + +func pair(str string) (int, int) { + numbers := strings.Split(str, " ") + + a, _ := strconv.Atoi(numbers[0]) + b, _ := strconv.Atoi(numbers[1]) + + return a, b +} + +func testCount(i implementation) { + qf := i.create(10) + + if qf.Count() != 10 { + log.Fatalf("%v: Before any union number of components should be equal to number of sites", i.name) + } +} + func TestCount(t *testing.T) { for _, implementation := range implementations { testCount(implementation)