gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/les/utils/expiredvalue_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package utils
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestValueExpiration(t *testing.T) {
    24  	var cases = []struct {
    25  		input      ExpiredValue
    26  		timeOffset Fixed64
    27  		expect     uint64
    28  	}{
    29  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 128},
    30  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 64},
    31  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(2), 32},
    32  		{ExpiredValue{Base: 128, Exp: 2}, Uint64ToFixed64(2), 128},
    33  		{ExpiredValue{Base: 128, Exp: 2}, Uint64ToFixed64(3), 64},
    34  	}
    35  	for _, c := range cases {
    36  		if got := c.input.Value(c.timeOffset); got != c.expect {
    37  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    38  		}
    39  	}
    40  }
    41  
    42  func TestValueAddition(t *testing.T) {
    43  	var cases = []struct {
    44  		input      ExpiredValue
    45  		addend     int64
    46  		timeOffset Fixed64
    47  		expect     uint64
    48  		expectNet  int64
    49  	}{
    50  		// Addition
    51  		{ExpiredValue{Base: 128, Exp: 0}, 128, Uint64ToFixed64(0), 256, 128},
    52  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(0), 640, 128},
    53  
    54  		// Addition with offset
    55  		{ExpiredValue{Base: 128, Exp: 0}, 128, Uint64ToFixed64(1), 192, 128},
    56  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(1), 384, 128},
    57  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(3), 192, 128},
    58  
    59  		// Subtraction
    60  		{ExpiredValue{Base: 128, Exp: 0}, -64, Uint64ToFixed64(0), 64, -64},
    61  		{ExpiredValue{Base: 128, Exp: 0}, -128, Uint64ToFixed64(0), 0, -128},
    62  		{ExpiredValue{Base: 128, Exp: 0}, -192, Uint64ToFixed64(0), 0, -128},
    63  
    64  		// Subtraction with offset
    65  		{ExpiredValue{Base: 128, Exp: 0}, -64, Uint64ToFixed64(1), 0, -64},
    66  		{ExpiredValue{Base: 128, Exp: 0}, -128, Uint64ToFixed64(1), 0, -64},
    67  		{ExpiredValue{Base: 128, Exp: 2}, -128, Uint64ToFixed64(1), 128, -128},
    68  		{ExpiredValue{Base: 128, Exp: 2}, -128, Uint64ToFixed64(2), 0, -128},
    69  	}
    70  	for _, c := range cases {
    71  		if net := c.input.Add(c.addend, c.timeOffset); net != c.expectNet {
    72  			t.Fatalf("Net amount mismatch, want=%d, got=%d", c.expectNet, net)
    73  		}
    74  		if got := c.input.Value(c.timeOffset); got != c.expect {
    75  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    76  		}
    77  	}
    78  }
    79  
    80  func TestExpiredValueAddition(t *testing.T) {
    81  	var cases = []struct {
    82  		input      ExpiredValue
    83  		another    ExpiredValue
    84  		timeOffset Fixed64
    85  		expect     uint64
    86  	}{
    87  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 256},
    88  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 384},
    89  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 1}, Uint64ToFixed64(0), 384},
    90  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 128},
    91  	}
    92  	for _, c := range cases {
    93  		c.input.AddExp(c.another)
    94  		if got := c.input.Value(c.timeOffset); got != c.expect {
    95  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    96  		}
    97  	}
    98  }
    99  
   100  func TestExpiredValueSubtraction(t *testing.T) {
   101  	var cases = []struct {
   102  		input      ExpiredValue
   103  		another    ExpiredValue
   104  		timeOffset Fixed64
   105  		expect     uint64
   106  	}{
   107  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 0},
   108  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 1}, Uint64ToFixed64(0), 0},
   109  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 128},
   110  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 64},
   111  	}
   112  	for _, c := range cases {
   113  		c.input.SubExp(c.another)
   114  		if got := c.input.Value(c.timeOffset); got != c.expect {
   115  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
   116  		}
   117  	}
   118  }