コード例 #1
0
ファイル: ConvertTest.cs プロジェクト: x64/bltoolkit
        public void StringTest()
        {
            string testStr = "123";

            Assert.AreEqual(123, Convert.ToSByte(testStr));
            Assert.AreEqual(123, Convert.ToInt16(testStr));
            Assert.AreEqual(123, Convert.ToInt32(testStr));
            Assert.AreEqual(123, Convert.ToInt64(testStr));

            Assert.AreEqual(123, Convert.ToByte(testStr));
            Assert.AreEqual(123, Convert.ToUInt16(testStr));
            Assert.AreEqual(123, Convert.ToUInt32(testStr));
            Assert.AreEqual(123, Convert.ToUInt64(testStr));

            Assert.AreEqual(123.0m, Convert.ToDecimal(testStr));
            Assert.AreEqual(123.0f, Convert.ToSingle(testStr));
            Assert.AreEqual(123.0, Convert.ToDouble(testStr));

            string zeroStr = "0";

            Assert.AreEqual(0, Convert.ToSByte((string)null));
            Assert.AreEqual(0, Convert.ToSByte(zeroStr));
            Assert.AreEqual(0, Convert.ToInt16(zeroStr));
            Assert.AreEqual(0, Convert.ToInt32(zeroStr));
            Assert.AreEqual(0, Convert.ToInt64(zeroStr));

            Assert.AreEqual(0, Convert.ToByte(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt16(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt32(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt64(zeroStr));

            Assert.AreEqual(0.0m, Convert.ToDecimal(zeroStr));
            Assert.AreEqual(0.0f, Convert.ToSingle(zeroStr));
            Assert.AreEqual(0.0, Convert.ToDouble(zeroStr));

            Assert.IsTrue(Convert.ToBoolean("True"));
            Assert.IsTrue(Convert.ToBoolean("true"));

            Assert.IsFalse(Convert.ToBoolean("false"));
            Assert.IsFalse(Convert.ToBoolean("FALSE"));
            Assert.IsFalse(Convert.ToBoolean((string)null));

            Assert.AreEqual('T', Convert.ToChar("T"));
            Assert.AreEqual(0, Convert.ToChar((string)null));

            Assert.AreEqual(DateTime.Today, Convert.ToDateTime(DateTime.Today.ToString()));
            Assert.AreEqual(DateTime.MinValue, Convert.ToDateTime((string)null));

            Assert.AreEqual(TimeSpan.FromDays(123.0), Convert.ToTimeSpan("123"));
            Assert.AreEqual(TimeSpan.MinValue, Convert.ToTimeSpan((string)null));

            Assert.AreEqual(typeof(int).GUID, Convert.ToGuid(typeof(int).GUID.ToString()));
            Assert.AreEqual(Guid.Empty, Convert.ToGuid((string)null));

            Assert.AreEqual(typeof(int), Convert.ToType("System.Int32, mscorlib"));
            Assert.IsNull(Convert.ToType((string)null));
        }
コード例 #2
0
ファイル: ConvertTest.cs プロジェクト: x64/bltoolkit
        public void ByteArrayTest()
        {
            Byte[] bytes;
            object value;

            bytes = new byte[] { 123 };
            value = (byte)123;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToByte(bytes));

            bytes = new byte[] { 123, 0 };
            value = (short)123;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToInt16(bytes));

            bytes = new byte[] { 210, 2, 150, 73 };
            value = 1234567890;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToInt32(bytes));

            bytes = new byte[] { (byte)'t', (byte)'s', (byte)'t' };
            value = "tst";
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToString(bytes));

            bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 70 };
            value = typeof(System.Runtime.InteropServices.ComTypes.IStream).GUID;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToGuid(bytes));

            bytes = new byte[] { 210, 10, 31, 235, 140, 169, 84, 171, 0, 0, 0, 0, 0, 0, 0, 0 };
            value = 12345678901234567890m;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToDecimal(bytes));

            bytes = new byte[] { 121, 233, 246, 66 };
            value = 123.456f;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToSingle(bytes));

            bytes = new byte[] { 119, 190, 159, 26, 47, 221, 94, 64 };
            value = 123.456;
            Assert.AreEqual(bytes, Convert.ToByteArray(value));
            Assert.AreEqual(value, Convert.ToDouble(bytes));
        }