Пример #1
0
        /// <summary>
        /// Implicit conversion between SmartInt and System.Int16.
        /// </summary>
        public static void AssignmentSingle()
        {
            try
            {
                SmartInt     v1 = 7;
                System.Int16 v2 = 5;

                Console.Write(v1.ToString());  // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);

                v2 = (System.Int16)v1;        // Explicit conversion

                Console.Write(v1.ToString()); // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);

                v2 = 4;
                v1 = v2;                      // Implicit conversion

                Console.Write(v1.ToString()); // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);
            }
            catch (Exception ex)
            {
                Console.Write("Exception:");
                Console.Write(ex.Message);
                Console.WriteLine();
            }
        }
Пример #2
0
        /// <summary>
        /// Implicit conversion between SmartInt and System.Decimal.
        /// </summary>
        public static void AssignmentDecimal()
        {
            try
            {
                SmartInt       v1 = 7;
                System.Decimal v2 = 5;

                Console.Write(v1.ToString());  // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);

                v2 = v1;                      // Implicit conversion

                Console.Write(v1.ToString()); // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);

                v2 = 4;
                v1 = (SmartInt)v2;            // Only explicit conversion allowed

                Console.Write(v1.ToString()); // Use v1.ToString() instead of single v1 to avoid implicit conversion to int
                Console.Write(", ");
                Console.WriteLine(v2);
            }
            catch (Exception ex)
            {
                Console.Write("Exception:");
                Console.Write(ex.Message);
                Console.WriteLine();
            }
        }
Пример #3
0
        /// <summary>
        /// Parse and error control of SmartInt and System.Int32.
        /// SmartInt takes SmartInt.BadValue value.
        /// System.Int32.Parse throws exception.
        /// </summary>
        public static void Parse1Error()
        {
            try
            {
                SmartInt i = SmartInt.Parse("abc");
                Console.Write("SmartInt:");
                Console.Write(i.ToString());  // Use i.ToString() instead of single i to avoid implicit conversion to int
                Console.Write("  IsBad:");
                Console.WriteLine(i.isBad());

                System.Int32 i2 = System.Int32.Parse("abc");
                Console.Write("System.Int32:");
                Console.WriteLine(i2);
            }
            catch (Exception ex)
            {
                Console.Write("Exception:");
                Console.Write(ex.Message);
                Console.WriteLine();
            }
        }
Пример #4
0
        public void SmartInt_ToString_BruteForce_Test()
        {
            int    test_count = 10000000;
            Random rnd        = new Random();

            for (int i = 0; i < test_count; i++)
            {
                int v  = rnd.Next();
                int vn = -v;

                string s  = v.ToString();
                string sn = vn.ToString();

                SmartInt siv  = v;
                SmartInt sivn = vn;

                string sis  = siv.ToString();
                string sisn = sivn.ToString();

                Assert.IsTrue((sis == s) && (sisn == sn), "ToString " + v.ToString());
            }
        }