generation_test.py revision a5712a2c71aa665dcca808963d152228890c8364
116d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 216d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# Use of this source code is governed by a BSD-style license that can be 316d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# found in the LICENSE file. 416d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long 549358b75c25a44760e884245440dc96e55812d04Yuheng Long"""Generation unittest. 649358b75c25a44760e884245440dc96e55812d04Yuheng Long 749358b75c25a44760e884245440dc96e55812d04Yuheng LongPart of the Chrome build flags optimization. 849358b75c25a44760e884245440dc96e55812d04Yuheng Long""" 9f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 10f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long__author__ = 'yuhenglong@google.com (Yuheng Long)' 11f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 128b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longimport random 13f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Longimport unittest 14f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 158b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longfrom generation import Generation 16a5712a2c71aa665dcca808963d152228890c8364Yuheng Longfrom mock_task import IdentifierMockTask 17f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 18f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 198b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# Pick an integer at random. 208b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng LongTESTSTAGE = -125 218b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 228b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# The number of tasks to be put in a generation to be tested. 238b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng LongNUMTASKS = 20 248b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 258b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# The stride of permutation used to shuffle the input list of tasks. Should be 268b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# relatively prime with NUMTASKS. 278b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng LongSTRIDE = 7 288b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 29f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 308b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longclass GenerationTest(unittest.TestCase): 318b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long """This class test the Generation class. 328b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 338b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long Given a set of tasks in the generation, if there is any task that is pending, 348b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long then the Done method will return false, and true otherwise. 358b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long """ 368b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 378b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long def testDone(self): 388b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long """"Test the Done method. 39f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 408b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long Produce a generation with a set of tasks. Set the cost of the task one by 418b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long one and verify that the Done method returns false before setting the cost 428b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long for all the tasks. After the costs of all the tasks are set, the Done method 438b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long should return true. 44f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long """ 45f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 468b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long random.seed(0) 478b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 488b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long testing_tasks = range(NUMTASKS) 498b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 508b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # The tasks for the generation to be tested. 51a5712a2c71aa665dcca808963d152228890c8364Yuheng Long generation_tasks = [IdentifierMockTask(TESTSTAGE, t) for t in testing_tasks] 528b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 538b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long gen = Generation(set(generation_tasks), None) 548b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 558b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # Permute the list. 568b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long permutation = [(t * STRIDE) % NUMTASKS for t in range(NUMTASKS)] 578b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long permuted_tasks = [testing_tasks[index] for index in permutation] 588b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 598b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # The Done method of the Generation should return false before all the tasks 608b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # in the permuted list are set. 618b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long for testing_task in permuted_tasks: 628b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long assert not gen.Done() 638b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 648b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # Mark a task as done by calling the UpdateTask method of the generation. 658b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # Send the generation the task as well as its results. 66a5712a2c71aa665dcca808963d152228890c8364Yuheng Long gen.UpdateTask(IdentifierMockTask(TESTSTAGE, testing_task)) 678b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long 688b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # The Done method should return true after all the tasks in the permuted 698b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long # list is set. 708b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long assert gen.Done() 71f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long 72f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Longif __name__ == '__main__': 73f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long unittest.main() 74