Tests.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "BitBufferCounter.h"
  3. #include "BitRateWindow.h"
  4. #include "gtest/gtest.h"
  5. using namespace ml;
  6. TEST(BitBufferCounterTest, Cap_4) {
  7. size_t Capacity = 4;
  8. BitBufferCounter BBC(Capacity);
  9. // No bits set
  10. EXPECT_EQ(BBC.numSetBits(), 0);
  11. // All ones
  12. for (size_t Idx = 0; Idx != (2 * Capacity); Idx++) {
  13. BBC.insert(true);
  14. EXPECT_EQ(BBC.numSetBits(), std::min(Idx + 1, Capacity));
  15. }
  16. // All zeroes
  17. for (size_t Idx = 0; Idx != Capacity; Idx++) {
  18. BBC.insert(false);
  19. if (Idx < Capacity)
  20. EXPECT_EQ(BBC.numSetBits(), Capacity - (Idx + 1));
  21. else
  22. EXPECT_EQ(BBC.numSetBits(), 0);
  23. }
  24. // Even ones/zeroes
  25. for (size_t Idx = 0; Idx != (2 * Capacity); Idx++)
  26. BBC.insert(Idx % 2 == 0);
  27. EXPECT_EQ(BBC.numSetBits(), Capacity / 2);
  28. }
  29. using State = BitRateWindow::State;
  30. using Edge = BitRateWindow::Edge;
  31. using Result = std::pair<Edge, size_t>;
  32. TEST(BitRateWindowTest, Cycles) {
  33. /* Test the FSM by going through its two cycles:
  34. * 1) NotFilled -> AboveThreshold -> Idle -> NotFilled
  35. * 2) NotFilled -> BelowThreshold -> AboveThreshold -> Idle -> NotFilled
  36. *
  37. * Check the window's length on every new state transition.
  38. */
  39. size_t MinLength = 4, MaxLength = 6, IdleLength = 5;
  40. size_t SetBitsThreshold = 3;
  41. Result R;
  42. BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  43. /*
  44. * 1st cycle
  45. */
  46. // NotFilled -> AboveThreshold
  47. R = BRW.insert(true);
  48. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  49. R = BRW.insert(true);
  50. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  51. R = BRW.insert(true);
  52. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  53. R = BRW.insert(true);
  54. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
  55. EXPECT_EQ(R.second, MinLength);
  56. // AboveThreshold -> Idle
  57. R = BRW.insert(true);
  58. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
  59. R = BRW.insert(true);
  60. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
  61. R = BRW.insert(true);
  62. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
  63. EXPECT_EQ(R.second, MaxLength);
  64. // Idle -> NotFilled
  65. R = BRW.insert(true);
  66. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  67. R = BRW.insert(true);
  68. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  69. R = BRW.insert(true);
  70. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  71. R = BRW.insert(true);
  72. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  73. R = BRW.insert(true);
  74. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
  75. EXPECT_EQ(R.second, 1);
  76. // NotFilled -> AboveThreshold
  77. R = BRW.insert(true);
  78. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  79. R = BRW.insert(true);
  80. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  81. R = BRW.insert(true);
  82. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
  83. EXPECT_EQ(R.second, MinLength);
  84. /*
  85. * 2nd cycle
  86. */
  87. BRW = BitRateWindow(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  88. // NotFilled -> BelowThreshold
  89. R = BRW.insert(false);
  90. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  91. R = BRW.insert(false);
  92. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  93. R = BRW.insert(false);
  94. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  95. R = BRW.insert(false);
  96. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::BelowThreshold));
  97. EXPECT_EQ(R.second, MinLength);
  98. // BelowThreshold -> BelowThreshold:
  99. // Check the state's self loop by adding set bits that will keep the
  100. // bit buffer below the specified threshold.
  101. //
  102. for (size_t Idx = 0; Idx != 2 * MaxLength; Idx++) {
  103. R = BRW.insert(Idx % 2 == 0);
  104. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
  105. EXPECT_EQ(R.second, MinLength);
  106. }
  107. // Verify that at the end of the loop the internal bit buffer contains
  108. // "1010". Do so by adding one set bit and checking that we remain below
  109. // the specified threshold.
  110. R = BRW.insert(true);
  111. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
  112. EXPECT_EQ(R.second, MinLength);
  113. // BelowThreshold -> AboveThreshold
  114. R = BRW.insert(true);
  115. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
  116. EXPECT_EQ(R.second, MinLength);
  117. // AboveThreshold -> Idle:
  118. // Do the transition without filling the max window size this time.
  119. R = BRW.insert(false);
  120. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
  121. EXPECT_EQ(R.second, MinLength);
  122. // Idle -> NotFilled
  123. R = BRW.insert(false);
  124. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  125. R = BRW.insert(false);
  126. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  127. R = BRW.insert(false);
  128. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  129. R = BRW.insert(false);
  130. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  131. R = BRW.insert(false);
  132. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
  133. EXPECT_EQ(R.second, 1);
  134. // NotFilled -> AboveThreshold
  135. R = BRW.insert(true);
  136. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  137. R = BRW.insert(true);
  138. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
  139. R = BRW.insert(true);
  140. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
  141. EXPECT_EQ(R.second, MinLength);
  142. }
  143. TEST(BitRateWindowTest, ConsecutiveOnes) {
  144. size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
  145. size_t SetBitsThreshold = 30;
  146. Result R;
  147. BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  148. for (size_t Idx = 0; Idx != MaxLength; Idx++)
  149. R = BRW.insert(false);
  150. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
  151. EXPECT_EQ(R.second, MinLength);
  152. for (size_t Idx = 0; Idx != SetBitsThreshold; Idx++) {
  153. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
  154. R = BRW.insert(true);
  155. }
  156. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
  157. EXPECT_EQ(R.second, MinLength);
  158. // At this point the window's buffer contains:
  159. // (MinLength - SetBitsThreshold = 90) 0s, followed by
  160. // (SetBitsThreshold = 30) 1s.
  161. //
  162. // To go below the threshold, we need to add (90 + 1) more 0s in the window's
  163. // buffer. At that point, the the window's buffer will contain:
  164. // (SetBitsThreshold = 29) 1s, followed by
  165. // (MinLength - SetBitsThreshold = 91) 0s.
  166. //
  167. // Right before adding the last 0, we expect the window's length to be equal to 210,
  168. // because the bit buffer has gone through these bits:
  169. // (MinLength - SetBitsThreshold = 90) 0s, followed by
  170. // (SetBitsThreshold = 30) 1s, followed by
  171. // (MinLength - SetBitsThreshold = 90) 0s.
  172. for (size_t Idx = 0; Idx != (MinLength - SetBitsThreshold); Idx++) {
  173. R = BRW.insert(false);
  174. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
  175. }
  176. EXPECT_EQ(R.second, 2 * MinLength - SetBitsThreshold);
  177. R = BRW.insert(false);
  178. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
  179. // Continue with the Idle -> NotFilled edge.
  180. for (size_t Idx = 0; Idx != IdleLength - 1; Idx++) {
  181. R = BRW.insert(false);
  182. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
  183. }
  184. R = BRW.insert(false);
  185. EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
  186. EXPECT_EQ(R.second, 1);
  187. }
  188. TEST(BitRateWindowTest, WithHoles) {
  189. size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
  190. size_t SetBitsThreshold = 30;
  191. Result R;
  192. BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  193. for (size_t Idx = 0; Idx != MaxLength; Idx++)
  194. R = BRW.insert(false);
  195. for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
  196. R = BRW.insert(true);
  197. for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
  198. R = BRW.insert(false);
  199. for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
  200. R = BRW.insert(true);
  201. for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
  202. R = BRW.insert(false);
  203. for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
  204. R = BRW.insert(true);
  205. EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
  206. EXPECT_EQ(R.second, MinLength);
  207. // The window's bit buffer contains:
  208. // 70 0s, 10 1s, 10 0s, 10 1s, 10 0s, 10 1s.
  209. // Where: 70 = MinLength - (5 / 3) * SetBitsThresholds, ie. we need
  210. // to add (70 + 1) more zeros to make the bit buffer go below the
  211. // threshold and then the window's length should be:
  212. // 70 + 50 + 70 = 190.
  213. BitRateWindow::Edge E;
  214. do {
  215. R = BRW.insert(false);
  216. E = R.first;
  217. } while (E.first != State::AboveThreshold || E.second != State::Idle);
  218. EXPECT_EQ(R.second, 2 * MinLength - (5 * SetBitsThreshold) / 3);
  219. }
  220. TEST(BitRateWindowTest, MinWindow) {
  221. size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
  222. size_t SetBitsThreshold = 30;
  223. Result R;
  224. BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  225. BRW.insert(true);
  226. BRW.insert(false);
  227. for (size_t Idx = 2; Idx != SetBitsThreshold; Idx++)
  228. BRW.insert(true);
  229. for (size_t Idx = SetBitsThreshold; Idx != MinLength - 1; Idx++)
  230. BRW.insert(false);
  231. R = BRW.insert(true);
  232. EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
  233. EXPECT_EQ(R.second, MinLength);
  234. R = BRW.insert(false);
  235. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
  236. }
  237. TEST(BitRateWindowTest, MaxWindow) {
  238. size_t MinLength = 100, MaxLength = 200, IdleLength = 30;
  239. size_t SetBitsThreshold = 50;
  240. Result R;
  241. BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);
  242. for (size_t Idx = 0; Idx != MaxLength; Idx++)
  243. R = BRW.insert(Idx % 2 == 0);
  244. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
  245. EXPECT_EQ(R.second, MaxLength);
  246. R = BRW.insert(false);
  247. EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
  248. }