Skip to content

Commit cd9fbde

Browse files
Fix range of returned by ChgCombined values (they were chopped by half :) great work, yeah)
1 parent 5fa4911 commit cd9fbde

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

YASL-Core/src/YASL/Hashing/ChgCombined.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,38 @@ public ChgCombined(IHashingAlgorithm<T>... hashes) {
2424

2525
@Override
2626
public IHasher<T> generate(int range, int levels) {
27+
final int bytesPerItem = bytesIn(range);
2728
return x -> {
28-
final int bytesPerItem = bytesIn(range);
2929
final ByteBuffer res = populate(x, levels * bytesPerItem);
3030
final int[] resArr = new int[levels];
3131
for (int i = 0; i < levels; i++) {
3232
switch (bytesPerItem) {
3333
case 4:
34-
resArr[i] = Math.abs(res.getInt() % range);
34+
final long value = res.getInt() & 0xFFFFFFFFl;
35+
final long lRange = range & 0xFFFFFFFFl;
36+
resArr[i] = (int) (value % lRange);
3537
break;
3638

3739
case 2:
38-
resArr[i] = Math.abs(res.getShort() % range);
40+
resArr[i] = (res.getShort() & 0xFFFF) % range;
3941
break;
4042

4143
case 1:
42-
resArr[i] = Math.abs(res.get() % range);
44+
resArr[i] = (res.get() & 0xFF) % range;
4345
break;
46+
47+
default:
48+
throw new RuntimeException("Unsupported hash size");
4449
}
4550
}
4651
return resArr;
4752
};
4853
}
4954

5055
public int bytesIn(int range) {
56+
if (range < 0) // unsigned
57+
return 4;
58+
5159
if (range <= 0xFF)
5260
return 1;
5361
if (range <= 0xFFFF)

YASL-Tests/src/Tests/Hashing/Test_hgCombined.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void respectRange() {
2626
IHasher<Integer> gen = new ChgCombined<Integer>( //
2727
(x, r) -> x, // salt
2828
(x, r) -> r.put((byte) 21), //
29-
(x, r) -> r.put((byte) -24) //
29+
(x, r) -> r.put((byte) 24) //
3030
).generate(5, 2);
3131

3232
Assert.assertArrayEquals( //
@@ -70,6 +70,7 @@ public void sizeOfRange() {
7070

7171
Assert.assertEquals(4, gen.bytesIn(0xFFFF00));
7272
Assert.assertEquals(4, gen.bytesIn(0x7FFFFF00));
73+
Assert.assertEquals(4, gen.bytesIn(0xFFFFFF00));
7374
}
7475

7576
@Test
@@ -96,4 +97,37 @@ public void use2bytes() {
9697
new int[] { 1, 2 }, gen.apply(0x00010002) //
9798
);
9899
}
100+
101+
@Test
102+
public void asUnsignedByte() {
103+
IHasher<Integer> gen = new ChgCombined<Integer>( //
104+
new CIntegerHashing<>(x -> 0xF0F0F0F0) //
105+
).generate(0xFF, 1);
106+
107+
Assert.assertArrayEquals( //
108+
new int[] { 0xF0 }, gen.apply(0) //
109+
);
110+
}
111+
112+
@Test
113+
public void asUnsignedWord() {
114+
IHasher<Integer> gen = new ChgCombined<Integer>( //
115+
new CIntegerHashing<>(x -> 0xFF00FF00) //
116+
).generate(0xFFFF, 1);
117+
118+
Assert.assertArrayEquals( //
119+
new int[] { 0xFF00 }, gen.apply(0) //
120+
);
121+
}
122+
123+
@Test
124+
public void asUnsignedInt() {
125+
IHasher<Integer> gen = new ChgCombined<Integer>( //
126+
new CIntegerHashing<>(x -> 0xFF00000F) //
127+
).generate(0xFFFFFFFF, 1);
128+
129+
Assert.assertEquals( //
130+
0xFF00000F, gen.apply(3)[0] //
131+
);
132+
}
99133
}

0 commit comments

Comments
 (0)