Skip to content

Commit

Permalink
Merge pull request #3 from ozoncp/feature/task-2
Browse files Browse the repository at this point in the history
Feature/task 2
  • Loading branch information
echekunov authored Jul 21, 2021
2 parents a7e8a34 + f8226e2 commit c27da00
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/utils/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

func defaultList() []string {
return []string{"1", "2", "3", "4", "5"}
}

func contain(source []string, val string) bool {
for i := range source {
if source[i] == val {
return true
}
}
return false
}

func Filter(source []string) []string {
result := make([]string, 0, len(source))
for _, val := range source {
if !contain(defaultList(), val) {
result = append(result, val)
}
}
return result
}
28 changes: 28 additions & 0 deletions internal/utils/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import "testing"

func TestFilter(t *testing.T) {
source := []string{"0", "1", "2", "3", "6", "10"}
expected := []string{"0", "6", "10"}

result := Filter(source)

if len(result) != len(expected) {
t.Errorf("Expected %v, received %v", expected, result)
}

for i := range result {
if expected[i] != result[i] {
t.Errorf("Mismatched at index %v expected %v, received %v", i, expected[i], result[i])
}
}
}

func TestFilterEmpty(t *testing.T) {
result := Filter(nil)

if len(result) != 0 {
t.Errorf("Expected empty, received %v", result)
}
}
11 changes: 11 additions & 0 deletions internal/utils/flip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

func Flip(source map[int]string) map[string]int {
result := make(map[string]int, len(source))

for key, value := range source {
result[value] = key
}

return result
}
31 changes: 31 additions & 0 deletions internal/utils/flip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import "testing"

func TestEmptyFlip(t *testing.T) {
val := Flip(nil)
if len(val) != 0 {
t.Errorf("Expected empty, received %v", val)
}
}

func TestFlip(t *testing.T) {
length := 3
source := make(map[int]string, length)
source[1] = "one"
source[2] = "two"
source[3] = "three"

val := Flip(source)
if len(val) != length {
t.Errorf("Expected length %v , received %v", length, val)
return
}

for key, value := range val {
if source[value] != key {
t.Errorf("Expected key %v, received %v", source[value], key)
return
}
}
}
26 changes: 26 additions & 0 deletions internal/utils/split.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import "errors"

func Split(source []string, batchLen int) ([][]string, error) {
if batchLen <= 0 {
return nil, errors.New("batch length cannot be a zero or negative")
}
sourceLen := len(source)
fullBatchesCount := sourceLen / batchLen
batchesCount := fullBatchesCount
if sourceLen % batchLen != 0 {
batchesCount++
}
result := make([][]string, batchesCount)

for i := 0; i < fullBatchesCount; i++ {
result[i] = append([]string{}, source[i * batchLen:(i + 1) * batchLen]...)
}

if fullBatchesCount != batchesCount {
result[fullBatchesCount] = append([]string{}, source[fullBatchesCount * batchLen:sourceLen]...)
}

return result, nil
}
99 changes: 99 additions & 0 deletions internal/utils/split_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package utils

import "testing"

func TestSplitNegativeBatchLen(t *testing.T) {
val, err := Split(nil, -1)
if err == nil {
t.Errorf("Expected error, received %v", val)
}
}

func TestSplitZeroBatchLen(t *testing.T) {
val, err := Split(nil, 0)
if err == nil {
t.Errorf("Expected error, received %v", val)
}
}

func TestSplitNilSource(t *testing.T) {
val, err := Split(nil, 3)
if err != nil {
t.Errorf("Expected value, received error %v", err)
}

if len(val) != 0 {
t.Errorf("Expected empty value, received %v", val)
}
}

func compareSlices(left []string, right []string) bool {
if len(left) != len(right) {
return false
}
for i := 0; i < len(left); i++ {
if left[i] != right[i] {
return false
}
}
return true
}

func TestSplitFullBatches(t *testing.T) {
source := []string{"one", "two", "three", "four", "five", "six"}
batchLen := 2
expectedCount := 3
val, err := Split(source, batchLen)
if err != nil {
t.Errorf("Expected value, received error %v", err)
}

if len(val) != expectedCount {
t.Fatalf("Expected %v slices, received %v", batchLen, val)
}

for i := 0; i < expectedCount; i++ {
expectedSlice := source[i * batchLen:(i+1) * batchLen]
if !compareSlices(expectedSlice, val[i]) {
t.Fatalf("Expected %v, received %v", expectedSlice, val[i])
}
}
}

func TestSplitPartialBatches(t *testing.T) {
source := []string{"one", "two", "three", "four", "five", "six"}
sourceLen := len(source)
batchLen := 4
expectedCount := 2
val, err := Split(source, batchLen)
if err != nil {
t.Errorf("Expected value, received error %v", err)
}

if len(val) != expectedCount {
t.Fatalf("Expected %v slices, received %v", batchLen, val)
}

for i := 0; i < expectedCount; i++ {
first := i * batchLen
last := first + batchLen
if last > sourceLen {
last = sourceLen
}
expectedSlice := source[first:last]
if !compareSlices(expectedSlice, val[i]) {
t.Fatalf("Expected %v, received %v", expectedSlice, val[i])
}
}
}

func TestSplitUnchanges(t *testing.T) {
source := []string{"one", "two", "three", "four", "five", "six"}
res, _ := Split(source, 3)
baseVal := res[0][0]
source[0] = "WOW!"
newVal := res[0][0]
if baseVal != newVal {
t.Errorf("res[0][0] is <%v>, <%v> expected", newVal, baseVal)
}
}

0 comments on commit c27da00

Please sign in to comment.