add forgoten tests

This commit is contained in:
2022-09-30 23:35:21 +03:00
parent 5374dd7375
commit 2a6f023fa6
12 changed files with 147 additions and 61 deletions

35
consecutive/cons.go Normal file
View File

@@ -0,0 +1,35 @@
package consecutive
func Consecutive(A []int) int {
if len(A) != 0 {
min, max := extremum(A)
if len(A) == max-min+1 && len(A) > 1 {
return 1
}
}
return 0
}
func extremum(A []int) (int, int) {
min := A[0]
max := A[0]
for i := 0; i < len(A); i++ {
if min > A[i] {
min = A[i]
}
if max < A[i] {
max = A[i]
}
}
return min, max
}

51
consecutive/cons_test.go Normal file
View File

@@ -0,0 +1,51 @@
package consecutive_test
import (
"fmt"
"gotest/consecutive"
"testing"
)
// Структура тестовых данных, input - входной массив,
// output - ожидаеммый вывод
type testdata struct {
input []int
output int
}
// Входной, повторения, ожидания
var test = []testdata{
{[]int{1, 2, 4, 3, 6, 5}, 1},
{[]int{1, 2, 3, 5}, 0},
{[]int{255, 256, 257, 258}, 1},
}
func TestCons(t *testing.T) {
t.Run(
"insert slices", func(t *testing.T) {
for _, data := range test {
// Передаём массив и повторения в функцию и записываем результат в переменную
v := consecutive.Consecutive(data.input)
// Сравниваем вывод с ожиданием
if v != data.output {
t.Error(
"For: ", data.input,
"Expected: ", data.output,
"Got: ", v,
)
} else {
fmt.Println(
"For: ", data.input,
"Expected: ", data.output,
"Got: ", v,
)
}
}
},
)
}