Debian Codegraph @

test/com/google/javascript/jscomp/regex/CharRangesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.regex;
18
19 import java.util.BitSet;
20 import java.util.Random;
21
22 import com.google.javascript.jscomp.regex.CharRanges;
23
24 import junit.framework.TestCase;
25
26 public class CharRangesTest extends TestCase {
27
28 static final long SEED = Long.parseLong(System.getProperty(
29 "junit.random.seed", "" + System.currentTimeMillis()));
30
31 public final void testAgainstRegularImplementation() {
32 Random rnd = new Random(SEED);
33
34 for (int run = 10; --run >= 0;) {
35 // Fill with bits in the range [0x1000, 0x3000).
36 BitSet bs = new BitSet();
37 for (int i = 0x1000; --i >= 0;) {
38 bs.set(0x1000 + rnd.nextInt(0x3000));
39 }
40
41 // Create an equivalent sparse bit set
42 int[] members = new int[bs.cardinality()];
43 for (int i = -1, k = 0; k < members.length; ++k) {
44 members[k] = i = bs.nextSetBit(i + 1);
45 }
46 CharRanges sbs = CharRanges.withMembers(members);
47
48 // Check all bits including past the min/max bit
49 for (int i = 0; i < 0x5000; ++i) {
50 if (bs.get(i) != sbs.contains(i)) {
51 fail("sbs=" + sbs + ", bs=" + bs + ", difference at bit " + i);
52 }
53 }
54 }
55 }
56
57 public final void testEmptyCharRanges() {
58 CharRanges sbs = CharRanges.EMPTY;
59 for (int i = -1000; i < 1000; ++i) {
60 assertFalse(sbs.contains(i));
61 }
62 assertEquals("[]", sbs.toString());
63 }
64
65 public final void testCharRangesFactories() {
66 CharRanges isbs = CharRanges.withMembers(new int[] { 0, 1, 4, 9 });
67 CharRanges isbs2 = CharRanges.withMembers(new int[] { 0, 1, 4, 9 });
68 assertEquals("[0x0-0x1 0x4 0x9]", isbs.toString());
69
70 CharRanges esbs = CharRanges.withMembers(new int[0]);
71
72 assertEquals(isbs, isbs);
73 assertEquals(isbs, isbs2);
74 assertFalse(isbs.equals(esbs));
75 assertFalse(isbs.equals(null));
76 assertFalse(isbs.equals(new Object()));
77
78 assertEquals(isbs.hashCode(), isbs2.hashCode());
79 assertFalse(isbs.hashCode() == esbs.hashCode());
80 }
81
82 public final void testRangeConstructor() {
83 try {
84 CharRanges.withRanges(new int[] { 1 });
85 fail("Mismatched ranges");
86 } catch (IllegalArgumentException ex) {
87 // pass
88 }
89
90 try {
91 CharRanges.withRanges(new int[] { 1, 4, 4, 5 });
92 fail("Discontiguous ranges");
93 } catch (IllegalArgumentException ex) {
94 // pass
95 }
96
97 try {
98 CharRanges.withRanges(new int[] { 4, 5, 1, 3 });
99 fail("Misordered ranges");
100 } catch (IllegalArgumentException ex) {
101 // pass
102 }
103
104 try {
105 CharRanges.withRanges(new int[] { 0, 0 });
106 fail("Empty range");
107 } catch (IllegalArgumentException ex) {
108 // pass
109 }
110 }
111
112 public final void testDupeMembers() {
113 CharRanges sbs1 = CharRanges.withMembers(new int[] { 0, 1, 4, 9 });
114 assertEquals(sbs1.toString(), "[0x0-0x1 0x4 0x9]", sbs1.toString());
115
116 CharRanges sbs2 = CharRanges.withMembers(new int[] { 9, 1, 4, 1, 0 });
117 assertEquals(sbs2.toString(), "[0x0-0x1 0x4 0x9]", sbs2.toString());
118
119 assertEquals(sbs1, sbs2);
120 assertEquals(sbs1.hashCode(), sbs2.hashCode());
121
122 for (int i = -10; i < 20; ++i) {
123 assertEquals("" + i, sbs1.contains(i), sbs2.contains(i));
124 }
125 }
126
127 public final void testDifference() {
128 // 1 2 3
129 // 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0
130 // b-a DD DD DDD D DDD
131 // a AAAAAAAAA A A A A A AAA AAA A A
132 // b BBB BBB BBB BBB B B BBB
133 // a-b DD DD D D D D DDD DDD D D
134 CharRanges a = CharRanges.withRanges(new int[] {
135 0x03, 0x0C, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
136 0x1C, 0x1D, 0x1E, 0x21, 0x24, 0x27, 0x28, 0x29, 0x2A, 0x2B });
137 CharRanges b = CharRanges.withRanges(new int[] {
138 0x01, 0x04, 0x06, 0x09, 0x0B, 0x0E, 0x0F, 0x12, 0x1A, 0x1B,
139 0x1C, 0x1D, 0x21, 0x24 });
140 CharRanges empty = CharRanges.withMembers(new int[0]);
141
142 assertEquals(empty, empty.union(empty));
143 assertEquals(a, a.union(empty));
144 assertEquals(b, empty.union(b));
145
146 CharRanges aSb = a.difference(b);
147 assertEquals(
148 "[0x4-0x5 0x9-0xa 0x12 0x14 0x16 0x18 0x1e-0x20 0x24-0x26 0x28 0x2a]",
149 aSb.toString());
150 assertTrue(a.containsAll(aSb));
151 assertFalse(aSb.containsAll(a));
152 assertFalse(aSb.containsAll(b));
153
154 CharRanges bSa = b.difference(a);
155 assertEquals(
156 "[0x1-0x2 0xc-0xd 0xf-0x11 0x1a 0x21-0x23]",
157 bSa.toString());
158 assertTrue(b.containsAll(bSa));
159 assertFalse(bSa.containsAll(a));
160 assertFalse(bSa.containsAll(b));
161
162 // Check that a and b not changed by operation
163 assertEquals(
164 "[0x3-0xb 0x12 0x14 0x16 0x18 0x1c 0x1e-0x20 0x24-0x26 0x28 0x2a]",
165 a.toString());
166 assertEquals(
167 "[0x1-0x3 0x6-0x8 0xb-0xd 0xf-0x11 0x1a 0x1c 0x21-0x23]",
168 b.toString());
169
170 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
171 // m: * * * * * * * * *
172 // s: * * * * * * * *
173 // d: * * * * *
174 CharRanges m = CharRanges.withMembers(0, 1, 2, 3, 6, 9, 0xa, 0xe, 0xf);
175 CharRanges s = CharRanges.withMembers(2, 5, 6, 7, 0xa, 0xb, 0xd, 0xe);
176 CharRanges d = m.difference(s);
177 assertEquals("[0x0-0x1 0x3 0x9 0xf]", d.toString());
178 assertTrue(m.containsAll(d));
179 assertFalse(d.containsAll(m));
180 assertFalse(d.containsAll(s));
181 assertFalse(s.containsAll(d));
182 assertTrue(d.containsAll(d));
183 }
184
185 public final void testUnion() {
186 // 1 2 3
187 // 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0
188 // AAAAAAAAA A A A A A AAA AAA A A
189 // BBB BBB BBB BBB B B BBB
190 // UUUUUUUUUUUUU UUUU U U U U U UUUUUUUUU U U
191 CharRanges a = CharRanges.withRanges(new int[] {
192 0x03, 0x0C, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
193 0x1C, 0x1D, 0x1E, 0x21, 0x24, 0x27, 0x28, 0x29, 0x2A, 0x2B });
194 CharRanges b = CharRanges.withRanges(new int[] {
195 0x01, 0x04, 0x06, 0x09, 0x0B, 0x0E, 0x0F, 0x12, 0x1A, 0x1B,
196 0x1C, 0x1D, 0x21, 0x24 });
197 CharRanges empty = CharRanges.withMembers(new int[0]);
198
199 assertEquals(empty, empty.union(empty));
200 assertEquals(a, a.union(empty));
201 assertEquals(b, empty.union(b));
202
203 CharRanges aUb = a.union(b);
204 assertEquals(
205 "[0x1-0xd 0xf-0x12 0x14 0x16 0x18 0x1a 0x1c 0x1e-0x26 0x28 0x2a]",
206 aUb.toString());
207 assertEquals(aUb, b.union(a));
208 assertTrue(aUb.containsAll(a));
209 assertTrue(aUb.containsAll(b));
210 assertFalse(a.containsAll(b));
211 assertFalse(b.containsAll(a));
212 assertTrue(a.containsAll(a));
213 assertTrue(b.containsAll(b));
214 assertTrue(aUb.containsAll(aUb));
215
216 // Check that a and b not changed by operation
217 assertEquals(
218 "[0x3-0xb 0x12 0x14 0x16 0x18 0x1c 0x1e-0x20 0x24-0x26 0x28 0x2a]",
219 a.toString());
220 assertEquals(
221 "[0x1-0x3 0x6-0x8 0xb-0xd 0xf-0x11 0x1a 0x1c 0x21-0x23]",
222 b.toString());
223 }
224 }