From 103ed0ddb3a380931a87eccb56eba6719abad829 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 12 Nov 2020 18:53:37 +0100 Subject: [PATCH] First test of CostForIntervals --- engine/rateprofile.go | 2 + engine/rateprofile_test.go | 87 ++++++++++++++++++++++++++++++++++++++ utils/decimal.go | 4 ++ 3 files changed, 93 insertions(+) diff --git a/engine/rateprofile.go b/engine/rateprofile.go index 1fbba3c6a..082cc9c90 100644 --- a/engine/rateprofile.go +++ b/engine/rateprofile.go @@ -189,6 +189,7 @@ func (rIv *RateSInterval) CompressEquals(rIv2 *RateSInterval) (eq bool) { func (rIv *RateSInterval) Cost() *utils.Decimal { if rIv.cost == nil { + rIv.cost = utils.NewDecimal() for _, incrm := range rIv.Increments { rIv.cost = utils.NewDecimal().Add(rIv.cost, incrm.Cost()) } @@ -229,6 +230,7 @@ func (rIcr *RateSIncrement) Cost() *utils.Decimal { // CostForIntervals sums the costs for all intervals func CostForIntervals(rtIvls []*RateSInterval) (cost *utils.Decimal) { + cost = utils.NewDecimal() for _, rtIvl := range rtIvls { cost = utils.NewDecimal().Add(cost, rtIvl.Cost()) } diff --git a/engine/rateprofile_test.go b/engine/rateprofile_test.go index 2dede01e2..a948fe22b 100644 --- a/engine/rateprofile_test.go +++ b/engine/rateprofile_test.go @@ -465,3 +465,90 @@ func TestRateProfileRunTimesPassingActivationTIme(t *testing.T) { t.Errorf("Expected %+v, received %+v", expectedTime, rTimes) } } + +func TestCostForIntervals(t *testing.T) { + rt0 := &Rate{ + ID: "RATE0", + IntervalRates: []*IntervalRate{ + { + IntervalStart: time.Duration(0), + Unit: time.Duration(1 * time.Minute), + Increment: time.Duration(1 * time.Minute), + Value: 2.4, + }, + { + IntervalStart: time.Duration(60 * time.Second), + Unit: time.Duration(1 * time.Minute), + Increment: time.Duration(1 * time.Second), + Value: 2.4, + }, + }, + } + rt0.Compile() + rt1 := &Rate{ + ID: "RATE1", + IntervalRates: []*IntervalRate{ + { + + IntervalStart: time.Duration(0), + Unit: time.Duration(1 * time.Minute), + Increment: time.Duration(1 * time.Second), + Value: 1.2, + }, + { + + IntervalStart: time.Duration(2 * time.Minute), + Unit: time.Duration(1 * time.Minute), + Increment: time.Duration(1 * time.Second), + Value: 0.6, + }, + }, + } + rt1.Compile() + rtIvls := []*RateSInterval{ + { + UsageStart: time.Duration(0), + Increments: []*RateSIncrement{ + { + UsageStart: time.Duration(0), + Usage: time.Duration(time.Minute), + Rate: rt0, + IntervalRateIndex: 0, + CompressFactor: 1, + }, + { + UsageStart: time.Duration(time.Minute), + Usage: time.Duration(30 * time.Second), + Rate: rt0, + IntervalRateIndex: 1, + CompressFactor: 30, + }, + }, + CompressFactor: 1, + }, + { + UsageStart: time.Duration(90 * time.Second), + Increments: []*RateSIncrement{ + { + UsageStart: time.Duration(90 * time.Second), + Usage: time.Duration(30 * time.Second), + Rate: rt1, + IntervalRateIndex: 0, + CompressFactor: 30, + }, + { + UsageStart: time.Duration(2 * time.Minute), + Usage: time.Duration(10 * time.Second), + Rate: rt1, + IntervalRateIndex: 1, + CompressFactor: 10, + }, + }, + CompressFactor: 1, + }, + } + eDcml := utils.NewDecimalFromFloat64(4.3) + if cost := CostForIntervals(rtIvls); cost.Float64() != eDcml.Float64() { + t.Errorf("eDcml: %f, received: %f\n", eDcml.Float64(), cost.Float64()) + } +} diff --git a/utils/decimal.go b/utils/decimal.go index 6dede3703..ca24c6a27 100644 --- a/utils/decimal.go +++ b/utils/decimal.go @@ -66,3 +66,7 @@ func (d *Decimal) Add(x, y *Decimal) *Decimal { d.Big.Add(x.Big, y.Big) return d } + +func (d *Decimal) Compare(y *Decimal) int { + return d.Big.Cmp(y.Big) +}