|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Nejcc\PhpDatatypes\Tests; |
| 5 | + |
| 6 | +use Nejcc\PhpDatatypes\Composite\Arrays\FloatArray; |
| 7 | +use Nejcc\PhpDatatypes\Exceptions\InvalidFloatException; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class FloatArrayTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Test creating a valid FloatArray instance. |
| 14 | + * @throws InvalidFloatException |
| 15 | + */ |
| 16 | + public function testCreateValidFloatArray(): void |
| 17 | + { |
| 18 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 19 | + $this->assertSame([10.5, 20.1, 30.7], $floatArray->getValue()); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Test creating a FloatArray with invalid float values. |
| 24 | + */ |
| 25 | + public function testCreateInvalidFloatArray(): void |
| 26 | + { |
| 27 | + $this->expectException(InvalidFloatException::class); |
| 28 | + $this->expectExceptionMessage("All elements must be floats. Invalid value: 1"); |
| 29 | + |
| 30 | + new FloatArray([10.5, 1, 'not a float']); // Invalid non-float values |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Test adding floats to a FloatArray. |
| 35 | + * @throws InvalidFloatException |
| 36 | + */ |
| 37 | + public function testAddFloats(): void |
| 38 | + { |
| 39 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 40 | + $newFloatArray = $floatArray->add(40.2, 50.3); |
| 41 | + |
| 42 | + $this->assertSame([10.5, 20.1, 30.7, 40.2, 50.3], $newFloatArray->getValue()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test removing floats from a FloatArray. |
| 47 | + * @throws InvalidFloatException |
| 48 | + */ |
| 49 | + public function testRemoveFloats(): void |
| 50 | + { |
| 51 | + $floatArray = new FloatArray([10.5, 20.1, 30.7, 40.2]); |
| 52 | + $modifiedFloatArray = $floatArray->remove(20.1, 30.7); |
| 53 | + |
| 54 | + $this->assertSame([10.5, 40.2], $modifiedFloatArray->getValue()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test calculating the sum of floats. |
| 59 | + * @throws InvalidFloatException |
| 60 | + */ |
| 61 | + public function testSumOfFloats(): void |
| 62 | + { |
| 63 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 64 | + $this->assertSame(61.3, $floatArray->sum()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test calculating the average of floats. |
| 69 | + * @throws InvalidFloatException |
| 70 | + */ |
| 71 | + public function testAverageOfFloats(): void |
| 72 | + { |
| 73 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 74 | + $this->assertSame(20.433333333333334, $floatArray->average()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test calculating the average of an empty FloatArray. |
| 79 | + */ |
| 80 | + public function testAverageOfEmptyFloatArray(): void |
| 81 | + { |
| 82 | + $this->expectException(InvalidFloatException::class); |
| 83 | + $this->expectExceptionMessage("Cannot calculate average of an empty array."); |
| 84 | + |
| 85 | + $floatArray = new FloatArray([]); |
| 86 | + $floatArray->average(); // Should throw exception |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test getting the count of floats in a FloatArray. |
| 91 | + * @throws InvalidFloatException |
| 92 | + */ |
| 93 | + public function testGetFloatCount(): void |
| 94 | + { |
| 95 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 96 | + $this->assertCount(3, $floatArray); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Test ArrayAccess implementation for accessing a float at a specific index. |
| 101 | + * @throws InvalidFloatException |
| 102 | + */ |
| 103 | + public function testArrayAccessGet(): void |
| 104 | + { |
| 105 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 106 | + $this->assertSame(20.1, $floatArray[1]); |
| 107 | + $this->assertNull($floatArray[999]); // Accessing an invalid index returns null |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test ArrayAccess prevents modification of FloatArray. |
| 112 | + */ |
| 113 | + public function testArrayAccessSetThrowsException(): void |
| 114 | + { |
| 115 | + $this->expectException(InvalidFloatException::class); |
| 116 | + $this->expectExceptionMessage("Cannot modify an immutable FloatArray."); |
| 117 | + |
| 118 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 119 | + $floatArray[1] = 50.3; // Should throw exception |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test ArrayAccess prevents unsetting a float in FloatArray. |
| 124 | + */ |
| 125 | + public function testArrayAccessUnsetThrowsException(): void |
| 126 | + { |
| 127 | + $this->expectException(InvalidFloatException::class); |
| 128 | + $this->expectExceptionMessage("Cannot unset a value in an immutable FloatArray."); |
| 129 | + |
| 130 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 131 | + unset($floatArray[1]); // Should throw exception |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Test iterating over FloatArray with IteratorAggregate. |
| 136 | + * @throws InvalidFloatException |
| 137 | + */ |
| 138 | + public function testIterationOverFloatArray(): void |
| 139 | + { |
| 140 | + $floatArray = new FloatArray([10.5, 20.1, 30.7]); |
| 141 | + $result = []; |
| 142 | + |
| 143 | + foreach ($floatArray as $float) { |
| 144 | + $result[] = $float; |
| 145 | + } |
| 146 | + |
| 147 | + $this->assertSame([10.5, 20.1, 30.7], $result); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test creating an empty FloatArray. |
| 152 | + * @throws InvalidFloatException |
| 153 | + */ |
| 154 | + public function testEmptyFloatArray(): void |
| 155 | + { |
| 156 | + $floatArray = new FloatArray([]); |
| 157 | + $this->assertSame([], $floatArray->getValue()); |
| 158 | + $this->assertCount(0, $floatArray); |
| 159 | + } |
| 160 | +} |
0 commit comments