BuffertoolsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitWasp\Buffertools\Tests;
  4. use BitWasp\Buffertools\Buffer;
  5. use BitWasp\Buffertools\Buffertools;
  6. use PHPUnit\Framework\TestCase;
  7. class BuffertoolsTest extends TestCase
  8. {
  9. /**
  10. * @return array
  11. */
  12. private function getUnsortedList(): array
  13. {
  14. return [
  15. '0101',
  16. '4102',
  17. 'a43e',
  18. '0000',
  19. '0120',
  20. 'd01b'
  21. ];
  22. }
  23. /**
  24. * @return array
  25. */
  26. private function getSortedList(): array
  27. {
  28. return [
  29. '0000',
  30. '0101',
  31. '0120',
  32. '4102',
  33. 'a43e',
  34. 'd01b'
  35. ];
  36. }
  37. /**
  38. * @return array
  39. */
  40. private function getUnsortedBufferList(): array
  41. {
  42. $results = [];
  43. foreach ($this->getUnsortedList() as $hex) {
  44. $results[] = Buffer::hex($hex);
  45. }
  46. return $results;
  47. }
  48. /**
  49. * @return array
  50. */
  51. private function getSortedBufferList(): array
  52. {
  53. $results = [];
  54. foreach ($this->getSortedList() as $hex) {
  55. $results[] = Buffer::hex($hex);
  56. }
  57. return $results;
  58. }
  59. public function testSortDefault()
  60. {
  61. $items = $this->getUnsortedBufferList();
  62. $v = Buffertools::sort($items);
  63. $this->assertEquals($this->getSortedBufferList(), $v);
  64. }
  65. public function testSortCallable()
  66. {
  67. $items = $this->getUnsortedList();
  68. $sorted = Buffertools::sort($items, function ($a) {
  69. return Buffer::hex($a);
  70. });
  71. $this->assertEquals($this->getSortedList(), $sorted);
  72. }
  73. public function testNumToVarInt()
  74. {
  75. // Should not prefix with anything. Just return chr($decimal);
  76. for ($i = 0; $i < 253; $i++) {
  77. $decimal = 1;
  78. $expected = chr($decimal);
  79. $val = Buffertools::numToVarInt($decimal)->getBinary();
  80. $this->assertSame($expected, $val);
  81. }
  82. }
  83. public function testNumToVarInt1LowerFailure()
  84. {
  85. // This decimal should NOT return a prefix
  86. $decimal = 0xfc; // 252;
  87. $val = Buffertools::numToVarInt($decimal)->getBinary();
  88. $this->assertSame($val[0], chr(0xfc));
  89. }
  90. public function testNumToVarInt1Lowest()
  91. {
  92. // Decimal > 253 requires a prefix
  93. $decimal = 0xfd;
  94. $expected = chr(0xfd).chr(0xfd).chr(0x00);
  95. $val = Buffertools::numToVarInt($decimal);//->getBinary();
  96. $this->assertSame($expected, $val->getBinary());
  97. }
  98. public function testNumToVarInt1Upper()
  99. {
  100. // This prefix is used up to 0xffff, because if we go higher,
  101. // the prefixes are no longer in agreement
  102. $decimal = 0xffff;
  103. $expected = chr(0xfd) . chr(0xff) . chr(0xff);
  104. $val = Buffertools::numToVarInt($decimal)->getBinary();
  105. $this->assertSame($expected, $val);
  106. }
  107. public function testNumToVarInt2LowerFailure()
  108. {
  109. // We can check that numbers this low don't yield a 0xfe prefix
  110. $decimal = 0xfffe;
  111. $expected = chr(0xfe) . chr(0xfe) . chr(0xff);
  112. $val = Buffertools::numToVarInt($decimal);
  113. $this->assertNotSame($expected, $val);
  114. }
  115. public function testNumToVarInt2Lowest()
  116. {
  117. // With this prefix, check that the lowest for this field IS prefictable.
  118. $decimal = 0xffff0001;
  119. $expected = chr(0xfe) . chr(0x01) . chr(0x00) . chr(0xff) . chr(0xff);
  120. $val = Buffertools::numToVarInt($decimal);
  121. $this->assertSame($expected, $val->getBinary());
  122. }
  123. public function testNumToVarInt2Upper()
  124. {
  125. // Last number that will share 0xfe prefix: 2^32
  126. $decimal = 0xffffffff;
  127. $expected = chr(0xfe) . chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff);
  128. $val = Buffertools::numToVarInt($decimal);//->getBinary();
  129. $this->assertSame($expected, $val->getBinary());
  130. }
  131. public function testFlipBytes()
  132. {
  133. $buffer = Buffer::hex('41');
  134. $string = $buffer->getBinary();
  135. $flip = Buffertools::flipBytes($string);
  136. $this->assertSame($flip, $string);
  137. $buffer = Buffer::hex('4141');
  138. $string = $buffer->getBinary();
  139. $flip = Buffertools::flipBytes($string);
  140. $this->assertSame($flip, $string);
  141. $buffer = Buffer::hex('4142');
  142. $string = $buffer->getBinary();
  143. $flip = Buffertools::flipBytes($string);
  144. $this->assertSame($flip, chr(0x42) . chr(0x41));
  145. $buffer = Buffer::hex('0102030405060708');
  146. $string = $buffer->getBinary();
  147. $flip = Buffertools::flipBytes($string);
  148. $this->assertSame($flip, chr(0x08) . chr(0x07) . chr(0x06) . chr(0x05) . chr(0x04) . chr(0x03) . chr(0x02) . chr(0x01));
  149. }
  150. public function testConcat()
  151. {
  152. $a = Buffer::hex("1100");
  153. $b = Buffer::hex("0011");
  154. $c = Buffer::hex("11", 2);
  155. $this->assertEquals("11000011", Buffertools::concat($a, $b)->getHex());
  156. $this->assertEquals("00111100", Buffertools::concat($b, $a)->getHex());
  157. $this->assertEquals("11000011", Buffertools::concat($a, $c)->getHex());
  158. $this->assertEquals("00111100", Buffertools::concat($c, $a)->getHex());
  159. }
  160. }