コード例 #1
0
        public void TestUInt16ToInt16OrDefault()
        {
            // Test conversion of source type minimum value
            UInt16 source = UInt16.MinValue;

            Assert.IsInstanceOfType(source, typeof(UInt16));
            Int16?result = source.ToInt16OrDefault((short)86);

            Assert.AreEqual((short)0, result);
            Assert.IsInstanceOfType(result, typeof(Int16));
            // Test conversion of source type value 42 to target type
            source = (ushort)42;
            Assert.IsInstanceOfType(source, typeof(UInt16));
            result = source.ToInt16OrDefault((short)86);
            Assert.AreEqual((short)42, result);
            Assert.IsInstanceOfType(result, typeof(Int16));

            // Test conversion of source type maximum value
            source = UInt16.MaxValue;
            Assert.IsInstanceOfType(source, typeof(UInt16));
            result = source.ToInt16OrDefault((short)86);
            // Here we would expect this conversion to fail (and return the default value of (short)86),
            // since the source type's maximum value (65535) is greater than the target type's maximum value (32767).
            Assert.AreEqual((short)86, result);
            Assert.IsInstanceOfType(result, typeof(Int16));
        }