go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/sync/dispatcher/buffer/moving_average_test.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package buffer
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestMovingAverage(t *testing.T) {
    25  	Convey(`movingAverage`, t, func() {
    26  
    27  		Convey(`construction`, func() {
    28  			Convey(`panics on bad window`, func() {
    29  				So(func() {
    30  					newMovingAverage(0, 100)
    31  				}, ShouldPanicLike, "window must be")
    32  			})
    33  		})
    34  
    35  		Convey(`usage`, func() {
    36  			ma := newMovingAverage(10, 17)
    37  
    38  			Convey(`avg matches seed`, func() {
    39  				So(ma.get(), ShouldEqual, 17.0)
    40  			})
    41  
    42  			Convey(`adding new data changes the average`, func() {
    43  				ma.record(100)
    44  				So(ma.get(), ShouldEqual, 25.3)
    45  			})
    46  
    47  			Convey(`adding a lot of data is fine`, func() {
    48  				for i := 0; i < 100; i++ {
    49  					ma.record(100)
    50  				}
    51  				So(ma.get(), ShouldEqual, 100.0)
    52  			})
    53  		})
    54  
    55  	})
    56  }