Skip to content

Commit

Permalink
add list.index() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stummjr committed Nov 2, 2016
1 parent d5a7393 commit 3c1df07
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
28 changes: 25 additions & 3 deletions python/common/org/python/types/List.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.python.types;

import org.Python;

import java.util.Collections;
import java.util.Comparator;

Expand Down Expand Up @@ -517,19 +518,40 @@ public org.python.Object extend(org.python.Object other) {
return org.python.types.NoneType.NONE;
}

private int toPositiveIndex(int index) {
if (index < 0) {
return this.value.size() + index;
}
return index;
}

@org.python.Method(
__doc__ = ""
__doc__ = "L.index(value, [start, [stop]]) -> integer -- return first index of value.\nRaises ValueError if the value is not present.",
args = {"item"},
default_args = {"start", "end"}
)
public org.python.Object index(org.python.Object item, org.python.Object start, org.python.Object end) {
if (start != null && !(start instanceof org.python.types.Int)) {
throw new org.python.exceptions.TypeError("list indices must be integers, not " + start.typeName());
}

if (end != null && !(end instanceof org.python.types.Int)) {
throw new org.python.exceptions.TypeError("list indices must be integers, not " + end.typeName());
}

throw new org.python.exceptions.NotImplementedError("list.index() has not been implemented.");
int iStart = 0, iEnd = this.value.size();
if (end != null) {
iEnd = toPositiveIndex(((Long) end.toJava()).intValue());
}
if (start != null) {
iStart = toPositiveIndex(((Long) start.toJava()).intValue());
}

for (int i = iStart; i < Math.min(iEnd, this.value.size()); i++) {
if (((org.python.types.Bool) this.value.get(i).__eq__(item)).value) {
return new org.python.types.Int(i);
}
}
throw new org.python.exceptions.ValueError(String.format("%d is not in list", ((org.python.types.Int)item).value));
}

@org.python.Method(
Expand Down
72 changes: 72 additions & 0 deletions tests/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,78 @@ def test_copy(self):
print(x[0] is y[0])
""")

def test_index(self):
self.assertCodeExecution("""
x = [1, 2, 3]
print(x.index(1))
""")

self.assertCodeExecution("""
x = [1, 2, 1]
print(x.index(1, 1))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(4, 0, len(x)))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(2, 1, 2))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(2, 0, 10))
""")

self.assertCodeExecution("""
x = [1, 2, 1]
print(x.index(1, 0, -2))
""")

self.assertCodeExecution("""
x = [1, 2, 1]
print(x.index(1, -3, -2))
""")

# cases for 'ValueError: not in list'
self.assertCodeExecution("""
x = [1, 2, 3]
print(x.index(4))
""")

self.assertCodeExecution("""
x = [1, 2, 1]
print(x.index(2, 0, 1))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(4, 0, 3))
""")

self.assertCodeExecution("""
x = [1, 2, 1]
print(x.index(3, 0, 10))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(2, 10, 20))
""")

self.assertCodeExecution("""
x = [1, 2, 3, 4]
print(x.index(2, 10, 0))
""")

self.assertCodeExecution("""
x = []
print(x.index(1, 0, 10))
""")


class UnaryListOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'list'
Expand Down

0 comments on commit 3c1df07

Please sign in to comment.