github.com/prysmaticlabs/prysm@v1.4.4/shared/slotutil/slottime_test.go (about)

     1  package slotutil
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  	"github.com/prysmaticlabs/prysm/shared/params"
     9  	"github.com/prysmaticlabs/prysm/shared/timeutils"
    10  )
    11  
    12  func TestSlotsSinceGenesis(t *testing.T) {
    13  	type args struct {
    14  		genesis time.Time
    15  	}
    16  	tests := []struct {
    17  		name string
    18  		args args
    19  		want types.Slot
    20  	}{
    21  		{
    22  			name: "pre-genesis",
    23  			args: args{
    24  				genesis: timeutils.Now().Add(1 * time.Hour), // 1 hour in the future
    25  			},
    26  			want: 0,
    27  		},
    28  		{
    29  			name: "post-genesis",
    30  			args: args{
    31  				genesis: timeutils.Now().Add(-5 * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second),
    32  			},
    33  			want: 5,
    34  		},
    35  	}
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			if got := SlotsSinceGenesis(tt.args.genesis); got != tt.want {
    39  				t.Errorf("SlotsSinceGenesis() = %v, want %v", got, tt.want)
    40  			}
    41  		})
    42  	}
    43  }
    44  
    45  func TestAbsoluteValueSlotDifference(t *testing.T) {
    46  	type args struct {
    47  		x types.Slot
    48  		y types.Slot
    49  	}
    50  	tests := []struct {
    51  		name string
    52  		args args
    53  		want uint64
    54  	}{
    55  		{
    56  			name: "x_<_y",
    57  			args: args{
    58  				x: types.Slot(3),
    59  				y: types.Slot(4),
    60  			},
    61  			want: 1,
    62  		},
    63  		{
    64  			name: "x_>_y",
    65  			args: args{
    66  				x: types.Slot(100),
    67  				y: types.Slot(4),
    68  			},
    69  			want: 96,
    70  		},
    71  		{
    72  			name: "x_==_y",
    73  			args: args{
    74  				x: types.Slot(100),
    75  				y: types.Slot(100),
    76  			},
    77  			want: 0,
    78  		},
    79  	}
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			if got := AbsoluteValueSlotDifference(tt.args.x, tt.args.y); got != tt.want {
    83  				t.Errorf("AbsoluteValueSlotDifference() = %v, want %v", got, tt.want)
    84  			}
    85  		})
    86  	}
    87  }
    88  
    89  func TestMultiplySlotBy(t *testing.T) {
    90  	type args struct {
    91  		times int64
    92  	}
    93  	tests := []struct {
    94  		name string
    95  		args args
    96  		want time.Duration
    97  	}{
    98  		{
    99  			name: "multiply by 1",
   100  			args: args{
   101  				times: 1,
   102  			},
   103  			want: time.Duration(12) * time.Second,
   104  		},
   105  		{
   106  			name: "multiply by 2",
   107  			args: args{
   108  				times: 2,
   109  			},
   110  			want: time.Duration(24) * time.Second,
   111  		},
   112  		{
   113  			name: "multiply by 10",
   114  			args: args{
   115  				times: 10,
   116  			},
   117  			want: time.Duration(120) * time.Second,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			if got := MultiplySlotBy(tt.args.times); got != tt.want {
   123  				t.Errorf("MultiplySlotBy() = %v, want %v", got, tt.want)
   124  			}
   125  		})
   126  	}
   127  }