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"""Generation unittest. 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 Liimport random 12*760c253cSXin Liimport unittest 13*760c253cSXin Li 14*760c253cSXin Lifrom generation import Generation 15*760c253cSXin Lifrom mock_task import IdentifierMockTask 16*760c253cSXin Li 17*760c253cSXin Li 18*760c253cSXin Li# Pick an integer at random. 19*760c253cSXin LiTEST_STAGE = -125 20*760c253cSXin Li 21*760c253cSXin Li# The number of tasks to be put in a generation to be tested. 22*760c253cSXin LiNUM_TASKS = 20 23*760c253cSXin Li 24*760c253cSXin Li# The stride of permutation used to shuffle the input list of tasks. Should be 25*760c253cSXin Li# relatively prime with NUM_TASKS. 26*760c253cSXin LiSTRIDE = 7 27*760c253cSXin Li 28*760c253cSXin Li 29*760c253cSXin Liclass GenerationTest(unittest.TestCase): 30*760c253cSXin Li """This class test the Generation class. 31*760c253cSXin Li 32*760c253cSXin Li Given a set of tasks in the generation, if there is any task that is pending, 33*760c253cSXin Li then the Done method will return false, and true otherwise. 34*760c253cSXin Li """ 35*760c253cSXin Li 36*760c253cSXin Li def testDone(self): 37*760c253cSXin Li """ "Test the Done method. 38*760c253cSXin Li 39*760c253cSXin Li Produce a generation with a set of tasks. Set the cost of the task one by 40*760c253cSXin Li one and verify that the Done method returns false before setting the cost 41*760c253cSXin Li for all the tasks. After the costs of all the tasks are set, the Done method 42*760c253cSXin Li should return true. 43*760c253cSXin Li """ 44*760c253cSXin Li 45*760c253cSXin Li random.seed(0) 46*760c253cSXin Li 47*760c253cSXin Li testing_tasks = range(NUM_TASKS) 48*760c253cSXin Li 49*760c253cSXin Li # The tasks for the generation to be tested. 50*760c253cSXin Li tasks = [IdentifierMockTask(TEST_STAGE, t) for t in testing_tasks] 51*760c253cSXin Li 52*760c253cSXin Li gen = Generation(set(tasks), None) 53*760c253cSXin Li 54*760c253cSXin Li # Permute the list. 55*760c253cSXin Li permutation = [(t * STRIDE) % NUM_TASKS for t in range(NUM_TASKS)] 56*760c253cSXin Li permuted_tasks = [testing_tasks[index] for index in permutation] 57*760c253cSXin Li 58*760c253cSXin Li # The Done method of the Generation should return false before all the tasks 59*760c253cSXin Li # in the permuted list are set. 60*760c253cSXin Li for testing_task in permuted_tasks: 61*760c253cSXin Li assert not gen.Done() 62*760c253cSXin Li 63*760c253cSXin Li # Mark a task as done by calling the UpdateTask method of the generation. 64*760c253cSXin Li # Send the generation the task as well as its results. 65*760c253cSXin Li gen.UpdateTask(IdentifierMockTask(TEST_STAGE, testing_task)) 66*760c253cSXin Li 67*760c253cSXin Li # The Done method should return true after all the tasks in the permuted 68*760c253cSXin Li # list is set. 69*760c253cSXin Li assert gen.Done() 70*760c253cSXin Li 71*760c253cSXin Li 72*760c253cSXin Liif __name__ == "__main__": 73*760c253cSXin Li unittest.main() 74