1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.commons.lang3; 20 21 import static org.junit.jupiter.api.Assertions.assertEquals; 22 import static org.junit.jupiter.api.Assertions.assertFalse; 23 import static org.junit.jupiter.api.Assertions.assertNotEquals; 24 import static org.junit.jupiter.api.Assertions.assertNotNull; 25 import static org.junit.jupiter.api.Assertions.assertThrows; 26 import static org.junit.jupiter.api.Assertions.assertTrue; 27 28 import java.lang.reflect.Modifier; 29 import java.util.Iterator; 30 import java.util.NoSuchElementException; 31 32 import org.junit.jupiter.api.Test; 33 34 /** 35 * Unit tests {@link org.apache.commons.lang3.CharRange}. 36 */ 37 public class CharRangeTest extends AbstractLangTest { 38 39 @Test testClass()40 public void testClass() { 41 // class changed to non-public in 3.0 42 assertFalse(Modifier.isPublic(CharRange.class.getModifiers())); 43 assertTrue(Modifier.isFinal(CharRange.class.getModifiers())); 44 } 45 46 @Test testConstructorAccessors_is()47 public void testConstructorAccessors_is() { 48 final CharRange rangea = CharRange.is('a'); 49 assertEquals('a', rangea.getStart()); 50 assertEquals('a', rangea.getEnd()); 51 assertFalse(rangea.isNegated()); 52 assertEquals("a", rangea.toString()); 53 } 54 55 @Test testConstructorAccessors_isNot()56 public void testConstructorAccessors_isNot() { 57 final CharRange rangea = CharRange.isNot('a'); 58 assertEquals('a', rangea.getStart()); 59 assertEquals('a', rangea.getEnd()); 60 assertTrue(rangea.isNegated()); 61 assertEquals("^a", rangea.toString()); 62 } 63 64 @Test testConstructorAccessors_isIn_Same()65 public void testConstructorAccessors_isIn_Same() { 66 final CharRange rangea = CharRange.isIn('a', 'a'); 67 assertEquals('a', rangea.getStart()); 68 assertEquals('a', rangea.getEnd()); 69 assertFalse(rangea.isNegated()); 70 assertEquals("a", rangea.toString()); 71 } 72 73 @Test testConstructorAccessors_isIn_Normal()74 public void testConstructorAccessors_isIn_Normal() { 75 final CharRange rangea = CharRange.isIn('a', 'e'); 76 assertEquals('a', rangea.getStart()); 77 assertEquals('e', rangea.getEnd()); 78 assertFalse(rangea.isNegated()); 79 assertEquals("a-e", rangea.toString()); 80 } 81 82 @Test testConstructorAccessors_isIn_Reversed()83 public void testConstructorAccessors_isIn_Reversed() { 84 final CharRange rangea = CharRange.isIn('e', 'a'); 85 assertEquals('a', rangea.getStart()); 86 assertEquals('e', rangea.getEnd()); 87 assertFalse(rangea.isNegated()); 88 assertEquals("a-e", rangea.toString()); 89 } 90 91 @Test testConstructorAccessors_isNotIn_Same()92 public void testConstructorAccessors_isNotIn_Same() { 93 final CharRange rangea = CharRange.isNotIn('a', 'a'); 94 assertEquals('a', rangea.getStart()); 95 assertEquals('a', rangea.getEnd()); 96 assertTrue(rangea.isNegated()); 97 assertEquals("^a", rangea.toString()); 98 } 99 100 @Test testConstructorAccessors_isNotIn_Normal()101 public void testConstructorAccessors_isNotIn_Normal() { 102 final CharRange rangea = CharRange.isNotIn('a', 'e'); 103 assertEquals('a', rangea.getStart()); 104 assertEquals('e', rangea.getEnd()); 105 assertTrue(rangea.isNegated()); 106 assertEquals("^a-e", rangea.toString()); 107 } 108 109 @Test testConstructorAccessors_isNotIn_Reversed()110 public void testConstructorAccessors_isNotIn_Reversed() { 111 final CharRange rangea = CharRange.isNotIn('e', 'a'); 112 assertEquals('a', rangea.getStart()); 113 assertEquals('e', rangea.getEnd()); 114 assertTrue(rangea.isNegated()); 115 assertEquals("^a-e", rangea.toString()); 116 } 117 118 @Test testEquals_Object()119 public void testEquals_Object() { 120 final CharRange rangea = CharRange.is('a'); 121 final CharRange rangeae = CharRange.isIn('a', 'e'); 122 final CharRange rangenotbf = CharRange.isIn('b', 'f'); 123 124 assertNotEquals(null, rangea); 125 126 assertEquals(rangea, rangea); 127 assertEquals(rangea, CharRange.is('a')); 128 assertEquals(rangeae, rangeae); 129 assertEquals(rangeae, CharRange.isIn('a', 'e')); 130 assertEquals(rangenotbf, rangenotbf); 131 assertEquals(rangenotbf, CharRange.isIn('b', 'f')); 132 133 assertNotEquals(rangea, rangeae); 134 assertNotEquals(rangea, rangenotbf); 135 assertNotEquals(rangeae, rangea); 136 assertNotEquals(rangeae, rangenotbf); 137 assertNotEquals(rangenotbf, rangea); 138 assertNotEquals(rangenotbf, rangeae); 139 } 140 141 @Test testHashCode()142 public void testHashCode() { 143 final CharRange rangea = CharRange.is('a'); 144 final CharRange rangeae = CharRange.isIn('a', 'e'); 145 final CharRange rangenotbf = CharRange.isIn('b', 'f'); 146 147 assertEquals(rangea.hashCode(), rangea.hashCode()); 148 assertEquals(rangea.hashCode(), CharRange.is('a').hashCode()); 149 assertEquals(rangeae.hashCode(), rangeae.hashCode()); 150 assertEquals(rangeae.hashCode(), CharRange.isIn('a', 'e').hashCode()); 151 assertEquals(rangenotbf.hashCode(), rangenotbf.hashCode()); 152 assertEquals(rangenotbf.hashCode(), CharRange.isIn('b', 'f').hashCode()); 153 154 assertNotEquals(rangea.hashCode(), rangeae.hashCode()); 155 assertNotEquals(rangea.hashCode(), rangenotbf.hashCode()); 156 assertNotEquals(rangeae.hashCode(), rangea.hashCode()); 157 assertNotEquals(rangeae.hashCode(), rangenotbf.hashCode()); 158 assertNotEquals(rangenotbf.hashCode(), rangea.hashCode()); 159 assertNotEquals(rangenotbf.hashCode(), rangeae.hashCode()); 160 } 161 162 @Test testContains_Char()163 public void testContains_Char() { 164 CharRange range = CharRange.is('c'); 165 assertFalse(range.contains('b')); 166 assertTrue(range.contains('c')); 167 assertFalse(range.contains('d')); 168 assertFalse(range.contains('e')); 169 170 range = CharRange.isIn('c', 'd'); 171 assertFalse(range.contains('b')); 172 assertTrue(range.contains('c')); 173 assertTrue(range.contains('d')); 174 assertFalse(range.contains('e')); 175 176 range = CharRange.isIn('d', 'c'); 177 assertFalse(range.contains('b')); 178 assertTrue(range.contains('c')); 179 assertTrue(range.contains('d')); 180 assertFalse(range.contains('e')); 181 182 range = CharRange.isNotIn('c', 'd'); 183 assertTrue(range.contains('b')); 184 assertFalse(range.contains('c')); 185 assertFalse(range.contains('d')); 186 assertTrue(range.contains('e')); 187 assertTrue(range.contains((char) 0)); 188 assertTrue(range.contains(Character.MAX_VALUE)); 189 } 190 191 @Test testContains_Charrange()192 public void testContains_Charrange() { 193 final CharRange a = CharRange.is('a'); 194 final CharRange b = CharRange.is('b'); 195 final CharRange c = CharRange.is('c'); 196 final CharRange c2 = CharRange.is('c'); 197 final CharRange d = CharRange.is('d'); 198 final CharRange e = CharRange.is('e'); 199 final CharRange cd = CharRange.isIn('c', 'd'); 200 final CharRange bd = CharRange.isIn('b', 'd'); 201 final CharRange bc = CharRange.isIn('b', 'c'); 202 final CharRange ab = CharRange.isIn('a', 'b'); 203 final CharRange de = CharRange.isIn('d', 'e'); 204 final CharRange ef = CharRange.isIn('e', 'f'); 205 final CharRange ae = CharRange.isIn('a', 'e'); 206 207 // normal/normal 208 assertFalse(c.contains(b)); 209 assertTrue(c.contains(c)); 210 assertTrue(c.contains(c2)); 211 assertFalse(c.contains(d)); 212 213 assertFalse(c.contains(cd)); 214 assertFalse(c.contains(bd)); 215 assertFalse(c.contains(bc)); 216 assertFalse(c.contains(ab)); 217 assertFalse(c.contains(de)); 218 219 assertTrue(cd.contains(c)); 220 assertTrue(bd.contains(c)); 221 assertTrue(bc.contains(c)); 222 assertFalse(ab.contains(c)); 223 assertFalse(de.contains(c)); 224 225 assertTrue(ae.contains(b)); 226 assertTrue(ae.contains(ab)); 227 assertTrue(ae.contains(bc)); 228 assertTrue(ae.contains(cd)); 229 assertTrue(ae.contains(de)); 230 231 final CharRange notb = CharRange.isNot('b'); 232 final CharRange notc = CharRange.isNot('c'); 233 final CharRange notd = CharRange.isNot('d'); 234 final CharRange notab = CharRange.isNotIn('a', 'b'); 235 final CharRange notbc = CharRange.isNotIn('b', 'c'); 236 final CharRange notbd = CharRange.isNotIn('b', 'd'); 237 final CharRange notcd = CharRange.isNotIn('c', 'd'); 238 final CharRange notde = CharRange.isNotIn('d', 'e'); 239 final CharRange notae = CharRange.isNotIn('a', 'e'); 240 final CharRange all = CharRange.isIn((char) 0, Character.MAX_VALUE); 241 final CharRange allbutfirst = CharRange.isIn((char) 1, Character.MAX_VALUE); 242 243 // normal/negated 244 assertFalse(c.contains(notc)); 245 assertFalse(c.contains(notbd)); 246 assertTrue(all.contains(notc)); 247 assertTrue(all.contains(notbd)); 248 assertFalse(allbutfirst.contains(notc)); 249 assertFalse(allbutfirst.contains(notbd)); 250 251 // negated/normal 252 assertTrue(notc.contains(a)); 253 assertTrue(notc.contains(b)); 254 assertFalse(notc.contains(c)); 255 assertTrue(notc.contains(d)); 256 assertTrue(notc.contains(e)); 257 258 assertTrue(notc.contains(ab)); 259 assertFalse(notc.contains(bc)); 260 assertFalse(notc.contains(bd)); 261 assertFalse(notc.contains(cd)); 262 assertTrue(notc.contains(de)); 263 assertFalse(notc.contains(ae)); 264 assertFalse(notc.contains(all)); 265 assertFalse(notc.contains(allbutfirst)); 266 267 assertTrue(notbd.contains(a)); 268 assertFalse(notbd.contains(b)); 269 assertFalse(notbd.contains(c)); 270 assertFalse(notbd.contains(d)); 271 assertTrue(notbd.contains(e)); 272 273 assertTrue(notcd.contains(ab)); 274 assertFalse(notcd.contains(bc)); 275 assertFalse(notcd.contains(bd)); 276 assertFalse(notcd.contains(cd)); 277 assertFalse(notcd.contains(de)); 278 assertFalse(notcd.contains(ae)); 279 assertTrue(notcd.contains(ef)); 280 assertFalse(notcd.contains(all)); 281 assertFalse(notcd.contains(allbutfirst)); 282 283 // negated/negated 284 assertFalse(notc.contains(notb)); 285 assertTrue(notc.contains(notc)); 286 assertFalse(notc.contains(notd)); 287 288 assertFalse(notc.contains(notab)); 289 assertTrue(notc.contains(notbc)); 290 assertTrue(notc.contains(notbd)); 291 assertTrue(notc.contains(notcd)); 292 assertFalse(notc.contains(notde)); 293 294 assertFalse(notbd.contains(notb)); 295 assertFalse(notbd.contains(notc)); 296 assertFalse(notbd.contains(notd)); 297 298 assertFalse(notbd.contains(notab)); 299 assertFalse(notbd.contains(notbc)); 300 assertTrue(notbd.contains(notbd)); 301 assertFalse(notbd.contains(notcd)); 302 assertFalse(notbd.contains(notde)); 303 assertTrue(notbd.contains(notae)); 304 } 305 306 @Test testContainsNullArg()307 public void testContainsNullArg() { 308 final CharRange range = CharRange.is('a'); 309 final NullPointerException e = assertThrows(NullPointerException.class, () -> range.contains(null)); 310 assertEquals("range", e.getMessage()); 311 } 312 313 @Test testIterator()314 public void testIterator() { 315 final CharRange a = CharRange.is('a'); 316 final CharRange ad = CharRange.isIn('a', 'd'); 317 final CharRange nota = CharRange.isNot('a'); 318 final CharRange emptySet = CharRange.isNotIn((char) 0, Character.MAX_VALUE); 319 final CharRange notFirst = CharRange.isNotIn((char) 1, Character.MAX_VALUE); 320 final CharRange notLast = CharRange.isNotIn((char) 0, (char) (Character.MAX_VALUE - 1)); 321 322 final Iterator<Character> aIt = a.iterator(); 323 assertNotNull(aIt); 324 assertTrue(aIt.hasNext()); 325 assertEquals(Character.valueOf('a'), aIt.next()); 326 assertFalse(aIt.hasNext()); 327 328 final Iterator<Character> adIt = ad.iterator(); 329 assertNotNull(adIt); 330 assertTrue(adIt.hasNext()); 331 assertEquals(Character.valueOf('a'), adIt.next()); 332 assertEquals(Character.valueOf('b'), adIt.next()); 333 assertEquals(Character.valueOf('c'), adIt.next()); 334 assertEquals(Character.valueOf('d'), adIt.next()); 335 assertFalse(adIt.hasNext()); 336 337 final Iterator<Character> notaIt = nota.iterator(); 338 assertNotNull(notaIt); 339 assertTrue(notaIt.hasNext()); 340 while (notaIt.hasNext()) { 341 final Character c = notaIt.next(); 342 assertNotEquals('a', c.charValue()); 343 } 344 345 final Iterator<Character> emptySetIt = emptySet.iterator(); 346 assertNotNull(emptySetIt); 347 assertFalse(emptySetIt.hasNext()); 348 assertThrows(NoSuchElementException.class, emptySetIt::next); 349 350 final Iterator<Character> notFirstIt = notFirst.iterator(); 351 assertNotNull(notFirstIt); 352 assertTrue(notFirstIt.hasNext()); 353 assertEquals(Character.valueOf((char) 0), notFirstIt.next()); 354 assertFalse(notFirstIt.hasNext()); 355 assertThrows(NoSuchElementException.class, notFirstIt::next); 356 357 final Iterator<Character> notLastIt = notLast.iterator(); 358 assertNotNull(notLastIt); 359 assertTrue(notLastIt.hasNext()); 360 assertEquals(Character.valueOf(Character.MAX_VALUE), notLastIt.next()); 361 assertFalse(notLastIt.hasNext()); 362 assertThrows(NoSuchElementException.class, notLastIt::next); 363 } 364 365 @Test testSerialization()366 public void testSerialization() { 367 CharRange range = CharRange.is('a'); 368 assertEquals(range, SerializationUtils.clone(range)); 369 range = CharRange.isIn('a', 'e'); 370 assertEquals(range, SerializationUtils.clone(range)); 371 range = CharRange.isNotIn('a', 'e'); 372 assertEquals(range, SerializationUtils.clone(range)); 373 } 374 375 @Test testIteratorRemove()376 public void testIteratorRemove() { 377 final CharRange a = CharRange.is('a'); 378 final Iterator<Character> aIt = a.iterator(); 379 assertThrows(UnsupportedOperationException.class, aIt::remove); 380 } 381 } 382