コード例 #1
0
        public virtual void test_toByte()
        {
            ProtocolSwitch protocolSwitch = new ProtocolSwitch();

            protocolSwitch.turnOn(0);
            Assert.Equal(1, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(1);
            Assert.Equal(2, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(2);
            Assert.Equal(4, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(3);
            Assert.Equal(8, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(0);
            protocolSwitch.turnOn(1);
            Assert.Equal(3, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(6);
            Assert.Equal(64, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            for (int i = 0; i < 7; ++i)
            {
                protocolSwitch.turnOn(i);
            }
            Assert.Equal(127, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            try
            {
                for (int i = 0; i < 9; ++i)
                {
                    protocolSwitch.turnOn(i);
                }

                protocolSwitch.toByte();
                Assert.Null("Should not reach here!");
            }
            catch (System.ArgumentOutOfRangeException)
            {
            }
        }
コード例 #2
0
        public virtual void test_bitSetToByte()
        {
            var bs = new BitArray(8);

            bs.Set(0, true);
            Assert.Equal(1, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(1, true);
            Assert.Equal(2, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(2, true);
            Assert.Equal(4, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(3, true);
            Assert.Equal(8, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(0, true);
            bs.Set(1, true);
            Assert.Equal(3, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(2, true);
            bs.Set(3, true);
            Assert.Equal(12, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(6, true);
            Assert.Equal(64, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            for (int i = 0; i <= 6; ++i)
            {
                bs.Set(i, true);
            }
            Assert.Equal(127, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            for (int i = 0; i <= 7; ++i)
            {
                bs.Set(i, true);
            }
            Assert.Equal(255, ProtocolSwitch.toByte(bs));


            bs.SetAll(false);
            try
            {
                for (int i = 0; i <= 8; ++i)
                {
                    bs.Set(i, true);
                }
                ProtocolSwitch.toByte(bs);
                Assert.Null("Should not reach here!");
            }
            catch (System.ArgumentOutOfRangeException)
            {
            }
        }