|
21 | 21 |
|
22 | 22 | import feeph.i2cmux as sut |
23 | 23 |
|
24 | | - |
25 | 24 | if os.environ.get('TEST_TCA9546A_CHIP', 'n') == 'y': |
26 | 25 | HAS_HARDWARE = True |
27 | 26 | else: |
@@ -84,3 +83,61 @@ def test_multiplexed_device(self): |
84 | 83 | computed = bh.read_register(0x01) |
85 | 84 | expected = 0x12 |
86 | 85 | self.assertEqual(computed, expected) |
| 86 | + |
| 87 | + # --------------------------------------------------------------------- |
| 88 | + |
| 89 | + def test_invalid_device_address(self): |
| 90 | + # this code tests the equivalent of: |
| 91 | + # with sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0xFFFF) as bh: |
| 92 | + # ... |
| 93 | + i2c_bus = EmulatedI2C(state={}) |
| 94 | + # ----------------------------------------------------------------- |
| 95 | + # ----------------------------------------------------------------- |
| 96 | + self.assertRaises(ValueError, sut.TCA9546A, i2c_bus=i2c_bus, i2c_adr=0xFFFF) |
| 97 | + |
| 98 | + def test_invalid_tca_address(self): |
| 99 | + # this code tests the equivalent of: |
| 100 | + # with sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0x4C, tca_adr=0x07) as bh: |
| 101 | + # ... |
| 102 | + i2c_bus = EmulatedI2C(state={}) |
| 103 | + # ----------------------------------------------------------------- |
| 104 | + # ----------------------------------------------------------------- |
| 105 | + self.assertRaises(ValueError, sut.TCA9546A, i2c_bus=i2c_bus, i2c_adr=0x4C, tca_adr=0x07) |
| 106 | + |
| 107 | + def test_invalid_timeout(self): |
| 108 | + # this code tests the equivalent of: |
| 109 | + # with sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0x4C, timeout_ms=0) as bh: |
| 110 | + # ... |
| 111 | + i2c_bus = EmulatedI2C(state={}, lock_chance=1) |
| 112 | + # ----------------------------------------------------------------- |
| 113 | + # ----------------------------------------------------------------- |
| 114 | + self.assertRaises(ValueError, sut.TCA9546A, i2c_bus=i2c_bus, i2c_adr=0x4C, timeout_ms=0) |
| 115 | + |
| 116 | + def test_hard_to_lock(self): |
| 117 | + state = { |
| 118 | + 0x70: { |
| 119 | + -1: 0x00, |
| 120 | + }, |
| 121 | + 0x4C: { |
| 122 | + 0x00: 0x12, |
| 123 | + }, |
| 124 | + } |
| 125 | + i2c_bus = EmulatedI2C(state=state, lock_chance=1) |
| 126 | + # ----------------------------------------------------------------- |
| 127 | + # simulating an extremely busy I²C bus |
| 128 | + # (there's a 1 percent chance to successfully lock the bus) |
| 129 | + with sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0x4C, timeout_ms=None) as bh: |
| 130 | + computed = bh.read_register(0x00) |
| 131 | + expected = 0x12 |
| 132 | + # ----------------------------------------------------------------- |
| 133 | + self.assertEqual(computed, expected) |
| 134 | + |
| 135 | + def test_unable_to_lock(self): |
| 136 | + # this code tests the equivalent of: |
| 137 | + # with sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0x4C) as bh: |
| 138 | + # ... |
| 139 | + i2c_bus = EmulatedI2C(state={}, lock_chance=0) # impossible to acquire a lock |
| 140 | + bh = sut.TCA9546A(i2c_bus=i2c_bus, i2c_adr=0x4C) |
| 141 | + # ----------------------------------------------------------------- |
| 142 | + # ----------------------------------------------------------------- |
| 143 | + self.assertRaises(RuntimeError, bh.__enter__) |
0 commit comments