Browse files

added Unit Tests for Net_IPv4 (based on PHPUnit2)

git-svn-id: http://svn.php.net/repository/pear/packages/Net_IPv4/trunk@207312 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 737af67 commit 086eb226664da65381467c5136e474d6c6d547b7 @winks winks committed Feb 17, 2006
Showing with 97 additions and 0 deletions.
  1. +55 −0 tests/IPv4Test.php
  2. +42 −0 tests/MyIPv4.php
View
55 tests/IPv4Test.php
@@ -0,0 +1,55 @@
+<?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);
+ }
+ }
+
+
+}
+
+
+?>
View
42 tests/MyIPv4.php
@@ -0,0 +1,42 @@
+<?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];
+ }
+}
+?>

0 comments on commit 086eb22

Please sign in to comment.