Skip to content

Commit 9fab7c8

Browse files
committed
one more test and apply format
1 parent c63de9e commit 9fab7c8

File tree

1 file changed

+150
-31
lines changed

1 file changed

+150
-31
lines changed

qendpoint-core/src/test/java/com/the_qa_company/qendpoint/core/triples/impl/BitmapTriplesIteratorTest.java

+150-31
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.IOException;
2222
import java.nio.charset.StandardCharsets;
2323
import java.nio.file.Path;
24-
import java.util.Random;
2524

2625
import static org.junit.Assert.assertEquals;
2726
import static org.junit.Assert.assertFalse;
@@ -261,13 +260,10 @@ public void jumpXYZTest() throws IOException, ParserException {
261260

262261
supplier.createAndSaveFakeHDT(spec, hdtPath);
263262

264-
Random rnd = new Random(34567);
265-
266263
try (HDT hdt = HDTManager.mapIndexedHDT(hdtPath, spec, ProgressListener.ignore())) {
267264
int elements = (int) hdt.getTriples().getNumberOfElements();
268265

269-
for (int i = 0; i < count; i++) {
270-
int idx = rnd.nextInt(elements);
266+
for (int idx = 0; idx < elements; idx++) {
271267

272268
IteratorTripleID it = hdt.getTriples().searchAll();
273269

@@ -280,28 +276,28 @@ public void jumpXYZTest() throws IOException, ParserException {
280276

281277
for (int member = 0; member < 3; member++) {
282278
IteratorTripleID itac = hdt.getTriples().searchAll(TripleComponentOrder.SPO.mask);
283-
assertSame("invalid order (" + member + "/" + i + ")", itac.getOrder(),
279+
assertSame("invalid order (" + member + "/" + idx + ")", itac.getOrder(),
284280
TripleComponentOrder.SPO);
285281

286282
// test subject
287-
assertTrue("Can't jump to subject " + current + " (" + member + "/" + i + ")",
283+
assertTrue("Can't jump to subject " + current + " (" + member + "/" + idx + ")",
288284
itac.canGoToSubject() && itac.gotoSubject(current.getSubject()));
289285

290286
if (member >= 1) {
291287
// test predicate
292-
assertTrue("Can't jump to predicate " + current + " (" + member + "/" + i + ")",
288+
assertTrue("Can't jump to predicate " + current + " (" + member + "/" + idx + ")",
293289
itac.canGoToPredicate() && itac.gotoPredicate(current.getPredicate()));
294290

295291
if (member >= 2) {
296292
// test object
297-
assertTrue("Can't jump to object " + current + " (" + member + "/" + i + ")",
293+
assertTrue("Can't jump to object " + current + " (" + member + "/" + idx + ")",
298294
itac.canGoToObject() && itac.gotoObject(current.getObject()));
299295
}
300296
}
301297

302-
assertTrue("for " + current + " (" + member + "/" + i + ")", itac.hasNext());
298+
assertTrue("for " + current + " (" + member + "/" + idx + ")", itac.hasNext());
303299
TripleID next = itac.next();
304-
String err = "invalid next " + next + " != " + current + " (" + member + "/" + i + ")";
300+
String err = "invalid next " + next + " != " + current + " (" + member + "/" + idx + ")";
305301
switch (member) {
306302
case 2: // object
307303
assertEquals("object err " + err, current.getObject(), next.getObject());
@@ -316,29 +312,33 @@ public void jumpXYZTest() throws IOException, ParserException {
316312
}
317313
if (member == 2) {
318314
assertEquals("idx err " + err, idx, itac.getLastTriplePosition());
319-
TripleID newCurrent = itac.next();
320-
assertTrue("idx err " + err, idx < itac.getLastTriplePosition());
321-
322-
if (current.getSubject() == newCurrent.getSubject()) {
323-
// no jump on X, we should have the sam
324-
assertTrue("Can't jump to subject " + current + " (" + member + "/" + i + ")",
325-
itac.gotoSubject(current.getSubject()));
315+
if (itac.hasNext()) {
316+
TripleID newCurrent = itac.next();
317+
assertTrue("idx err " + err, idx < itac.getLastTriplePosition());
318+
319+
if (current.getSubject() == newCurrent.getSubject()) {
320+
// no jump on X, we should have the sam
321+
assertTrue("Can't jump to subject " + current + " (" + member + "/" + idx + ")",
322+
itac.gotoSubject(current.getSubject()));
323+
324+
if (current.getPredicate() == newCurrent.getPredicate()) {
325+
// no jump on Y, we should have the same
326+
assertTrue("Can't jump to subject " + current + " (" + member + "/" + idx + ")",
327+
itac.gotoPredicate(current.getPredicate()));
328+
329+
assertFalse(
330+
"Can't jump to subject " + current + " (" + member + "/" + idx + ")",
331+
itac.gotoObject(current.getObject()));
332+
} else {
333+
assertFalse(
334+
"Can't jump to subject " + current + " (" + member + "/" + idx + ")",
335+
itac.gotoPredicate(current.getPredicate()));
336+
}
326337

327-
if (current.getPredicate() == newCurrent.getPredicate()) {
328-
// no jump on Y, we should have the same
329-
assertTrue("Can't jump to subject " + current + " (" + member + "/" + i + ")",
330-
itac.gotoPredicate(current.getPredicate()));
331-
332-
assertFalse("Can't jump to subject " + current + " (" + member + "/" + i + ")",
333-
itac.gotoObject(current.getObject()));
334338
} else {
335-
assertFalse("Can't jump to subject " + current + " (" + member + "/" + i + ")",
336-
itac.gotoPredicate(current.getPredicate()));
339+
assertFalse("Can't jump to subject " + current + " (" + member + "/" + idx + ")",
340+
itac.gotoSubject(current.getSubject()));
337341
}
338-
339-
} else {
340-
assertFalse("Can't jump to subject " + current + " (" + member + "/" + i + ")",
341-
itac.gotoSubject(current.getSubject()));
342342
}
343343

344344
} else {
@@ -351,4 +351,123 @@ public void jumpXYZTest() throws IOException, ParserException {
351351
PathUtils.deleteDirectory(root);
352352
}
353353
}
354+
355+
@Test
356+
public void jumpXYZNextTest() throws IOException, ParserException {
357+
Path root = tempDir.newFolder().toPath();
358+
359+
try {
360+
Path hdtPath = root.resolve("test.hdt");
361+
362+
HDTOptions spec = HDTOptions.of(HDTOptionsKeys.BITMAPTRIPLES_INDEX_OTHERS, "spo,sop,pos,pso,ops,osp",
363+
HDTOptionsKeys.BITMAPTRIPLES_INDEX_NO_FOQ, true);
364+
final int count = 10_000;
365+
LargeFakeDataSetStreamSupplier supplier = LargeFakeDataSetStreamSupplier
366+
.createSupplierWithMaxTriples(count, 567890987).withMaxElementSplit(50).withMaxLiteralSize(20);
367+
368+
supplier.createAndSaveFakeHDT(spec, hdtPath);
369+
370+
try (HDT hdt = HDTManager.mapIndexedHDT(hdtPath, spec, ProgressListener.ignore())) {
371+
int elements = (int) hdt.getTriples().getNumberOfElements();
372+
for (int idx = 0; idx < elements; idx++) {
373+
374+
IteratorTripleID it = hdt.getTriples().searchAll();
375+
376+
assertTrue(it.canGoTo());
377+
378+
it.goTo(idx);
379+
380+
TripleID current = it.next().clone();
381+
assertEquals(idx, it.getLastTriplePosition());
382+
383+
nextCountLoop:
384+
for (int nextCount = 0; nextCount < 10; nextCount++) {
385+
for (int member = 1; member < 3; member++) {
386+
String memberInfo = " (" + member + "/" + idx + "/" + nextCount + ")";
387+
IteratorTripleID itac = hdt.getTriples().searchAll(TripleComponentOrder.SPO.mask);
388+
assertSame("invalid order" + memberInfo, itac.getOrder(), TripleComponentOrder.SPO);
389+
390+
// test subject
391+
assertTrue("Can't jump to subject " + current + memberInfo,
392+
itac.canGoToSubject() && itac.gotoSubject(current.getSubject()));
393+
394+
for (int j = 0; j < nextCount; j++) {
395+
assertTrue(itac.hasNext());
396+
TripleID pvid = itac.next();
397+
398+
if (itac.getLastTriplePosition() == idx) {
399+
assertEquals(pvid, current);
400+
break nextCountLoop; // we consumed the one
401+
// we were searching
402+
// for, it can't be
403+
// used
404+
}
405+
}
406+
407+
// test predicate
408+
assertTrue("Can't jump to predicate " + current + memberInfo,
409+
itac.canGoToPredicate() && itac.gotoPredicate(current.getPredicate()));
410+
411+
if (member >= 2) {
412+
// test object
413+
assertTrue("Can't jump to object " + current + memberInfo,
414+
itac.canGoToObject() && itac.gotoObject(current.getObject()));
415+
}
416+
417+
assertTrue("for " + current + memberInfo, itac.hasNext());
418+
TripleID next = itac.next();
419+
String err = "invalid next " + next + " != " + current + memberInfo;
420+
switch (member) {
421+
case 2: // object
422+
assertEquals("object err " + err, current.getObject(), next.getObject());
423+
case 1: // predicate
424+
assertEquals("predicate err " + err, current.getPredicate(), next.getPredicate());
425+
case 0: // subject only
426+
assertEquals("subject err " + err, current.getSubject(), next.getSubject());
427+
break;
428+
default:
429+
fail("bad member: " + member);
430+
break;
431+
}
432+
if (member == 2) {
433+
assertEquals("idx err " + err, idx, itac.getLastTriplePosition());
434+
if (itac.hasNext()) {
435+
TripleID newCurrent = itac.next();
436+
assertTrue("idx err " + err, idx < itac.getLastTriplePosition());
437+
438+
if (current.getSubject() == newCurrent.getSubject()) {
439+
// no jump on X, we should have the sam
440+
assertTrue("Can't jump to subject " + current + memberInfo + newCurrent,
441+
itac.gotoSubject(current.getSubject()));
442+
443+
if (current.getPredicate() == newCurrent.getPredicate()) {
444+
// no jump on Y, we should have the
445+
// same
446+
assertTrue("Can't jump to subject " + current + memberInfo + newCurrent,
447+
itac.gotoPredicate(current.getPredicate()));
448+
449+
assertFalse("Can't jump to subject " + current + memberInfo + newCurrent,
450+
itac.gotoObject(current.getObject()));
451+
} else {
452+
assertFalse("Can't jump to subject " + current + memberInfo + newCurrent,
453+
itac.gotoPredicate(current.getPredicate()));
454+
}
455+
} else {
456+
assertFalse("Can't jump to subject " + current + memberInfo + newCurrent,
457+
itac.gotoSubject(current.getSubject()));
458+
}
459+
}
460+
461+
} else {
462+
assertTrue("idx err " + err, idx >= itac.getLastTriplePosition());
463+
}
464+
}
465+
}
466+
467+
}
468+
}
469+
} finally {
470+
PathUtils.deleteDirectory(root);
471+
}
472+
}
354473
}

0 commit comments

Comments
 (0)