Debian Codegraph @

src/test/java/ezvcard/util/CharacterBitSetTest.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import static ezvcard.util.TestUtils.assertEqualsAndHash;
4 import static ezvcard.util.TestUtils.assertEqualsMethodEssentials;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertNotEquals;
8 import static org.junit.Assert.assertTrue;
9
10 import java.util.BitSet;
11
12 import org.junit.Test;
13
14 /*
15 Copyright (c) 2012-2023, Michael Angstadt
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21 1. Redistributions of source code must retain the above copyright notice, this
22 list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 The views and conclusions contained in the software and documentation are those
39 of the authors and should not be interpreted as representing official policies,
40 either expressed or implied, of the FreeBSD Project.
41 */
42
43 /**
44 * @author Michael Angstadt
45 */
46 public class CharacterBitSetTest {
47 @Test
48 public void expandCharacterList() {
49 BitSet actual = new CharacterBitSet("abc123").bitSet();
50 BitSet expected = new BitSet();
51 expected.set('a', 'd');
52 expected.set('1', '4');
53 assertEquals(expected, actual);
54
55 actual = new CharacterBitSet("a-f").bitSet();
56 expected = new BitSet();
57 expected.set('a', 'g');
58 assertEquals(expected, actual);
59
60 actual = new CharacterBitSet("a-fa-f").bitSet();
61 expected = new BitSet();
62 expected.set('a', 'g');
63 assertEquals(expected, actual);
64
65 actual = new CharacterBitSet("f-a").bitSet();
66 expected = new BitSet();
67 expected.set('a', 'g');
68 assertEquals(expected, actual);
69
70 actual = new CharacterBitSet("a-fz").bitSet();
71 expected = new BitSet();
72 expected.set('a', 'g');
73 expected.set('z');
74 assertEquals(expected, actual);
75
76 actual = new CharacterBitSet("-a-f").bitSet();
77 expected = new BitSet();
78 expected.set('a', 'g');
79 expected.set('-');
80 assertEquals(expected, actual);
81
82 actual = new CharacterBitSet("a-f-").bitSet();
83 expected = new BitSet();
84 expected.set('a', 'g');
85 expected.set('-');
86 assertEquals(expected, actual);
87
88 actual = new CharacterBitSet("-a-f0-9xyz*").bitSet();
89 expected = new BitSet();
90 expected.set('a', 'g');
91 expected.set('-');
92 expected.set('0', '9' + 1);
93 expected.set('x');
94 expected.set('y');
95 expected.set('z');
96 expected.set('*');
97 assertEquals(expected, actual);
98
99 actual = new CharacterBitSet("").bitSet();
100 expected = new BitSet();
101 assertEquals(expected, actual);
102 }
103
104 @Test
105 public void characters() {
106 CharacterBitSet bitSet = new CharacterBitSet("a-z");
107 assertEquals("a-z", bitSet.characters());
108 }
109
110 @Test
111 public void containsOnly_empty() {
112 CharacterBitSet bitSet = new CharacterBitSet("");
113 assertFalse(bitSet.containsOnly("anything"));
114 assertTrue(bitSet.containsOnly(""));
115 }
116
117 @Test
118 public void containsOnly() {
119 CharacterBitSet bitSet = new CharacterBitSet("b-f0-9");
120 assertTrue(bitSet.containsOnly("bf3"));
121 assertFalse(bitSet.containsOnly("af3"));
122 assertTrue(bitSet.containsOnly("bf3", 1));
123 assertTrue(bitSet.containsOnly("af3", 1));
124 assertTrue(bitSet.containsOnly(""));
125 }
126
127 @Test
128 public void containsAny_empty() {
129 CharacterBitSet bitSet = new CharacterBitSet("");
130 assertFalse(bitSet.containsAny("anything"));
131 assertFalse(bitSet.containsAny(""));
132 }
133
134 @Test
135 public void containsAny() {
136 CharacterBitSet bitSet = new CharacterBitSet("b-f0-9");
137 assertTrue(bitSet.containsAny("bxx"));
138 assertFalse(bitSet.containsAny("aaa"));
139 assertFalse(bitSet.containsAny("bxx", 1));
140 assertFalse(bitSet.containsAny(""));
141 }
142
143 @Test
144 public void equals_contract() {
145 CharacterBitSet one = new CharacterBitSet("b-f0-9");
146 CharacterBitSet two = new CharacterBitSet("0-9f-b");
147 assertEqualsMethodEssentials(one);
148 assertEqualsAndHash(one, two);
149
150 one = new CharacterBitSet("b-f0-9");
151 two = new CharacterBitSet("bcfed9786452310");
152 assertEqualsAndHash(one, two);
153
154 one = new CharacterBitSet("a-f0-9");
155 two = new CharacterBitSet("0-9f-b");
156 assertNotEquals(one, two);
157 assertNotEquals(two, one);
158 }
159 }