From 81eb97970a7d326a975987afad9aedd37150aca8 Mon Sep 17 00:00:00 2001
From: Nikita <abracham.mitchell@gmail.com>
Date: Fri, 3 Jan 2014 11:53:52 +0400
Subject: [PATCH] RedissonList.lastIndexOf tests added

---
 src/main/java/org/redisson/RedissonList.java  | 33 ++++++--
 .../java/org/redisson/RedissonListTest.java   | 83 +++++++++++++++++++
 2 files changed, 110 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java
index 2168eb483..0bd0c22cb 100644
--- a/src/main/java/org/redisson/RedissonList.java
+++ b/src/main/java/org/redisson/RedissonList.java
@@ -10,6 +10,8 @@ import com.lambdaworks.redis.RedisConnection;
 
 public class RedissonList<V> implements List<V> {
 
+    private int batchSize = 50;
+
     private RedisConnection<Object, Object> connection;
     private String name;
 
@@ -160,6 +162,17 @@ public class RedissonList<V> implements List<V> {
         }
     }
 
+    private int div(int p, int q) {
+        int div = p / q;
+        int rem = p - q * div; // equal to p % q
+
+        if (rem == 0) {
+          return div;
+        }
+
+        return div + 1;
+    }
+
     @Override
     public V remove(int index) {
         checkIndex(index);
@@ -174,8 +187,7 @@ public class RedissonList<V> implements List<V> {
             return -1;
         }
 
-        int batchSize = 50;
-        int to = Math.max(size()/batchSize, 1);
+        int to = div(size(), batchSize);
         for (int i = 0; i < to; i++) {
             List<Object> range = connection.lrange(name, i*batchSize, i*batchSize + batchSize - 1);
             int index = range.indexOf(o);
@@ -193,12 +205,14 @@ public class RedissonList<V> implements List<V> {
             return -1;
         }
 
-        int to = Math.max(size()/100, 1);
+        int size = size();
+        int to = div(size, batchSize);
         for (int i = 1; i <= to; i++) {
-            List<Object> range = connection.lrange(name, -i*100, 100);
-            int index = range.indexOf(o);
+            int startIndex = -i*batchSize;
+            List<Object> range = connection.lrange(name, startIndex, size - (i-1)*batchSize);
+            int index = range.lastIndexOf(o);
             if (index != -1) {
-                return index + i*100;
+                return Math.max(size + startIndex, 0) + index;
             }
         }
 
@@ -283,6 +297,13 @@ public class RedissonList<V> implements List<V> {
 
     @Override
     public List<V> subList(int fromIndex, int toIndex) {
+        int size = size();
+        if (fromIndex < 0 || toIndex > size) {
+            throw new IndexOutOfBoundsException("fromIndex: " + fromIndex + " toIndex: " + toIndex + " size: " + size);
+        }
+        if (fromIndex > toIndex) {
+            throw new IllegalArgumentException("fromIndex: " + fromIndex + " toIndex: " + toIndex);
+        }
         return (List<V>) connection.lrange(name, fromIndex, toIndex - 1);
     }
 
diff --git a/src/test/java/org/redisson/RedissonListTest.java b/src/test/java/org/redisson/RedissonListTest.java
index 26efcda58..61d50c39d 100644
--- a/src/test/java/org/redisson/RedissonListTest.java
+++ b/src/test/java/org/redisson/RedissonListTest.java
@@ -11,6 +11,89 @@ import org.junit.Test;
 
 public class RedissonListTest {
 
+    @Test
+    public void testLastIndexOf2() {
+        Redisson redisson = Redisson.create();
+        List<Integer> list = redisson.getList("list");
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(4);
+        list.add(5);
+        list.add(0);
+        list.add(7);
+        list.add(8);
+        list.add(0);
+        list.add(10);
+
+        int index = list.lastIndexOf(3);
+        Assert.assertEquals(2, index);
+
+        clear(list);
+    }
+
+    @Test
+    public void testLastIndexOf1() {
+        Redisson redisson = Redisson.create();
+        List<Integer> list = redisson.getList("list");
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(4);
+        list.add(5);
+        list.add(3);
+        list.add(7);
+        list.add(8);
+        list.add(0);
+        list.add(10);
+
+        int index = list.lastIndexOf(3);
+        Assert.assertEquals(5, index);
+
+        clear(list);
+    }
+
+    @Test
+    public void testLastIndexOf() {
+        Redisson redisson = Redisson.create();
+        List<Integer> list = redisson.getList("list");
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(4);
+        list.add(5);
+        list.add(3);
+        list.add(7);
+        list.add(8);
+        list.add(3);
+        list.add(10);
+
+        int index = list.lastIndexOf(3);
+        Assert.assertEquals(8, index);
+
+        clear(list);
+    }
+
+    @Test
+    public void testLastIndexOfList() {
+        List<Integer> list = new LinkedList<Integer>();
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(4);
+        list.add(5);
+        list.add(3);
+        list.add(7);
+        list.add(8);
+        list.add(3);
+        list.add(10);
+
+        int index = list.lastIndexOf(3);
+        Assert.assertEquals(8, index);
+
+        clear(list);
+    }
+
     @Test
     public void testSubListList() {
         List<Integer> list = new LinkedList<Integer>();