Home | History | Annotate | Download | only in bestflags
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 """Generation unittest.
      5 
      6 Part of the Chrome build flags optimization.
      7 """
      8 
      9 __author__ = 'yuhenglong (at] google.com (Yuheng Long)'
     10 
     11 import random
     12 import unittest
     13 
     14 from generation import Generation
     15 from mock_task import IdentifierMockTask
     16 
     17 # Pick an integer at random.
     18 TEST_STAGE = -125
     19 
     20 # The number of tasks to be put in a generation to be tested.
     21 NUM_TASKS = 20
     22 
     23 # The stride of permutation used to shuffle the input list of tasks. Should be
     24 # relatively prime with NUM_TASKS.
     25 STRIDE = 7
     26 
     27 
     28 class GenerationTest(unittest.TestCase):
     29   """This class test the Generation class.
     30 
     31   Given a set of tasks in the generation, if there is any task that is pending,
     32   then the Done method will return false, and true otherwise.
     33   """
     34 
     35   def testDone(self):
     36     """"Test the Done method.
     37 
     38     Produce a generation with a set of tasks. Set the cost of the task one by
     39     one and verify that the Done method returns false before setting the cost
     40     for all the tasks. After the costs of all the tasks are set, the Done method
     41     should return true.
     42     """
     43 
     44     random.seed(0)
     45 
     46     testing_tasks = range(NUM_TASKS)
     47 
     48     # The tasks for the generation to be tested.
     49     tasks = [IdentifierMockTask(TEST_STAGE, t) for t in testing_tasks]
     50 
     51     gen = Generation(set(tasks), None)
     52 
     53     # Permute the list.
     54     permutation = [(t * STRIDE) % NUM_TASKS for t in range(NUM_TASKS)]
     55     permuted_tasks = [testing_tasks[index] for index in permutation]
     56 
     57     # The Done method of the Generation should return false before all the tasks
     58     # in the permuted list are set.
     59     for testing_task in permuted_tasks:
     60       assert not gen.Done()
     61 
     62       # Mark a task as done by calling the UpdateTask method of the generation.
     63       # Send the generation the task as well as its results.
     64       gen.UpdateTask(IdentifierMockTask(TEST_STAGE, testing_task))
     65 
     66     # The Done method should return true after all the tasks in the permuted
     67     # list is set.
     68     assert gen.Done()
     69 
     70 
     71 if __name__ == '__main__':
     72   unittest.main()
     73