001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2006-2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.util;
020:
021: import java.util.Arrays;
022:
023: /**
024: * Unit tests for utilities.
025: *
026: * @author Robert Grimm
027: * @version $Revision: 1.5 $
028: */
029: public class UtilitiesTest extends junit.framework.TestCase {
030:
031: /** Create a new test class. */
032: public UtilitiesTest() { /* Nothing to do. */
033: }
034:
035: /** Test {@link Utilities#isQualified(String)}. */
036: public void testIsQualified() {
037: assertFalse(Utilities.isQualified(""));
038: assertFalse(Utilities.isQualified("a"));
039: assertFalse(Utilities.isQualified("aa"));
040: assertFalse(Utilities.isQualified("aaa"));
041: assertFalse(Utilities.isQualified("a()"));
042: assertFalse(Utilities.isQualified("a(a)"));
043: assertFalse(Utilities.isQualified("a(.)"));
044: assertFalse(Utilities.isQualified("a(..)"));
045: assertFalse(Utilities.isQualified("a(...)"));
046: assertFalse(Utilities.isQualified("a(...)()"));
047: assertFalse(Utilities.isQualified("a(...)(.)"));
048: assertFalse(Utilities.isQualified("a(...)(..)"));
049: assertFalse(Utilities.isQualified("a(...)(...)"));
050: assertTrue(Utilities.isQualified("."));
051: assertTrue(Utilities.isQualified("a."));
052: assertTrue(Utilities.isQualified(".b"));
053: assertTrue(Utilities.isQualified("a.b"));
054: assertTrue(Utilities.isQualified("aa.bb"));
055: assertTrue(Utilities.isQualified("aaa.bbb"));
056: assertTrue(Utilities.isQualified("().()"));
057: assertTrue(Utilities.isQualified("()().()()"));
058: assertTrue(Utilities.isQualified("()()().()()()"));
059: assertTrue(Utilities.isQualified("aaa.bbb."));
060: assertTrue(Utilities.isQualified(".aaa.bbb"));
061: }
062:
063: /** Test {@link Utilities#getQualifier(String)}. */
064: public void testGetQualifier() {
065: assertNull(Utilities.getQualifier(""));
066: assertNull(Utilities.getQualifier("a"));
067: assertNull(Utilities.getQualifier("aa"));
068: assertNull(Utilities.getQualifier("aaa"));
069: assertEquals("", Utilities.getQualifier("."));
070: assertEquals("", Utilities.getQualifier(".a"));
071: assertEquals("", Utilities.getQualifier(".aa"));
072: assertEquals("", Utilities.getQualifier(".aaa"));
073: assertEquals(".", Utilities.getQualifier("..a"));
074: assertEquals("..", Utilities.getQualifier("...aa"));
075: assertEquals("...", Utilities.getQualifier("....aa"));
076: assertEquals("..(.)", Utilities.getQualifier("..(.).aa"));
077: assertEquals("..", Utilities.getQualifier("...(.)"));
078: assertEquals("..", Utilities.getQualifier("...(.)a"));
079: assertEquals("..", Utilities.getQualifier("...(.)aa"));
080: assertEquals("..", Utilities.getQualifier("...(.)(.)"));
081: assertEquals("..", Utilities.getQualifier("...(.)(.)a"));
082: }
083:
084: /** Test {@link Utilities#getName(String)}. */
085: public void testGetName() {
086: assertEquals("", Utilities.getName(""));
087: assertEquals("a", Utilities.getName("a"));
088: assertEquals("aa", Utilities.getName("aa"));
089: assertEquals("aaa", Utilities.getName("aaa"));
090: assertEquals("", Utilities.getName("."));
091: assertEquals("", Utilities.getName("a."));
092: assertEquals("", Utilities.getName("aa."));
093: assertEquals("", Utilities.getName("aaa."));
094: assertEquals("a", Utilities.getName(".a"));
095: assertEquals("aa", Utilities.getName(".aa"));
096: assertEquals("aaa", Utilities.getName(".aaa"));
097: assertEquals("aaa", Utilities.getName("..aaa"));
098: assertEquals("aaa", Utilities.getName("...aaa"));
099: assertEquals("(.)", Utilities.getName("..(.)"));
100: assertEquals("(.)a", Utilities.getName("..(.)a"));
101: assertEquals("(.)aa", Utilities.getName("..(.)aa"));
102: assertEquals("(.)aaa", Utilities.getName("..(.)aaa"));
103: assertEquals("(..)aaa", Utilities.getName(".(..)aaa"));
104: assertEquals("()", Utilities.getName("..()"));
105: assertEquals("()()", Utilities.getName("..()()"));
106: assertEquals("(.)(.)", Utilities.getName("..(.)(.)"));
107: }
108:
109: /**
110: * Test {@link Utilities#toIdentifier(String)}. Note that {@link
111: * Utilities#toPath(String)} and {@link
112: * Utilities#toPath(String,String)} build on the same internal
113: * helper method.
114: */
115: public void testToIdentifier() {
116: assertEquals("", Utilities.toIdentifier(""));
117: assertEquals("$", Utilities.toIdentifier("."));
118: assertEquals("$$", Utilities.toIdentifier(".."));
119: assertEquals("$$$", Utilities.toIdentifier("..."));
120: assertEquals("$a", Utilities.toIdentifier(".a"));
121: assertEquals("$$a", Utilities.toIdentifier("..a"));
122: assertEquals("$$$a", Utilities.toIdentifier("...a"));
123: assertEquals("a$", Utilities.toIdentifier("a."));
124: assertEquals("a$$", Utilities.toIdentifier("a.."));
125: assertEquals("a$$$", Utilities.toIdentifier("a..."));
126: assertEquals("a$a", Utilities.toIdentifier("a.a"));
127: assertEquals("a$$a", Utilities.toIdentifier("a..a"));
128: assertEquals("a$$$a", Utilities.toIdentifier("a...a"));
129: assertEquals("$aa", Utilities.toIdentifier(".aa"));
130: assertEquals("aa$", Utilities.toIdentifier("aa."));
131: assertEquals("aa$aa", Utilities.toIdentifier("aa.aa"));
132: assertEquals("$aaa", Utilities.toIdentifier(".aaa"));
133: assertEquals("aaa$", Utilities.toIdentifier("aaa."));
134: assertEquals("aaa$aaa", Utilities.toIdentifier("aaa.aaa"));
135: assertEquals("()", Utilities.toIdentifier("()"));
136: assertEquals("(.)", Utilities.toIdentifier("(.)"));
137: assertEquals("(.)(.)", Utilities.toIdentifier("(.)(.)"));
138: assertEquals("$(.)(.)", Utilities.toIdentifier(".(.)(.)"));
139: assertEquals("(.)$(.)", Utilities.toIdentifier("(.).(.)"));
140: assertEquals("(.)(.)$", Utilities.toIdentifier("(.)(.)."));
141: }
142:
143: /** Test {@link Utilities#toComponents(String)}. */
144: public void testToComponents() {
145: assertTrue(Arrays.equals(new String[] { "" }, Utilities
146: .toComponents("")));
147: assertTrue(Arrays.equals(new String[] { "a" }, Utilities
148: .toComponents("a")));
149: assertTrue(Arrays.equals(new String[] { "aa" }, Utilities
150: .toComponents("aa")));
151: assertTrue(Arrays.equals(new String[] { "aaa" }, Utilities
152: .toComponents("aaa")));
153: assertTrue(Arrays.equals(new String[] { "()" }, Utilities
154: .toComponents("()")));
155: assertTrue(Arrays.equals(new String[] { "(.)" }, Utilities
156: .toComponents("(.)")));
157: assertTrue(Arrays.equals(new String[] { "(.)(.)" }, Utilities
158: .toComponents("(.)(.)")));
159: assertTrue(Arrays.equals(new String[] { "", "" }, Utilities
160: .toComponents(".")));
161: assertTrue(Arrays.equals(new String[] { "", "", "" }, Utilities
162: .toComponents("..")));
163: assertTrue(Arrays.equals(new String[] { "", "", "", "" },
164: Utilities.toComponents("...")));
165: assertTrue(Arrays.equals(new String[] { "a", "" }, Utilities
166: .toComponents("a.")));
167: assertTrue(Arrays.equals(new String[] { "", "a" }, Utilities
168: .toComponents(".a")));
169: assertTrue(Arrays.equals(new String[] { "a", "a" }, Utilities
170: .toComponents("a.a")));
171: assertTrue(Arrays.equals(new String[] { "a", "a", "a" },
172: Utilities.toComponents("a.a.a")));
173: assertTrue(Arrays.equals(new String[] { "a", "a", "a", "a" },
174: Utilities.toComponents("a.a.a.a")));
175: }
176:
177: /** Test {@link Pair#append(Pair)} and {@link Pair#setLastTail(Pair)}. */
178: public void testAppendix() {
179: Pair<Integer> l1 = Pair.empty();
180: l1 = new Pair<Integer>(2, l1);
181: l1 = new Pair<Integer>(1, l1);
182:
183: Pair<Integer> l2 = Pair.empty();
184: l2 = new Pair<Integer>(4, l2);
185: l2 = new Pair<Integer>(3, l2);
186:
187: Pair<Integer> l3 = Pair.empty();
188: l3 = new Pair<Integer>(4, l3);
189: l3 = new Pair<Integer>(3, l3);
190: l3 = new Pair<Integer>(2, l3);
191: l3 = new Pair<Integer>(1, l3);
192:
193: Pair<Integer> l4 = Pair.empty();
194: l4 = new Pair<Integer>(2, l4);
195: l4 = new Pair<Integer>(1, l4);
196:
197: Pair<Integer> l5 = l1.append(l2);
198: assertTrue(l5 != l1);
199: assertTrue(l5.equals(l3));
200: assertTrue(l1.equals(l4));
201:
202: l1.setLastTail(l2);
203: assertTrue(l1.equals(l3));
204: }
205:
206: }
|