Skip to content

Commit 01382bc

Browse files
Test max/min with offset and limit, also for findIds and 32-bit.
1 parent d5e2644 commit 01382bc

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/TestUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public static boolean isWindows() {
3838
return osName.contains("windows");
3939
}
4040

41+
/**
42+
* Returns true if the JVM this code runs on is in 32-bit mode,
43+
* so may not necessarily mean the system has a 32-bit architecture.
44+
*/
45+
public static boolean is32BitJVM() {
46+
final String bitness = System.getProperty("sun.arch.data.model");
47+
return "32".equals(bitness);
48+
}
49+
4150
public static String loadFile(String filename) {
4251
try {
4352
InputStream in = openInputStream("/" + filename);

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.objectbox.DebugFlags;
2323
import io.objectbox.TestEntity;
2424
import io.objectbox.TestEntity_;
25+
import io.objectbox.TestUtils;
2526
import io.objectbox.exception.DbExceptionListener;
2627
import io.objectbox.exception.NonUniqueResultException;
2728
import io.objectbox.query.QueryBuilder.StringOrder;
@@ -255,17 +256,69 @@ public void testLongNotIn() {
255256
}
256257

257258
@Test
258-
public void testOffsetLimit() {
259+
public void offset_limit_find() {
259260
putTestEntitiesScalars();
260261
Query<TestEntity> query = box.query().greater(simpleInt, 2002).less(simpleShort, 2108).build();
261262
assertEquals(5, query.count());
263+
262264
assertEquals(4, query.find(1, 0).size());
263265
assertEquals(1, query.find(4, 0).size());
264266
assertEquals(2, query.find(0, 2).size());
265267
List<TestEntity> list = query.find(1, 2);
266268
assertEquals(2, list.size());
267269
assertEquals(2004, list.get(0).getSimpleInt());
268270
assertEquals(2005, list.get(1).getSimpleInt());
271+
272+
OffsetLimitFunction find = (offset, limit) -> query.find(offset, limit).size();
273+
assertOffsetLimitEdgeCases(find);
274+
}
275+
276+
@Test
277+
public void offset_limit_findIds() {
278+
putTestEntitiesScalars();
279+
Query<TestEntity> query = box.query().greater(simpleInt, 2002).less(simpleShort, 2108).build();
280+
assertEquals(5, query.count());
281+
282+
assertEquals(4, query.findIds(1, 0).length);
283+
assertEquals(1, query.findIds(4, 0).length);
284+
assertEquals(2, query.findIds(0, 2).length);
285+
long[] list = query.findIds(1, 2);
286+
assertEquals(2, list.length);
287+
assertEquals(5, list[0]);
288+
assertEquals(6, list[1]);
289+
290+
OffsetLimitFunction findIds = (offset, limit) -> query.findIds(offset, limit).length;
291+
assertOffsetLimitEdgeCases(findIds);
292+
}
293+
294+
private interface OffsetLimitFunction {
295+
int applyAndCount(long offset, long limit);
296+
}
297+
298+
private void assertOffsetLimitEdgeCases(OffsetLimitFunction function) {
299+
// Max value
300+
if (TestUtils.is32BitJVM()) {
301+
// When running 32-bit ObjectBox limit and offset max is limited to 32-bit unsigned integer.
302+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
303+
() -> function.applyAndCount(Long.MAX_VALUE, Long.MAX_VALUE));
304+
assertEquals("Invalid offset (9223372036854775807): exceeds the maximum value allowed on this platform (4294967295)",
305+
ex.getMessage());
306+
// Ensure max allowed value works.
307+
// Note: currently offset + limit must not exceed 32-bit unsigned integer max.
308+
assertEquals(0, function.applyAndCount(Integer.MAX_VALUE * 2L + 1, 0));
309+
assertEquals(5, function.applyAndCount(0, Integer.MAX_VALUE * 2L + 1));
310+
} else {
311+
// 64-bit JVM
312+
assertEquals(0, function.applyAndCount(Long.MAX_VALUE, Long.MAX_VALUE));
313+
}
314+
315+
// Min value
316+
IllegalArgumentException exOffset = assertThrows(IllegalArgumentException.class,
317+
() -> function.applyAndCount(Long.MIN_VALUE, 0));
318+
assertEquals("Invalid offset (-9223372036854775808): must be zero or positive", exOffset.getMessage());
319+
IllegalArgumentException exLimit = assertThrows(IllegalArgumentException.class,
320+
() -> function.applyAndCount(0, Long.MIN_VALUE));
321+
assertEquals("Invalid limit (-9223372036854775808): must be zero or positive", exLimit.getMessage());
269322
}
270323

271324
@Test

0 commit comments

Comments
 (0)