Added check for valid interval in Add function. Added overlap testcases.
This commit is contained in:
@@ -1,22 +1,28 @@
|
||||
package interval
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
func (intvls *intervals) getInclusives(intervalLow, intervalHigh int) (int, int, error) {
|
||||
low := intvls.getInclusiveLow(intervalLow)
|
||||
high := intvls.getInclusiveHigh(intervalHigh)
|
||||
|
||||
err := intvls.checkValidInterval(low, high)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
return low, high, nil
|
||||
}
|
||||
|
||||
func (intvls *intervals) checkValidInterval(low, high int) error {
|
||||
// if we get an incoherent result it's because it's invalid, for example in this case:
|
||||
// (low,high)=(0,1) --> if we add 1 to the Low and substract 1 to the High ends with (1,0) which is not valid
|
||||
if low > high {
|
||||
return 0, 0, fmt.Errorf("low (%v) can not be bigger than high (%v)", low, high)
|
||||
return fmt.Errorf("low (%v) can not be bigger than high (%v)", low, high)
|
||||
}
|
||||
if high < low {
|
||||
return 0, 0, fmt.Errorf("high (%v) can not be smaller than low (%v)", high, low)
|
||||
return fmt.Errorf("high (%v) can not be smaller than low (%v)", high, low)
|
||||
}
|
||||
return low, high, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (intvls *intervals) getInclusiveLow(value int) int {
|
||||
|
||||
Reference in New Issue
Block a user