blob: bf6e79fceeaf4fa951f94f14a1c74826325de492 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Tests for Copy-on-Write (cow.py)
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006# Copyright 2006 Holger Freyther <freyther@handhelds.org>
7#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
9import unittest
Andrew Geissler82c905d2020-04-13 13:39:40 -050010
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12class COWTestCase(unittest.TestCase):
13 """
14 Test case for the COW module from mithro
15 """
16
17 def testGetSet(self):
18 """
19 Test and set
20 """
21 from bb.COW import COWDictBase
22 a = COWDictBase.copy()
23
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024 self.assertEqual(False, 'a' in a)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26 a['a'] = 'a'
27 a['b'] = 'b'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028 self.assertEqual(True, 'a' in a)
29 self.assertEqual(True, 'b' in a)
30 self.assertEqual('a', a['a'] )
31 self.assertEqual('b', a['b'] )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032
33 def testCopyCopy(self):
34 """
35 Test the copy of copies
36 """
37
38 from bb.COW import COWDictBase
39
40 # create two COW dict 'instances'
41 b = COWDictBase.copy()
42 c = COWDictBase.copy()
43
44 # assign some keys to one instance, some keys to another
45 b['a'] = 10
46 b['c'] = 20
47 c['a'] = 30
48
49 # test separation of the two instances
Patrick Williamsc0f7c042017-02-23 20:41:17 -060050 self.assertEqual(False, 'c' in c)
51 self.assertEqual(30, c['a'])
52 self.assertEqual(10, b['a'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
54 # test copy
55 b_2 = b.copy()
56 c_2 = c.copy()
57
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 self.assertEqual(False, 'c' in c_2)
59 self.assertEqual(10, b_2['a'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
61 b_2['d'] = 40
Patrick Williamsc0f7c042017-02-23 20:41:17 -060062 self.assertEqual(False, 'd' in c_2)
63 self.assertEqual(True, 'd' in b_2)
64 self.assertEqual(40, b_2['d'])
65 self.assertEqual(False, 'd' in b)
66 self.assertEqual(False, 'd' in c)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68 c_2['d'] = 30
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 self.assertEqual(True, 'd' in c_2)
70 self.assertEqual(True, 'd' in b_2)
71 self.assertEqual(30, c_2['d'])
72 self.assertEqual(40, b_2['d'])
73 self.assertEqual(False, 'd' in b)
74 self.assertEqual(False, 'd' in c)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
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
81 c_3['e'] = 4711
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 self.assertEqual(4711, c_3['e'])
83 self.assertEqual(False, 'e' in c_2)
84 self.assertEqual(False, 'e' in b_3)
85 self.assertEqual(False, 'e' in b_3_2)
86 self.assertEqual(False, 'e' in b_2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
88 b_3['e'] = 'viel'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060089 self.assertEqual('viel', b_3['e'])
90 self.assertEqual(4711, c_3['e'])
91 self.assertEqual(False, 'e' in c_2)
92 self.assertEqual(True, 'e' in b_3)
93 self.assertEqual(False, 'e' in b_3_2)
94 self.assertEqual(False, 'e' in b_2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
96 def testCow(self):
97 from bb.COW import COWDictBase
98 c = COWDictBase.copy()
99 c['123'] = 1027
100 c['other'] = 4711
101 c['d'] = { 'abc' : 10, 'bcd' : 20 }
102
103 copy = c.copy()
104
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 self.assertEqual(1027, c['123'])
106 self.assertEqual(4711, c['other'])
107 self.assertEqual({'abc':10, 'bcd':20}, c['d'])
108 self.assertEqual(1027, copy['123'])
109 self.assertEqual(4711, copy['other'])
110 self.assertEqual({'abc':10, 'bcd':20}, copy['d'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112 # cow it now
113 copy['123'] = 1028
114 copy['other'] = 4712
115 copy['d']['abc'] = 20
116
117
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600118 self.assertEqual(1027, c['123'])
119 self.assertEqual(4711, c['other'])
120 self.assertEqual({'abc':10, 'bcd':20}, c['d'])
121 self.assertEqual(1028, copy['123'])
122 self.assertEqual(4712, copy['other'])
123 self.assertEqual({'abc':20, 'bcd':20}, copy['d'])