NumTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Tests Num
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.num
  8. * @package Kohana
  9. * @category Tests
  10. * @author Kohana Team
  11. * @author BRMatt <matthew@sigswitch.com>
  12. * @copyright (c) Kohana Team
  13. * @license https://koseven.ga/LICENSE.md
  14. */
  15. class Kohana_NumTest extends Unittest_TestCase
  16. {
  17. protected $default_locale;
  18. /**
  19. * SetUp test enviroment
  20. */
  21. // @codingStandardsIgnoreStart
  22. public function setUp()
  23. // @codingStandardsIgnoreEnd
  24. {
  25. parent::setUp();
  26. $this->default_locale = setlocale(LC_ALL, 0);
  27. setlocale(LC_ALL, 'en_US.utf8');
  28. }
  29. /**
  30. * Tear down environment
  31. */
  32. // @codingStandardsIgnoreStart
  33. public function tearDown()
  34. // @codingStandardsIgnoreEnd
  35. {
  36. parent::tearDown();
  37. setlocale(LC_ALL, $this->default_locale);
  38. }
  39. /**
  40. * Provides test data for test_bytes()
  41. *
  42. * @return array
  43. */
  44. public function provider_bytes()
  45. {
  46. return [
  47. [204800.0, '200K'],
  48. [5242880.0, '5MiB'],
  49. [1000.0, 1000],
  50. [2684354560.0, '2.5GB'],
  51. ];
  52. }
  53. /**
  54. * Tests Num::bytes()
  55. *
  56. * @test
  57. * @covers Num::bytes
  58. * @dataProvider provider_bytes
  59. * @param integer Expected Value
  60. * @param string Input value
  61. */
  62. public function test_bytes($expected, $size)
  63. {
  64. $this->assertSame($expected, Num::bytes($size));
  65. }
  66. /**
  67. * Provides test data for test_ordinal()
  68. * @return array
  69. */
  70. public function provider_ordinal()
  71. {
  72. return [
  73. [0, 'th'],
  74. [1, 'st'],
  75. [21, 'st'],
  76. [112, 'th'],
  77. [23, 'rd'],
  78. [42, 'nd'],
  79. ];
  80. }
  81. /**
  82. *
  83. * @test
  84. * @dataProvider provider_ordinal
  85. * @param integer $number
  86. * @param <type> $expected
  87. */
  88. public function test_ordinal($number, $expected)
  89. {
  90. $this->assertSame($expected, Num::ordinal($number));
  91. }
  92. /**
  93. * Provides test data for test_format()
  94. * @return array
  95. */
  96. public function provider_format()
  97. {
  98. return [
  99. // English
  100. [10000, 2, FALSE, '10,000.00'],
  101. [10000, 2, TRUE, '10,000.00'],
  102. // Additional dp's should be removed
  103. [123.456, 2, FALSE, '123.46'],
  104. [123.456, 2, TRUE, '123.46'],
  105. ];
  106. }
  107. /**
  108. * @todo test locales
  109. * @test
  110. * @dataProvider provider_format
  111. * @param integer $number
  112. * @param integer $places
  113. * @param boolean $monetary
  114. * @param string $expected
  115. */
  116. public function test_format($number, $places, $monetary, $expected)
  117. {
  118. $this->assertSame($expected, Num::format($number, $places, $monetary));
  119. }
  120. /**
  121. * Provides data for test_round()
  122. * @return array
  123. */
  124. function provider_round()
  125. {
  126. return [
  127. [5.5, 0, [
  128. 6.0,
  129. 5.0,
  130. 6.0,
  131. 5.0,
  132. ]],
  133. [42.5, 0, [
  134. 43.0,
  135. 42.0,
  136. 42.0,
  137. 43.0,
  138. ]],
  139. [10.4, 0, [
  140. 10.0,
  141. 10.0,
  142. 10.0,
  143. 10.0,
  144. ]],
  145. [10.8, 0, [
  146. 11.0,
  147. 11.0,
  148. 11.0,
  149. 11.0,
  150. ]],
  151. [-5.5, 0, [
  152. -6.0,
  153. -5.0,
  154. -6.0,
  155. -5.0,
  156. ]],
  157. [-10.5, 0, [
  158. -11.0,
  159. -10.0,
  160. -10.0,
  161. -11.0,
  162. ]],
  163. [26.12375, 4, [
  164. 26.1238,
  165. 26.1237,
  166. 26.1238,
  167. 26.1237,
  168. ]],
  169. [26.12325, 4, [
  170. 26.1233,
  171. 26.1232,
  172. 26.1232,
  173. 26.1233,
  174. ]],
  175. ];
  176. }
  177. /**
  178. * @test
  179. * @dataProvider provider_round
  180. * @param number $input
  181. * @param integer $precision
  182. * @param integer $mode
  183. * @param number $expected
  184. */
  185. function test_round($input, $precision, $expected)
  186. {
  187. foreach ([Num::ROUND_HALF_UP, Num::ROUND_HALF_DOWN, Num::ROUND_HALF_EVEN, Num::ROUND_HALF_ODD] as $i => $mode)
  188. {
  189. $this->assertSame($expected[$i], Num::round($input, $precision, $mode, FALSE));
  190. }
  191. }
  192. }