home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / tmp / Structures_Graph-1.0.2 / tests / testCase / BasicGraph.php
Encoding:
PHP Script  |  2007-02-01  |  9.0 KB  |  183 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +-----------------------------------------------------------------------------+
  4. // | Copyright (c) 2003 SΘrgio Gonτalves Carvalho                                |
  5. // +-----------------------------------------------------------------------------+
  6. // | This file is part of Structures_Graph.                                      |
  7. // |                                                                             |
  8. // | Structures_Graph is free software; you can redistribute it and/or modify    |
  9. // | it under the terms of the GNU Lesser General Public License as published by |
  10. // | the Free Software Foundation; either version 2.1 of the License, or         |
  11. // | (at your option) any later version.                                         |
  12. // |                                                                             |
  13. // | Structures_Graph is distributed in the hope that it will be useful,         |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  16. // | GNU Lesser General Public License for more details.                         |
  17. // |                                                                             |
  18. // | You should have received a copy of the GNU Lesser General Public License    |
  19. // | along with Structures_Graph; if not, write to the Free Software             |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  21. // | 02111-1307 USA                                                              |
  22. // +-----------------------------------------------------------------------------+
  23. // | Author: SΘrgio Carvalho <sergio.carvalho@portugalmail.com>                  |
  24. // +-----------------------------------------------------------------------------+
  25. //
  26.  
  27. require_once 'Structures/Graph.php';
  28. require_once 'PHPUnit.php';
  29.  
  30. /**
  31.  * @access private
  32.  */
  33. class BasicGraph extends PHPUnit_TestCase
  34. {
  35.     var $_graph = null;
  36.  
  37.     // constructor of the test suite
  38.     function StringTest($name) {
  39.        $this->PHPUnit_TestCase($name);
  40.     }
  41.  
  42.     function setUp() {
  43.     }
  44.  
  45.     function tearDown() {
  46.     }
  47.  
  48.     function test_create_graph() {
  49.         $this->_graph = new Structures_Graph();
  50.         $this->assertTrue(is_a($this->_graph, 'Structures_Graph')); 
  51.     }
  52.  
  53.     function test_add_node() {
  54.         $this->_graph = new Structures_Graph();
  55.         $data = 1;
  56.         $node = new Structures_Graph_Node($data);
  57.         $this->_graph->addNode($node);
  58.         $node = new Structures_Graph_Node($data);
  59.         $this->_graph->addNode($node);
  60.         $node = new Structures_Graph_Node($data);
  61.         $this->_graph->addNode($node);
  62.     }
  63.  
  64.     function test_connect_node() {
  65.         $this->_graph = new Structures_Graph();
  66.         $data = 1;
  67.         $node1 = new Structures_Graph_Node($data);
  68.         $node2 = new Structures_Graph_Node($data);
  69.         $this->_graph->addNode($node1);
  70.         $this->_graph->addNode($node2);
  71.         $node1->connectTo($node2);
  72.  
  73.         $node =& $this->_graph->getNodes();
  74.         $node =& $node[0];
  75.         $node = $node->getNeighbours();
  76.         $node =& $node[0];
  77.         /* 
  78.          ZE1 == and === operators fail on $node,$node2 because of the recursion introduced
  79.          by the _graph field in the Node object. So, we'll use the stupid method for reference
  80.          testing
  81.         */
  82.         $node = true;
  83.         $this->assertTrue($node2);
  84.         $node = false;
  85.         $this->assertFalse($node2);
  86.     }
  87.  
  88.     function test_data_references() {
  89.         $this->_graph = new Structures_Graph();
  90.         $data = 1;
  91.         $node = new Structures_Graph_Node();
  92.         $node->setData(&$data);
  93.         $this->_graph->addNode($node);
  94.         $data = 2;
  95.         $dataInNode =& $this->_graph->getNodes();
  96.         $dataInNode =& $dataInNode[0];
  97.         $dataInNode =& $dataInNode->getData();
  98.         $this->assertTrue($data === $dataInNode);
  99.     }
  100.  
  101.     function test_metadata_references() {
  102.         $this->_graph = new Structures_Graph();
  103.         $data = 1;
  104.         $node = new Structures_Graph_Node();
  105.         $node->setMetadata('5', &$data);
  106.         $data = 2;
  107.         $dataInNode =& $node->getMetadata('5');
  108.         $this->assertTrue($data === $dataInNode);
  109.     }
  110.    
  111.     function test_metadata_key_exists() {
  112.         $this->_graph = new Structures_Graph();
  113.         $data = 1;
  114.         $node = new Structures_Graph_Node();
  115.         $node->setMetadata('5', $data);
  116.         $this->assertTrue($node->metadataKeyExists('5'));
  117.         $this->assertFalse($node->metadataKeyExists('1'));
  118.     }
  119.  
  120.     function test_directed_degree() {
  121.         $this->_graph = new Structures_Graph(true);
  122.         $node = array();
  123.         $node[] = new Structures_Graph_Node();
  124.         $node[] = new Structures_Graph_Node();
  125.         $node[] = new Structures_Graph_Node();
  126.         $this->_graph->addNode($node[0]);
  127.         $this->_graph->addNode($node[1]);
  128.         $this->_graph->addNode($node[2]);
  129.         $this->assertEquals(0, $node[0]->inDegree(), 'inDegree test failed for node 0 with 0 arcs');
  130.         $this->assertEquals(0, $node[1]->inDegree(), 'inDegree test failed for node 1 with 0 arcs');
  131.         $this->assertEquals(0, $node[2]->inDegree(), 'inDegree test failed for node 2 with 0 arcs');
  132.         $this->assertEquals(0, $node[0]->outDegree(), 'outDegree test failed for node 0 with 0 arcs');
  133.         $this->assertEquals(0, $node[1]->outDegree(), 'outDegree test failed for node 1 with 0 arcs');
  134.         $this->assertEquals(0, $node[2]->outDegree(), 'outDegree test failed for node 2 with 0 arcs');
  135.         $node[0]->connectTo($node[1]);
  136.         $this->assertEquals(0, $node[0]->inDegree(), 'inDegree test failed for node 0 with 1 arc');
  137.         $this->assertEquals(1, $node[1]->inDegree(), 'inDegree test failed for node 1 with 1 arc');
  138.         $this->assertEquals(0, $node[2]->inDegree(), 'inDegree test failed for node 2 with 1 arc');
  139.         $this->assertEquals(1, $node[0]->outDegree(), 'outDegree test failed for node 0 with 1 arc');
  140.         $this->assertEquals(0, $node[1]->outDegree(), 'outDegree test failed for node 1 with 1 arc');
  141.         $this->assertEquals(0, $node[2]->outDegree(), 'outDegree test failed for node 2 with 1 arc');
  142.         $node[0]->connectTo($node[2]);
  143.         $this->assertEquals(0, $node[0]->inDegree(), 'inDegree test failed for node 0 with 2 arcs');
  144.         $this->assertEquals(1, $node[1]->inDegree(), 'inDegree test failed for node 1 with 2 arcs');
  145.         $this->assertEquals(1, $node[2]->inDegree(), 'inDegree test failed for node 2 with 2 arcs');
  146.         $this->assertEquals(2, $node[0]->outDegree(), 'outDegree test failed for node 0 with 2 arcs');
  147.         $this->assertEquals(0, $node[1]->outDegree(), 'outDegree test failed for node 1 with 2 arcs');
  148.         $this->assertEquals(0, $node[2]->outDegree(), 'outDegree test failed for node 2 with 2 arcs');
  149.     }
  150.  
  151.     function test_undirected_degree() {
  152.         $this->_graph = new Structures_Graph(false);
  153.         $node = array();
  154.         $node[] = new Structures_Graph_Node();
  155.         $node[] = new Structures_Graph_Node();
  156.         $node[] = new Structures_Graph_Node();
  157.         $this->_graph->addNode($node[0]);
  158.         $this->_graph->addNode($node[1]);
  159.         $this->_graph->addNode($node[2]);
  160.         $this->assertEquals(0, $node[0]->inDegree(), 'inDegree test failed for node 0 with 0 arcs');
  161.         $this->assertEquals(0, $node[1]->inDegree(), 'inDegree test failed for node 1 with 0 arcs');
  162.         $this->assertEquals(0, $node[2]->inDegree(), 'inDegree test failed for node 2 with 0 arcs');
  163.         $this->assertEquals(0, $node[0]->outDegree(), 'outDegree test failed for node 0 with 0 arcs');
  164.         $this->assertEquals(0, $node[1]->outDegree(), 'outDegree test failed for node 1 with 0 arcs');
  165.         $this->assertEquals(0, $node[2]->outDegree(), 'outDegree test failed for node 2 with 0 arcs');
  166.         $node[0]->connectTo($node[1]);
  167.         $this->assertEquals(1, $node[0]->inDegree(), 'inDegree test failed for node 0 with 1 arc');
  168.         $this->assertEquals(1, $node[1]->inDegree(), 'inDegree test failed for node 1 with 1 arc');
  169.         $this->assertEquals(0, $node[2]->inDegree(), 'inDegree test failed for node 2 with 1 arc');
  170.         $this->assertEquals(1, $node[0]->outDegree(), 'outDegree test failed for node 0 with 1 arc');
  171.         $this->assertEquals(1, $node[1]->outDegree(), 'outDegree test failed for node 1 with 1 arc');
  172.         $this->assertEquals(0, $node[2]->outDegree(), 'outDegree test failed for node 2 with 1 arc');
  173.         $node[0]->connectTo($node[2]);
  174.         $this->assertEquals(2, $node[0]->inDegree(), 'inDegree test failed for node 0 with 2 arcs');
  175.         $this->assertEquals(1, $node[1]->inDegree(), 'inDegree test failed for node 1 with 2 arcs');
  176.         $this->assertEquals(1, $node[2]->inDegree(), 'inDegree test failed for node 2 with 2 arcs');
  177.         $this->assertEquals(2, $node[0]->outDegree(), 'outDegree test failed for node 0 with 2 arcs');
  178.         $this->assertEquals(1, $node[1]->outDegree(), 'outDegree test failed for node 1 with 2 arcs');
  179.         $this->assertEquals(1, $node[2]->outDegree(), 'outDegree test failed for node 2 with 2 arcs');
  180.     }
  181. }
  182. ?>
  183.