Exemple #1
0
        public void Ascii_Basic256()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 256; i++)
            {
                sb.Append((char)i);
            }
            string  s = sb.ToString();
            BVector d = new BVector();

            d.AddAscii(s);
            Assert.AreEqual(s, d.GetAscii());
        }
Exemple #2
0
        /// <summary>
        /// Adds a random value to the specified BVector
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static object WriteRandomValue(BVector d)
        {
            int           stringLen = 0;
            StringBuilder sb;
            string        unicodeChars;

            object result    = null;
            var    rnd       = new CryptoRandom();
            int    valueType = rnd.Next(12);
            bool   nullable  = rnd.Next(2) == 1;

            d.Add1(nullable);
            d.Add8((byte)valueType);

            switch (valueType)
            {
            case 0:     // int
                result = rnd.Next(int.MinValue, int.MaxValue);
                if (nullable)
                {
                    d.Add((int?)result);
                }
                else
                {
                    d.Add((int)result);
                }
                break;

            case 1:     // long
                result = rnd.NextLong(long.MinValue, long.MaxValue);
                if (nullable)
                {
                    d.Add((long?)result);
                }
                else
                {
                    d.Add((long)result);
                }
                break;

            case 2:     // short
                result = (short)rnd.Next(short.MinValue, short.MaxValue);
                if (nullable)
                {
                    d.Add((short?)result);
                }
                else
                {
                    d.Add((short)result);
                }
                break;

            case 3:     // byte
                result = (byte)rnd.Next(byte.MinValue, byte.MaxValue);
                if (nullable)
                {
                    d.Add((byte?)result);
                }
                else
                {
                    d.Add((byte)result);
                }
                break;

            case 4:     // string (unicode)
                stringLen    = rnd.Next(0, 1000);
                sb           = new StringBuilder();
                unicodeChars = GetUnicodeCharacters();
                for (int i = 0; i < stringLen; i++)
                {
                    int index = rnd.Next(unicodeChars.Length);
                    sb.Append(unicodeChars[index]);
                }
                result = sb.ToString();
                d.Add((string)result);
                break;

            case 5:     // ascii
                stringLen = rnd.Next(0, 1000);
                sb        = new StringBuilder();
                for (int i = 0; i < stringLen; i++)
                {
                    int ascii = rnd.Next(255);
                    sb.Append((char)ascii);
                }
                result = sb.ToString();
                d.AddAscii((string)result);
                break;

            case 6:     // DateTime
                result = new DateTime(rnd.NextLong(DateTime.MinValue.Ticks, DateTime.MaxValue.Ticks));
                if (nullable)
                {
                    d.Add((DateTime?)result);
                }
                else
                {
                    d.Add((DateTime)result);
                }
                break;

            case 7:     // decimal
                result = (decimal)rnd.NextDouble() * decimal.MaxValue;
                if (nullable)
                {
                    d.Add((decimal?)result);
                }
                else
                {
                    d.Add((decimal)result);
                }
                break;

            case 8:     // double
                result = rnd.NextDouble() * double.MaxValue;
                if (nullable)
                {
                    d.Add((double?)result);
                }
                else
                {
                    d.Add((double)result);
                }
                break;

            case 9:     // bool
                result = rnd.Next(2) == 1;
                d.Add1((bool)result);
                break;

            case 10:     // TimeSpan
                result = new TimeSpan(rnd.NextLong(TimeSpan.MinValue.Ticks, TimeSpan.MaxValue.Ticks));
                d.Add((TimeSpan)result);
                break;

            case 11:     // byte[]
                result = rnd.NextBytes(rnd.Next(1000));
                d.Add((byte[])result);
                break;
            }

            return(result);
        }