1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  import org.onemind.commons.java.datastructure.NametableStack;
24  import junit.framework.TestCase;
25  /***
26   * Test for nametable stack
27   * @author TiongHiang Lee (thlee@onemindsoft.org)
28   * @version $Id: NametableStackTest.java,v 1.3 2005/04/06 15:40:43 thlee Exp $ $Name:  $
29   */
30  public class NametableStackTest extends TestCase
31  {
32  
33      public void testCloseScope()
34      {
35          NametableStack stack = new NametableStack();
36          //add 0-4
37          for (int i = 0; i < 5; i++)
38          {
39              String name = String.valueOf(i);
40              stack.declare(name, name);
41          }
42          //open new scope
43          int scope = stack.newScope();
44          //add 5-9
45          for (int i = 5; i < 10; i++)
46          {
47              String name = String.valueOf(i);
48              stack.declare(name, name);
49          }
50          // check all values valid
51          for (int i = 0; i < 10; i++)
52          {
53              String name = String.valueOf(i);
54              assertEquals(stack.access(name), name);
55          }
56          //close scope
57          stack.closeScope(scope);
58          //check only  0-4 valid
59          for (int i = 5; i < 10; i++)
60          {
61              String name = String.valueOf(i);
62              assertFalse(stack.containsName(name));
63          }
64          for (int i = 0; i < 5; i++)
65          {
66              String name = String.valueOf(i);
67              assertEquals(stack.access(name), name);
68          }
69      }
70  
71      public void testLocalScope()
72      {
73          NametableStack stack = new NametableStack();
74          //add 0-4
75          for (int i = 0; i < 5; i++)
76          {
77              String name = String.valueOf(i);
78              stack.declare(name, name);
79          }
80          //open new local scope
81          int scope = stack.newLocalScope();
82          //add 5-9
83          for (int i = 2; i < 7; i++)
84          {
85              String name = String.valueOf(i);
86              stack.declare(name, name);
87          }
88          // check all values valid
89          for (int i = 0; i < 7; i++)
90          {
91              String name = String.valueOf(i);
92              assertEquals(stack.access(name), name);
93          }
94          //close scope
95          stack.closeLocalScope(scope);
96          //check only  0-4 valid
97          for (int i = 5; i < 7; i++)
98          {
99              String name = String.valueOf(i);
100             assertFalse(stack.containsName(name));
101         }
102         for (int i = 0; i < 5; i++)
103         {
104             String name = String.valueOf(i);
105             assertEquals(stack.access(name), name);
106         }
107         
108         
109     }
110 }