Ejemplo n.º 1
0
  def testRepeatedScalarFieldSortArguments(self):
    """Check sorting a scalar field using list.sort() arguments."""
    message = unittest_pb2.TestAllTypes()

    abs_cmp = lambda a, b: cmp(abs(a), abs(b))
    message.repeated_int32.append(-3)
    message.repeated_int32.append(-2)
    message.repeated_int32.append(-1)
    message.repeated_int32.sort(key=abs)
    self.assertEqual(list(message.repeated_int32), [-1, -2, -3])
    message.repeated_int32.sort(key=abs, reverse=True)
    self.assertEqual(list(message.repeated_int32), [-3, -2, -1])
    message.repeated_int32.sort(sort_function=abs_cmp)
    self.assertEqual(list(message.repeated_int32), [-1, -2, -3])
    message.repeated_int32.sort(cmp=abs_cmp, reverse=True)
    self.assertEqual(list(message.repeated_int32), [-3, -2, -1])

    len_cmp = lambda a, b: cmp(len(a), len(b))
    message.repeated_string.append('aaa')
    message.repeated_string.append('bb')
    message.repeated_string.append('c')
    message.repeated_string.sort(key=len)
    self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa'])
    message.repeated_string.sort(key=len, reverse=True)
    self.assertEqual(list(message.repeated_string), ['aaa', 'bb', 'c'])
    message.repeated_string.sort(sort_function=len_cmp)
    self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa'])
    message.repeated_string.sort(cmp=len_cmp, reverse=True)
    self.assertEqual(list(message.repeated_string), ['aaa', 'bb', 'c'])
Ejemplo n.º 2
0
    def testRepeatedScalarFieldSortArguments(self):
        """Check sorting a scalar field using list.sort() arguments."""
        message = unittest_pb2.TestAllTypes()

        abs_cmp = lambda a, b: cmp(abs(a), abs(b))
        message.repeated_int32.append(-3)
        message.repeated_int32.append(-2)
        message.repeated_int32.append(-1)
        message.repeated_int32.sort(key=abs)
        self.assertEqual(list(message.repeated_int32), [-1, -2, -3])
        message.repeated_int32.sort(key=abs, reverse=True)
        self.assertEqual(list(message.repeated_int32), [-3, -2, -1])
        message.repeated_int32.sort(sort_function=abs_cmp)
        self.assertEqual(list(message.repeated_int32), [-1, -2, -3])
        message.repeated_int32.sort(cmp=abs_cmp, reverse=True)
        self.assertEqual(list(message.repeated_int32), [-3, -2, -1])

        len_cmp = lambda a, b: cmp(len(a), len(b))
        message.repeated_string.append('aaa')
        message.repeated_string.append('bb')
        message.repeated_string.append('c')
        message.repeated_string.sort(key=len)
        self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa'])
        message.repeated_string.sort(key=len, reverse=True)
        self.assertEqual(list(message.repeated_string), ['aaa', 'bb', 'c'])
        message.repeated_string.sort(sort_function=len_cmp)
        self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa'])
        message.repeated_string.sort(cmp=len_cmp, reverse=True)
        self.assertEqual(list(message.repeated_string), ['aaa', 'bb', 'c'])
Ejemplo n.º 3
0
  def testSortingRepeatedScalarFieldsCustomComparator(self):
    """Check some different types with custom comparator."""
    message = unittest_pb2.TestAllTypes()

    message.repeated_int32.append(-3)
    message.repeated_int32.append(-2)
    message.repeated_int32.append(-1)
    message.repeated_int32.sort(lambda x,y: cmp(abs(x), abs(y)))
    self.assertEqual(message.repeated_int32[0], -1)
    self.assertEqual(message.repeated_int32[1], -2)
    self.assertEqual(message.repeated_int32[2], -3)

    message.repeated_string.append('aaa')
    message.repeated_string.append('bb')
    message.repeated_string.append('c')
    message.repeated_string.sort(lambda x,y: cmp(len(x), len(y)))
    self.assertEqual(message.repeated_string[0], 'c')
    self.assertEqual(message.repeated_string[1], 'bb')
    self.assertEqual(message.repeated_string[2], 'aaa')
Ejemplo n.º 4
0
    def testSortingRepeatedScalarFieldsCustomComparator(self):
        """Check some different types with custom comparator."""
        message = unittest_pb2.TestAllTypes()

        message.repeated_int32.append(-3)
        message.repeated_int32.append(-2)
        message.repeated_int32.append(-1)
        message.repeated_int32.sort(lambda x, y: cmp(abs(x), abs(y)))
        self.assertEqual(message.repeated_int32[0], -1)
        self.assertEqual(message.repeated_int32[1], -2)
        self.assertEqual(message.repeated_int32[2], -3)

        message.repeated_string.append('aaa')
        message.repeated_string.append('bb')
        message.repeated_string.append('c')
        message.repeated_string.sort(lambda x, y: cmp(len(x), len(y)))
        self.assertEqual(message.repeated_string[0], 'c')
        self.assertEqual(message.repeated_string[1], 'bb')
        self.assertEqual(message.repeated_string[2], 'aaa')
Ejemplo n.º 5
0
  def testSortingRepeatedCompositeFieldsCustomComparator(self):
    """Check passing a custom comparator to sort a repeated composite field."""
    message = unittest_pb2.TestAllTypes()

    message.repeated_nested_message.add().bb = 1
    message.repeated_nested_message.add().bb = 3
    message.repeated_nested_message.add().bb = 2
    message.repeated_nested_message.add().bb = 6
    message.repeated_nested_message.add().bb = 5
    message.repeated_nested_message.add().bb = 4
    message.repeated_nested_message.sort(lambda x,y: cmp(x.bb, y.bb))
    self.assertEqual(message.repeated_nested_message[0].bb, 1)
    self.assertEqual(message.repeated_nested_message[1].bb, 2)
    self.assertEqual(message.repeated_nested_message[2].bb, 3)
    self.assertEqual(message.repeated_nested_message[3].bb, 4)
    self.assertEqual(message.repeated_nested_message[4].bb, 5)
    self.assertEqual(message.repeated_nested_message[5].bb, 6)
Ejemplo n.º 6
0
    def testSortingRepeatedCompositeFieldsCustomComparator(self):
        """Check passing a custom comparator to sort a repeated composite field."""
        message = unittest_pb2.TestAllTypes()

        message.repeated_nested_message.add().bb = 1
        message.repeated_nested_message.add().bb = 3
        message.repeated_nested_message.add().bb = 2
        message.repeated_nested_message.add().bb = 6
        message.repeated_nested_message.add().bb = 5
        message.repeated_nested_message.add().bb = 4
        message.repeated_nested_message.sort(lambda x, y: cmp(x.bb, y.bb))
        self.assertEqual(message.repeated_nested_message[0].bb, 1)
        self.assertEqual(message.repeated_nested_message[1].bb, 2)
        self.assertEqual(message.repeated_nested_message[2].bb, 3)
        self.assertEqual(message.repeated_nested_message[3].bb, 4)
        self.assertEqual(message.repeated_nested_message[4].bb, 5)
        self.assertEqual(message.repeated_nested_message[5].bb, 6)
Ejemplo n.º 7
0
  def testRepeatedCompositeFieldSortArguments(self):
    """Check sorting a repeated composite field using list.sort() arguments."""
    message = unittest_pb2.TestAllTypes()

    get_bb = operator.attrgetter('bb')
    cmp_bb = lambda a, b: cmp(a.bb, b.bb)
    message.repeated_nested_message.add().bb = 1
    message.repeated_nested_message.add().bb = 3
    message.repeated_nested_message.add().bb = 2
    message.repeated_nested_message.add().bb = 6
    message.repeated_nested_message.add().bb = 5
    message.repeated_nested_message.add().bb = 4
    message.repeated_nested_message.sort(key=get_bb)
    self.assertEqual([k.bb for k in message.repeated_nested_message],
                     [1, 2, 3, 4, 5, 6])
    message.repeated_nested_message.sort(key=get_bb, reverse=True)
    self.assertEqual([k.bb for k in message.repeated_nested_message],
                     [6, 5, 4, 3, 2, 1])
    message.repeated_nested_message.sort(sort_function=cmp_bb)
    self.assertEqual([k.bb for k in message.repeated_nested_message],
                     [1, 2, 3, 4, 5, 6])
    message.repeated_nested_message.sort(cmp=cmp_bb, reverse=True)
    self.assertEqual([k.bb for k in message.repeated_nested_message],
                     [6, 5, 4, 3, 2, 1])
Ejemplo n.º 8
0
    def testRepeatedCompositeFieldSortArguments(self):
        """Check sorting a repeated composite field using list.sort() arguments."""
        message = unittest_pb2.TestAllTypes()

        get_bb = operator.attrgetter('bb')
        cmp_bb = lambda a, b: cmp(a.bb, b.bb)
        message.repeated_nested_message.add().bb = 1
        message.repeated_nested_message.add().bb = 3
        message.repeated_nested_message.add().bb = 2
        message.repeated_nested_message.add().bb = 6
        message.repeated_nested_message.add().bb = 5
        message.repeated_nested_message.add().bb = 4
        message.repeated_nested_message.sort(key=get_bb)
        self.assertEqual([k.bb for k in message.repeated_nested_message],
                         [1, 2, 3, 4, 5, 6])
        message.repeated_nested_message.sort(key=get_bb, reverse=True)
        self.assertEqual([k.bb for k in message.repeated_nested_message],
                         [6, 5, 4, 3, 2, 1])
        message.repeated_nested_message.sort(sort_function=cmp_bb)
        self.assertEqual([k.bb for k in message.repeated_nested_message],
                         [1, 2, 3, 4, 5, 6])
        message.repeated_nested_message.sort(cmp=cmp_bb, reverse=True)
        self.assertEqual([k.bb for k in message.repeated_nested_message],
                         [6, 5, 4, 3, 2, 1])