PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/Unit/RandomLib/GeneratorTest.php

https://github.com/ezimuel/RandomLib
PHP | 162 lines | 127 code | 18 blank | 17 comment | 4 complexity | 7020c582bc7a2f72c8a15a6da463bc20 MD5 | raw file
  1. <?php
  2. namespace RandomLib;
  3. class GeneratorTest extends \PHPUnit_Framework_TestCase {
  4. protected $generator = null;
  5. protected $mixer = null;
  6. protected $sources = array();
  7. public static function provideGenerate() {
  8. return array(
  9. array(0, ''),
  10. array(1, chr(0)),
  11. array(2, chr(1) . chr(1)),
  12. array(3, chr(2) . chr(0) . chr(2)),
  13. array(4, chr(3) . chr(3) . chr(3) . chr(3)),
  14. );
  15. }
  16. public static function provideGenerateInt() {
  17. return array(
  18. array(1, 1, 1),
  19. array(0, 1, 0),
  20. array(0, 255, 0),
  21. array(400, 655, 400),
  22. array(0, 65535, 257),
  23. array(65535, 131070, 65792),
  24. array(0, 16777215, (2<<16) + 2),
  25. array(-10, 0, -10),
  26. array(-655, -400, -655),
  27. array(-131070, -65535, -130813),
  28. );
  29. }
  30. public static function provideGenerateIntRangeTest() {
  31. return array(
  32. array(0, 0),
  33. array(0, 1),
  34. array(1, 10000),
  35. array(100000, \PHP_INT_MAX),
  36. );
  37. }
  38. public static function provideGenerateStringTest() {
  39. return array(
  40. array(0, 'ab', ''),
  41. array(1, 'ab', 'a'),
  42. array(1, 'a', ''),
  43. array(2, 'ab', 'aa'),
  44. array(3, 'abc', 'aaa'),
  45. array(8, '0123456789abcdef', '42024420'),
  46. array(16, '0123456789abcdef', '9955115599995511'),
  47. array(16, '', 'dd99dd11dd99dddd'),
  48. );
  49. }
  50. public function setUp() {
  51. $source1 = $this->getMock('RandomLib\Source');
  52. $source1->expects($this->any())
  53. ->method('generate')
  54. ->will($this->returnCallback(function ($size) {
  55. $r = '';
  56. for ($i = 0; $i < $size; $i++) {
  57. $r .= chr($i);
  58. }
  59. return $r;
  60. }
  61. ));
  62. $source2 = $this->getMock('RandomLib\Source');
  63. $source2->expects($this->any())
  64. ->method('generate')
  65. ->will($this->returnCallback(function ($size) {
  66. $r = '';
  67. for ($i = $size - 1; $i >= 0; $i--) {
  68. $r .= chr($i);
  69. }
  70. return $r;
  71. }
  72. ));
  73. $this->mixer = $this->getMock('RandomLib\Mixer');
  74. $this->mixer->expects($this->any())
  75. ->method('mix')
  76. ->will($this->returnCallback(function(array $sources) {
  77. if (empty($sources)) return '';
  78. $start = array_pop($sources);
  79. return array_reduce(
  80. $sources,
  81. function($el1, $el2) {
  82. return $el1 ^ $el2;
  83. },
  84. $start
  85. );
  86. }));
  87. $this->sources = array($source1, $source2);
  88. $this->generator = new Generator($this->sources, $this->mixer);
  89. }
  90. public function testConstruct() {
  91. $this->assertTrue($this->generator instanceof Generator);
  92. }
  93. public function testGetMixer() {
  94. $this->assertSame($this->mixer, $this->generator->getMixer());
  95. }
  96. public function testGetSources() {
  97. $this->assertSame($this->sources, $this->generator->getSources());
  98. }
  99. /**
  100. * @dataProvider provideGenerate
  101. */
  102. public function testGenerate($size, $expect) {
  103. $this->assertEquals($expect, $this->generator->generate($size));
  104. }
  105. /**
  106. * @dataProvider provideGenerateInt
  107. */
  108. public function testGenerateInt($min, $max, $expect) {
  109. $this->assertEquals($expect, $this->generator->generateInt($min, $max));
  110. }
  111. /**
  112. * @dataProvider provideGenerateIntRangeTest
  113. */
  114. public function testGenerateIntRange($min, $max) {
  115. $n = $this->generator->generateInt($min, $max);
  116. $this->assertTrue($min <= $n);
  117. $this->assertTrue($max >= $n);
  118. }
  119. /**
  120. * @expectedException RangeException
  121. */
  122. public function testGenerateIntFail() {
  123. $n = $this->generator->generateInt(-1, PHP_INT_MAX);
  124. }
  125. /**
  126. */
  127. public function testGenerateIntLargeTest() {
  128. $bits = 30;
  129. $expected = 50529027;
  130. if (PHP_INT_MAX > 4000000000) {
  131. $bits = 55;
  132. $expected = 1693273676973062;
  133. }
  134. $n = $this->generator->generateInt(0, (int) pow(2, $bits));
  135. $this->assertEquals($expected, $n);
  136. }
  137. /**
  138. * @dataProvider provideGenerateStringTest
  139. */
  140. public function testGenerateString($length, $chars, $expected) {
  141. $n = $this->generator->generateString($length, $chars);
  142. $this->assertEquals($expected, $n);
  143. }
  144. }