go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/distribution/bucketer_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 TestFixedWidthBucketer(t *testing.T) {
    24  	Convey("Invalid values panic", t, func() {
    25  		So(func() { FixedWidthBucketer(10, -1) }, ShouldPanic)
    26  		So(func() { FixedWidthBucketer(-1, 1) }, ShouldPanic)
    27  	})
    28  
    29  	Convey("Zero size", t, func() {
    30  		b := FixedWidthBucketer(10, 0)
    31  		So(b.NumBuckets(), ShouldEqual, 2)
    32  		So(b.Bucket(-100), ShouldEqual, 0)
    33  		So(b.Bucket(-1), ShouldEqual, 0)
    34  		So(b.Bucket(0), ShouldEqual, 1)
    35  		So(b.Bucket(100), ShouldEqual, 1)
    36  	})
    37  
    38  	Convey("One size", t, func() {
    39  		b := FixedWidthBucketer(10, 1)
    40  		So(b.NumBuckets(), ShouldEqual, 3)
    41  		So(b.Bucket(-100), ShouldEqual, 0)
    42  		So(b.Bucket(-1), ShouldEqual, 0)
    43  		So(b.Bucket(0), ShouldEqual, 1)
    44  		So(b.Bucket(5), ShouldEqual, 1)
    45  		So(b.Bucket(10), ShouldEqual, 2)
    46  		So(b.Bucket(100), ShouldEqual, 2)
    47  	})
    48  }
    49  
    50  func TestGeometricBucketer(t *testing.T) {
    51  	Convey("Invalid values panic", t, func() {
    52  		So(func() { GeometricBucketer(10, -1) }, ShouldPanic)
    53  		So(func() { GeometricBucketer(-1, 10) }, ShouldPanic)
    54  		So(func() { GeometricBucketer(0, 10) }, ShouldPanic)
    55  		So(func() { GeometricBucketer(1, 10) }, ShouldPanic)
    56  	})
    57  
    58  	Convey("Zero size", t, func() {
    59  		b := GeometricBucketer(10, 0)
    60  		So(b.NumBuckets(), ShouldEqual, 2)
    61  		So(b.Bucket(-100), ShouldEqual, 0)
    62  		So(b.Bucket(-1), ShouldEqual, 0)
    63  		So(b.Bucket(0), ShouldEqual, 0)
    64  		So(b.Bucket(100), ShouldEqual, 1)
    65  	})
    66  
    67  	Convey("One size", t, func() {
    68  		b := GeometricBucketer(4, 4)
    69  		So(b.NumBuckets(), ShouldEqual, 6)
    70  		So(b.Bucket(-100), ShouldEqual, 0)
    71  		So(b.Bucket(-1), ShouldEqual, 0)
    72  		So(b.Bucket(0), ShouldEqual, 0)
    73  		So(b.Bucket(1), ShouldEqual, 1)
    74  		So(b.Bucket(3), ShouldEqual, 1)
    75  		So(b.Bucket(4), ShouldEqual, 2)
    76  		So(b.Bucket(15), ShouldEqual, 2)
    77  		So(b.Bucket(16), ShouldEqual, 3)
    78  		So(b.Bucket(63), ShouldEqual, 3)
    79  		So(b.Bucket(64), ShouldEqual, 4)
    80  	})
    81  }