| TestBumpyStack |

    1   /*
    2    *  TestBumpyStack.java
    3    *
    4    *  Copyright (c) 1998-2001, The University of Sheffield.
    5    *
    6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    7    *  software, licenced under the GNU Library General Public License,
    8    *  Version 2, June 1991 (in the distribution as file licence.html,
    9    *  and also available at http://gate.ac.uk/gate/licence.html).
    10   *
    11   *  Hamish Cunningham, 10/June/00
    12   *
    13   *  $Id: TestBumpyStack.java,v 1.9 2001/11/24 18:23:24 hamish Exp $
    14   */
    15
    16  package gate.util;
    17
    18  import java.util.*;
    19  import java.io.*;
    20  import junit.framework.*;
    21  import java.net.*;
    22
    23  import gate.*;
    24  import gate.creole.*;
    25
    26  /** BumpyStack test class.
    27    */
    28  public class TestBumpyStack extends TestCase
    29  {
    30    /** Debug flag */
    31    private static final boolean DEBUG = false;
    32
    33    /** Construction */
    34    public TestBumpyStack(String name) { super(name); }
    35
    36    /** Fixture set up */
    37    public void setUp() {
    38    } // setUp
    39
    40    /** Test suite routine for the test runner */
    41    public static Test suite() {
    42      return new TestSuite(TestBumpyStack.class);
    43    } // suite
    44
    45    public static void callGC(){
    46      Runtime runtime = Runtime.getRuntime();
    47      long memory;
    48      do {
    49        memory = runtime.totalMemory() - runtime.freeMemory();
    50        runtime.gc();
    51        try {
    52          Thread.currentThread().wait(300);
    53        } catch (Exception e) {}
    54        runtime.gc();
    55      } while (memory < runtime.totalMemory() - runtime.freeMemory());
    56    }
    57
    58    /** Test the bumpiness of the thing. */
    59    public void testBumpiness() throws Exception {
    60      WeakBumpyStack bumper = new WeakBumpyStack();
    61
    62      String s1 = new String("s1");
    63      String s2 = new String("s2");
    64      String s3 = new String("s3");
    65
    66      bumper.push(s3);
    67      bumper.push(s2);
    68      bumper.push(s1);
    69
    70      assertTrue(
    71        "managed to bump non-existent element",
    72        ! bumper.bump(new String("something"))
    73      );
    74
    75      assertTrue("stack wrong length (I): " + bumper.size(), bumper.size() == 3);
    76      assertTrue("couldn't bump s2", bumper.bump(s2));
    77      assertTrue("s2 not front of stack", ((String) bumper.pop()).equals("s2"));
    78      assertTrue("stack wrong length (II)" + bumper.size(), bumper.size() == 2);
    79    } // testBumpiness()
    80
    81    /**
    82     * Tests whether the CreoleRegisterImpl keeps unreacheable resourecs alive.
    83     * <B>WARNING: see notes on WeakBumptyStack</B>.
    84     */
    85    public void testSelfCleaning() throws Exception {
    86      //count instances
    87      Collection instances = ((ResourceData)
    88                             Gate.getCreoleRegister().
    89                             get("gate.corpora.DocumentImpl")).
    90                             getInstantiations();
    91      int docCnt = instances == null ? 0: instances.size();
    92
    93      instances = ((ResourceData)
    94                    Gate.getCreoleRegister().
    95                    get("gate.corpora.CorpusImpl")).
    96                    getInstantiations();
    97      int corpusCnt = instances == null ? 0: instances.size();
    98
    99      instances = ((ResourceData)
    100                   Gate.getCreoleRegister().
    101                   get("gate.creole.tokeniser.DefaultTokeniser")).
    102                   getInstantiations();
    103     int tokCnt = instances == null ? 0: instances.size();
    104
    105     instances = ((ResourceData)
    106                   Gate.getCreoleRegister().
    107                   get("gate.creole.ANNIETransducer")).
    108                   getInstantiations();
    109     int japeCnt = instances == null ? 0: instances.size();
    110
    111     instances = ((ResourceData)
    112                   Gate.getCreoleRegister().
    113                   get("gate.creole.SerialController")).
    114                   getInstantiations();
    115     int serctlCnt = instances == null ? 0: instances.size();
    116
    117     instances = null;
    118     //create some unreacheable resources
    119     //LRs
    120
    121     Resource res = Factory.newCorpus("corpus1");
    122     res.getFeatures().put("large", new byte[500000]);
    123     res = Factory.newCorpus("corpus2");
    124     res.getFeatures().put("large", new byte[500000]);
    125     res = Factory.newCorpus("corpus3");
    126     res.getFeatures().put("large", new byte[500000]);
    127
    128     res = Factory.newDocument("content");
    129     res.getFeatures().put("large", new byte[500000]);
    130     res = Factory.newDocument(Gate.getUrl("tests/doc0.html"));
    131     res.getFeatures().put("large", new byte[500000]);
    132     res = Factory.newDocument(Gate.getUrl("tests/doc0.html"));
    133     res.getFeatures().put("large", new byte[500000]);
    134     res = Factory.newDocument(Gate.getUrl("tests/doc0.html"));
    135     res.getFeatures().put("large", new byte[500000]);
    136
    137     //PRs
    138     res = Factory.createResource("gate.creole.tokeniser.DefaultTokeniser");
    139     res.getFeatures().put("large", new byte[500000]);
    140     res = Factory.createResource("gate.creole.tokeniser.DefaultTokeniser");
    141     res.getFeatures().put("large", new byte[500000]);
    142     res = Factory.createResource("gate.creole.tokeniser.DefaultTokeniser");
    143     res.getFeatures().put("large", new byte[500000]);
    144
    145     res = Factory.createResource("gate.creole.ANNIETransducer");
    146     res.getFeatures().put("large", new byte[500000]);
    147
    148     //Controllers
    149     res = Factory.createResource("gate.creole.SerialController");
    150     res.getFeatures().put("large", new byte[500000]);
    151     res = Factory.createResource("gate.creole.SerialController");
    152     res.getFeatures().put("large", new byte[500000]);
    153     res = Factory.createResource("gate.creole.SerialController");
    154     res.getFeatures().put("large", new byte[500000]);
    155     res = null;
    156
    157
    158     //force GC
    159     callGC();
    160
    161     //check instances count
    162     int newDocCnt = ((ResourceData)
    163                   Gate.getCreoleRegister().
    164                   get("gate.corpora.DocumentImpl")).
    165                   getInstantiations().size();
    166
    167     int newCorpusCnt = ((ResourceData)
    168                   Gate.getCreoleRegister().
    169                   get("gate.corpora.CorpusImpl")).
    170                   getInstantiations().size();
    171     int newTokCnt = ((ResourceData)
    172                   Gate.getCreoleRegister().
    173                   get("gate.creole.tokeniser.DefaultTokeniser")).
    174                   getInstantiations().size();
    175     int newJapeCnt = ((ResourceData)
    176                   Gate.getCreoleRegister().
    177                   get("gate.creole.ANNIETransducer")).
    178                   getInstantiations().size();
    179     int newSerctlCnt = ((ResourceData)
    180                   Gate.getCreoleRegister().
    181                   get("gate.creole.SerialController")).
    182                   getInstantiations().size();
    183
    184     String message =
    185           "\nWARNING: testSelfCleaning: GC didn't run:" +
    186           "\nDocs expected: " + docCnt + ", got: " + newDocCnt +
    187           "\nCorpora expected: " + corpusCnt + ", got: " + newCorpusCnt +
    188           "\nTokenisers expected: " + tokCnt + ", got: " + newTokCnt +
    189           "\nJapes expected: " + japeCnt + ", got: " + newJapeCnt +
    190           "\nSerCtls expected: " + serctlCnt + ", got: " + newSerctlCnt;
    191
    192     // ordinarily the following "if" would be an assert, and if the
    193     // conditions failed then the test would fail. see notes on
    194     // WeakBumptyStack
    195     if(! (
    196       docCnt + 4 > newDocCnt &&
    197       corpusCnt + 3 > newCorpusCnt &&
    198       tokCnt + 3 > newTokCnt &&
    199       japeCnt + 1 > newJapeCnt &&
    200       serctlCnt + 3 > newSerctlCnt
    201     ) && DEBUG)
    202       Err.prln(message);
    203   }
    204 } // class TestBumpyStack
    205

| TestBumpyStack |