xref: /aosp_15_r20/external/toolchain-utils/bestflags/mock_task.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li# Copyright 2013 The ChromiumOS Authors
2*760c253cSXin Li# Use of this source code is governed by a BSD-style license that can be
3*760c253cSXin Li# found in the LICENSE file.
4*760c253cSXin Li"""This module defines the common mock tasks used by various unit tests.
5*760c253cSXin Li
6*760c253cSXin LiPart of the Chrome build flags optimization.
7*760c253cSXin Li"""
8*760c253cSXin Li
9*760c253cSXin Li__author__ = "[email protected] (Yuheng Long)"
10*760c253cSXin Li
11*760c253cSXin Li# Pick an integer at random.
12*760c253cSXin LiPOISONPILL = 975
13*760c253cSXin Li
14*760c253cSXin Li
15*760c253cSXin Liclass MockTask(object):
16*760c253cSXin Li    """This class emulates an actual task.
17*760c253cSXin Li
18*760c253cSXin Li    It does not do the actual work, but simply returns the result as given when
19*760c253cSXin Li    this task is constructed.
20*760c253cSXin Li    """
21*760c253cSXin Li
22*760c253cSXin Li    def __init__(self, stage, identifier, cost=0):
23*760c253cSXin Li        """Set up the results for this task.
24*760c253cSXin Li
25*760c253cSXin Li        Args:
26*760c253cSXin Li          stage: the stage of this test is in.
27*760c253cSXin Li          identifier: the identifier of this task.
28*760c253cSXin Li          cost: the mock cost of this task.
29*760c253cSXin Li
30*760c253cSXin Li          The _cost field stored the cost. Once this task is performed, i.e., by
31*760c253cSXin Li          calling the work method or by setting the result from other task, the
32*760c253cSXin Li          _cost field will have this cost. The stage field verifies that the module
33*760c253cSXin Li          being tested and the unitest are in the same stage. If the unitest does
34*760c253cSXin Li          not care about cost of this task, the cost parameter should be leaved
35*760c253cSXin Li          blank.
36*760c253cSXin Li        """
37*760c253cSXin Li
38*760c253cSXin Li        self._identifier = identifier
39*760c253cSXin Li        self._cost = cost
40*760c253cSXin Li        self._stage = stage
41*760c253cSXin Li
42*760c253cSXin Li        # Indicate that this method has not been performed yet.
43*760c253cSXin Li        self._performed = False
44*760c253cSXin Li
45*760c253cSXin Li    def __eq__(self, other):
46*760c253cSXin Li        if isinstance(other, MockTask):
47*760c253cSXin Li            return self._identifier == other.GetIdentifier(
48*760c253cSXin Li                self._stage
49*760c253cSXin Li            ) and self._cost == other.GetResult(self._stage)
50*760c253cSXin Li        return False
51*760c253cSXin Li
52*760c253cSXin Li    def GetIdentifier(self, stage):
53*760c253cSXin Li        assert stage == self._stage
54*760c253cSXin Li        return self._identifier
55*760c253cSXin Li
56*760c253cSXin Li    def SetResult(self, stage, cost):
57*760c253cSXin Li        assert stage == self._stage
58*760c253cSXin Li        self._cost = cost
59*760c253cSXin Li        self._performed = True
60*760c253cSXin Li
61*760c253cSXin Li    def Work(self, stage):
62*760c253cSXin Li        assert stage == self._stage
63*760c253cSXin Li        self._performed = True
64*760c253cSXin Li
65*760c253cSXin Li    def GetResult(self, stage):
66*760c253cSXin Li        assert stage == self._stage
67*760c253cSXin Li        return self._cost
68*760c253cSXin Li
69*760c253cSXin Li    def Done(self, stage):
70*760c253cSXin Li        """Indicates whether the task has been performed."""
71*760c253cSXin Li
72*760c253cSXin Li        assert stage == self._stage
73*760c253cSXin Li        return self._performed
74*760c253cSXin Li
75*760c253cSXin Li    def LogSteeringCost(self):
76*760c253cSXin Li        pass
77*760c253cSXin Li
78*760c253cSXin Li
79*760c253cSXin Liclass IdentifierMockTask(MockTask):
80*760c253cSXin Li    """This class defines the mock task that does not consider the cost.
81*760c253cSXin Li
82*760c253cSXin Li    The task instances will be inserted into a set. Therefore the hash and the
83*760c253cSXin Li    equal methods are overridden. The unittests that compares identities of the
84*760c253cSXin Li    tasks for equality can use this mock task instead of the base mock tack.
85*760c253cSXin Li    """
86*760c253cSXin Li
87*760c253cSXin Li    def __hash__(self):
88*760c253cSXin Li        return self._identifier
89*760c253cSXin Li
90*760c253cSXin Li    def __eq__(self, other):
91*760c253cSXin Li        if isinstance(other, MockTask):
92*760c253cSXin Li            return self._identifier == other.GetIdentifier(self._stage)
93*760c253cSXin Li        return False
94