github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/les/utils/expiredvalue_test.go (about)

     1  // Copyright 2021 The adkgo Authors
     2  // This file is part of the adkgo library (adapted for adkgo from go--ethereum v1.10.8).
     3  //
     4  // the adkgo 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 adkgo 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 adkgo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package utils
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/aidoskuneen/adk-node/common/mclock"
    23  )
    24  
    25  func TestValueExpiration(t *testing.T) {
    26  	var cases = []struct {
    27  		input      ExpiredValue
    28  		timeOffset Fixed64
    29  		expect     uint64
    30  	}{
    31  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 128},
    32  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 64},
    33  		{ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(2), 32},
    34  		{ExpiredValue{Base: 128, Exp: 2}, Uint64ToFixed64(2), 128},
    35  		{ExpiredValue{Base: 128, Exp: 2}, Uint64ToFixed64(3), 64},
    36  	}
    37  	for _, c := range cases {
    38  		if got := c.input.Value(c.timeOffset); got != c.expect {
    39  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    40  		}
    41  	}
    42  }
    43  
    44  func TestValueAddition(t *testing.T) {
    45  	var cases = []struct {
    46  		input      ExpiredValue
    47  		addend     int64
    48  		timeOffset Fixed64
    49  		expect     uint64
    50  		expectNet  int64
    51  	}{
    52  		// Addition
    53  		{ExpiredValue{Base: 128, Exp: 0}, 128, Uint64ToFixed64(0), 256, 128},
    54  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(0), 640, 128},
    55  
    56  		// Addition with offset
    57  		{ExpiredValue{Base: 128, Exp: 0}, 128, Uint64ToFixed64(1), 192, 128},
    58  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(1), 384, 128},
    59  		{ExpiredValue{Base: 128, Exp: 2}, 128, Uint64ToFixed64(3), 192, 128},
    60  
    61  		// Subtraction
    62  		{ExpiredValue{Base: 128, Exp: 0}, -64, Uint64ToFixed64(0), 64, -64},
    63  		{ExpiredValue{Base: 128, Exp: 0}, -128, Uint64ToFixed64(0), 0, -128},
    64  		{ExpiredValue{Base: 128, Exp: 0}, -192, Uint64ToFixed64(0), 0, -128},
    65  
    66  		// Subtraction with offset
    67  		{ExpiredValue{Base: 128, Exp: 0}, -64, Uint64ToFixed64(1), 0, -64},
    68  		{ExpiredValue{Base: 128, Exp: 0}, -128, Uint64ToFixed64(1), 0, -64},
    69  		{ExpiredValue{Base: 128, Exp: 2}, -128, Uint64ToFixed64(1), 128, -128},
    70  		{ExpiredValue{Base: 128, Exp: 2}, -128, Uint64ToFixed64(2), 0, -128},
    71  	}
    72  	for _, c := range cases {
    73  		if net := c.input.Add(c.addend, c.timeOffset); net != c.expectNet {
    74  			t.Fatalf("Net amount mismatch, want=%d, got=%d", c.expectNet, net)
    75  		}
    76  		if got := c.input.Value(c.timeOffset); got != c.expect {
    77  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    78  		}
    79  	}
    80  }
    81  
    82  func TestExpiredValueAddition(t *testing.T) {
    83  	var cases = []struct {
    84  		input      ExpiredValue
    85  		another    ExpiredValue
    86  		timeOffset Fixed64
    87  		expect     uint64
    88  	}{
    89  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 256},
    90  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 384},
    91  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 1}, Uint64ToFixed64(0), 384},
    92  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 128},
    93  	}
    94  	for _, c := range cases {
    95  		c.input.AddExp(c.another)
    96  		if got := c.input.Value(c.timeOffset); got != c.expect {
    97  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
    98  		}
    99  	}
   100  }
   101  
   102  func TestExpiredValueSubtraction(t *testing.T) {
   103  	var cases = []struct {
   104  		input      ExpiredValue
   105  		another    ExpiredValue
   106  		timeOffset Fixed64
   107  		expect     uint64
   108  	}{
   109  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 0},
   110  		{ExpiredValue{Base: 128, Exp: 0}, ExpiredValue{Base: 128, Exp: 1}, Uint64ToFixed64(0), 0},
   111  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(0), 128},
   112  		{ExpiredValue{Base: 128, Exp: 1}, ExpiredValue{Base: 128, Exp: 0}, Uint64ToFixed64(1), 64},
   113  	}
   114  	for _, c := range cases {
   115  		c.input.SubExp(c.another)
   116  		if got := c.input.Value(c.timeOffset); got != c.expect {
   117  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, got)
   118  		}
   119  	}
   120  }
   121  
   122  func TestLinearExpiredValue(t *testing.T) {
   123  	var cases = []struct {
   124  		value  LinearExpiredValue
   125  		now    mclock.AbsTime
   126  		expect uint64
   127  	}{
   128  		{LinearExpiredValue{
   129  			Offset: 0,
   130  			Val:    0,
   131  			Rate:   mclock.AbsTime(1),
   132  		}, 0, 0},
   133  
   134  		{LinearExpiredValue{
   135  			Offset: 1,
   136  			Val:    1,
   137  			Rate:   mclock.AbsTime(1),
   138  		}, 0, 1},
   139  
   140  		{LinearExpiredValue{
   141  			Offset: 1,
   142  			Val:    1,
   143  			Rate:   mclock.AbsTime(1),
   144  		}, mclock.AbsTime(2), 0},
   145  
   146  		{LinearExpiredValue{
   147  			Offset: 1,
   148  			Val:    1,
   149  			Rate:   mclock.AbsTime(1),
   150  		}, mclock.AbsTime(3), 0},
   151  	}
   152  	for _, c := range cases {
   153  		if value := c.value.Value(c.now); value != c.expect {
   154  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, value)
   155  		}
   156  	}
   157  }
   158  
   159  func TestLinearExpiredAddition(t *testing.T) {
   160  	var cases = []struct {
   161  		value  LinearExpiredValue
   162  		amount int64
   163  		now    mclock.AbsTime
   164  		expect uint64
   165  	}{
   166  		{LinearExpiredValue{
   167  			Offset: 0,
   168  			Val:    0,
   169  			Rate:   mclock.AbsTime(1),
   170  		}, -1, 0, 0},
   171  
   172  		{LinearExpiredValue{
   173  			Offset: 1,
   174  			Val:    1,
   175  			Rate:   mclock.AbsTime(1),
   176  		}, -1, 0, 0},
   177  
   178  		{LinearExpiredValue{
   179  			Offset: 1,
   180  			Val:    2,
   181  			Rate:   mclock.AbsTime(1),
   182  		}, -1, mclock.AbsTime(2), 0},
   183  
   184  		{LinearExpiredValue{
   185  			Offset: 1,
   186  			Val:    2,
   187  			Rate:   mclock.AbsTime(1),
   188  		}, -2, mclock.AbsTime(2), 0},
   189  	}
   190  	for _, c := range cases {
   191  		if value := c.value.Add(c.amount, c.now); value != c.expect {
   192  			t.Fatalf("Value mismatch, want=%d, got=%d", c.expect, value)
   193  		}
   194  	}
   195  }