Пример #1
0
class TestDiscreteDimension(unittest.TestCase):
    def setUp(self):
        self.just_one = DiscreteDimension(name='x', min=1, max=1)
        self.one_two = DiscreteDimension(name='x', min=1, max=2)
        self.one_two_three = DiscreteDimension(name='x', min=1, max=3)
        self.one_to_hundred = DiscreteDimension(name='x', min=1, max=100)
        #self.even_one_to_hundred = DiscreteDimension(name='x', min=2, max=100, stride=2)
        #self.odd_one_to_hundred = DiscreteDimension(name='x', min=1, max=99, stride=2)
        #self.divisible_by_three_one_to_hundred = DiscreteDimension(name='x', min=3, max=99, stride=3)
        #self.divisible_by_six_one_to_hundred = DiscreteDimension(name='x', min=6, max=96, stride=6)

        self.all_dims = [
            self.just_one,
            self.one_two,
            self.one_two_three,
            self.one_to_hundred,
            #self.even_one_to_hundred,
            #self.odd_one_to_hundred,
            #self.divisible_by_three_one_to_hundred,
            #self.divisible_by_six_one_to_hundred
        ]

    def test_string_representation(self):
        self.assertTrue(str(self.just_one) == "x: {1}")
        self.assertTrue(str(self.one_two) == "x: {1, 2}")
        self.assertTrue(str(self.one_two_three) == "x: {1, 2, 3}")
        self.assertTrue(str(self.one_to_hundred) == "x: {1, 2, 3, ... , 100}")
        #self.assertTrue(str(self.even_one_to_hundred) == "x: {2, 2 + 2, ... , 100}")
        #self.assertTrue(str(self.odd_one_to_hundred) == "x: {1, 1 + 2, ... , 99}")
        #self.assertTrue(str(self.divisible_by_three_one_to_hundred) == "x: {3, 3 + 3, ... , 99}")

    def test_point_containment(self):
        self.assertTrue(1 in self.just_one)

        #self.assertTrue(2 in self.even_one_to_hundred)
        #self.assertTrue(20 in self.even_one_to_hundred)
        #self.assertTrue(100 in self.even_one_to_hundred)
        #self.assertTrue(7 not in self.even_one_to_hundred)
        #self.assertTrue(-1 not in self.even_one_to_hundred)
        #self.assertTrue(102 not in self.even_one_to_hundred)

        #self.assertTrue(3 in self.divisible_by_three_one_to_hundred)
        #self.assertTrue(66 in self.divisible_by_three_one_to_hundred)
        #self.assertTrue(99 in self.divisible_by_three_one_to_hundred)

    def test_discrete_dimension_containment(self):
        self.assertTrue(self.just_one in self.one_two)
        self.assertTrue(self.just_one in self.one_two_three)
        #self.assertTrue(self.even_one_to_hundred in self.one_to_hundred)
        #self.assertTrue(self.odd_one_to_hundred in self.one_to_hundred)
        #self.assertTrue(self.divisible_by_three_one_to_hundred in self.one_to_hundred)
        #self.assertTrue(self.divisible_by_six_one_to_hundred in self.divisible_by_three_one_to_hundred)
        #self.assertTrue(self.divisible_by_six_one_to_hundred in self.even_one_to_hundred)
        #self.assertTrue(self.divisible_by_three_one_to_hundred not in self.odd_one_to_hundred)
        #self.assertTrue(self.divisible_by_three_one_to_hundred not in self.even_one_to_hundred)

    def test_discrete_dimension_set_operations(self):
        #self.assertTrue(self.divisible_by_three_one_to_hundred in self.even_one_to_hundred.union(self.odd_one_to_hundred))
        #self.assertTrue(self.divisible_by_six_one_to_hundred in self.divisible_by_three_one_to_hundred.intersection(self.even_one_to_hundred))
        self.assertTrue(self.just_one not in self.one_two_three - self.one_two)
        #self.assertTrue(self.divisible_by_six_one_to_hundred not in self.one_to_hundred - self.even_one_to_hundred)
        self.assertTrue(1 in self.just_one.intersection(
            self.one_two).intersection(self.one_two_three))
        self.assertTrue(1 not in self.just_one - self.just_one)
        #self.assertTrue(42 not in self.even_one_to_hundred.intersection(self.odd_one_to_hundred))
        self.assertTrue(42 not in self.just_one.union(self.just_one))

    def test_arbitrary_composition_of_discrete_dimensions(self):

        random.seed(1)
        for k in range(1, 10):
            # let's do random mixes of our dimensions and make sure they behave sanely
            unions = random.choices(self.all_dims, k=k)
            diffs = random.choices(self.all_dims, k=k)
            intersects = random.choices(self.all_dims, k=k)

            # let's put together the resulting set
            resulting_set = unions[0]
            for i in range(k):
                resulting_set = resulting_set.union(unions[i])
                resulting_set = resulting_set.difference(diffs[i])
                resulting_set = resulting_set.intersection(intersects[i])

            # now let's iterate over values in unions and make sure they belong
            for union in unions:
                for value in union.linspace():
                    # let's see if it should belong to the resulting_set
                    should_belong = False
                    for j in range(k):
                        should_belong = should_belong or (value in unions[j])
                        should_belong = should_belong and (value
                                                           not in diffs[j])
                        should_belong = should_belong and (value
                                                           in intersects[j])

                    if not should_belong == (value in resulting_set):
                        self.assertTrue(False)

    def test_random(self):
        self.assertTrue(self.just_one.random() in self.just_one)
        for _ in range(10):
            self.assertTrue(self.one_two.random() in self.one_two_three)
Пример #2
0
class TestDiscreteDimension:
    def setup_method(self, method):
        self.just_one = DiscreteDimension(name='x', min=1, max=1)
        self.one_two = DiscreteDimension(name='x', min=1, max=2)
        self.one_two_three = DiscreteDimension(name='x', min=1, max=3)
        self.one_two_three_four = DiscreteDimension(name='x', min=1, max=4)
        self.one_to_hundred = DiscreteDimension(name='x', min=1, max=100)

        self.all_dims = [
            self.just_one,
            self.one_two,
            self.one_two_three,
            self.one_two_three_four,
            self.one_to_hundred,
        ]

    def test_string_representation(self):
        assert str(self.just_one) == "x: {1}"
        assert str(self.one_two) == "x: {1, 2}"
        assert str(self.one_two_three) == "x: {1, 2, 3}"
        assert str(self.one_two_three_four) == "x: {1, 2, ... , 4}"
        assert str(self.one_to_hundred) == "x: {1, 2, ... , 100}"

    def test_point_containment(self):
        assert 1 in self.just_one

    def test_discrete_dimension_containment(self):
        assert self.just_one in self.one_two
        assert self.just_one in self.one_two_three

    def test_discrete_dimension_set_operations(self):

        assert self.just_one not in self.one_two_three - self.one_two
        assert 1 in self.just_one.intersection(self.one_two).intersection(
            self.one_two_three)
        assert 1 not in self.just_one - self.just_one
        assert 42 not in self.just_one.union(self.just_one)

    def test_arbitrary_composition_of_discrete_dimensions(self):

        random.seed(1)
        for k in range(1, 10):
            # let's do random mixes of our dimensions and make sure they behave sanely
            unions = random.choices(self.all_dims, k=k)
            diffs = random.choices(self.all_dims, k=k)
            intersects = random.choices(self.all_dims, k=k)

            # let's put together the resulting set
            resulting_set = unions[0]
            for i in range(k):
                resulting_set = resulting_set.union(unions[i])
                resulting_set = resulting_set.difference(diffs[i])
                resulting_set = resulting_set.intersection(intersects[i])

            # now let's iterate over values in unions and make sure they belong
            for union in unions:
                for value in union.linspace():
                    # let's see if it should belong to the resulting_set
                    should_belong = False
                    for j in range(k):
                        should_belong = should_belong or (value in unions[j])
                        should_belong = should_belong and (value
                                                           not in diffs[j])
                        should_belong = should_belong and (value
                                                           in intersects[j])

                    if not should_belong == (value in resulting_set):
                        assert False

    def test_random(self):
        assert self.just_one.random() in self.just_one
        for _ in range(10):
            assert self.one_two.random() in self.one_two_three