예제 #1
0
    def test_append_raises(self):
        """Tests that "append" raises when given the wrong type."""
        unique_list = helpers.UniqueList(element_type=list)
        self.assertEqual(0, len(unique_list))
        self.assertEqual([], unique_list.list)

        with self.assertRaises(TypeError):
            # Since we passed element_type=list to the UniqueList constructor,
            # attempting to add any non-list should raise.
            unique_list.append(42)
예제 #2
0
 def __init__(self, collection=None):
     """Creates a new `DeferredTensorInputList` from a collection."""
     # We need to wrap _DeferredTensorInput around every element so that we have
     # __hash__ and __eq__, allowing us to use UniqueList (which is basically the
     # entire point of this class).
     if collection is not None:
         collection = [
             _DeferredTensorInput(element) for element in collection
         ]
     self._list = helpers.UniqueList(collection=collection,
                                     element_type=_DeferredTensorInput)
예제 #3
0
  def test_construct(self):
    """Tests the `UniqueList` constructor."""
    element1 = 1
    element2 = 2
    element3 = element1
    element4 = 4
    element5 = element4
    element6 = 6

    unique_list = helpers.UniqueList(
        [element1, element2, element3, element4, element5, element6])
    self.assertEqual(4, len(unique_list))
    self.assertEqual([element1, element2, element4, element6], unique_list.list)
예제 #4
0
  def test_radd(self):
    """Tests `UniqueList`'s "__radd__" method."""
    element1 = 1
    element2 = 2
    element3 = element1
    element4 = 4
    element5 = element4
    element6 = 6

    lhs = [element1, element2]
    rhs = [element3, element4, element5, element6]

    unique_list = helpers.UniqueList(rhs)
    self.assertEqual(3, len(unique_list))
    self.assertEqual([element1, element4, element6], unique_list.list)

    unique_list = lhs + unique_list
    self.assertEqual(4, len(unique_list))
    self.assertEqual([element1, element2, element4, element6], unique_list.list)