Yuheng Long | 16d7a52 | 2013-07-19 16:29:13 -0700 | [diff] [blame] | 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. |
Yuheng Long | 49358b7 | 2013-07-10 14:45:29 -0700 | [diff] [blame] | 4 | """Generation unittest. |
| 5 | |
| 6 | Part of the Chrome build flags optimization. |
| 7 | """ |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 8 | |
| 9 | __author__ = 'yuhenglong@google.com (Yuheng Long)' |
| 10 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 11 | import random |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 12 | import unittest |
| 13 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 14 | from generation import Generation |
Yuheng Long | a5712a2 | 2013-07-22 13:51:17 -0700 | [diff] [blame] | 15 | from mock_task import IdentifierMockTask |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 16 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 17 | # Pick an integer at random. |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 18 | TEST_STAGE = -125 |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 19 | |
| 20 | # The number of tasks to be put in a generation to be tested. |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 21 | NUM_TASKS = 20 |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 22 | |
| 23 | # The stride of permutation used to shuffle the input list of tasks. Should be |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 24 | # relatively prime with NUM_TASKS. |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 25 | STRIDE = 7 |
| 26 | |
| 27 | |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 28 | class GenerationTest(unittest.TestCase): |
| 29 | """This class test the Generation class. |
| 30 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 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. |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 33 | """ |
| 34 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 35 | def testDone(self): |
| 36 | """"Test the Done method. |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 37 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 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. |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 42 | """ |
| 43 | |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 44 | random.seed(0) |
| 45 | |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 46 | testing_tasks = range(NUM_TASKS) |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 47 | |
| 48 | # The tasks for the generation to be tested. |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 49 | tasks = [IdentifierMockTask(TEST_STAGE, t) for t in testing_tasks] |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 50 | |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 51 | gen = Generation(set(tasks), None) |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 52 | |
| 53 | # Permute the list. |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 54 | permutation = [(t * STRIDE) % NUM_TASKS for t in range(NUM_TASKS)] |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 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. |
Yuheng Long | c1fb0f1 | 2013-08-06 15:30:38 -0700 | [diff] [blame] | 64 | gen.UpdateTask(IdentifierMockTask(TEST_STAGE, testing_task)) |
Yuheng Long | 8b9c0f1 | 2013-07-16 09:38:16 -0700 | [diff] [blame] | 65 | |
| 66 | # The Done method should return true after all the tasks in the permuted |
| 67 | # list is set. |
| 68 | assert gen.Done() |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 69 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 70 | |
Yuheng Long | f20cffa | 2013-06-03 18:46:00 -0700 | [diff] [blame] | 71 | if __name__ == '__main__': |
| 72 | unittest.main() |