ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
DataSizeTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2017 Daniel Weise <daniel.weise@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
4require_once("./libs/composer/vendor/autoload.php");
5
7use PHPUnit\Framework\TestCase;
8
14class DataSizeTest extends TestCase
15{
19 public function test_normal($a, $b, $expected, $expected_in_bytes)
20 {
21 $ds = new DataSize($a, $b);
22 $this->assertEquals($a / $b, $ds->getSize());
23 $this->assertEquals($b, $ds->getUnit());
24 $this->assertEquals($expected, $ds->__toString());
25 if ($expected_in_bytes) {
26 $this->assertEquals($expected_in_bytes, $ds->inBytes());
27 }
28 }
29
30 public function test_division_by_zero()
31 {
32 try {
33 $ds = new DataSize(4533, 0);
34 $this->assertFalse("This should not happen");
35 } catch (\Exception $e) {
36 $this->assertTrue(true);
37 }
38 }
39
40 public function tDataProvider()
41 {
42 return [
43 [122, 1000, "0.122 KB", 122],
44 [-122, 1000, "-0.122 KB", -122],
45 [122, 1000000, "0.000122 MB", 122],
46 [-122, 1000000, "-0.000122 MB", -122],
47 [122, 1000000000, "1.22E-7 GB", 122],
48 [-122, 1000000000, "-1.22E-7 GB", -122],
49 [122, 1000000000000, "1.22E-10 TB", null], // There is a float rounding error here
50 [-122, 1000000000000, "-1.22E-10 TB", null], // There is a float rounding error here
51 [122, 1000000000000000, "1.22E-13 PB", 122],
52 [-122, 1000000000000000, "-1.22E-13 PB", -122],
53 [122, 1000000000000000000, "1.22E-16 EB", 122],
54 [-122, 1000000000000000000, "-1.22E-16 EB", -122],
55 [122, 1024, "0.119140625 KiB", 122],
56 [-122, 1024, "-0.119140625 KiB", -122],
57 [122, 1048576, "0.00011634826660156 MiB", 122],
58 [-122, 1048576, "-0.00011634826660156 MiB", -122],
59 [122, 1073741824, "1.1362135410309E-7 GiB", 122],
60 [-122, 1073741824, "-1.1362135410309E-7 GiB", -122],
61 [122, 1099511627776, "1.109583536163E-10 TiB", 122],
62 [-122, 1099511627776, "-1.109583536163E-10 TiB", -122],
63 [122, 1125899906842624, "1.0835776720342E-13 PiB", 122],
64 [-122, 1125899906842624, "-1.0835776720342E-13 PiB", -122],
65 [122, 1152921504606846976, "1.0581813203459E-16 EiB", 122],
66 [-122, 1152921504606846976, "-1.0581813203459E-16 EiB", -122],
67 [10 * DataSize::KiB, DataSize::KiB, "10 KiB", 10 * DataSize::KiB]
68
69 // This tests will fail because the second param of DataSize
70 // needs an integer and this numbers are to big.
71 // [122, 1000000000000000000000, "1.22E-19 ZB"],
72 // [-122, 1000000000000000000000, "-1.22E-19 ZB"],
73 // [122, 1000000000000000000000000, "1.22E-19 YB"],
74 // [-122, 1000000000000000000000000, "-1.22E-19 YB"]
75 ];
76 }
77}
An exception for terminatinating execution or to throw for unit testing.
Testing the DataSize object.
test_normal($a, $b, $expected, $expected_in_bytes)
@dataProvider tDataProvider
Class DataSize.
Definition: DataSize.php:16
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples