remove duplicate assignments
[civicrm-core.git] / tests / phpunit / CRM / Utils / NumberTest.php
CommitLineData
ac5f2ccd
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3class CRM_Utils_NumberTest extends CiviUnitTestCase {
4
5 function randomDecimalCases() {
6 $cases = array();
7 // array(array $precision, int $expectedMinInclusive, int $expectedMaxExclusive)
8 $cases[] = array(array(1, 0), 0, 10);
9 $cases[] = array(array(5, 2), 0, 1000);
10 $cases[] = array(array(10, 8), 0, 100);
11 return $cases;
12 }
13
14 /**
15 * @param array $precision
16 * @param int $expectedMinInclusive
17 * @param int $expectedMaxExclusive
18 * @dataProvider randomDecimalCases
19 */
20 function testCreateRandomDecimal($precision, $expectedMinInclusive, $expectedMaxExclusive) {
21 list ($sigFigs, $decFigs) = $precision;
22 for ($i = 0; $i < 10; $i++) {
23 $decimal = CRM_Utils_Number::createRandomDecimal($precision);
24 // print "Assert $decimal between $expectedMinInclusive and $expectedMaxExclusive\n";
25 $this->assertTrue(($expectedMinInclusive <= $decimal) && ($decimal < $expectedMaxExclusive), "Assert $decimal between $expectedMinInclusive and $expectedMaxExclusive");
26 if (strpos($decimal, '.') === FALSE) {
27 $decimal .= '.';
28 }
29 list ($before, $after) = explode('.', $decimal);
30 $this->assertTrue(strlen($before) + strlen($after) <= $sigFigs, "Assert $decimal [$before;$after] has <= $sigFigs sigFigs");
31 $this->assertTrue(strlen($after) <= $decFigs, "Assert $decimal [$before;$after] has <= $decFigs decFigs");
32 }
33 }
34
35 function truncDecimalCases() {
36 $cases = array();
37 // array($value, $precision, $expectedValue)
38 $cases[] = array(523, array(1,0), 5);
39 $cases[] = array(523, array(5,2), 523);
40 $cases[] = array(523, array(10,8), 52.3);
41 $cases[] = array(12345, array(3,3), 0.123);
42 $cases[] = array(0.12345, array(10,0), 12345);
43 $cases[] = array(-123.45, array(4,2), -12.34);
44 return $cases;
45 }
46
47 /**
48 * @param $value
49 * @param $precision
50 * @param $expectedValue
51 * @dataProvider truncDecimalCases
52 */
53 function testCreateTruncatedDecimal($value, $precision, $expectedValue) {
54 list ($sigFigs, $decFigs) = $precision;
55 $this->assertEquals($expectedValue, CRM_Utils_Number::createTruncatedDecimal($value, $precision),
56 "assert createTruncatedValue($value, ($sigFigs,$decFigs)) == $expectedValue"
57 );
58 }
59}