public void FactorOfDivideByZeroTest()
        {
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action test = () => Int64Ex.FactorOf(0, 100);

            test.ShouldThrow <DivideByZeroException>();
        }
Exemple #2
0
        public void PercentOfTestDivideByZero()
        {
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action test = () => Int64Ex.PercentOf(0, (Int64)100);

            test.ShouldThrow <DivideByZeroException>();
        }
Exemple #3
0
        public void PercentOfTest3DivideByZero()
        {
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action test = () => Int64Ex.PercentOf(0, 100);

            Assert.Throws <DivideByZeroException>(test);
        }
        public void FactorOfTest()
        {
            var value       = RandomValueEx.GetRandomInt32();
            var factorNumer = RandomValueEx.GetRandomInt32();

            var expected = factorNumer % value == 0;
            var actual   = Int64Ex.FactorOf(value, factorNumer);

            actual
            .Should()
            .Be(expected);

            actual = Int64Ex.FactorOf(10, 100);
            actual
            .Should()
            .Be(true);

            actual = Int64Ex.FactorOf(100, 10);
            actual
            .Should()
            .Be(false);

            actual = Int64Ex.FactorOf(11, 100);
            actual
            .Should()
            .Be(false);
        }
Exemple #5
0
        public void IsEvenOTest()
        {
            var actual = Int64Ex.IsEven(0);

            actual.Should()
            .Be(true);
        }
        /// <summary>
        ///     Parses most common JSON date formats
        /// </summary>
        /// <param name="input">JSON value to parse</param>
        /// <param name="culture"></param>
        /// <returns>DateTime</returns>
        public static DateTime ParseJsonDate(this string input, CultureInfo culture)
        {
            const long maxAllowedTimestamp = 253402300799;

            input = input.Replace("\n", "");
            input = input.Replace("\r", "");
            input = input.RemoveSurroundingQuotes();

            long unix;

            if (Int64Ex.TryParse(input, out unix))
            {
                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                return(unix > maxAllowedTimestamp?epoch.AddMilliseconds(unix) : epoch.AddSeconds(unix));
            }

            if (input.Contains("/Date("))
            {
                return(ExtractDate(input, DateRegex, culture));
            }

            if (input.Contains("new Date("))
            {
                input = input.Replace(" ", "");

                // because all whitespace is removed, match against newDate( instead of new Date(
                return(ExtractDate(input, NewDateRegex, culture));
            }

            return(ParseFormattedDate(input, culture));
        }
Exemple #7
0
        public void IsOddTest()
        {
            var value = RandomValueEx.GetRandomInt32();

            var expected = value % 2 != 0;
            var actual   = Int64Ex.IsOdd(value);

            Assert.Equal(expected, actual);
        }
Exemple #8
0
        public void IsEvenTest()
        {
            var value = RandomValueEx.GetRandomInt32();

            var expected = value % 2 == 0;
            var actual   = Int64Ex.IsEven(value);

            actual.Should()
            .Be(expected);
        }
Exemple #9
0
        private static byte[] readEntityBody(Stream stream, string length)
        {
            long len;

#if NETCF
            if (!Int64Ex.TryParse(length, out len))
#else
            if (!Int64.TryParse(length, out len))
#endif
            { throw new ArgumentException("Cannot be parsed.", "length"); }

            if (len < 0)
            {
                throw new ArgumentOutOfRangeException("length", "Less than zero.");
            }

            return(len > 1024
                                   ? stream.ReadBytes(len, 1024)
                                   : len > 0
                                         ? stream.ReadBytes((int)len)
                                         : null);
        }
Exemple #10
0
        internal void AddHeader(string headerField)
        {
            var start = headerField[0];

            if (start == ' ' || start == '\t')
            {
                _context.ErrorMessage = "Invalid header field";
                return;
            }

            var colon = headerField.IndexOf(':');

            if (colon < 1)
            {
                _context.ErrorMessage = "Invalid header field";
                return;
            }

            var name = headerField.Substring(0, colon).Trim();

            if (name.Length == 0 || !name.IsToken())
            {
                _context.ErrorMessage = "Invalid header name";
                return;
            }

            var val = colon < headerField.Length - 1
                                                 ? headerField.Substring(colon + 1).Trim()
                                                 : String.Empty;

            _headers.InternalSet(name, val, false);

            var lower = name.ToLower(CultureInfo.InvariantCulture);

            if (lower == "host")
            {
                if (_userHostName != null)
                {
                    _context.ErrorMessage = "Invalid Host header";
                    return;
                }

                if (val.Length == 0)
                {
                    _context.ErrorMessage = "Invalid Host header";
                    return;
                }

                _userHostName = val;

                return;
            }

            if (lower == "content-length")
            {
                if (_contentLength > -1)
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                long len;
#if SSHARP
                if (!Int64Ex.TryParse(val, out len))
#else
                if (!Int64.TryParse(val, out len))
#endif
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                if (len < 0)
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                _contentLength = len;
            }
        }
        internal void AddHeader(string header)
        {
            var colon = header.IndexOf(':');

            if (colon == -1)
            {
                _context.ErrorMessage = "Invalid header";
                return;
            }

            var name = header.Substring(0, colon).Trim();
            var val  = header.Substring(colon + 1).Trim();

            _headers.InternalSet(name, val, false);

            var lower = name.ToLower(CultureInfo.InvariantCulture);

            if (lower == "accept")
            {
                _acceptTypes = new List <string> (val.SplitHeaderValue(',')).ToArray();
                return;
            }

            if (lower == "accept-language")
            {
                _userLanguages = val.Split(',');
                return;
            }

            if (lower == "content-length")
            {
                long len;
#if SSHARP
                if (Int64Ex.TryParse(val, out len) && len >= 0)
#else
                if (Int64.TryParse(val, out len) && len >= 0)
#endif
                {
                    _contentLength    = len;
                    _contentLengthSet = true;
                }
                else
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                }

                return;
            }

            if (lower == "content-type")
            {
                try
                {
                    _contentType     = HttpUtility.GetMimeType(val);
                    _contentEncoding = HttpUtility.GetEncoding(val);
                }
                catch
                {
                    _context.ErrorMessage = "Invalid Content-Type header";
                }

                return;
            }

            if (lower == "referer")
            {
                _referer = val.ToUri();
            }
        }