#  Copyright 2020 Modelling, Simulation and Design Lab (MSDL) at
#  McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""Test the packaging types.

See Also:
    :mod:`pypdevsbbl.extra.packaging`
"""

import unittest
from pypdevsbbl.extra.packaging import Package, SizedPackage


class TestPackaging(unittest.TestCase):
    def setUp(self):
        self.group = [1, 2, 5, 7, 16, 3, 5, 9, 17, 29, 4, 1, 666, 0, -19, 22, 11, 13]

    def test_package(self):
        """Tests the :class:`pypdevsbbl.extra.packaging.Package` class."""
        package = Package()
        for n in range(15):
            for i in self.group:
                package.pack(i, 0, 0)
                assert not package.finished()
            assert package.contents == self.group
            assert package.unpack() == self.group
            assert len(package.contents) == 0

    def test_sized_package(self):
        """Tests the :class:`pypdevsbbl.extra.packaging.SizedPackage` class."""
        package = SizedPackage(0)
        for i in self.group:
            package.pack(i, 0, 0)
            if package.finished():
                break
        assert package.contents == self.group[:1]
        assert package.unpack() == self.group[:1]
        assert len(package.contents) == 0

        for n in range(1, 15):
            package = SizedPackage(n)
            for i in self.group:
                package.pack(i, 0, 0)
                if package.finished():
                    break
            assert package.contents == self.group[:n]
            assert package.unpack() == self.group[:n]
            assert len(package.contents) == 0

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