View Javadoc
1   /*
2    * Created on Apr 21, 2007
3    *
4    * Copyright (c) 2007, The JUNG Authors 
5    *
6    * All rights reserved.
7    *
8    * This software is open-source under the BSD license; see either
9    * "license.txt" or
10   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11   */
12  package edu.uci.ics.jung.graph;
13  
14  import java.util.ArrayList;
15  import java.util.Collection;
16  
17  import junit.framework.TestCase;
18  
19  import com.google.common.base.Supplier;
20  
21  import edu.uci.ics.jung.graph.util.Pair;
22  
23  
24  public abstract class AbstractHypergraphTest extends TestCase
25  {
26      protected Supplier<? extends Hypergraph<Integer,Character>> factory;
27      protected Hypergraph<Integer,Character> h;
28      
29      public AbstractHypergraphTest(Supplier<? extends Hypergraph<Integer,Character>> factory)
30      {
31          this.factory = factory;
32      }
33      
34      @Override
35      public void runTest() throws Exception {
36          setUp();
37          testAddVertex();
38          testAddEdge();
39          testEdgeEndpoints();
40          tearDown();
41      }
42  
43      /**
44       * test for the following:
45       * <ul>
46       * <li>add successful iff arg is not present
47       * <li>count increases by 1 iff add is successful
48       * <li>null vertex argument actively rejected
49       * <li>vertex reported as present iff add is successful
50       * </ul>
51       */
52      public void testAddVertex()
53      {
54          int count = h.getVertexCount();
55          assertTrue(h.addVertex(new Integer(1)));
56          assertEquals(count+1, h.getVertexCount());
57          assertTrue(h.containsVertex(1));
58          boolean success = false;
59          try
60          {
61              success = h.addVertex(null);
62              fail("Implementation should disallow null vertices");
63          }
64          catch (IllegalArgumentException iae) {}
65          catch (NullPointerException npe)
66          {
67              fail("Implementation should actively prevent null vertices");
68          }
69          assertFalse(success);
70          assertFalse(h.addVertex(1));
71          assertEquals(count+1, h.getVertexCount());
72          assertFalse(h.containsVertex(2));
73      }
74      
75      /**
76       * test for the following:
77       * <ul>
78       * <li>add successful iff edge is not present 
79       * <li>edge count increases by 1 iff add successful
80       * <li>null edge arg actively rejected
81       * <li>edge reported as present iff add is successful
82       * <li>throw if edge is present with different endpoints
83       * </ul>
84       */
85      public void testAddEdge()
86      {
87          int edge_count = h.getEdgeCount();
88          int vertex_count = h.getVertexCount();
89          Pair<Integer> p = new Pair<Integer>(2, 3);
90          assertTrue(h.addEdge('a', p));
91          assertEquals(edge_count+1, h.getEdgeCount());
92          assertEquals(vertex_count+2, h.getVertexCount());
93          assertTrue(h.containsEdge('a'));
94          boolean success = false;
95          try
96          {
97              success = h.addEdge('b', null);
98              fail("Implementation should disallow null pairs/collections");
99              success = h.addEdge(null, p);
100             fail("Implementation should disallow null edges");
101         }
102         catch (IllegalArgumentException iae) {}
103         catch (NullPointerException npe)
104         {
105             fail("Implementation should actively prevent null edges, pairs, and collections");
106         }
107         assertFalse(success);
108         // adding the same edge with an equal Pair should return false
109         assertFalse(h.addEdge('a', new Pair<Integer>(2,3)));
110         // adding the same edge with the same Pair should return false
111         assertFalse(h.addEdge('a', p));
112         try
113         {
114             success = h.addEdge('a', new Pair<Integer>(3,4));
115             fail("Implementation should disallow existing edge objects from connecting new pairs/collections");
116         }
117         catch (IllegalArgumentException iae) {}
118         assertEquals(edge_count+1, h.getEdgeCount());
119         assertFalse(h.containsEdge('b'));
120     }
121     
122     /**
123      * test for the following:
124      * <ul>
125      * <li>if Graph, reject # of endpoints != 2
126      * <li>otherwise, accept any (non-negative) number of endpoints
127      * 
128      * </ul>
129      *
130      */
131     public void testEdgeEndpoints()
132     {
133         Collection<Integer> c = new ArrayList<Integer>();
134         for (int i = 0; i < 10; i++)
135         {
136             try
137             {
138                 h.addEdge((char)i, c);
139                 c.add(i);
140             }
141             catch (IllegalArgumentException iae)
142             {
143                 if (h instanceof Graph)
144                 {
145                     if (c.size() == 2)
146                         fail("improperly rejected incident vertex collection " + c);
147                 }
148                 else
149                     fail("hypergraph implementations should accept any positive number of incident vertices");
150             }
151         }
152     }
153     
154     /**
155      * should return null if any of the following is true
156      * <ul>
157      * <li>v1 is null 
158      * <li>v2 is null
159      * <li>there is no edge connecting v1 to v2 in this graph
160      * </ul>
161      * otherwise should return _an_ edge connecting v1 to v2.
162      * May be directed or undirected (depending on the graph);
163      * may be any of the edges in the graph that so connect v1 and v2.
164      * 
165      * Must _not_ return any directed edge for which v1 and v2 are distinct
166      * and v2 is the source.
167      */
168     public void testFindEdge()
169     {
170         
171     }
172 }