Yuheng Long | 49358b7 | 2013-07-10 14:45:29 -0700 | [diff] [blame] | 1 | """Generation unittest. |
| 2 | |
| 3 | Part of the Chrome build flags optimization. |
| 4 | """ |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 5 | |
| 6 | __author__ = 'yuhenglong@google.com (Yuheng Long)' |
| 7 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 8 | import random |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 9 | import unittest |
| 10 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 11 | from generation import Generation |
| 12 | from mock_task import MockTask |
| 13 | |
| 14 | |
| 15 | # Pick an integer at random. |
| 16 | TESTSTAGE = -125 |
| 17 | |
| 18 | # The number of tasks to be put in a generation to be tested. |
| 19 | NUMTASKS = 20 |
| 20 | |
| 21 | # The stride of permutation used to shuffle the input list of tasks. Should be |
| 22 | # relatively prime with NUMTASKS. |
| 23 | STRIDE = 7 |
| 24 | |
| 25 | |
| 26 | class 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 Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 42 | |
| 43 | |
| 44 | class GenerationTest(unittest.TestCase): |
| 45 | """This class test the Generation class. |
| 46 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 47 | 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 Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 49 | """ |
| 50 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 51 | def testDone(self): |
| 52 | """"Test the Done method. |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 53 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 54 | 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 Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 58 | """ |
| 59 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame^] | 60 | 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 Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 85 | |
| 86 | if __name__ == '__main__': |
| 87 | unittest.main() |