fix tests

This commit is contained in:
Gregory 2021-12-14 12:44:57 +02:00
parent 62b4399322
commit a90f8032bf
5 changed files with 95 additions and 108 deletions

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)