1cc1dc7a3Sopenharmony_ci# SPDX-License-Identifier: Apache-2.0
2cc1dc7a3Sopenharmony_ci# -----------------------------------------------------------------------------
3cc1dc7a3Sopenharmony_ci# Copyright 2020 Arm Limited
4cc1dc7a3Sopenharmony_ci#
5cc1dc7a3Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6cc1dc7a3Sopenharmony_ci# use this file except in compliance with the License. You may obtain a copy
7cc1dc7a3Sopenharmony_ci# of the License at:
8cc1dc7a3Sopenharmony_ci#
9cc1dc7a3Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10cc1dc7a3Sopenharmony_ci#
11cc1dc7a3Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12cc1dc7a3Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13cc1dc7a3Sopenharmony_ci# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14cc1dc7a3Sopenharmony_ci# License for the specific language governing permissions and limitations
15cc1dc7a3Sopenharmony_ci# under the License.
16cc1dc7a3Sopenharmony_ci# -----------------------------------------------------------------------------
17cc1dc7a3Sopenharmony_ci"""
18cc1dc7a3Sopenharmony_ciAn ASTC TestSet is comprised of a set of TestImages. Images are stored in a
19cc1dc7a3Sopenharmony_cistructured directory layout. This structure encodes important metadata about
20cc1dc7a3Sopenharmony_cieach image - such as color profile and data encoding - in the directory and
21cc1dc7a3Sopenharmony_cifile names used.
22cc1dc7a3Sopenharmony_ci
23cc1dc7a3Sopenharmony_ciTestSets are built by using reflection on a root directory to automatically
24cc1dc7a3Sopenharmony_cifind all of the test images that comprise the set.
25cc1dc7a3Sopenharmony_ci"""
26cc1dc7a3Sopenharmony_ci
27cc1dc7a3Sopenharmony_ciimport os
28cc1dc7a3Sopenharmony_ci
29cc1dc7a3Sopenharmony_cifrom testlib.image import TestImage
30cc1dc7a3Sopenharmony_ci
31cc1dc7a3Sopenharmony_ci
32cc1dc7a3Sopenharmony_ciclass TSetException(Exception):
33cc1dc7a3Sopenharmony_ci    """
34cc1dc7a3Sopenharmony_ci    Exception thrown for bad test set specification.
35cc1dc7a3Sopenharmony_ci    """
36cc1dc7a3Sopenharmony_ci
37cc1dc7a3Sopenharmony_ci
38cc1dc7a3Sopenharmony_ciclass TestSet():
39cc1dc7a3Sopenharmony_ci    """
40cc1dc7a3Sopenharmony_ci    Generate a list of images that are test candidates.
41cc1dc7a3Sopenharmony_ci
42cc1dc7a3Sopenharmony_ci    This reflection is built automatically based on a directory of images on
43cc1dc7a3Sopenharmony_ci    disk, provided that the images follow a standard structure.
44cc1dc7a3Sopenharmony_ci
45cc1dc7a3Sopenharmony_ci    Attributes:
46cc1dc7a3Sopenharmony_ci        name: The name of the test set.
47cc1dc7a3Sopenharmony_ci        tests: The list of TestImages forming the set.
48cc1dc7a3Sopenharmony_ci    """
49cc1dc7a3Sopenharmony_ci
50cc1dc7a3Sopenharmony_ci    def __init__(self, name, rootDir, profiles, formats, imageFilter=None):
51cc1dc7a3Sopenharmony_ci        """
52cc1dc7a3Sopenharmony_ci        Create a new TestSet through reflection.
53cc1dc7a3Sopenharmony_ci
54cc1dc7a3Sopenharmony_ci        Args:
55cc1dc7a3Sopenharmony_ci            name (str): The name of the test set.
56cc1dc7a3Sopenharmony_ci            rootDir (str): The root directory of the test set.
57cc1dc7a3Sopenharmony_ci            profiles (list(str)): The ASTC profiles to allow.
58cc1dc7a3Sopenharmony_ci            formats (list(str)): The image formats to allow.
59cc1dc7a3Sopenharmony_ci            imageFilter (str): The name of the image to include (for bug repo).
60cc1dc7a3Sopenharmony_ci
61cc1dc7a3Sopenharmony_ci        Raises:
62cc1dc7a3Sopenharmony_ci            TSetException: The specified TestSet could not be loaded.
63cc1dc7a3Sopenharmony_ci        """
64cc1dc7a3Sopenharmony_ci        self.name = name
65cc1dc7a3Sopenharmony_ci
66cc1dc7a3Sopenharmony_ci        if not os.path.exists(rootDir) and not os.path.isdir(rootDir):
67cc1dc7a3Sopenharmony_ci            raise TSetException("Bad test set root directory (%s)" % rootDir)
68cc1dc7a3Sopenharmony_ci
69cc1dc7a3Sopenharmony_ci        self.tests = []
70cc1dc7a3Sopenharmony_ci
71cc1dc7a3Sopenharmony_ci        for (dirPath, dirNames, fileNames) in os.walk(rootDir):
72cc1dc7a3Sopenharmony_ci            for fileName in fileNames:
73cc1dc7a3Sopenharmony_ci                # Only select image files
74cc1dc7a3Sopenharmony_ci                fileExt = os.path.splitext(fileName)[1]
75cc1dc7a3Sopenharmony_ci                if fileExt not in TestImage.TEST_EXTS:
76cc1dc7a3Sopenharmony_ci                    continue
77cc1dc7a3Sopenharmony_ci
78cc1dc7a3Sopenharmony_ci                # Create the TestImage for each file on disk
79cc1dc7a3Sopenharmony_ci                filePath = os.path.join(dirPath, fileName)
80cc1dc7a3Sopenharmony_ci                image = TestImage(filePath)
81cc1dc7a3Sopenharmony_ci
82cc1dc7a3Sopenharmony_ci                # Filter out the ones we don't want to allow
83cc1dc7a3Sopenharmony_ci                if image.colorProfile not in profiles:
84cc1dc7a3Sopenharmony_ci                    continue
85cc1dc7a3Sopenharmony_ci
86cc1dc7a3Sopenharmony_ci                if image.colorFormat not in formats:
87cc1dc7a3Sopenharmony_ci                    continue
88cc1dc7a3Sopenharmony_ci
89cc1dc7a3Sopenharmony_ci                if imageFilter and image.testFile != imageFilter:
90cc1dc7a3Sopenharmony_ci                    continue
91cc1dc7a3Sopenharmony_ci
92cc1dc7a3Sopenharmony_ci                self.tests.append((filePath, image))
93cc1dc7a3Sopenharmony_ci
94cc1dc7a3Sopenharmony_ci        # Sort the TestImages so they are in a stable order
95cc1dc7a3Sopenharmony_ci        self.tests.sort()
96cc1dc7a3Sopenharmony_ci        self.tests = [x[1] for x in self.tests]
97