forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversionsTest.php
88 lines (76 loc) · 3.5 KB
/
ConversionsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Conversions/BinaryToDecimal.php';
require_once __DIR__ . '/../../Conversions/DecimalToBinary.php';
require_once __DIR__ . '/../../Conversions/OctalToDecimal.php';
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
class ConversionsTest extends TestCase
{
public function testBinaryToDecimal()
{
$this->assertEquals(7, binaryToDecimal(111));
$this->assertEquals(5, binaryToDecimal(101));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Binary Number for Converting it to a Decimal Number.');
binaryToDecimal("this is a string");
}
public function testDecimalToBinary()
{
$this->assertEquals(111, decimalToBinary(7));
$this->assertEquals(101, decimalToBinary(5));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to a Binary Number.');
decimalToBinary("this is a string");
}
public function testOctalToDecimal()
{
$this->assertEquals(8, octalToDecimal(10));
$this->assertEquals(9, octalToDecimal(11));
$this->assertEquals(589, octalToDecimal(1115));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Octal Number for Converting it to a Decimal Number.');
octalToDecimal("this is a string");
}
public function testDecimalToOctal()
{
$this->assertEquals(10, decimalToOctal(8));
$this->assertEquals(11, decimalToOctal(9));
$this->assertEquals(1115, decimalToOctal(589));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to an Octal Number.');
decimalToOctal("this is a string");
}
public function testDecimalToHex()
{
$this->assertEquals('A', decimalToHex(10));
$this->assertEquals('1D28A0D3', decimalToHex(489201875));
$this->assertEquals('AB', decimalToHex(171));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to a Hexadecimal Number.');
decimalToHex("this is a string");
}
public function testHexToDecimal()
{
$this->assertEquals(10, hexToDecimal('A'));
$this->assertEquals(489201875, hexToDecimal('1D28A0D3'));
$this->assertEquals(171, hexToDecimal('AB'));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Please pass a valid Hexadecimal Number for Converting it to a Decimal Number.');
hexToDecimal("this is a string");
}
public function testSpeedConversion()
{
$this->assertEquals(11.18, convertSpeed(5, 'm/s', 'mph'));
$this->assertEquals(5.49, convertSpeed(5, 'ft/s', 'km/h'));
$this->assertEquals(3, convertSpeed(3, 'km/h', 'km/h'));
$this->assertEquals(12.96, convertSpeed(7, 'kn', 'km/h'));
$this->assertEquals(19.31, convertSpeed(12, 'mph', 'km/h'));
$this->assertEquals(1, convertSpeed(0.514, 'm/s', 'kn'));
$this->expectException(\Exception::class);
convertSpeed('1', 'km/h', 'mph');
$this->expectException(\Exception::class);
convertSpeed(1, 'km/h', 'miles');
}
}