From ad7b48cd4ecf1a7ba5864823149e1b4e029602f2 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 26 Feb 2009 15:07:27 -0800 Subject: [PATCH] Test for indexing --- tests/run/index.pyx | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/run/index.pyx diff --git a/tests/run/index.pyx b/tests/run/index.pyx new file mode 100644 index 00000000..492598db --- /dev/null +++ b/tests/run/index.pyx @@ -0,0 +1,55 @@ +__doc__ = u""" +>>> index_tuple((1,1,2,3,5), 0) +1 +>>> index_tuple((1,1,2,3,5), 3) +3 +>>> index_tuple((1,1,2,3,5), -1) +5 +>>> index_tuple((1,1,2,3,5), 100) +Traceback (most recent call last): +... +IndexError: tuple index out of range + +>>> index_list([2,3,5,7,11,13,17,19], 0) +2 +>>> index_list([2,3,5,7,11,13,17,19], 5) +13 +>>> index_list([2,3,5,7,11,13,17,19], -1) +19 +>>> index_list([2,3,5,7,11,13,17,19], 100) +Traceback (most recent call last): +... +IndexError: list index out of range + +>>> index_object([2,3,5,7,11,13,17,19], 1) +3 +>>> index_object([2,3,5,7,11,13,17,19], -1) +19 +>>> index_object((1,1,2,3,5), 2) +2 +>>> index_object((1,1,2,3,5), -2) +3 +>>> index_object("abcdef...z", 0) +'a' +>>> index_object("abcdef...z", -1) +'z' +>>> index_object("abcdef...z", 100) +Traceback (most recent call last): +... +IndexError: string index out of range + +>>> index_object(100, 100) +Traceback (most recent call last): +... +TypeError: 'int' object is unsubscriptable +""" + + +def index_tuple(tuple t, int i): + return t[i] + +def index_list(list L, int i): + return L[i] + +def index_object(object o, int i): + return o[i] -- 2.26.2