class TestCase(unittest.TestCase):
    def assertFormatMatches(self, format_str, data_type, native, memory_le, memory_be):
        fd = pfg.describe(format_str)
        self.assertEqual(data_type, fd.data_type)
        self.assertEqual(native, fd.native)
        self.assertEqual(memory_le, fd.memory_le)
        self.assertEqual(memory_be, fd.memory_be)

    def assertFormatMatchesUnorm(self, format_str, native, memory_le, memory_be):
        self.assertFormatMatches(format_str, "UNORM", native, memory_le, memory_be)

    def assertFormatIsUnknown(self, format_str):
        self.assertIsNone(pfg.describe(format_str))

    def assertFindCompatibleMatches(self, format_str, family_str,
                                    everywhere, little_endian, big_endian,
                                    treat_x_as_a=False,
                                    treat_srgb_as_unorm=False,
                                    ignore_data_types=False):
        compatibility = pfg.find_compatible(
            format_str, family_str,
            treat_x_as_a=treat_x_as_a,
            treat_srgb_as_unorm=treat_srgb_as_unorm,
            ignore_data_types=ignore_data_types)
        # assertCountEqual checks for the existence of items regardless
        # of order (and has a misleading name...)
        self.assertCountEqual(everywhere, compatibility.everywhere)
        self.assertCountEqual(little_endian, compatibility.little_endian)
        self.assertCountEqual(big_endian, compatibility.big_endian)

    def assertHasDocumentationFor(self, family):
        documentation = pfg.document(family)
        self.assertEqual(util.read_documentation(family + ".md"), documentation) def R(m,l): return util.component_bits("R", m, l) def G(m,l): return util.component_bits("G", m, l) def B(m,l): return util.component_bits("B", m, l) def A(m,l): return util.component_bits("A", m, l) def X(m,l): return util.component_bits("X", m, l) def Rn(n,m,l): return util.component_bits("(R+%d)" % n, m, l) def Gn(n,m,l): return util.component_bits("(G+%d)" % n, m, l) def Bn(n,m,l): return util.component_bits("(B+%d)" % n, m, l) def An(n,m,l): return util.component_bits("(A+%d)" % n, m, l)
from .pfgtest import TestCase, R, G, B, A, X, Rn, Gn, Bn, An
# Copyright © 2017 Collabora Ltd.
#
# This file is part of pfg.
#
# pfg is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# pfg is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
# more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pfg. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#   Alexandros Frantzis <alexandros.frantzis@collabora.com>


class PixmanTest(TestCase):
    def test_32bpp_formats(self):
        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_b8g8r8a8",
            native = B(7, 0) + G(7, 0) + R(7, 0) + A(7, 0),
            memory_le = [A(7, 0), R(7, 0), G(7, 0), B(7, 0)],
            memory_be = [B(7, 0), G(7, 0), R(7, 0), A(7, 0)])

        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_x2r10g10b10",
            native = X(1, 0) + R(9, 0) + G(9, 0) + B(9, 0),
            memory_le = [
                B(7, 0),
                G(5, 0) + B(9, 8),
                R(3, 0) + G(9, 6),
                X(1, 0) + R(9, 4)],
            memory_be = [
                X(1, 0) + R(9, 4),
                R(3, 0) + G(9, 6),
                G(5, 0) + B(9, 8),
                B(7, 0)])

    def test_24bpp_formats(self):
        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_r8g8b8",
            native = R(7, 0) + G(7, 0) + B(7, 0),
            memory_le = [B(7, 0), G(7, 0), R(7, 0)],
            memory_be = [R(7, 0), G(7, 0), B(7, 0)])

    def test_16bpp_formats(self):
        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_r5g6b5",
            native = R(4, 0) + G(5, 0) + B(4, 0),
            memory_le = [G(2, 0) + B(4, 0), R(4, 0) + G(5, 3)],
            memory_be = [R(4, 0) + G(5, 3), G(2, 0) + B(4, 0)])

    def test_8bpp_formats(self):
        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_a8",
            native = A(7, 0),
            memory_le = [A(7, 0)],
            memory_be = [A(7, 0)])

        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_a2b2g2r2",
            native = A(1, 0) + B(1, 0) + G(1, 0) + R(1, 0),
            memory_le = [A(1, 0) + B(1, 0) + G(1, 0) + R(1, 0)],
            memory_be = [A(1, 0) + B(1, 0) + G(1, 0) + R(1, 0)])

    def test_4bpp_formats(self):
        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_a4",
            native = A(3, 0),
            memory_le = [An(1, 3, 0) + A(3, 0)],
            memory_be = [A(3, 0) + An(1, 3, 0)])

        self.assertFormatMatchesUnorm(
            format_str = "PIXMAN_a1r1g1b1",
            native = A(0, 0) + R(0, 0) + G(0, 0) + B(0, 0),
            memory_le = [
                An(1, 0, 0) + Rn(1, 0, 0) + Gn(1, 0, 0) + Bn(1, 0, 0) +
                A(0, 0) + R(0, 0) + G(0, 0) + B(0, 0)],
            memory_be = [
                A(0, 0) + R(0, 0) + G(0, 0) + B(0, 0) +

                An(1, 0, 0) + Rn(1, 0, 0) + Gn(1, 0, 0) + Bn(1, 0, 0)])