NumTest.php 3.6 KB

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