cow.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. #
  4. # BitBake Tests for Copy-on-Write (cow.py)
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. # Copyright 2006 Holger Freyther <freyther@handhelds.org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import unittest
  24. import os
  25. class COWTestCase(unittest.TestCase):
  26. """
  27. Test case for the COW module from mithro
  28. """
  29. def testGetSet(self):
  30. """
  31. Test and set
  32. """
  33. from bb.COW import COWDictBase
  34. a = COWDictBase.copy()
  35. self.assertEqual(False, 'a' in a)
  36. a['a'] = 'a'
  37. a['b'] = 'b'
  38. self.assertEqual(True, 'a' in a)
  39. self.assertEqual(True, 'b' in a)
  40. self.assertEqual('a', a['a'] )
  41. self.assertEqual('b', a['b'] )
  42. def testCopyCopy(self):
  43. """
  44. Test the copy of copies
  45. """
  46. from bb.COW import COWDictBase
  47. # create two COW dict 'instances'
  48. b = COWDictBase.copy()
  49. c = COWDictBase.copy()
  50. # assign some keys to one instance, some keys to another
  51. b['a'] = 10
  52. b['c'] = 20
  53. c['a'] = 30
  54. # test separation of the two instances
  55. self.assertEqual(False, 'c' in c)
  56. self.assertEqual(30, c['a'])
  57. self.assertEqual(10, b['a'])
  58. # test copy
  59. b_2 = b.copy()
  60. c_2 = c.copy()
  61. self.assertEqual(False, 'c' in c_2)
  62. self.assertEqual(10, b_2['a'])
  63. b_2['d'] = 40
  64. self.assertEqual(False, 'd' in c_2)
  65. self.assertEqual(True, 'd' in b_2)
  66. self.assertEqual(40, b_2['d'])
  67. self.assertEqual(False, 'd' in b)
  68. self.assertEqual(False, 'd' in c)
  69. c_2['d'] = 30
  70. self.assertEqual(True, 'd' in c_2)
  71. self.assertEqual(True, 'd' in b_2)
  72. self.assertEqual(30, c_2['d'])
  73. self.assertEqual(40, b_2['d'])
  74. self.assertEqual(False, 'd' in b)
  75. self.assertEqual(False, 'd' in c)
  76. # test copy of the copy
  77. c_3 = c_2.copy()
  78. b_3 = b_2.copy()
  79. b_3_2 = b_2.copy()
  80. c_3['e'] = 4711
  81. self.assertEqual(4711, c_3['e'])
  82. self.assertEqual(False, 'e' in c_2)
  83. self.assertEqual(False, 'e' in b_3)
  84. self.assertEqual(False, 'e' in b_3_2)
  85. self.assertEqual(False, 'e' in b_2)
  86. b_3['e'] = 'viel'
  87. self.assertEqual('viel', b_3['e'])
  88. self.assertEqual(4711, c_3['e'])
  89. self.assertEqual(False, 'e' in c_2)
  90. self.assertEqual(True, 'e' in b_3)
  91. self.assertEqual(False, 'e' in b_3_2)
  92. self.assertEqual(False, 'e' in b_2)
  93. def testCow(self):
  94. from bb.COW import COWDictBase
  95. c = COWDictBase.copy()
  96. c['123'] = 1027
  97. c['other'] = 4711
  98. c['d'] = { 'abc' : 10, 'bcd' : 20 }
  99. copy = c.copy()
  100. self.assertEqual(1027, c['123'])
  101. self.assertEqual(4711, c['other'])
  102. self.assertEqual({'abc':10, 'bcd':20}, c['d'])
  103. self.assertEqual(1027, copy['123'])
  104. self.assertEqual(4711, copy['other'])
  105. self.assertEqual({'abc':10, 'bcd':20}, copy['d'])
  106. # cow it now
  107. copy['123'] = 1028
  108. copy['other'] = 4712
  109. copy['d']['abc'] = 20
  110. self.assertEqual(1027, c['123'])
  111. self.assertEqual(4711, c['other'])
  112. self.assertEqual({'abc':10, 'bcd':20}, c['d'])
  113. self.assertEqual(1028, copy['123'])
  114. self.assertEqual(4712, copy['other'])
  115. self.assertEqual({'abc':20, 'bcd':20}, copy['d'])