expiredvalue_test.go 5.7 KB

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