go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/distribution/distribution_test.go (about)

     1  // Copyright 2015 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 distribution
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestNew(t *testing.T) {
    24  	Convey("Passing nil uses the default bucketer", t, func() {
    25  		d := New(nil)
    26  		So(d.Bucketer(), ShouldEqual, DefaultBucketer)
    27  	})
    28  }
    29  
    30  func TestAdd(t *testing.T) {
    31  	Convey("Add", t, func() {
    32  		d := New(FixedWidthBucketer(10, 2))
    33  		So(d.Sum(), ShouldEqual, 0)
    34  		So(d.Count(), ShouldEqual, 0)
    35  
    36  		d.Add(1)
    37  		So(d.Buckets(), ShouldResemble, []int64{0, 1})
    38  		d.Add(10)
    39  		So(d.Buckets(), ShouldResemble, []int64{0, 1, 1})
    40  		d.Add(20)
    41  		So(d.Buckets(), ShouldResemble, []int64{0, 1, 1, 1})
    42  		d.Add(30)
    43  		So(d.Buckets(), ShouldResemble, []int64{0, 1, 1, 2})
    44  		So(d.Sum(), ShouldEqual, 61)
    45  		So(d.Count(), ShouldEqual, 4)
    46  	})
    47  }
    48  
    49  func TestClone(t *testing.T) {
    50  	Convey("Clone empty", t, func() {
    51  		d := New(FixedWidthBucketer(10, 2))
    52  		So(d, ShouldResemble, d.Clone())
    53  	})
    54  
    55  	Convey("Clone populated", t, func() {
    56  		d := New(FixedWidthBucketer(10, 2))
    57  		d.Add(1)
    58  		d.Add(10)
    59  		d.Add(20)
    60  
    61  		clone := d.Clone()
    62  		So(d, ShouldResemble, clone)
    63  
    64  		d.Add(30)
    65  		So(d, ShouldNotResemble, clone)
    66  	})
    67  }