116d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
216d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# Use of this source code is governed by a BSD-style license that can be
316d7a5204e347855b1c3a68c982c22f931a12866Yuheng Long# found in the LICENSE file.
449358b75c25a44760e884245440dc96e55812d04Yuheng Long"""Generation unittest.
549358b75c25a44760e884245440dc96e55812d04Yuheng Long
649358b75c25a44760e884245440dc96e55812d04Yuheng LongPart of the Chrome build flags optimization.
749358b75c25a44760e884245440dc96e55812d04Yuheng Long"""
8f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
9f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long__author__ = 'yuhenglong@google.com (Yuheng Long)'
10f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
118b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longimport random
12f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Longimport unittest
13f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
148b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longfrom generation import Generation
15a5712a2c71aa665dcca808963d152228890c8364Yuheng Longfrom mock_task import IdentifierMockTask
16f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
178b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# Pick an integer at random.
18c1fb0f1051208907df137371f3d100132f19cf10Yuheng LongTEST_STAGE = -125
198b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
208b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# The number of tasks to be put in a generation to be tested.
21c1fb0f1051208907df137371f3d100132f19cf10Yuheng LongNUM_TASKS = 20
228b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
238b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long# The stride of permutation used to shuffle the input list of tasks. Should be
24c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long# relatively prime with NUM_TASKS.
258b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng LongSTRIDE = 7
268b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
27f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
288b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Longclass GenerationTest(unittest.TestCase):
298b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long  """This class test the Generation class.
308b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
318b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long  Given a set of tasks in the generation, if there is any task that is pending,
328b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long  then the Done method will return false, and true otherwise.
338b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long  """
348b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
358b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long  def testDone(self):
368b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    """"Test the Done method.
37f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
388b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    Produce a generation with a set of tasks. Set the cost of the task one by
398b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    one and verify that the Done method returns false before setting the cost
408b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    for all the tasks. After the costs of all the tasks are set, the Done method
418b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    should return true.
42f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long    """
43f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
448b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    random.seed(0)
458b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
46c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long    testing_tasks = range(NUM_TASKS)
478b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
488b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # The tasks for the generation to be tested.
49c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long    tasks = [IdentifierMockTask(TEST_STAGE, t) for t in testing_tasks]
508b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
51c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long    gen = Generation(set(tasks), None)
528b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
538b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # Permute the list.
54c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long    permutation = [(t * STRIDE) % NUM_TASKS for t in range(NUM_TASKS)]
558b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    permuted_tasks = [testing_tasks[index] for index in permutation]
568b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
578b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # The Done method of the Generation should return false before all the tasks
588b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # in the permuted list are set.
598b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    for testing_task in permuted_tasks:
608b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long      assert not gen.Done()
618b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
628b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long      # Mark a task as done by calling the UpdateTask method of the generation.
638b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long      # Send the generation the task as well as its results.
64c1fb0f1051208907df137371f3d100132f19cf10Yuheng Long      gen.UpdateTask(IdentifierMockTask(TEST_STAGE, testing_task))
658b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long
668b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # The Done method should return true after all the tasks in the permuted
678b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    # list is set.
688b9c0f140b48253cdbcc7c050f115c5e3bda6d88Yuheng Long    assert gen.Done()
69f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long
70f2a3ef46f75d2196a93d3ed27f4d1fcf22b54fbeLuis Lozano
71f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Longif __name__ == '__main__':
72f20cffac082e3d920818f230ffc80ae6976267c0Yuheng Long  unittest.main()
73