Skip to content

Commit

Permalink
Fixes #13 Remove compiler warnings and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Dev committed Jun 15, 2016
1 parent e0ac31c commit ee61aea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ This can be used to build a LRU cache. Usage is almost like a dict.
# Would print [(3, '3'), (5, '5'), (2, '2')]
print l.get_size()
# Would print 3
print l.has_key(5)
# Would print True
print 2 in l
# Would print True
l.get_stats()
# Would print (1, 0)
Expand Down
45 changes: 28 additions & 17 deletions lru.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,30 @@ lru_length(LRU *self)
}

static PyObject *
LRU_contains(LRU *self, PyObject *args)
LRU_contains_key(LRU *self, PyObject *key)
{
PyObject *key;
if (!PyArg_ParseTuple(args, "O", &key))
return NULL;

if (PyDict_Contains(self->dict, key)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}

static PyObject *
LRU_contains(LRU *self, PyObject *args)
{
PyObject *key;
if (!PyArg_ParseTuple(args, "O", &key))
return NULL;
return LRU_contains_key(self, key);
}

static int
LRU_seq_contains(LRU *self, PyObject *key)
{
return PyDict_Contains(self->dict, key);
}

static PyObject *
lru_subscript(LRU *self, register PyObject *key)
{
Expand Down Expand Up @@ -407,21 +418,21 @@ LRU_get_stats(LRU *self)

/* Hack to implement "key in lru" */
static PySequenceMethods lru_as_sequence = {
0, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
0, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
LRU_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
0, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
0, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
(objobjproc) LRU_seq_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};

static PyMethodDef LRU_methods[] = {
{"__contains__", (PyCFunction)LRU_contains, METH_O | METH_COEXIST,
PyDoc_STR("L.has_key(key) -> Check if key is there in L")},
{"__contains__", (PyCFunction)LRU_contains_key, METH_O | METH_COEXIST,
PyDoc_STR("L.__contains__(key) -> Check if key is there in L")},
{"keys", (PyCFunction)LRU_keys, METH_NOARGS,
PyDoc_STR("L.keys() -> list of L's keys in MRU order")},
{"values", (PyCFunction)LRU_values, METH_NOARGS,
Expand Down
8 changes: 8 additions & 0 deletions test/test_lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def test_access_within_size(self):
self.assertEqual(l[i], str(i))
self.assertEqual(l.get(i,None), str(i))

def test_contains(self):
for size in SIZES:
l = LRU(size)
for i in range(size):
l[i] = str(i)
for i in range(size):
self.assertTrue(i in l)

def test_access(self):
for size in SIZES:
l = LRU(size)
Expand Down

0 comments on commit ee61aea

Please sign in to comment.