1#!/usr/bin/python
2
3# Copyright 2014 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""
9Test OrderedSet.
10"""
11
12import sys
13import test_variables
14import unittest
15
16sys.path.append(test_variables.GYP_GEN_DIR)
17
18from vars_dict_lib import OrderedSet
19
20
21def create_dummy_var(i):
22  return 'dummy_var' + str(i)
23
24
25class OrderedSetTest(unittest.TestCase):
26
27  def setUp(self):
28    self.__set = OrderedSet()
29
30  def test_methods(self):
31    """Test methods on OrderedSet.
32    """
33    RANGE = 10
34    for i in range(RANGE):
35      dummy_var = create_dummy_var(i)
36      # Add to the list. This should succeed.
37      self.__set.add(dummy_var)
38      self.assertEqual(len(self.__set), i+1)
39      self.assertTrue(dummy_var in self.__set)
40      self.assertEqual(self.__set[i], dummy_var)
41
42      # Now attempt to add it again. This should fail.
43      self.__set.add(dummy_var)
44      self.assertEqual(len(self.__set), i+1)
45      self.assertEqual(self.__set[i], dummy_var)
46
47    # Test iterator.
48    counter = 0
49    for set_member in self.__set:
50      self.assertEqual(create_dummy_var(counter), set_member)
51      counter += 1
52    self.assertEqual(counter, len(self.__set))
53
54    # Now test removal.
55    for i in range(RANGE):
56      dummy_var = create_dummy_var(i)
57      self.__set.remove(dummy_var)
58      self.assertEqual(len(self.__set), RANGE-i-1)
59      self.assertFalse(dummy_var in self.__set)
60
61    # Test reset(), for a range of ranges.
62    for subrange in range(RANGE):
63      for i in range(subrange):
64        self.__set.add(create_dummy_var(i))
65      self.assertEqual(len(self.__set), subrange)
66      self.__set.reset()
67      self.assertEqual(len(self.__set), 0)
68
69  def test_set(self):
70    """Test OrderedSet.set().
71    """
72    # Create a set with dummy values.
73    my_set = OrderedSet()
74    RANGE = 10
75    for i in range(RANGE):
76      my_set.add(create_dummy_var(i))
77    my_len = len(my_set)
78    self.assertEqual(my_len, RANGE)
79
80    # Copy it to another set.
81    other_set = OrderedSet()
82    self.assertEqual(len(other_set), 0)
83    other_set.set(my_set)
84
85    # Both sets should contain the same values, in the same order.
86    iterator = iter(my_set)
87    for item in other_set:
88      self.assertTrue(item == iterator.next())
89    with self.assertRaises(StopIteration):
90      iterator.next()
91    self.assertEqual(my_len, len(other_set))
92
93    # But the sets are different. Changing one will not affect the other.
94    self.assertFalse(other_set is my_set)
95    other_var = 'something_else'
96    other_set.add(other_var)
97    self.assertEqual(my_len + 1, len(other_set))
98    self.assertEqual(my_len, len(my_set))
99    self.assertNotIn(other_var, my_set)
100
101
102def main():
103  loader = unittest.TestLoader()
104  suite = loader.loadTestsFromTestCase(OrderedSetTest)
105  unittest.TextTestRunner(verbosity=2).run(suite)
106
107if __name__ == "__main__":
108  main()
109
110