RingBufferTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Tests\Data\Util;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Intl\Data\Util\RingBuffer;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class RingBufferTest extends TestCase
  17. {
  18. /**
  19. * @var RingBuffer
  20. */
  21. private $buffer;
  22. protected function setUp()
  23. {
  24. $this->buffer = new RingBuffer(2);
  25. }
  26. public function testWriteWithinBuffer()
  27. {
  28. $this->buffer[0] = 'foo';
  29. $this->buffer['bar'] = 'baz';
  30. $this->assertTrue(isset($this->buffer[0]));
  31. $this->assertTrue(isset($this->buffer['bar']));
  32. $this->assertSame('foo', $this->buffer[0]);
  33. $this->assertSame('baz', $this->buffer['bar']);
  34. }
  35. public function testWritePastBuffer()
  36. {
  37. $this->buffer[0] = 'foo';
  38. $this->buffer['bar'] = 'baz';
  39. $this->buffer[2] = 'bam';
  40. $this->assertTrue(isset($this->buffer['bar']));
  41. $this->assertTrue(isset($this->buffer[2]));
  42. $this->assertSame('baz', $this->buffer['bar']);
  43. $this->assertSame('bam', $this->buffer[2]);
  44. }
  45. /**
  46. * @expectedException \Symfony\Component\Intl\Exception\OutOfBoundsException
  47. */
  48. public function testReadNonExistingFails()
  49. {
  50. $this->buffer['foo'];
  51. }
  52. public function testQueryNonExisting()
  53. {
  54. $this->assertFalse(isset($this->buffer['foo']));
  55. }
  56. public function testUnsetNonExistingSucceeds()
  57. {
  58. unset($this->buffer['foo']);
  59. $this->assertFalse(isset($this->buffer['foo']));
  60. }
  61. /**
  62. * @expectedException \Symfony\Component\Intl\Exception\OutOfBoundsException
  63. */
  64. public function testReadOverwrittenFails()
  65. {
  66. $this->buffer[0] = 'foo';
  67. $this->buffer['bar'] = 'baz';
  68. $this->buffer[2] = 'bam';
  69. $this->buffer[0];
  70. }
  71. public function testQueryOverwritten()
  72. {
  73. $this->assertFalse(isset($this->buffer[0]));
  74. }
  75. public function testUnsetOverwrittenSucceeds()
  76. {
  77. $this->buffer[0] = 'foo';
  78. $this->buffer['bar'] = 'baz';
  79. $this->buffer[2] = 'bam';
  80. unset($this->buffer[0]);
  81. $this->assertFalse(isset($this->buffer[0]));
  82. }
  83. }