Browse Source

master > master: codego panic statt err in utils + unit tests

master
RD 1 year ago
parent
commit
2c9b4762a4
  1. 21
      codego/core/utils/utils.go
  2. 34
      codego/core/utils/utils_test.go

21
codego/core/utils/utils.go

@ -2,7 +2,6 @@ package utils
import (
"encoding/json"
"log"
"sort"
"strings"
)
@ -49,12 +48,10 @@ func Max2(x int, y int) int {
}
func MinList(x []int) int {
var val int
if len(x) == 0 {
log.Fatal("Cannot compute the maximum of an empty array.")
return 0
panic("Cannot compute the maximum of an empty array.")
}
val = x[0]
var val int = x[0]
for i, val_ := range x {
if i > 0 {
val = Min2(val, val_)
@ -64,12 +61,10 @@ func MinList(x []int) int {
}
func MaxList(x []int) int {
var val int
if len(x) == 0 {
log.Fatal("Cannot compute the maximum of an empty array.")
return 0
panic("Cannot compute the maximum of an empty array.")
}
val = x[0]
var val int = x[0]
for i, val_ := range x {
if i > 0 {
val = Max2(val, val_)
@ -78,6 +73,14 @@ func MaxList(x []int) int {
return val
}
func SumList(x []int) int {
var val int = 0
for _, val_ := range x {
val += val_
}
return val
}
/* ---------------------------------------------------------------- *
* METHODS: for string lists
* ---------------------------------------------------------------- */

34
codego/core/utils/utils_test.go

@ -13,7 +13,28 @@ import (
)
/* ---------------------------------------------------------------- *
* TESTCASE Min2, Max2, MinList, MaxList
* TESTCASE JsonToArrayOfStrings
* ---------------------------------------------------------------- */
func TestJsonToArrayOfStrings(test *testing.T) {
var assert = assert.New(test)
var result []string
utils.JsonToArrayOfStrings("[]", &result)
assert.Equal([]string{}, result)
utils.JsonToArrayOfStrings("[ \"ganimed\" ]", &result)
assert.Equal([]string{"ganimed"}, result)
utils.JsonToArrayOfStrings("[ \"ganimed\", \"io\" ]", &result)
assert.Equal([]string{"ganimed", "io"}, result)
assert.Panics(func() { utils.JsonToArrayOfStrings("[ 178 ]", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("true", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("nil", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("\"somestring\"", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("'somestring'", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
}
/* ---------------------------------------------------------------- *
* TESTCASE Min2, Max2, MinList, MaxList, SumList
* ---------------------------------------------------------------- */
func TestMin(test *testing.T) {
@ -23,6 +44,7 @@ func TestMin(test *testing.T) {
assert.Equal(50, utils.MinList([]int{50}))
assert.Equal(1, utils.MinList([]int{50, 1}))
assert.Equal(2, utils.MinList([]int{50, 34, 10, 2, 8, 89}))
assert.Panics(func() { utils.MinList([]int{}) }, "Min of list should panic on empty list!")
}
func TestMax(test *testing.T) {
@ -32,6 +54,16 @@ func TestMax(test *testing.T) {
assert.Equal(50, utils.MaxList([]int{50}))
assert.Equal(50, utils.MaxList([]int{50, 1}))
assert.Equal(89, utils.MaxList([]int{50, 34, 10, 2, 8, 89}))
assert.Panics(func() { utils.MaxList([]int{}) }, "Max of list should panic on empty list!")
}
func TestSumList(test *testing.T) {
var assert = assert.New(test)
assert.Equal(0, utils.SumList([]int{}), "Sum of empty list should be 0.")
assert.Equal(2198, utils.SumList([]int{2198}))
assert.Equal(15, utils.SumList([]int{0, 1, 2, 3, 4, 5}))
assert.Equal(1038, utils.SumList([]int{1000, 1, 37}))
assert.Equal(237, utils.SumList([]int{1000, -800, 37}))
}
/* ---------------------------------------------------------------- *

Loading…
Cancel
Save