#!/usr/bin/env python import os import sys import unittest sys.path.append(os.getcwd()) import attenuator class TestSetAttenuation(unittest.TestCase): # assign attenuation value to expected gpio pin settings att_pins = { 0.0: [1,1,1,1,1,1], 0.5: [0,1,1,1,1,1], 1.0: [1,0,1,1,1,1], 1.5: [0,0,1,1,1,1], 2.0: [1,1,0,1,1,1], 2.5: [0,1,0,1,1,1], 3.0: [1,0,0,1,1,1], 3.5: [0,0,0,1,1,1], 4.0: [1,1,1,0,1,1], 4.5: [0,1,1,0,1,1], 5.0: [1,0,1,0,1,1], 5.5: [0,0,1,0,1,1], 6.0: [1,1,0,0,1,1], 6.5: [0,1,0,0,1,1], 7.0: [1,0,0,0,1,1], 7.5: [0,0,0,0,1,1], 8.0: [1,1,1,1,0,1], 8.5: [0,1,1,1,0,1], 9.0: [1,0,1,1,0,1], 9.5: [0,0,1,1,0,1], 10.0: [1,1,0,1,0,1], 10.5: [0,1,0,1,0,1], 11.0: [1,0,0,1,0,1], 11.5: [0,0,0,1,0,1], 12.0: [1,1,1,0,0,1], 12.5: [0,1,1,0,0,1], 13.0: [1,0,1,0,0,1], 13.5: [0,0,1,0,0,1], 14.0: [1,1,0,0,0,1], 14.5: [0,1,0,0,0,1], 15.0: [1,0,0,0,0,1], 15.5: [0,0,0,0,0,1], 16.0: [1,1,1,1,1,0], 16.5: [0,1,1,1,1,0], 17.0: [1,0,1,1,1,0], 17.5: [0,0,1,1,1,0], 18.0: [1,1,0,1,1,0], 18.5: [0,1,0,1,1,0], 19.0: [1,0,0,1,1,0], 19.5: [0,0,0,1,1,0], 20.0: [1,1,1,0,1,0], 20.5: [0,1,1,0,1,0], 21.0: [1,0,1,0,1,0], 21.5: [0,0,1,0,1,0], 22.0: [1,1,0,0,1,0], 22.5: [0,1,0,0,1,0], 23.0: [1,0,0,0,1,0], 23.5: [0,0,0,0,1,0], 24.0: [1,1,1,1,0,0], 24.5: [0,1,1,1,0,0], 25.0: [1,0,1,1,0,0], 25.5: [0,0,1,1,0,0], 26.0: [1,1,0,1,0,0], 26.5: [0,1,0,1,0,0], 27.0: [1,0,0,1,0,0], 27.5: [0,0,0,1,0,0], 28.0: [1,1,1,0,0,0], 28.5: [0,1,1,0,0,0], 29.0: [1,0,1,0,0,0], 29.5: [0,0,1,0,0,0], 30.0: [1,1,0,0,0,0], 30.5: [0,1,0,0,0,0], 31.0: [1,0,0,0,0,0], 31.5: [0,0,0,0,0,0], } def test_type_error(self): pin = attenuator.Pin(0x20, 1) with self.assertRaises(TypeError): attenuator.Attenuator(pin, pin, pin, pin, pin, 1) with self.assertRaises(TypeError): attenuator.Attenuator(pin, pin, pin, pin, 1, 1) with self.assertRaises(TypeError): attenuator.Attenuator(pin, pin, pin, 1, 1, 1) with self.assertRaises(TypeError): attenuator.Attenuator(pin, pin, 1, 1, 1, 1) with self.assertRaises(TypeError): attenuator.Attenuator(pin, 1, 1, 1, 1, 1) with self.assertRaises(TypeError): attenuator.Attenuator( 1, 1, 1, 1, 1, 1) def test_bus_addr_config_matching(self): """Check for correct i2s bus address""" dummy = 1 bus_addr = 0x20 pin = attenuator.Pin(bus_addr, dummy) dut = attenuator.Attenuator(pin, pin, pin, pin, pin, pin) for i in range(0, 6): self.assertEqual(bus_addr, dut.pin[i].bus_address) def test_bus_addr_out_of_range(self): """Check behaviour in case of out of range i2s bus address""" pin_addr_wrong = attenuator.Pin(0x1F, 1) pin_addr_correct = attenuator.Pin(0x20, 1) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) pin_addr_wrong = attenuator.Pin(0x24, 1) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_correct, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) with self.assertRaises(IndexError): attenuator.Attenuator(pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong, pin_addr_wrong) def test_gpio_config_matching(self): """Check if gpio numbers are set correctly""" gpio_0_5db = attenuator.Pin(0x20, 1) gpio_1_0db = attenuator.Pin(0x20, 2) gpio_2_0db = attenuator.Pin(0x20, 3) gpio_4_0db = attenuator.Pin(0x20, 4) gpio_8_0db = attenuator.Pin(0x20, 5) gpio_16_0db = attenuator.Pin(0x20, 6) dut = attenuator.Attenuator(gpio_0_5db, gpio_1_0db, gpio_2_0db, gpio_4_0db, gpio_8_0db, gpio_16_0db) expected_gpios = [gpio_0_5db.pin_number, gpio_1_0db.pin_number, gpio_2_0db.pin_number, gpio_4_0db.pin_number, gpio_8_0db.pin_number, gpio_16_0db.pin_number] result_gpios = list() for i in range(0, 6): result_gpios.append(dut.pin[i].pin_number) self.assertSequenceEqual(result_gpios, expected_gpios) def test_gpio_out_of_range(self): """Check if IndexError is raised if gpio config is out of range""" pin_valid = attenuator.Pin(0x20, 1) pin_invalid_low = attenuator.Pin(0x20, 0) pin_invalid_high = attenuator.Pin(0x20, 0) with self.assertRaises(IndexError): attenuator.Attenuator(pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_invalid_low, pin_invalid_low, pin_invalid_low, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_invalid_low, pin_invalid_low, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_valid, pin_invalid_low, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_valid, pin_valid, pin_invalid_low) with self.assertRaises(IndexError): attenuator.Attenuator(pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_invalid_high, pin_invalid_high, pin_invalid_high, pin_invalid_high) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_invalid_high, pin_invalid_high, pin_invalid_high) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_valid, pin_invalid_high, pin_invalid_high) with self.assertRaises(IndexError): attenuator.Attenuator(pin_valid, pin_valid, pin_valid, pin_valid, pin_valid, pin_invalid_high) def test_attenuation(self): """Test setting an attenuation.""" dut = attenuator.Attenuator( attenuator.Pin(0X20, 1), attenuator.Pin(0X20, 2), attenuator.Pin(0X20, 3), attenuator.Pin(0X20, 4), attenuator.Pin(0X20, 5), attenuator.Pin(0X20, 6)) for att in self.att_pins: dut.set_attenuation(att) result_pins = list() for i in range(0, 6): result_pins.append(dut.pin[i].bus.read_pin(dut.pin[i].pin_number)) self.assertSequenceEqual(result_pins, self.att_pins[att]) def test_attennuation_too_big(self): dut = attenuator.Attenuator( attenuator.Pin(0X20, 1), attenuator.Pin(0X20, 2), attenuator.Pin(0X20, 3), attenuator.Pin(0X20, 4), attenuator.Pin(0X20, 5), attenuator.Pin(0X20, 6)) dut.set_attenuation(31.6) result_pins = list() for i in range(0, 6): result_pins.append(dut.pin[i].bus.read_pin(dut.pin[i].pin_number)) self.assertSequenceEqual(result_pins, self.att_pins[31.5]) def test_attennuation_negative(self): dut = attenuator.Attenuator( attenuator.Pin(0X20, 1), attenuator.Pin(0X20, 2), attenuator.Pin(0X20, 3), attenuator.Pin(0X20, 4), attenuator.Pin(0X20, 5), attenuator.Pin(0X20, 6)) dut.set_attenuation(-0.5) result_pins = list() for i in range(0, 6): result_pins.append(dut.pin[i].bus.read_pin(dut.pin[i].pin_number)) self.assertSequenceEqual(result_pins, self.att_pins[0]) if __name__ == '__main__': unittest.main()