View Javadoc

1   /* LongFPSetTestCase
2    *
3    * $Id: LongFPSetTestCase.java 3437 2005-05-06 02:49:04Z stack-sf $
4    *
5    * Created Wed Jan 21 09:00:29 CET 2004
6    *
7    * Copyright (C) 2004 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  
26  package org.archive.util.fingerprint;
27  
28  import junit.framework.TestCase;
29  
30  /***
31   * JUnit test suite for LongFPSet.  This is an abstract class which defines
32   * the generic tests that test the {@link LongFPSet} interface.  Subclasses
33   * may test specifics of {@link LongFPSet} subclass implementations
34   *
35   * @author <a href="mailto:me@jamesc.net">James Casey</a>
36   * @version $ Id:$
37   */
38  abstract public class LongFPSetTestCase extends TestCase {
39  
40      /*** the unerlying FPSet we wish to test */
41      private LongFPSet fpSet;
42  
43      /***
44       * Create a new LongFPSetTest object
45       *
46       * @param testName the name of the test
47       */
48      public LongFPSetTestCase(final String testName) {
49          super(testName);
50      }
51  
52      public void setUp() {
53          fpSet = makeLongFPSet();
54      }
55  
56      abstract LongFPSet makeLongFPSet();
57  
58      /*** check that we can add fingerprints */
59      public void testAdd() {
60          long l1 = (long)1234;
61          long l2 = (long)2345;
62  
63          assertEquals("empty set to start", 0, fpSet.count());
64          assertTrue("set changed on addition of l1", fpSet.add(l1));
65          assertTrue("set changed on addition of l2", fpSet.add(l2));
66          assertFalse("set didn't change on re-addition of l1", fpSet.add(l1));
67      }
68  
69      /*** check we can call add/remove/contains() with 0 as a value */
70      public void testWithZero() {
71          long zero = (long)0;
72  
73          assertEquals("empty set to start", 0, fpSet.count());
74          assertFalse("zero is not there", fpSet.contains(zero));
75          assertTrue("zero added", fpSet.add(zero));
76  
77          // now one element
78          assertEquals("one fp in set", 1, fpSet.count());
79          assertTrue("zero is the element", fpSet.contains(zero));
80  
81          // and remove
82          assertTrue("zero removed", fpSet.remove(zero));
83          assertEquals("empty set again", 0, fpSet.count());
84      }
85  
86      /*** check that contains() does what we expect */
87      public void testContains() {
88          long l1 = (long) 1234;
89          long l2 = (long) 2345;
90          long l3 = (long) 1334;
91  
92          assertEquals("empty set to start", 0, fpSet.count());
93          fpSet.add(l1);
94          fpSet.add(l2);
95  
96          assertTrue("contains l1", fpSet.contains(l1));
97          assertTrue("contains l2", fpSet.contains(l2));
98          assertFalse("does not contain l3", fpSet.contains(l3));
99      }
100 
101     /*** test remove() works as expected */
102     public void testRemove() {
103         long l1 = (long) 1234;
104 
105         assertEquals("empty set to start", 0, fpSet.count());
106 
107         // remove before it's there
108         assertFalse("fp not in set", fpSet.remove(l1));
109         // now add
110         fpSet.add(l1);
111         // and remove again
112         assertTrue("fp was in set", fpSet.remove(l1));
113         // check set is empty again
114         assertEquals("empty set again", 0, fpSet.count());
115     }
116 
117     /*** check count works ok */
118     public void testCount() {
119         final int NUM = 1000;
120         assertEquals("empty set to start", 0, fpSet.count());
121 
122         for(int i = 1; i < NUM; ++i) {
123             fpSet.add((long)i);
124             assertEquals("correct num", i, fpSet.count());
125         }
126         for (int i = NUM - 1; i > 0; --i) {
127             fpSet.remove((long) i);
128             assertEquals("correct num", i -1, fpSet.count());
129         }
130         assertEquals("empty set to start", 0, fpSet.count());
131 
132     }
133 }
134