def test_copy_indexable_fields_to(self): # Populate a destination array with erroneous data to ensure it is overwritten destination = Array(Object(None), 5) for i in range(4): destination.set_indexable_field(i, "foo") self.array._copy_indexable_fields_to(destination) self.assertEqual(self.array, destination)
def new_array_with_length(self, length): # Allocate a new array and set its class to be the array class result = Array(self._nilObject, length) result.set_class(self._arrayClass) # Return the freshly allocated array return result
def __init__(self, nilObject, num_elements, method, context, previous_frame): Array.__init__(self, nilObject, num_elements) self._stack_pointer = 0 self._bytecode_index = 0 self._local_offset = 0 self._method = method self._context = context self._previous_frame = previous_frame if previous_frame else nilObject
def new_array_with_length(self, length): # Allocate a new array and set its class to be the array class result = Array(self._nilObject) result.set_class(self._arrayClass) # Set the number of indexable fields to the given value (length) result.set_number_of_indexable_fields_and_clear(length, self._nilObject) # Return the freshly allocated array return result
def setUp(self): # Universe definition self.universe = Universe() # Array definition, 5 strings "bar" self.array = Array(Object(None), 5) for i in range(4): self.array.set_indexable_field(i, "bar") # Extended array definition, 5 strings "bar" followed by a string "foo" self.extended_array = Array(Object(None), 6) for i in range(4): self.extended_array.set_indexable_field(i, "bar") self.extended_array.set_indexable_field(5, "foo")
def __init__(self, nilObject, num_literals, num_locals, max_stack_elements, num_bytecodes, signature): Array.__init__(self, nilObject, num_literals) # Set the number of bytecodes in this method self._bytecodes = array('b', [0] * num_bytecodes) self._inline_cache_class = [None] * num_bytecodes self._inline_cache_invokable = [None] * num_bytecodes self._number_of_locals = num_locals self._maximum_number_of_stack_elements = max_stack_elements self._set_signature(signature)
def test_empty_to_bool(self): arr = Array.from_size(1) self.assertIsInstance(arr._strategy, _EmptyStrategy) arr.set_indexable_field(0, trueObject) self.assertIsInstance(arr._strategy, _BoolStrategy) self.assertEqual(trueObject, arr.get_indexable_field(0))
def test_empty_to_obj(self): arr = Array.from_size(1) self.assertIsInstance(arr._strategy, _EmptyStrategy) arr.set_indexable_field(0, arr) self.assertIsInstance(arr._strategy, _ObjectStrategy) self.assertIs(arr, arr.get_indexable_field(0))
def test_empty_to_int(self): arr = Array.from_size(1) self.assertIsInstance(arr._strategy, _EmptyStrategy) int_obj = Integer(42) arr.set_indexable_field(0, int_obj) self.assertIsInstance(arr._strategy, _LongStrategy) self.assertEqual(42, arr.get_indexable_field(0).get_embedded_integer())
def test_copy_and_extend_partially_empty(self): arr = Array.from_size(3) int_obj = Integer(42) arr.set_indexable_field(0, int_obj) self.assertIsInstance(arr._strategy, _PartiallyEmptyStrategy) new_arr = arr.copy_and_extend_with(int_obj) self.assertIsNot(arr, new_arr) self.assertEqual(4, new_arr.get_number_of_indexable_fields()) self.assertIsInstance(new_arr._strategy, _PartiallyEmptyStrategy)
def invoke(self, receiver, arguments, meta_level): if meta_level: return self._som_node.invoke(receiver, arguments, meta_level) # Falta hacer llegar el frame environment = receiver.get_meta_object_environment() # No esta definido o es Nil if environment is None or not isinstance(environment, Object): return self._som_node.invoke(receiver, arguments, meta_level) method = self._lookup_node.lookup_meta_invokable(environment) if method is None: # El mate enviroment no define el methodo correspondiente a este nodo return self._som_node.invoke(receiver, arguments, meta_level) return method.invoke(receiver, [self._som_node.get_method(), Array.from_objects(arguments), environment], True)
def invoke(self, receiver, arguments, meta_level): if meta_level: return self._som_node.invoke(receiver, arguments, meta_level) # Falta hacer llegar el frame environment = receiver.get_meta_object_environment() # No esta definido o es Nil if environment is None or not isinstance(environment, Object): return self._som_node.invoke(receiver, arguments, meta_level) method = self._lookup_node.lookup_meta_invokable(environment) if method is None: # El mate enviroment no define el methodo correspondiente a este nodo return self._som_node.invoke(receiver, arguments, meta_level) return method.invoke(receiver, [ self._som_node.get_method(), Array.from_objects(arguments), environment ], True)
class ArrayTestCase(unittest.TestCase): def setUp(self): # Universe definition self.universe = Universe() # Array definition, 5 strings "bar" self.array = Array(Object(None), 5) for i in range(4): self.array.set_indexable_field(i, "bar") # Extended array definition, 5 strings "bar" followed by a string "foo" self.extended_array = Array(Object(None), 6) for i in range(4): self.extended_array.set_indexable_field(i, "bar") self.extended_array.set_indexable_field(5, "foo") def test_get_set_indexable_field(self): self.array.set_indexable_field(1, "foo") self.assertEqual("foo", self.array.get_indexable_field(1)) def test_get_number_of_indexable_fields(self): self.assertEqual(5, self.array.get_number_of_indexable_fields()) def test_copy_and_extend_with(self): extended_array = self.array.copy_and_extend_with("foo", self.universe) self.assertEqual(self.extended_array, extended_array) def test_copy_indexable_fields_to(self): # Populate a destination array with erroneous data to ensure it is overwritten destination = Array(Object(None), 5) for i in range(4): destination.set_indexable_field(i, "foo") self.array._copy_indexable_fields_to(destination) self.assertEqual(self.array, destination)
def test_empty_array(self): arr = Array.from_size(0) self.assertIsInstance(arr._strategy, _EmptyStrategy)
def _to(ivkbl, rcvr, args, meta_level): assert isinstance(rcvr, Integer) arg = args[0] assert isinstance(arg, Integer) return Array.from_integers(range(rcvr.get_embedded_integer(), arg.get_embedded_integer() + 1))
def new_array_with_strings(self, strings): # Allocate a new array with the same length as the string array values = [self.new_string(s) for s in strings] return Array.from_objects(values)
def new_array_from_list(self, values): make_sure_not_resized(values) return Array.from_values(values)
def new_array_with_length(self, length): return Array(self.nilObject, length)
def _to(ivkbl, rcvr, args): assert isinstance(rcvr, Integer) arg = args[0] assert isinstance(arg, Integer) return Array.from_integers(range(rcvr.get_embedded_integer(), arg.get_embedded_integer() + 1))
def new_array_with_length(self, length): return Array.from_size(length)
def new_array_from_list(self, values): make_sure_not_resized(values) return Array(self.nilObject, 0, values)