“6f09801ef97d58ac9a2cc4962edbb812287a9df5”上不存在“projects/n9705/imports.yml”
test_data_structures.py 3.2 KB
Newer Older
1
import unittest
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

from avocado.utils import data_structures


class TestDataStructures(unittest.TestCase):

    """
    Unit tests for avocado.utils.data_structures
    """

    def test_ordered_list_unique(self):
        """
        Verify only unique items are there and the order is kept
        """
        orig = [1, 5, unittest, 1, 3, 'a', 7, 7, None, 0, unittest]
        self.assertEqual(data_structures.ordered_list_unique(orig),
                         [1, 5, unittest, 3, 'a', 7, None, 0])

    def test_geometric_mean(self):
        """
        Verify the correct value is produced and it allows processing of long
        lists of values where some algorithm fails.
        """
        self.assertEqual(data_structures.geometric_mean(xrange(1, 180)),
                         67.1555819421869)

H
Harish 已提交
28 29 30 31 32 33 34 35 36
    def test_compare_matrices(self):
        """
        Verify the correct value is produced when comparing matrices.
        """
        # Note that first row contains header in first column, while the
        # second contains only values (for testing purposes)
        matrix1 = [["header", 51.7, 60], [1, 0, 0]]
        matrix2 = [["header", 57.2, 54], [2, 51, 0]]
        self.assertEqual(data_structures.compare_matrices(matrix1, matrix2),
L
Lucas Meneghel Rodrigues 已提交
37 38
                         ([["header", '+10.6383', -10.0],
                           ['+100', 'error_51/0', '.']], 3, 1, 5))
H
Harish 已提交
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
    def test_lazy_property(self):
        """
        Verify the value is initialized lazily with the correct value
        """
        class DummyClass(object):
            value = False

            @data_structures.LazyProperty
            def dummy_method(self):
                return not self.value

        item = DummyClass()
        self.assertNotIn('dummy_method', item.__dict__)
        self.assertEqual(item.dummy_method, True)
        self.assertIn('dummy_method', item.__dict__)

    def test_callback_register(self):
        """
        Checks CallbackRegister
        """
        class Log(object):
            msgs = []

            def error(self, *args, **kwargs):
                self.msgs.append((args, kwargs))

        def ret_arg(arg):
            return arg

        log = Log()
        register = data_structures.CallbackRegister("MyName", log)
        # Register few correct functions
        register.register(ret_arg, [True], {})
        register.register(ret_arg, [], {"arg": False})
        # Register few incorrect functions (incorrect number of arguments)
        register.register(ret_arg, [True, "incorrect_twice"], {})
        register.register(ret_arg, [True, "incorrect_twice"], {})
        register.register(ret_arg, [True, "incorrect_unique_only"], {}, True)
        # Register incorrect function, which is removed before cleanup
        register.register(ret_arg, [True, "incorrect_removed"], {})
        register.unregister(ret_arg, [True, "incorrect_removed"], {})
        # Run registered functions and check errors were produced accordingly
        register.run()
        self.assertEqual(len(log.msgs), 3)
        self.assertIn("incorrect_unique_only", str(log.msgs[0]))
        self.assertIn("incorrect_twice", str(log.msgs[1]))
        self.assertIn("incorrect_twice", str(log.msgs[2]))


if __name__ == "__main__":
    unittest.main()