blob: a8bdf5995d9101834a0592c0d6dfba9363be07dd [file] [log] [blame]
Yuheng Long49358b72013-07-10 14:45:29 -07001"""Generation unittest.
2
3Part of the Chrome build flags optimization.
4"""
Yuheng Longf20cffa2013-06-03 18:46:00 -07005
6__author__ = 'yuhenglong@google.com (Yuheng Long)'
7
Yuheng Long8b9c0f12013-07-16 09:38:16 -07008import random
Yuheng Longf20cffa2013-06-03 18:46:00 -07009import unittest
10
Yuheng Long8b9c0f12013-07-16 09:38:16 -070011from generation import Generation
12from mock_task import MockTask
13
14
15# Pick an integer at random.
16TESTSTAGE = -125
17
18# The number of tasks to be put in a generation to be tested.
19NUMTASKS = 20
20
21# The stride of permutation used to shuffle the input list of tasks. Should be
22# relatively prime with NUMTASKS.
23STRIDE = 7
24
25
26class GenerationMockTask(MockTask):
27 """This class defines the mock task to test the Generation class.
28
29 The task instances will be inserted into a set. Therefore the hash and the
30 equal methods are overridden. The generation class considers the identifier to
31 set the cost of the task in a set, thus the identifier is used in the
32 overriding methods.
33 """
34
35 def __hash__(self):
36 return self._identifier
37
38 def __eq__(self, other):
39 if isinstance(other, MockTask):
40 return self._identifier == other.GetIdentifier(self._stage)
41 return False
Yuheng Longf20cffa2013-06-03 18:46:00 -070042
43
44class GenerationTest(unittest.TestCase):
45 """This class test the Generation class.
46
Yuheng Long8b9c0f12013-07-16 09:38:16 -070047 Given a set of tasks in the generation, if there is any task that is pending,
48 then the Done method will return false, and true otherwise.
Yuheng Longf20cffa2013-06-03 18:46:00 -070049 """
50
Yuheng Long8b9c0f12013-07-16 09:38:16 -070051 def testDone(self):
52 """"Test the Done method.
Yuheng Longf20cffa2013-06-03 18:46:00 -070053
Yuheng Long8b9c0f12013-07-16 09:38:16 -070054 Produce a generation with a set of tasks. Set the cost of the task one by
55 one and verify that the Done method returns false before setting the cost
56 for all the tasks. After the costs of all the tasks are set, the Done method
57 should return true.
Yuheng Longf20cffa2013-06-03 18:46:00 -070058 """
59
Yuheng Long8b9c0f12013-07-16 09:38:16 -070060 random.seed(0)
61
62 testing_tasks = range(NUMTASKS)
63
64 # The tasks for the generation to be tested.
65 generation_tasks = [GenerationMockTask(TESTSTAGE, t) for t in testing_tasks]
66
67 gen = Generation(set(generation_tasks), None)
68
69 # Permute the list.
70 permutation = [(t * STRIDE) % NUMTASKS for t in range(NUMTASKS)]
71 permuted_tasks = [testing_tasks[index] for index in permutation]
72
73 # The Done method of the Generation should return false before all the tasks
74 # in the permuted list are set.
75 for testing_task in permuted_tasks:
76 assert not gen.Done()
77
78 # Mark a task as done by calling the UpdateTask method of the generation.
79 # Send the generation the task as well as its results.
80 gen.UpdateTask(GenerationMockTask(TESTSTAGE, testing_task))
81
82 # The Done method should return true after all the tasks in the permuted
83 # list is set.
84 assert gen.Done()
Yuheng Longf20cffa2013-06-03 18:46:00 -070085
86if __name__ == '__main__':
87 unittest.main()