blob: 2e042d491e24021cd141edb2b3c60922ee49b649 [file] [log] [blame]
Yuheng Long16d7a522013-07-19 16:29:13 -07001# 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 Long49358b72013-07-10 14:45:29 -07004"""Generation unittest.
5
6Part of the Chrome build flags optimization.
7"""
Yuheng Longf20cffa2013-06-03 18:46:00 -07008
9__author__ = 'yuhenglong@google.com (Yuheng Long)'
10
Yuheng Long8b9c0f12013-07-16 09:38:16 -070011import random
Yuheng Longf20cffa2013-06-03 18:46:00 -070012import unittest
13
Yuheng Long8b9c0f12013-07-16 09:38:16 -070014from generation import Generation
Yuheng Longa5712a22013-07-22 13:51:17 -070015from mock_task import IdentifierMockTask
Yuheng Long8b9c0f12013-07-16 09:38:16 -070016
Yuheng Long8b9c0f12013-07-16 09:38:16 -070017# Pick an integer at random.
Yuheng Longc1fb0f12013-08-06 15:30:38 -070018TEST_STAGE = -125
Yuheng Long8b9c0f12013-07-16 09:38:16 -070019
20# The number of tasks to be put in a generation to be tested.
Yuheng Longc1fb0f12013-08-06 15:30:38 -070021NUM_TASKS = 20
Yuheng Long8b9c0f12013-07-16 09:38:16 -070022
23# The stride of permutation used to shuffle the input list of tasks. Should be
Yuheng Longc1fb0f12013-08-06 15:30:38 -070024# relatively prime with NUM_TASKS.
Yuheng Long8b9c0f12013-07-16 09:38:16 -070025STRIDE = 7
26
27
Yuheng Longf20cffa2013-06-03 18:46:00 -070028class GenerationTest(unittest.TestCase):
29 """This class test the Generation class.
30
Yuheng Long8b9c0f12013-07-16 09:38:16 -070031 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 Longf20cffa2013-06-03 18:46:00 -070033 """
34
Yuheng Long8b9c0f12013-07-16 09:38:16 -070035 def testDone(self):
36 """"Test the Done method.
Yuheng Longf20cffa2013-06-03 18:46:00 -070037
Yuheng Long8b9c0f12013-07-16 09:38:16 -070038 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 Longf20cffa2013-06-03 18:46:00 -070042 """
43
Yuheng Long8b9c0f12013-07-16 09:38:16 -070044 random.seed(0)
45
Yuheng Longc1fb0f12013-08-06 15:30:38 -070046 testing_tasks = range(NUM_TASKS)
Yuheng Long8b9c0f12013-07-16 09:38:16 -070047
48 # The tasks for the generation to be tested.
Yuheng Longc1fb0f12013-08-06 15:30:38 -070049 tasks = [IdentifierMockTask(TEST_STAGE, t) for t in testing_tasks]
Yuheng Long8b9c0f12013-07-16 09:38:16 -070050
Yuheng Longc1fb0f12013-08-06 15:30:38 -070051 gen = Generation(set(tasks), None)
Yuheng Long8b9c0f12013-07-16 09:38:16 -070052
53 # Permute the list.
Yuheng Longc1fb0f12013-08-06 15:30:38 -070054 permutation = [(t * STRIDE) % NUM_TASKS for t in range(NUM_TASKS)]
Yuheng Long8b9c0f12013-07-16 09:38:16 -070055 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 Longc1fb0f12013-08-06 15:30:38 -070064 gen.UpdateTask(IdentifierMockTask(TEST_STAGE, testing_task))
Yuheng Long8b9c0f12013-07-16 09:38:16 -070065
66 # The Done method should return true after all the tasks in the permuted
67 # list is set.
68 assert gen.Done()
Yuheng Longf20cffa2013-06-03 18:46:00 -070069
Luis Lozanof2a3ef42015-12-15 13:49:30 -080070
Yuheng Longf20cffa2013-06-03 18:46:00 -070071if __name__ == '__main__':
72 unittest.main()