• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2020 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import sys
8import unittest
9
10TOOLS_PATH = os.path.dirname(
11    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12sys.path.append(TOOLS_PATH)
13
14from testrunner.testproc.util import FixedSizeTopList
15
16
17class TestOrderedFixedSizeList(unittest.TestCase):
18
19  def test_empty(self):
20    ofsl = FixedSizeTopList(3)
21    self.assertEqual(ofsl.as_list(), [])
22
23  def test_12(self):
24    ofsl = FixedSizeTopList(3)
25    ofsl.add(1)
26    ofsl.add(2)
27    self.assertEqual(ofsl.as_list(), [2, 1])
28
29  def test_4321(self):
30    ofsl = FixedSizeTopList(3)
31    ofsl.add(4)
32    ofsl.add(3)
33    ofsl.add(2)
34    ofsl.add(1)
35    data = ofsl.as_list()
36    self.assertEqual(data, [4, 3, 2])
37
38  def test_544321(self):
39    ofsl = FixedSizeTopList(4)
40    ofsl.add(5)
41    ofsl.add(4)
42    ofsl.add(4)
43    ofsl.add(3)
44    ofsl.add(2)
45    ofsl.add(1)
46    data = ofsl.as_list()
47    self.assertEqual(data, [5, 4, 4, 3])
48
49  def test_withkey(self):
50    ofsl = FixedSizeTopList(3, key=lambda x: x['val'])
51    ofsl.add({'val': 4, 'something': 'four'})
52    ofsl.add({'val': 3, 'something': 'three'})
53    ofsl.add({'val': -1, 'something': 'minusone'})
54    ofsl.add({'val': 5, 'something': 'five'})
55    ofsl.add({'val': 0, 'something': 'zero'})
56    data = [e['something'] for e in ofsl.as_list()]
57    self.assertEqual(data, ['five', 'four', 'three'])
58
59  def test_withkeyclash(self):
60    # Test that a key clash does not throw exeption
61    ofsl = FixedSizeTopList(2, key=lambda x: x['val'])
62    ofsl.add({'val': 2, 'something': 'two'})
63    ofsl.add({'val': 2, 'something': 'two'})
64    ofsl.add({'val': 0, 'something': 'zero'})
65    data = [e['something'] for e in ofsl.as_list()]
66    self.assertEqual(data, ['two', 'two'])
67
68
69if __name__ == '__main__':
70  unittest.main()
71