generation_test.py revision 16d7a5204e347855b1c3a68c982c22f931a12866
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 MockTask
17
18
19# Pick an integer at random.
20TESTSTAGE = -125
21
22# The number of tasks to be put in a generation to be tested.
23NUMTASKS = 20
24
25# The stride of permutation used to shuffle the input list of tasks. Should be
26# relatively prime with NUMTASKS.
27STRIDE = 7
28
29
30class GenerationMockTask(MockTask):
31  """This class defines the mock task to test the Generation class.
32
33  The task instances will be inserted into a set. Therefore the hash and the
34  equal methods are overridden. The generation class considers the identifier to
35  set the cost of the task in a set, thus the identifier is used in the
36  overriding methods.
37  """
38
39  def __hash__(self):
40    return self._identifier
41
42  def __eq__(self, other):
43    if isinstance(other, MockTask):
44      return self._identifier == other.GetIdentifier(self._stage)
45    return False
46
47
48class GenerationTest(unittest.TestCase):
49  """This class test the Generation class.
50
51  Given a set of tasks in the generation, if there is any task that is pending,
52  then the Done method will return false, and true otherwise.
53  """
54
55  def testDone(self):
56    """"Test the Done method.
57
58    Produce a generation with a set of tasks. Set the cost of the task one by
59    one and verify that the Done method returns false before setting the cost
60    for all the tasks. After the costs of all the tasks are set, the Done method
61    should return true.
62    """
63
64    random.seed(0)
65
66    testing_tasks = range(NUMTASKS)
67
68    # The tasks for the generation to be tested.
69    generation_tasks = [GenerationMockTask(TESTSTAGE, t) for t in testing_tasks]
70
71    gen = Generation(set(generation_tasks), None)
72
73    # Permute the list.
74    permutation = [(t * STRIDE) % NUMTASKS for t in range(NUMTASKS)]
75    permuted_tasks = [testing_tasks[index] for index in permutation]
76
77    # The Done method of the Generation should return false before all the tasks
78    # in the permuted list are set.
79    for testing_task in permuted_tasks:
80      assert not gen.Done()
81
82      # Mark a task as done by calling the UpdateTask method of the generation.
83      # Send the generation the task as well as its results.
84      gen.UpdateTask(GenerationMockTask(TESTSTAGE, testing_task))
85
86    # The Done method should return true after all the tasks in the permuted
87    # list is set.
88    assert gen.Done()
89
90if __name__ == '__main__':
91  unittest.main()
92