• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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.
4
5from __future__ import absolute_import
6from __future__ import division
7from __future__ import print_function
8from multithreaded_processor import MultithreadedProcessor
9from six.moves import range
10
11
12class EvenNumbersGenerator():
13    """
14    An example and a simple test for multithreaded_processor.py.
15
16    These class is supposed to generate a table of even numbers using
17    many threads.
18
19    """
20    def __init__(self):
21        # creates a processor class with 13 threads
22        self._processor = MultithreadedProcessor(13)
23
24    def getEvenNumbers(self, count):
25        """
26        @param count: a count of even numbers to return
27
28        @returns a list of first 'count' even numbers in ascending order
29
30        """
31        # prepares input data
32        self._data = list(range(count))
33        # runs the calculations and returns an output
34        return self._processor.run(self._thread, count)
35
36    def _thread(self, task_id):
37        # calculates single even number
38        return (self._data[task_id]*2)
39
40
41def main():
42    """
43    Unit test for MultithreadedProcessor
44
45    """
46    # runs the test
47    gen = EvenNumbersGenerator()
48    evenNumbers = gen.getEvenNumbers(12345)
49    # checks the output
50    assert len(evenNumbers) == 12345
51    for i, number in enumerate(evenNumbers):
52        assert number == i*2
53
54
55if __name__ == '__main__':
56    main()
57