Browse Source

made errors more explicit

master
Pixdigit 4 years ago
parent
commit
45b487eb32
  1. 16
      errorTypes.go
  2. 2
      list.go
  3. 6
      set.go
  4. 16
      set_test.go

16
errorTypes.go

@ -1,26 +1,30 @@
package sorted
import (
"fmt"
"gitlab.com/Pixdigit/uniqueID"
)
//List is empty
type ErrListEmpty struct {
S string
}
func (err ErrListEmpty) Error() string {
return err.S
return "list is empty"
}
type ErrDuplicateElem struct {
S string
DuplicateID uniqueID.ID
}
func (err ErrDuplicateElem) Error() string {
return err.S
return fmt.Sprint("Duplicate element with ID = ", err.DuplicateID)
}
type ErrNoElem struct {
S string
MissingID uniqueID.ID
}
func (err ErrNoElem) Error() string {
return err.S
return fmt.Sprint("no element with ID =", err.MissingID)
}

2
list.go

@ -41,7 +41,7 @@ func (sl *List) Insert(elem interface{}, value Num) {
func (sl *List) Nearest(value Num) (interface{}, error) {
if len(sl.elems) == 0 {
return listElem{}, &ErrListEmpty{"List has no nearest element"}
return listElem{}, &ErrListEmpty{}
}
if sl.elems[0].value > value {

6
set.go

@ -30,7 +30,7 @@ func (se *setElem) ID() uniqueID.ID {
func (s *Set) Insert(uElem uniqueElem, value Num) error {
for _, elem := range s.elems {
if elem.ID() == uElem.ID() {
return &ErrDuplicateElem{"element already present in set"}
return &ErrDuplicateElem{elem.ID()}
}
}
@ -70,13 +70,13 @@ func (s *Set) SetValue(ID uniqueID.ID, value Num) error {
return nil
}
}
return ErrNoElem{"the list has no item with that ID"}
return ErrNoElem{ID}
}
func (s *Set) Nearest(value Num) (interface{}, error) {
if len(s.elems) == 0 {
return setElem{}, &ErrListEmpty{"List has no nearest element"}
return setElem{}, &ErrListEmpty{}
}
if s.elems[0].value > value {

16
set_test.go

@ -68,13 +68,13 @@ func TestSetValue(t *testing.T) {
err = set.Insert(d, 5); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(e, 4); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
set.SetValue(a.ID(), 3.5)
set.SetValue(e.ID(), -2)
tools.Test(set.Elems()[0] == e, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[1] == b, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[2] == c, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[3] == a, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[4] == d, "did not correctly change position when set new value", t)
set.SetValue(a.ID(), 3.5)
set.SetValue(e.ID(), -2)
tools.Test(set.Elems()[0] == e, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[1] == b, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[2] == c, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[3] == a, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[4] == d, "did not correctly change position when set new value", t)
}

Loading…
Cancel
Save