Added control to prevent invalid low/high ranges

This commit is contained in:
Daniel Gil
2018-05-29 17:51:41 +02:00
parent 6702d50231
commit 90c4e64922
6 changed files with 64 additions and 23 deletions

View File

@@ -1,15 +1,22 @@
package interval
func (intvls *intervals) getInclusives(intervalLow, intervalHigh int) (int, int) {
import (
"fmt"
)
func (intvls *intervals) getInclusives(intervalLow, intervalHigh int) (int, int, error) {
low := intvls.getInclusiveLow(intervalLow)
high := intvls.getInclusiveHigh(intervalHigh)
if high < low {
high = low
}
// 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 {
low = high
return 0, 0, fmt.Errorf("low (%v) can not be bigger than high (%v)", low, high)
}
return low, high
if high < low {
return 0, 0, fmt.Errorf("high (%v) can not be smaller than low (%v)", high, low)
}
return low, high, nil
}
func (intvls *intervals) getInclusiveLow(value int) int {