Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Tests for Copy-on-Write (cow.py) |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # Copyright 2006 Holger Freyther <freyther@handhelds.org> |
| 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | import unittest |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 10 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
| 12 | class 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | self.assertEqual(False, 'a' in a) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
| 26 | a['a'] = 'a' |
| 27 | a['b'] = 'b' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 50 | self.assertEqual(False, 'c' in c) |
| 51 | self.assertEqual(30, c['a']) |
| 52 | self.assertEqual(10, b['a']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
| 54 | # test copy |
| 55 | b_2 = b.copy() |
| 56 | c_2 = c.copy() |
| 57 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | self.assertEqual(False, 'c' in c_2) |
| 59 | self.assertEqual(10, b_2['a']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
| 61 | b_2['d'] = 40 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 62 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
| 68 | c_2['d'] = 30 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 69 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 82 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
| 88 | b_3['e'] = 'viel' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 89 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 105 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | |
| 112 | # cow it now |
| 113 | copy['123'] = 1028 |
| 114 | copy['other'] = 4712 |
| 115 | copy['d']['abc'] = 20 |
| 116 | |
| 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(1028, copy['123']) |
| 122 | self.assertEqual(4712, copy['other']) |
| 123 | self.assertEqual({'abc':20, 'bcd':20}, copy['d']) |