cvs: pear /Net_IPv4/tests IPv4Test.php MyIPv4.php
| From: | Florian Anderiasch | Date: | Fri, 17 Feb 2006 03:50:15 +0000 |
| Subject: | cvs: pear /Net_IPv4/tests IPv4Test.php MyIPv4.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-38297@lists.php.net to get a copy of this message | ||
fa Fri Feb 17 03:50:15 2006 UTC
Added files:
/pear/Net_IPv4/tests IPv4Test.php MyIPv4.php
Log:
added Unit Tests for Net_IPv4 (based on PHPUnit2)
http://cvs.php.net/viewcvs.cgi/pear/Net_IPv4/tests/IPv4Test.php?view=markup&rev=1.1
Index: pear/Net_IPv4/tests/IPv4Test.php
+++ pear/Net_IPv4/tests/IPv4Test.php
<?php
require_once 'PHPUnit2/Framework/TestCase.php';
require_once 'Net/IPv4.php';
require_once 'MyIPv4.php';
class IPv4Test extends PHPUnit2_Framework_TestCase {
protected $net;
protected $quadIPs;
protected $hexIPs;
#function IPv4Test($name) {
#$this->PHPUnit_TestCase($name);
#}
protected function setUp() {
$this->net = new Net_IPv4();
$this->quadIPs = array('10.0.0.15' => 167772175, '172.16.0.155'
=> 2886729883,
'192.168.0.255' => 3232235775, '1.2.3.4' => 16909060,
'1.0.0.0' => 16777216,
'255.255.255.255' => 4294967295, '127.0.0.1' => 2130706433);
$this->hexIPs = array('0a00000f', 'ac10009b', 'c0a800ff',
'01020304',
'01000000', 'ffffffff', '7f000001');
}
protected function tearDown() {
$this->net = null;
}
public function test_atoh() {
foreach ($this->quadIPs as $ip => $v) {
$this->assertTrue(Net_IPv4::atoh($ip) === MyIPv4::atoh($ip));
}
}
public function test_htoa() {
foreach ($this->hexIPs as $ip => $v) {
$this->assertTrue(Net_IPv4::htoa($ip) === MyIPv4::htoa($ip));
}
}
public function test_validateIP() {
foreach ($this->quadIPs as $ip => $v) {
$this->assertTrue(Net_IPv4::validateIP($ip) === MyIPv4::validateIP($ip));
}
}
public function test_ip2double() {
foreach ($this->quadIPs as $k => $v) {
$this->assertTrue(Net_IPv4::ip2double($k) == $v);
}
}
}
?>
http://cvs.php.net/viewcvs.cgi/pear/Net_IPv4/tests/MyIPv4.php?view=markup&rev=1.1
Index: pear/Net_IPv4/tests/MyIPv4.php
+++ pear/Net_IPv4/tests/MyIPv4.php
<?php
class MyIPv4 {
public function validateIP($ip) {
$quad = split('\.', $ip);
if (count($quad) != 4) {
return false;
}
foreach($quad as $q) {
if (!is_numeric($q)) {
return false;
} else if (intval($q) < 0 || intval($q) > 255) {
return false;
}
}
return true;
}
public function atoh($ip) {
$quad = split('\.', $ip);
$new = array();
foreach($quad as $k => $v) {
$n = str_pad(dechex($v), 2, "0", STR_PAD_LEFT);
$new[$k] = $n;
#var_dump($n);
}
return implode($new);
}
public function htoa($ip) {
$quad = str_split($ip, 2);
if (count($quad) != 4) {
return false;
}
$new = array();
foreach($quad as $k => $v) {
$new[$k] = hexdec($v);
#var_dump($n);
}
return $new[0].'.'.$new[1].'.'.$new[2].'.'.$new[3];
}
}
?>