1fc05d3ad78eb0a12b7c9f539e9344a5b5c7089b
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / NumberTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Number;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * Tests number manipulation utilities.
10  *
11  * @group Utility
12  *
13  * @coversDefaultClass \Drupal\Component\Utility\Number
14  *
15  * @see \Drupal\Component\Utility\Number
16  */
17 class NumberTest extends TestCase {
18
19   /**
20    * Tests Number::validStep() without offset.
21    *
22    * @dataProvider providerTestValidStep
23    * @covers ::validStep
24    *
25    * @param numeric $value
26    *   The value argument for Number::validStep().
27    * @param numeric $step
28    *   The step argument for Number::validStep().
29    * @param bool $expected
30    *   Expected return value from Number::validStep().
31    */
32   public function testValidStep($value, $step, $expected) {
33     $return = Number::validStep($value, $step);
34     $this->assertEquals($expected, $return);
35   }
36
37   /**
38    * Tests Number::validStep() with offset.
39    *
40    * @dataProvider providerTestValidStepOffset
41    * @covers ::validStep
42    *
43    * @param numeric $value
44    *   The value argument for Number::validStep().
45    * @param numeric $step
46    *   The step argument for Number::validStep().
47    * @param numeric $offset
48    *   The offset argument for Number::validStep().
49    * @param bool $expected
50    *   Expected return value from Number::validStep().
51    */
52   public function testValidStepOffset($value, $step, $offset, $expected) {
53     $return = Number::validStep($value, $step, $offset);
54     $this->assertEquals($expected, $return);
55   }
56
57   /**
58    * Provides data for self::testNumberStep().
59    *
60    * @see \Drupal\Tests\Component\Utility\Number::testValidStep
61    */
62   public static function providerTestValidStep() {
63     return [
64       // Value and step equal.
65       [10.3, 10.3, TRUE],
66
67       // Valid integer steps.
68       [42, 21, TRUE],
69       [42, 3, TRUE],
70
71       // Valid float steps.
72       [42, 10.5, TRUE],
73       [1, 1 / 3, TRUE],
74       [-100, 100 / 7, TRUE],
75       [1000, -10, TRUE],
76
77       // Valid and very small float steps.
78       [1000.12345, 1e-10, TRUE],
79       [3.9999999999999, 1e-13, TRUE],
80
81       // Invalid integer steps.
82       [100, 30, FALSE],
83       [-10, 4, FALSE],
84
85       // Invalid float steps.
86       [6, 5 / 7, FALSE],
87       [10.3, 10.25, FALSE],
88
89       // Step mismatches very close to being valid.
90       [70 + 9e-7, 10 + 9e-7, FALSE],
91       [1936.5, 3e-8, FALSE],
92     ];
93   }
94
95   /**
96    * Data provider for \Drupal\Test\Component\Utility\NumberTest::testValidStepOffset().
97    *
98    * @see \Drupal\Test\Component\Utility\NumberTest::testValidStepOffset()
99    */
100   public static function providerTestValidStepOffset() {
101     return [
102       // Try obvious fits.
103       [11.3, 10.3, 1, TRUE],
104       [100, 10, 50, TRUE],
105       [-100, 90 / 7, -10, TRUE],
106       [2 / 7 + 5 / 9, 1 / 7, 5 / 9, TRUE],
107
108       // Ensure a small offset is still invalid.
109       [10.3, 10.3, 0.0001, FALSE],
110       [1 / 5, 1 / 7, 1 / 11, FALSE],
111
112       // Try negative values and offsets.
113       [1000, 10, -5, FALSE],
114       [-10, 4, 0, FALSE],
115       [-10, 4, -4, FALSE],
116     ];
117   }
118
119   /**
120    * Tests the alphadecimal conversion functions.
121    *
122    * @dataProvider providerTestConversions
123    * @covers ::intToAlphadecimal
124    * @covers ::alphadecimalToInt
125    *
126    * @param int $value
127    *   The integer value.
128    * @param string $expected
129    *   The expected alphadecimal value.
130    */
131   public function testConversions($value, $expected) {
132     $this->assertSame(Number::intToAlphadecimal($value), $expected);
133     $this->assertSame($value, Number::alphadecimalToInt($expected));
134   }
135
136   /**
137    * Data provider for testConversions().
138    *
139    * @see testConversions()
140    *
141    * @return array
142    *   An array containing:
143    *     - The integer value.
144    *     - The alphadecimal value.
145    */
146   public function providerTestConversions() {
147     return [
148       [0, '00'],
149       [1, '01'],
150       [10, '0a'],
151       [20, '0k'],
152       [35, '0z'],
153       [36, '110'],
154       [100, '12s'],
155     ];
156   }
157
158 }