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