File: test_java_list_delegate.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (181 lines) | stat: -rw-r--r-- 5,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from java.util import ArrayList, List, Vector

from copy import copy

import unittest
import test.test_support

class CollectionProxyTest(unittest.TestCase):
    def _perform_op(self, value, op_func):
        """
        Perform an operation

            value - the value to operate on
            op_func - the function that applies the operation to value

        Returns:
            the result of calling op_func, OR the exception that was raised in op_func
        """
        try:
            return op_func(value)
        except Exception, e:
            return type(e)

    def check_list(self, control, results, initial):
        for result in results:
            try:
                len(result)
            except:
                print result
            self.assertEquals(len(control), len(result), "%s is wrong for %s" % (type(result), initial))
            for pvalue, jvalue in zip(control, result):
                self.assertEquals(pvalue, jvalue)

    def _list_op_test(self, initial_value, op_func, check_value):
        """
        Tests a list operation

        Ensures that performing an operation on:
            - a python list
            - a java.util.List instance

        givens the same result in both cases
        """
        lists = [list(initial_value), ArrayList(initial_value), Vector(initial_value)]

        results = [self._perform_op(l, op_func) for l in lists]
        self.check_list(lists[0], lists[1:], initial_value)
        if check_value or not isinstance(results[0], list):
            for r in results[1:]:
                self.assertEquals(results[0], r)
        else:
            self.check_list(results[0], results[1:], initial_value)

    def test_get_integer(self):
        initial_value = range(0, 5)

        for i in xrange(-7, 7):
            self._list_op_test(initial_value, lambda xs: xs[i], True)

    def test_set_integer(self):
        initial_value = range(0, 5)

        def make_op_func(index):
            def _f(xs):
                xs[index] = 100
            return _f

        for i in xrange(-7, 7):
            self._list_op_test(initial_value, make_op_func(i), True)

    def test_set_slice(self):
        initial_value = range(0, 10)

        def make_op_func(i, j, k, v):
            def _f(xs):
                xs[i:j:k] = v
            return _f

        for i in xrange(-12, 12):
            for j in xrange(-12, 12):
                for k in xrange(-12, 12):
                    self._list_op_test(initial_value, make_op_func(i, j, k, []), True)
                    self._list_op_test(initial_value, make_op_func(i, j, k, range(0,2)), True)
                    self._list_op_test(initial_value, make_op_func(i, j, k, range(0,4)), True)
                    self._list_op_test(initial_value, make_op_func(i, j, k, xrange(0,2)), True)

    def test_del_integer(self):
        initial_value = range(0,5)

        def make_op_func(index):
            def _f(xs):
                del xs[index]
            return _f

        for i in xrange(-7, 7):
            self._list_op_test(initial_value, make_op_func(i), True)

    def test_del_slice(self):
        initial_value = range(0,10)

        def make_op_func(i, j, k):
            def _f(xs):
                del xs[i:j:k]
            return _f

        for i in xrange(-12, 12):
            for j in xrange(-12, 12):
                for k in xrange(-12, 12):
                    self._list_op_test(initial_value, make_op_func(i, j, k), True)

    def test_len(self):
        jlist = ArrayList()
        jlist.addAll(range(0, 10))

        self.assert_(len(jlist) == 10)

    def test_iter(self):
        jlist = ArrayList()
        jlist.addAll(range(0, 10))

        i = iter(jlist)

        x = list(i)

        self.assert_(x == range(0, 10))

    def test_override_len(self):
        class MyList (ArrayList):
            def __len__(self):
                return self.size() + 1;

        m = MyList()
        m.addAll(range(0,10))

        self.assert_(len(m) == 11)

    def test_override_iter(self):
        class MyList (ArrayList):
            def __iter__(self):
                return iter(self.subList(0, self.size() - 1));


        m = MyList()
        m.addAll(range(0,10))
        i = iter(m)
        x = list(i)

        self.assert_(x == range(0, 9))

    def test_override_getsetdelitem(self):
        # Create an ArrayList subclass that provides some silly overrides for get/set/del item
        class MyList (ArrayList):
            def __getitem__(self, key):
                return self.get(key) * 2;

            def __setitem__(self, key, value):
                return self.set(key, value * 2);

            def __delitem__(self, key):
                self.add(84)


        m = MyList()
        m.addAll(range(0,10))

        self.assert_(m[1] == 2)
        self.assert_(m.get(1) == 1)

        m[0] = 3
        self.assert_(m.get(0) == 6)
        self.assert_(m[0] == 12)

        del m[0]
        self.assert_(m.size() == 11)
        self.assert_(m.get(10) == 84)

def test_main():
    test.test_support.run_unittest(CollectionProxyTest)

if __name__ == "__main__":
    test_main()