github.com/searKing/golang/go@v1.2.117/time/cost_test.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time_test
     6  
     7  import (
     8  	"sort"
     9  	"testing"
    10  	"time"
    11  
    12  	time_ "github.com/searKing/golang/go/time"
    13  )
    14  
    15  func TestCostTick_Costs(t *testing.T) {
    16  	tests := []struct {
    17  		msg       string
    18  		repeat    int
    19  		wantCosts int
    20  	}{
    21  		{
    22  			msg:       "",
    23  			repeat:    10,
    24  			wantCosts: 10,
    25  		},
    26  	}
    27  
    28  	for _, tt := range tests {
    29  		var cost time_.CostTick
    30  		for i := 0; i < tt.repeat; i++ {
    31  			cost.Tick("")
    32  		}
    33  		if len(cost.Costs()) != tt.wantCosts {
    34  			t.Errorf("%s: expected %q got %q", tt.msg, tt.wantCosts, len(cost.Costs()))
    35  		}
    36  	}
    37  }
    38  
    39  func TestCostTick_Sort(t *testing.T) {
    40  	tests := []struct {
    41  		msg    string
    42  		repeat int
    43  		Lesser func(i time.Duration, j time.Duration) bool
    44  	}{
    45  		{
    46  			msg:    "",
    47  			repeat: 10,
    48  			Lesser: nil,
    49  		},
    50  		{
    51  			msg:    "",
    52  			repeat: 10,
    53  			Lesser: func(i time.Duration, j time.Duration) bool {
    54  				return i < j
    55  			},
    56  		},
    57  		{
    58  			msg:    "",
    59  			repeat: 10,
    60  			Lesser: func(i time.Duration, j time.Duration) bool {
    61  				return i > j
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, tt := range tests {
    67  		var cost time_.CostTick
    68  		for i := 0; i < tt.repeat; i++ {
    69  			cost.Tick("")
    70  			time.Sleep(time.Duration(i) * time.Millisecond)
    71  		}
    72  		cost.Sort()
    73  
    74  		if !sort.IsSorted(&cost) {
    75  			t.Errorf("%s: expected sorted got %q", tt.msg, cost)
    76  		}
    77  	}
    78  }