@@ -152,111 +152,170 @@ public static void cleanup() throws Exception {
152
152
153
153
@ Test
154
154
public void naturalIndex () throws Exception {
155
- testIteration (NATURAL_ORDER , view (), null );
155
+ testIteration (NATURAL_ORDER , view (), null , null );
156
156
}
157
157
158
158
@ Test
159
159
public void refIndex () throws Exception {
160
- testIteration (REF_INDEX_ORDER , view ().index ("id" ), null );
160
+ testIteration (REF_INDEX_ORDER , view ().index ("id" ), null , null );
161
161
}
162
162
163
163
@ Test
164
164
public void copyIndex () throws Exception {
165
- testIteration (COPY_INDEX_ORDER , view ().index ("name" ), null );
165
+ testIteration (COPY_INDEX_ORDER , view ().index ("name" ), null , null );
166
166
}
167
167
168
168
@ Test
169
169
public void numericIndex () throws Exception {
170
- testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ), null );
170
+ testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ), null , null );
171
171
}
172
172
173
173
@ Test
174
174
public void naturalIndexDescending () throws Exception {
175
- testIteration (NATURAL_ORDER , view ().reverse (), null );
175
+ testIteration (NATURAL_ORDER , view ().reverse (), null , null );
176
176
}
177
177
178
178
@ Test
179
179
public void refIndexDescending () throws Exception {
180
- testIteration (REF_INDEX_ORDER , view ().index ("id" ).reverse (), null );
180
+ testIteration (REF_INDEX_ORDER , view ().index ("id" ).reverse (), null , null );
181
181
}
182
182
183
183
@ Test
184
184
public void copyIndexDescending () throws Exception {
185
- testIteration (COPY_INDEX_ORDER , view ().index ("name" ).reverse (), null );
185
+ testIteration (COPY_INDEX_ORDER , view ().index ("name" ).reverse (), null , null );
186
186
}
187
187
188
188
@ Test
189
189
public void numericIndexDescending () throws Exception {
190
- testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ).reverse (), null );
190
+ testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ).reverse (), null , null );
191
191
}
192
192
193
193
@ Test
194
194
public void naturalIndexWithStart () throws Exception {
195
- CustomType1 first = pickFirst ();
196
- testIteration (NATURAL_ORDER , view ().first (first .key ), first );
195
+ CustomType1 first = pickLimit ();
196
+ testIteration (NATURAL_ORDER , view ().first (first .key ), first , null );
197
197
}
198
198
199
199
@ Test
200
200
public void refIndexWithStart () throws Exception {
201
- CustomType1 first = pickFirst ();
202
- testIteration (REF_INDEX_ORDER , view ().index ("id" ).first (first .id ), first );
201
+ CustomType1 first = pickLimit ();
202
+ testIteration (REF_INDEX_ORDER , view ().index ("id" ).first (first .id ), first , null );
203
203
}
204
204
205
205
@ Test
206
206
public void copyIndexWithStart () throws Exception {
207
- CustomType1 first = pickFirst ();
208
- testIteration (COPY_INDEX_ORDER , view ().index ("name" ).first (first .name ), first );
207
+ CustomType1 first = pickLimit ();
208
+ testIteration (COPY_INDEX_ORDER , view ().index ("name" ).first (first .name ), first , null );
209
209
}
210
210
211
211
@ Test
212
212
public void numericIndexWithStart () throws Exception {
213
- CustomType1 first = pickFirst ();
214
- testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ).first (first .num ), first );
213
+ CustomType1 first = pickLimit ();
214
+ testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ).first (first .num ), first , null );
215
215
}
216
216
217
217
@ Test
218
218
public void naturalIndexDescendingWithStart () throws Exception {
219
- CustomType1 first = pickFirst ();
220
- testIteration (NATURAL_ORDER , view ().reverse ().first (first .key ), first );
219
+ CustomType1 first = pickLimit ();
220
+ testIteration (NATURAL_ORDER , view ().reverse ().first (first .key ), first , null );
221
221
}
222
222
223
223
@ Test
224
224
public void refIndexDescendingWithStart () throws Exception {
225
- CustomType1 first = pickFirst ();
226
- testIteration (REF_INDEX_ORDER , view ().reverse ().index ("id" ).first (first .id ), first );
225
+ CustomType1 first = pickLimit ();
226
+ testIteration (REF_INDEX_ORDER , view ().reverse ().index ("id" ).first (first .id ), first , null );
227
227
}
228
228
229
229
@ Test
230
230
public void copyIndexDescendingWithStart () throws Exception {
231
- CustomType1 first = pickFirst ();
232
- testIteration (COPY_INDEX_ORDER , view ().reverse ().index ("name" ).first (first .name ),
233
- first );
231
+ CustomType1 first = pickLimit ();
232
+ testIteration (COPY_INDEX_ORDER , view ().reverse ().index ("name" ).first (first .name ), first , null );
234
233
}
235
234
236
235
@ Test
237
236
public void numericIndexDescendingWithStart () throws Exception {
238
- CustomType1 first = pickFirst ();
239
- testIteration (NUMERIC_INDEX_ORDER , view ().reverse ().index ("int" ).first (first .num ),
240
- first );
237
+ CustomType1 first = pickLimit ();
238
+ testIteration (NUMERIC_INDEX_ORDER , view ().reverse ().index ("int" ).first (first .num ), first , null );
241
239
}
242
240
243
241
@ Test
244
242
public void naturalIndexWithSkip () throws Exception {
245
- testIteration (NATURAL_ORDER , view ().skip (RND .nextInt (allEntries .size () / 2 )), null );
243
+ testIteration (NATURAL_ORDER , view ().skip (RND .nextInt (allEntries .size () / 2 )), null , null );
246
244
}
247
245
248
246
@ Test
249
247
public void refIndexWithSkip () throws Exception {
250
248
testIteration (REF_INDEX_ORDER , view ().index ("id" ).skip (RND .nextInt (allEntries .size () / 2 )),
251
- null );
249
+ null , null );
252
250
}
253
251
254
252
@ Test
255
253
public void copyIndexWithSkip () throws Exception {
256
254
testIteration (COPY_INDEX_ORDER , view ().index ("name" ).skip (RND .nextInt (allEntries .size () / 2 )),
257
- null );
255
+ null , null );
258
256
}
259
257
258
+ @ Test
259
+ public void naturalIndexWithMax () throws Exception {
260
+ testIteration (NATURAL_ORDER , view ().max (RND .nextInt (allEntries .size () / 2 )), null , null );
261
+ }
262
+
263
+ @ Test
264
+ public void copyIndexWithMax () throws Exception {
265
+ testIteration (COPY_INDEX_ORDER , view ().index ("name" ).max (RND .nextInt (allEntries .size () / 2 )),
266
+ null , null );
267
+ }
268
+
269
+ @ Test
270
+ public void naturalIndexWithLast () throws Exception {
271
+ CustomType1 last = pickLimit ();
272
+ testIteration (NATURAL_ORDER , view ().last (last .key ), null , last );
273
+ }
274
+
275
+ @ Test
276
+ public void refIndexWithLast () throws Exception {
277
+ CustomType1 last = pickLimit ();
278
+ testIteration (REF_INDEX_ORDER , view ().index ("id" ).last (last .id ), null , last );
279
+ }
280
+
281
+ @ Test
282
+ public void copyIndexWithLast () throws Exception {
283
+ CustomType1 last = pickLimit ();
284
+ testIteration (COPY_INDEX_ORDER , view ().index ("name" ).last (last .name ), null , last );
285
+ }
286
+
287
+ @ Test
288
+ public void numericIndexWithLast () throws Exception {
289
+ CustomType1 last = pickLimit ();
290
+ testIteration (NUMERIC_INDEX_ORDER , view ().index ("int" ).last (last .num ), null , last );
291
+ }
292
+
293
+ @ Test
294
+ public void naturalIndexDescendingWithLast () throws Exception {
295
+ CustomType1 last = pickLimit ();
296
+ testIteration (NATURAL_ORDER , view ().reverse ().last (last .key ), null , last );
297
+ }
298
+
299
+ @ Test
300
+ public void refIndexDescendingWithLast () throws Exception {
301
+ CustomType1 last = pickLimit ();
302
+ testIteration (REF_INDEX_ORDER , view ().reverse ().index ("id" ).last (last .id ), null , last );
303
+ }
304
+
305
+ @ Test
306
+ public void copyIndexDescendingWithLast () throws Exception {
307
+ CustomType1 last = pickLimit ();
308
+ testIteration (COPY_INDEX_ORDER , view ().reverse ().index ("name" ).last (last .name ),
309
+ null , last );
310
+ }
311
+
312
+ @ Test
313
+ public void numericIndexDescendingWithLast () throws Exception {
314
+ CustomType1 last = pickLimit ();
315
+ testIteration (NUMERIC_INDEX_ORDER , view ().reverse ().index ("int" ).last (last .num ),
316
+ null , last );
317
+ }
318
+
260
319
@ Test
261
320
public void testRefWithIntNaturalKey () throws Exception {
262
321
LevelDBSuite .IntKeyType i = new LevelDBSuite .IntKeyType ();
@@ -272,8 +331,8 @@ public void testRefWithIntNaturalKey() throws Exception {
272
331
}
273
332
}
274
333
275
- private CustomType1 pickFirst () {
276
- // Picks a first element that has clashes with other elements in the given index.
334
+ private CustomType1 pickLimit () {
335
+ // Picks an element that has clashes with other elements in the given index.
277
336
return clashingEntries .get (RND .nextInt (clashingEntries .size ()));
278
337
}
279
338
@@ -297,22 +356,32 @@ private <T extends Comparable<T>> int compareWithFallback(
297
356
private void testIteration (
298
357
final BaseComparator order ,
299
358
final KVStoreView <CustomType1 > params ,
300
- final CustomType1 first ) throws Exception {
359
+ final CustomType1 first ,
360
+ final CustomType1 last ) throws Exception {
301
361
List <CustomType1 > indexOrder = sortBy (order .fallback ());
302
362
if (!params .ascending ) {
303
363
indexOrder = Lists .reverse (indexOrder );
304
364
}
305
365
306
366
Iterable <CustomType1 > expected = indexOrder ;
367
+ BaseComparator expectedOrder = params .ascending ? order : order .reverse ();
368
+
307
369
if (first != null ) {
308
- final BaseComparator expectedOrder = params .ascending ? order : order .reverse ();
309
370
expected = Iterables .filter (expected , v -> expectedOrder .compare (first , v ) <= 0 );
310
371
}
311
372
373
+ if (last != null ) {
374
+ expected = Iterables .filter (expected , v -> expectedOrder .compare (v , last ) <= 0 );
375
+ }
376
+
312
377
if (params .skip > 0 ) {
313
378
expected = Iterables .skip (expected , (int ) params .skip );
314
379
}
315
380
381
+ if (params .max != Long .MAX_VALUE ) {
382
+ expected = Iterables .limit (expected , (int ) params .max );
383
+ }
384
+
316
385
List <CustomType1 > actual = collect (params );
317
386
compareLists (expected , actual );
318
387
}
0 commit comments