Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | # 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 | |
| 22 | import unittest |
| 23 | import os |
| 24 | |
| 25 | class 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | self.assertEqual(False, 'a' in a) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
| 39 | a['a'] = 'a' |
| 40 | a['b'] = 'b' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | self.assertEqual(False, 'c' in c) |
| 64 | self.assertEqual(30, c['a']) |
| 65 | self.assertEqual(10, b['a']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
| 67 | # test copy |
| 68 | b_2 = b.copy() |
| 69 | c_2 = c.copy() |
| 70 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | self.assertEqual(False, 'c' in c_2) |
| 72 | self.assertEqual(10, b_2['a']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
| 74 | b_2['d'] = 40 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
| 81 | c_2['d'] = 30 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 82 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 95 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
| 101 | b_3['e'] = 'viel' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | |
| 125 | # cow it now |
| 126 | copy['123'] = 1028 |
| 127 | copy['other'] = 4712 |
| 128 | copy['d']['abc'] = 20 |
| 129 | |
| 130 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | 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']) |