blob: d149d84d0d92d7807bab69df5a6bd293fa0c0e06 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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# Copyright 2006 Holger Freyther <freyther@handhelds.org>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22import unittest
23import os
24
25class COWTestCase(unittest.TestCase):
26 """
27 Test case for the COW module from mithro
28 """
29
30 def testGetSet(self):
31 """
32 Test and set
33 """
34 from bb.COW import COWDictBase
35 a = COWDictBase.copy()
36
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037 self.assertEqual(False, 'a' in a)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39 a['a'] = 'a'
40 a['b'] = 'b'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041 self.assertEqual(True, 'a' in a)
42 self.assertEqual(True, 'b' in a)
43 self.assertEqual('a', a['a'] )
44 self.assertEqual('b', a['b'] )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
46 def testCopyCopy(self):
47 """
48 Test the copy of copies
49 """
50
51 from bb.COW import COWDictBase
52
53 # create two COW dict 'instances'
54 b = COWDictBase.copy()
55 c = COWDictBase.copy()
56
57 # assign some keys to one instance, some keys to another
58 b['a'] = 10
59 b['c'] = 20
60 c['a'] = 30
61
62 # test separation of the two instances
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063 self.assertEqual(False, 'c' in c)
64 self.assertEqual(30, c['a'])
65 self.assertEqual(10, b['a'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
67 # test copy
68 b_2 = b.copy()
69 c_2 = c.copy()
70
Patrick Williamsc0f7c042017-02-23 20:41:17 -060071 self.assertEqual(False, 'c' in c_2)
72 self.assertEqual(10, b_2['a'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073
74 b_2['d'] = 40
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075 self.assertEqual(False, 'd' in c_2)
76 self.assertEqual(True, 'd' in b_2)
77 self.assertEqual(40, b_2['d'])
78 self.assertEqual(False, 'd' in b)
79 self.assertEqual(False, 'd' in c)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
81 c_2['d'] = 30
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 self.assertEqual(True, 'd' in c_2)
83 self.assertEqual(True, 'd' in b_2)
84 self.assertEqual(30, c_2['d'])
85 self.assertEqual(40, b_2['d'])
86 self.assertEqual(False, 'd' in b)
87 self.assertEqual(False, 'd' in c)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
89 # test copy of the copy
90 c_3 = c_2.copy()
91 b_3 = b_2.copy()
92 b_3_2 = b_2.copy()
93
94 c_3['e'] = 4711
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 self.assertEqual(4711, c_3['e'])
96 self.assertEqual(False, 'e' in c_2)
97 self.assertEqual(False, 'e' in b_3)
98 self.assertEqual(False, 'e' in b_3_2)
99 self.assertEqual(False, 'e' in b_2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
101 b_3['e'] = 'viel'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600102 self.assertEqual('viel', b_3['e'])
103 self.assertEqual(4711, c_3['e'])
104 self.assertEqual(False, 'e' in c_2)
105 self.assertEqual(True, 'e' in b_3)
106 self.assertEqual(False, 'e' in b_3_2)
107 self.assertEqual(False, 'e' in b_2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
109 def testCow(self):
110 from bb.COW import COWDictBase
111 c = COWDictBase.copy()
112 c['123'] = 1027
113 c['other'] = 4711
114 c['d'] = { 'abc' : 10, 'bcd' : 20 }
115
116 copy = c.copy()
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(1027, copy['123'])
122 self.assertEqual(4711, copy['other'])
123 self.assertEqual({'abc':10, 'bcd':20}, copy['d'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125 # cow it now
126 copy['123'] = 1028
127 copy['other'] = 4712
128 copy['d']['abc'] = 20
129
130
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131 self.assertEqual(1027, c['123'])
132 self.assertEqual(4711, c['other'])
133 self.assertEqual({'abc':10, 'bcd':20}, c['d'])
134 self.assertEqual(1028, copy['123'])
135 self.assertEqual(4712, copy['other'])
136 self.assertEqual({'abc':20, 'bcd':20}, copy['d'])