github.com/haraldrudell/parl@v0.4.176/ptime/averager_test.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package ptime
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestNewAverager(t *testing.T) {
    14  	value1 := time.Second
    15  	value2 := time.Minute
    16  	exp := (value1 + value2) / 2
    17  
    18  	var a *Averager[time.Duration]
    19  	var b float64
    20  	var duration time.Duration
    21  
    22  	a = NewAverager[time.Duration]()
    23  	a.Add(value1)
    24  	a.Add(value2)
    25  
    26  	b, _ = a.Average()
    27  	duration = time.Duration(b)
    28  	if duration != exp {
    29  		t.Errorf("average: %s exp %s", duration, exp)
    30  	}
    31  }