Skip to content

Commit 2c15aec

Browse files
Merge pull request #171 from OpenHFT/codex/add-tests-for-utilities.tohexstring-and-tobinarystring
Add Utilities string conversion tests
2 parents 7993fa8 + 310a0da commit 2c15aec

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2016-2025 chronicle.software
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 net.openhft.affinity.impl;
18+
19+
import org.junit.Test;
20+
21+
import java.util.BitSet;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
public class UtilitiesTest {
26+
27+
private static String hex(BitSet set, int... bits) {
28+
set.clear();
29+
for (int b : bits) {
30+
set.set(b);
31+
}
32+
return Utilities.toHexString(set);
33+
}
34+
35+
private static String bin(BitSet set, int... bits) {
36+
set.clear();
37+
for (int b : bits) {
38+
set.set(b);
39+
}
40+
return Utilities.toBinaryString(set);
41+
}
42+
43+
@Test
44+
public void testToHexString() {
45+
BitSet set = new BitSet();
46+
assertEquals("", hex(set));
47+
assertEquals("1", hex(set, 0));
48+
assertEquals("10", hex(set, 4));
49+
assertEquals(Long.toHexString(1L << 63), hex(set, 63));
50+
assertEquals("01", hex(set, 64));
51+
assertEquals("101", hex(set, 0, 128));
52+
}
53+
54+
@Test
55+
public void testToBinaryString() {
56+
BitSet set = new BitSet();
57+
assertEquals("", bin(set));
58+
assertEquals("1", bin(set, 0));
59+
assertEquals("10000", bin(set, 4));
60+
assertEquals(Long.toBinaryString(1L << 63), bin(set, 63));
61+
assertEquals("01", bin(set, 64));
62+
assertEquals("101", bin(set, 0, 128));
63+
}
64+
}

0 commit comments

Comments
 (0)