Skip to content

Commit

Permalink
draft: Prepare a new module
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatsnake committed Feb 11, 2023
1 parent 584702f commit 839b724
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pkg/memory/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ var _pairSizes = map[int]int{
}

var errNegativeNumbers = errors.New("negative numbers are not allowed")
var errPairSizeNotAllowed = errors.New("pair size not allowed")
var errTooManyLetters = errors.New("total number of letters must not exceed 52 multiplied by the size of the pair")
var errIncorrectLettersAmount = errors.New("total number of letters must be even to the size of the pair")
var errPairSizeNotAllowed = errors.New("given pair size not allowed")
var errTooBigGrid = errors.New("total number of cells in the grid must not exceed 52 multiplied by the pair size")
var errIncorrectGridSize = errors.New("total number of cells in the grid must be even to the size of the pair")
106 changes: 106 additions & 0 deletions pkg/memory/core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package memory

import (
"errors"
"testing"
)

func TestGenerate(t *testing.T) {
t.Run("valid params", func(t *testing.T) {
values := [][]int{
{2, 2, 2, 0},
{2, 2, 4, 1},
{6, 4, 3, 0},
{9, 6, 2, 1},
{13, 8, 2, 0},
{12, 13, 3, 1},
{16, 13, 4, 0},
}

for _, v := range values {
_, err := Generate(v[0], v[1], v[2], v[3] == 1)
if err != nil {
t.Errorf("should be fine, but got error: %s, with this params: %v", err.Error(), v)
}
}
})

t.Run("incorrect pair size", func(t *testing.T) {
values := [][]int{
{2, 2, 1, 0},
{2, 2, 5, 1},
{6, 4, -2, 0},
{9, 6, 10, 1},
{13, 8, 0, 0},
{12, 13, -3, 1},
{16, 13, 12, 0},
}

for _, v := range values {
_, err := Generate(v[0], v[1], v[2], v[3] == 1)
if err == nil {
t.Errorf("should be error, but got fine with this params: %v", v)
}
if !errors.Is(err, errPairSizeNotAllowed) {
t.Errorf("should be %s, but got %s", errPairSizeNotAllowed.Error(), err.Error())
}
}
})

t.Run("nagative grid size", func(t *testing.T) {
values := [][]int{
{-2, 4, 2, 0},
{4, -2, 3, 1},
{-10, -4, 4, 0},
}

for _, v := range values {
_, err := Generate(v[0], v[1], v[2], v[3] == 1)
if err == nil {
t.Errorf("should be error, but got fine with this params: %v", v)
}
if !errors.Is(err, errNegativeNumbers) {
t.Errorf("should be %s, but got %s", errNegativeNumbers.Error(), err.Error())
}
}
})

t.Run("too big grid size", func(t *testing.T) {
values := [][]int{
{9, 13, 2, 0},
{13, 9, 2, 1},
{12, 14, 3, 0},
{14, 12, 3, 1},
{16, 14, 4, 0},
{14, 16, 4, 1},
}

for _, v := range values {
_, err := Generate(v[0], v[1], v[2], v[3] == 1)
if err == nil {
t.Errorf("should be error, but got fine with this params: %v", v)
}
if !errors.Is(err, errTooBigGrid) {
t.Errorf("should be %s, but got %s", errTooBigGrid.Error(), err.Error())
}
}
})

t.Run("incorrect grid size", func(t *testing.T) {
values := [][]int{
{3, 5, 2, 0},
{10, 4, 3, 1},
{5, 5, 4, 0},
}

for _, v := range values {
_, err := Generate(v[0], v[1], v[2], v[3] == 1)
if err == nil {
t.Errorf("should be error, but got fine with this params: %v", v)
}
if !errors.Is(err, errIncorrectGridSize) {
t.Errorf("should be %s, but got %s", errIncorrectGridSize.Error(), err.Error())
}
}
})
}
12 changes: 6 additions & 6 deletions pkg/memory/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
)

func generator(w, h, pairSize int, showPositions bool) ([][]string, []PairPositions, error) {
if w < 0 || h < 0 {
return nil, nil, errNegativeNumbers
}

pool, usedLetters, err := lettersPool(w*h, pairSize)
if err != nil {
return nil, nil, err
Expand All @@ -26,21 +30,17 @@ func generator(w, h, pairSize int, showPositions bool) ([][]string, []PairPositi
}

func lettersPool(totalLetters, pairSize int) ([]string, string, error) {
if totalLetters < 0 {
return nil, "", errNegativeNumbers
}

_, ok := _pairSizes[pairSize]
if !ok {
return nil, "", errPairSizeNotAllowed
}

if totalLetters > len(_lowerCaseLetters)*2*pairSize {
return nil, "", errTooManyLetters
return nil, "", errTooBigGrid
}

if totalLetters%pairSize != 0 {
return nil, "", errIncorrectLettersAmount
return nil, "", errIncorrectGridSize
}

totalPairs := totalLetters / pairSize
Expand Down

0 comments on commit 839b724

Please sign in to comment.