mirror of https://github.com/kivy/pyjnius.git
unit tests for pass_by_reference
This commit is contained in:
parent
b838402897
commit
1e159689c1
|
@ -0,0 +1,32 @@
|
|||
package org.jnius;
|
||||
|
||||
public class VariablePassing {
|
||||
|
||||
private static void squareNumbers(int[] numbers) {
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = i * i;
|
||||
}
|
||||
}
|
||||
|
||||
public static void singleParamStatic(int[] numbers) {
|
||||
squareNumbers(numbers);
|
||||
}
|
||||
|
||||
public static void multipleParamsStatic(int[] numbers1, int[] numbers2, int[] numbers3, int[] numbers4) {
|
||||
squareNumbers(numbers1);
|
||||
squareNumbers(numbers2);
|
||||
squareNumbers(numbers3);
|
||||
squareNumbers(numbers4);
|
||||
}
|
||||
|
||||
public void singleParam(int[] numbers) {
|
||||
squareNumbers(numbers);
|
||||
}
|
||||
|
||||
public void multipleParams(int[] numbers1, int[] numbers2, int[] numbers3, int[] numbers4) {
|
||||
squareNumbers(numbers1);
|
||||
squareNumbers(numbers2);
|
||||
squareNumbers(numbers3);
|
||||
squareNumbers(numbers4);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
from __future__ import absolute_import
|
||||
import unittest
|
||||
from jnius import autoclass
|
||||
|
||||
class PassByReferenceOrValueTest(unittest.TestCase):
|
||||
|
||||
def _verify(self, numbers, changed):
|
||||
for i in range(len(numbers)):
|
||||
self.assertEqual(numbers[i], i * i if changed else i)
|
||||
|
||||
def _verify_all(self, numbers, changed):
|
||||
for n, c in zip(numbers, changed):
|
||||
self._verify(n, c)
|
||||
|
||||
def test_single_param(self):
|
||||
VariablePassing = autoclass('org.jnius.VariablePassing')
|
||||
variablePassing = VariablePassing()
|
||||
|
||||
# passed by reference (default), numbers should change
|
||||
numbers = list(range(10))
|
||||
VariablePassing.singleParamStatic(numbers)
|
||||
self._verify(numbers, True)
|
||||
|
||||
# passed by reference, numbers should change
|
||||
numbers = list(range(10))
|
||||
VariablePassing.singleParamStatic(numbers, pass_by_reference=True)
|
||||
self._verify(numbers, True)
|
||||
|
||||
# passed by value, numbers should not change
|
||||
numbers = list(range(10))
|
||||
VariablePassing.singleParamStatic(numbers, pass_by_reference=False)
|
||||
self._verify(numbers, False)
|
||||
|
||||
# passed by reference (default), numbers should change
|
||||
numbers = list(range(10))
|
||||
variablePassing.singleParam(numbers)
|
||||
self._verify(numbers, True)
|
||||
|
||||
# passed by reference, numbers should change
|
||||
numbers = list(range(10))
|
||||
variablePassing.singleParam(numbers, pass_by_reference=True)
|
||||
self._verify(numbers, True)
|
||||
|
||||
# passed by value, numbers should not change
|
||||
numbers = list(range(10))
|
||||
variablePassing.singleParam(numbers, pass_by_reference=False)
|
||||
self._verify(numbers, False)
|
||||
|
||||
def test_multiple_params(self):
|
||||
VariablePassing = autoclass('org.jnius.VariablePassing')
|
||||
variablePassing = VariablePassing()
|
||||
|
||||
# passed by reference (default), all numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
VariablePassing.multipleParamsStatic(*numbers)
|
||||
self._verify_all(numbers, [True] * 4)
|
||||
|
||||
# passed by reference, all numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=True)
|
||||
self._verify_all(numbers, [True] * 4)
|
||||
|
||||
# passed by value, no numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=False)
|
||||
self._verify_all(numbers, [False] * 4)
|
||||
|
||||
# only the first set of numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=[True, False])
|
||||
self._verify_all(numbers, [True, False, False, False])
|
||||
|
||||
# only the first set of numbers should not change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=[False, True])
|
||||
self._verify_all(numbers, [False, True, True, True])
|
||||
|
||||
# only the odd sets of numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
changed = (True, False, True, False)
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=changed)
|
||||
self._verify_all(numbers, changed)
|
||||
|
||||
# only the even sets of numbers should change
|
||||
numbers = [list(range(10)) for _ in range(4)]
|
||||
changed = (False, True, False, True)
|
||||
VariablePassing.multipleParamsStatic(*numbers, pass_by_reference=changed)
|
||||
self._verify_all(numbers, changed)
|
Loading…
Reference in New Issue