コード例 #1
0
        public static void Deconstruct <T>(
            this T[] array,
            out T item1,
            out T item2,
            out T item3,
            out T item4,
            out T item5,
            out T item6,
            out T item7,
            out T item8,
            out T item9,
            out T item10)
        {
            Code.NotNull(array, nameof(array));
            Code.AssertArgument(array.Length >= 10, nameof(array), _arrayTooShortMsg);

            item1  = array[0];
            item2  = array[1];
            item3  = array[2];
            item4  = array[3];
            item5  = array[4];
            item6  = array[5];
            item7  = array[6];
            item8  = array[7];
            item9  = array[8];
            item10 = array[9];
        }
コード例 #2
0
        private static MetricUnit[] GetMetricUnits(Type metricEnumType)
        {
            var result = new List <MetricUnit>();
            var fields = EnumHelper.GetEnumValues(metricEnumType)
                         .OrderBy(f => Convert.ToDouble(f.Value, CultureInfo.InvariantCulture));

            MetricUnit previousUnit = null;

            foreach (var field in fields)
            {
                var unit = GetMetricUnit(field.UnderlyingField);

                if (previousUnit != null)
                {
                    Code.AssertArgument(
                        previousUnit.AppliesFrom < unit.AppliesFrom,
                        nameof(metricEnumType),
                        $"The applies from value of {metricEnumType.Name}.{unit.EnumValue} ({unit.AppliesFrom.ToInvariantString()}) " +
                        $"should be greater than prev unit {metricEnumType.Name}.{previousUnit.EnumValue} value ({previousUnit.AppliesFrom.ToInvariantString()})");
                }
                previousUnit = unit;

                result.Add(unit);
            }

            Code.AssertArgument(
                result.Count > 0,
                nameof(metricEnumType),
                $"The enum {metricEnumType} should be not empty.");

            return(result.ToArray());
        }
コード例 #3
0
        /// <summary>Initializes a new instance of the <see cref="TargetSourceLines"/> class.</summary>
        /// <param name="targetMethodHandle">Benchmark target method handle.</param>
        /// <param name="primaryAttributeLineNumber">Number of source line that contain primary annotation attribute.</param>
        /// <param name="attributeCandidateLineNumbers">Range of source lines that may contain attribute annotations.</param>
        /// <param name="attributeLineNumbers">Source lines that contain attribute annotations.</param>
        public TargetSourceLines(
            RuntimeMethodHandle targetMethodHandle,
            int primaryAttributeLineNumber,
            Range <int> attributeCandidateLineNumbers,
            Dictionary <RuntimeTypeHandle, int> attributeLineNumbers)
        {
            Code.InRange(primaryAttributeLineNumber, nameof(primaryAttributeLineNumber), 1, int.MaxValue);

            Code.AssertArgument(
                Range.Create(1, int.MaxValue).Contains(attributeCandidateLineNumbers),
                nameof(attributeCandidateLineNumbers),
                "Incorrect candidate line numbers range.");

            Code.AssertArgument(
                attributeCandidateLineNumbers.Contains(primaryAttributeLineNumber),
                nameof(primaryAttributeLineNumber),
                "Incorrect primery attribute line number.");

            DebugCode.AssertArgument(
                attributeLineNumbers.Values.All(l => attributeCandidateLineNumbers.Contains(l)),
                nameof(attributeLineNumbers),
                "Incorrect attribute line numbers.");

            TargetMethodHandle            = targetMethodHandle;
            PrimaryAttributeLineNumber    = primaryAttributeLineNumber;
            AttributeCandidateLineNumbers = attributeCandidateLineNumbers;
            _attributeLineNumbers         = attributeLineNumbers;
        }
コード例 #4
0
        public T Clamp(T value)
        {
            Code.AssertArgument(IsNotEmpty, nameof(value), "Cannot fit the value into empty range.");
            Code.AssertArgument(
                !From.IsExclusiveBoundary, nameof(value), "The clamp range boundary From is exclusive and has no value.");
            Code.AssertArgument(
                !To.IsExclusiveBoundary, nameof(value), "The clamp range boundary To is exclusive and has no value.");

            // case for the positive infinity
            if (!RangeBoundaryFrom <T> .IsValid(value))
            {
                if (To < RangeBoundaryTo <T> .PositiveInfinity)
                {
                    return(To.Value);
                }
                return(value);
            }

            if (From > value)
            {
                return(From.Value);
            }

            if (To < value)
            {
                return(To.Value);
            }

            return(value);
        }
コード例 #5
0
        /// <summary>Adjusts the specified value so that it fits into a range specified.</summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <param name="range">The range the value will be fitted to.</param>
        /// <param name="value">The value to be adjusted.</param>
        /// <exception cref="ArgumentException">The range is empty or any of its boundaries is exclusive.</exception>
        /// <returns>A new value that fits into a range specified</returns>
        public static T Adjust <T>(this Range <T> range, T value)
        {
            Code.AssertArgument(
                range.IsNotEmpty, nameof(range), "Cannot fit the value into empty range.");
            Code.AssertArgument(
                !range.From.IsExclusiveBoundary, nameof(range), "The boundary From is exclusive and has no value.");
            Code.AssertArgument(
                !range.To.IsExclusiveBoundary, nameof(range), "The boundary To is exclusive and has no value.");

            // case for the positive infinity
            if (!RangeBoundaryFrom <T> .IsValid(value))
            {
                if (range.To < RangeBoundaryTo <T> .PositiveInfinity)
                {
                    return(range.To.Value);
                }
                return(value);
            }

            if (range.From > value)
            {
                return(range.From.Value);
            }

            if (range.To < value)
            {
                return(range.To.Value);
            }

            return(value);
        }
コード例 #6
0
ファイル: MetricUnit.cs プロジェクト: agrawalram020/CodeJam-1
        /// <summary>Initializes a new instance of the <see cref="MetricUnit"/> class.</summary>
        /// <param name="name">The name of the metric measurement unit.</param>
        /// <param name="enumValue">Enum value for the measurement unit.</param>
        /// <param name="scaleCoefficient">The scale coefficient for the metric measurement unit.</param>
        /// <param name="appliesFrom">The apply threshold for the metric unit.</param>
        /// <param name="roundingDigits">
        /// Number of fractional digits for storing and comparing metric values
        /// or <c>null</c> if auto-rounding feature shold be used.
        /// </param>
        internal MetricUnit(
            [NotNull] string name,
            [NotNull] Enum enumValue,
            double scaleCoefficient,
            double appliesFrom,
            [CanBeNull] int?roundingDigits)
        {
            Code.NotNullNorEmpty(name, nameof(name));
            Code.NotNull(enumValue, nameof(enumValue));
            Code.AssertArgument(
                scaleCoefficient > 0 && scaleCoefficient <= double.MaxValue,
                nameof(scaleCoefficient),
                "The scale coefficient has to be non-zero positive numeric value.");
            Code.AssertArgument(
                appliesFrom >= 0 && appliesFrom <= double.MaxValue,
                nameof(appliesFrom),
                "The applies from value has to be zero or positive numeric value.");

            Code.InRange(roundingDigits ?? 0, nameof(roundingDigits), 0, MaxRoundingDigits);

            DisplayName      = name;
            IsEmpty          = false;
            EnumValue        = enumValue;
            ScaleCoefficient = scaleCoefficient;
            AppliesFrom      = appliesFrom;
            RoundingDigits   = roundingDigits;
        }
コード例 #7
0
        public static void Deconstruct <T>(
            this IEnumerable <T> enumerable,
            out T item1,
            out T item2,
            out T item3,
            out T item4,
            out T item5,
            out T item6,
            out T item7,
            out T item8,
            out T item9)
        {
            Code.NotNull(enumerable, nameof(enumerable));

            using var enumerator = enumerable.GetEnumerator();
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item1 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item2 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item3 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item4 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item5 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item6 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item7 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item8 = enumerator.Current;
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item9 = enumerator.Current;
        }
コード例 #8
0
        public static void Deconstruct <T>(
            this IList <T> list,
            out T item1,
            out T item2,
            out T item3,
            out T item4,
            out T item5,
            out T item6,
            out T item7,
            out T item8,
            out T item9,
            out T item10)
        {
            Code.NotNull(list, nameof(list));
            Code.AssertArgument(list.Count >= 10, nameof(list), _listTooShortMsg);

            item1  = list[0];
            item2  = list[1];
            item3  = list[2];
            item4  = list[3];
            item5  = list[4];
            item6  = list[5];
            item7  = list[6];
            item8  = list[7];
            item9  = list[8];
            item10 = list[9];
        }
コード例 #9
0
        public static TableDataParser.Parser CreateParser([NotNull] int[] widths)
        {
            Code.NotNull(widths, nameof(widths));
            Code.AssertArgument(widths.Length > 0, nameof(widths), "At least one column must be specified");
            Code.AssertArgument(widths.All(w => w > 0), nameof(widths), "Column width must be greater than 0");

            return((TextReader rdr, ref int ln) => Parse(rdr, ref ln, widths));
        }
コード例 #10
0
        /// <summary>
        /// Inserts an element into the <see cref="Collection{T}"/> at the specified index.
        /// Sets owner for the items being added.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        protected override void InsertItem(int index, TItem item)
        {
            Code.NotNull(item, nameof(item));

            Code.AssertArgument(GetOwner(item) == null, nameof(item), "Cannot add an item as it is mapped to another owner.");
            base.InsertItem(index, item);
            SetOwner(item, Owner);
        }
コード例 #11
0
        /// <summary>Gets diagnosers the metric values.</summary>
        /// <param name="metric">The metric to get diagnosers for.</param>
        /// <returns>Diagnosers for the metric values</returns>
        public IDiagnoser[] GetDiagnosers(MetricInfo metric)
        {
            Code.NotNull(metric, nameof(metric));
            Code.AssertArgument(
                metric.ValuesProvider == this, nameof(metric),
                "Passed ValuesProvider does not match to this one.");

            return(GetDiagnosersOverride(metric));
        }
コード例 #12
0
        public static void Deconstruct <T>(
            this T[] array,
            out T item1)
        {
            Code.NotNull(array, nameof(array));
            Code.AssertArgument(array.Length >= 1, nameof(array), _arrayTooShortMsg);

            item1 = array[0];
        }
コード例 #13
0
        public static void Deconstruct <T>(
            this IList <T> list,
            out T item1)
        {
            Code.NotNull(list, nameof(list));
            Code.AssertArgument(list.Count >= 1, nameof(list), _listTooShortMsg);

            item1 = list[0];
        }
コード例 #14
0
        /// <summary>Gets column provider for the metric values.</summary>
        /// <param name="metric">The metric to get column for.</param>
        /// <returns>Column provider for the metric values</returns>
        public IColumnProvider GetColumnProvider(MetricInfo metric)
        {
            Code.NotNull(metric, nameof(metric));
            Code.AssertArgument(
                metric.ValuesProvider == this, nameof(metric),
                "Passed ValuesProvider does not match to this one.");

            return(GetColumnProviderOverride(metric));
        }
コード例 #15
0
            /// <summary>Adds target key for the document.</summary>
            /// <param name="annotationDocument">The annotation document.</param>
            /// <param name="key">The target key.</param>
            public void AddTargetKey(AnnotationDocument annotationDocument, AnnotationTargetKey key)
            {
                Code.AssertArgument(
                    annotationDocument.Parsed &&
                    ReferenceEquals(annotationDocument.AnnotationContext, Owner),
                    nameof(annotationDocument),
                    "The document is not parsed or does not belongs to the context.");

                _documentsByTargets.Add(key, annotationDocument);
            }
コード例 #16
0
        /// <summary>Gets column provider for the metric values.</summary>
        /// <param name="metric">The metric to get columns for.</param>
        /// <param name="columns">The columns to include.</param>
        /// <returns>Column provider for the metric values</returns>
        public IColumnProvider GetColumnProvider(MetricInfo metric, MetricValueColumns columns)
        {
            Code.NotNull(metric, nameof(metric));
            EnumCode.FlagsDefined(columns, nameof(columns));
            Code.AssertArgument(
                metric.ValuesProvider == this, nameof(metric),
                "Passed ValuesProvider does not match to this one.");

            return(GetColumnProviderOverride(metric, columns));
        }
コード例 #17
0
        public static void Deconstruct <T>(
            this IEnumerable <T> enumerable,
            out T item1)
        {
            Code.NotNull(enumerable, nameof(enumerable));

            using var enumerator = enumerable.GetEnumerator();
            Code.AssertArgument(enumerator.MoveNext(), nameof(enumerable), _enumTooShortMsg);
            item1 = enumerator.Current;
        }
コード例 #18
0
ファイル: CodeTests.cs プロジェクト: viktortat/CodeJam
        public void TestAssertArgument()
        {
            var ex = Assert.Throws <ArgumentException>(
                () => Code.AssertArgument(false, "arg00", "someUniqueMessage {0}", "someUniqueFormatArg"));

            Assert.That(ex.Message, Does.Contain("arg00"));
            Assert.That(ex.Message, Does.Contain("someUniqueMessage"));
            Assert.That(ex.Message, Does.Contain("someUniqueFormatArg"));

            Assert.DoesNotThrow(() => Code.AssertArgument(true, "arg00", "someUniqueMessage"));
        }
コード例 #19
0
        /// <summary>
        /// Replaces the element at the specified index.
        /// Sets owner for the items being added.
        /// Clears owner for the item being removed.
        /// </summary>
        /// <param name="index">The zero-based index of the element to replace.</param>
        /// <param name="item">
        /// The new value for the element at the specified index. The value can be null for reference types.
        /// </param>
        protected override void SetItem(int index, TItem item)
        {
            Code.NotNull(item, nameof(item));
            Code.AssertArgument(GetOwner(item) == null, nameof(item), "Cannot add an item as it is mapped to another owner.");

            var oldItem = this[index];

            Code.BugIf(oldItem == null, "One of items in collection is null.");
            SetOwner(oldItem, null);
            base.SetItem(index, item);
            SetOwner(item, Owner);
        }
コード例 #20
0
        public static void Deconstruct <T>(
            [NotNull] this T[] array,
            out T item1,
            out T item2,
            out T item3)
        {
            Code.NotNull(array, nameof(array));
            Code.AssertArgument(array.Length >= 3, nameof(array), _arrayTooShortMsg);

            item1 = array[0];
            item2 = array[1];
            item3 = array[2];
        }
コード例 #21
0
        public static void Deconstruct <T>(
            [NotNull] this IList <T> list,
            out T item1,
            out T item2,
            out T item3)
        {
            Code.NotNull(list, nameof(list));
            Code.AssertArgument(list.Count >= 3, nameof(list), _listTooShortMsg);

            item1 = list[0];
            item2 = list[1];
            item3 = list[2];
        }
コード例 #22
0
            public string FormatLine(string[] values, int[] columnWidths)
            {
                Code.NotNull(values, nameof(values));
                Code.NotNull(columnWidths, nameof(columnWidths));
                Code.AssertArgument(
                    values.Length <= columnWidths.Length,
                    nameof(columnWidths),
                    "columnWidth array to short");

                return(values
                       .Select(EscapeValue).Zip(columnWidths, (s, w) => s.PadRight(w))
                       .Join(", "));
            }
コード例 #23
0
        public static FieldInfo GetField <TEnum>(TEnum value) where TEnum : struct
        {
            Code.AssertArgument(value is Enum, nameof(value), "The value should Enum.");

            var type = typeof(TEnum);
            var name = Enum.GetName(type, value);

            if (name == null)
            {
                return(null);
            }

            return(type.GetField(name, BindingFlags.Static | BindingFlags.Public));
        }
コード例 #24
0
        public string Test03CodeAssertArgument()
        {
            var result = "";
            var count  = Count;

            for (var i = 0; i < count; i++)
            {
                var arg = GetArg(i);

                Code.AssertArgument(arg != null, nameof(arg), "Argument should be not null");
                result = arg;
            }

            return(result);
        }
コード例 #25
0
        public string Test04CodeAssertArgumentFormat()
        {
            var result = "";
            var count  = Count;

            for (var i = 0; i < count; i++)
            {
                var arg = GetArg(i);

                // ReSharper disable once PassStringInterpolation
                Code.AssertArgument(arg != null, nameof(arg), "Argument {0} should be not null", nameof(arg));
                result = arg;
            }

            return(result);
        }
コード例 #26
0
            public string FormatLine(string[] values, int[] columnWidths)
            {
                Code.NotNull(values, nameof(values));
                Code.NotNull(columnWidths, nameof(columnWidths));
                Code.AssertArgument(
                    values.Length <= columnWidths.Length,
                    nameof(columnWidths),
                    "columnWidth array to short");

                return
                    (EnumerableClass
                     // ReSharper disable once InvokeAsExtensionMethod
                     .Zip(
                         values.Select(EscapeValue),
                         columnWidths,
                         (s, w) => s.PadLeft(w))
                     .Join(", "));
            }
コード例 #27
0
ファイル: PdbHelpers.cs プロジェクト: agrawalram020/CodeJam-1
        public static byte[] TryGetChecksum(ResourceKey resourceKey, PdbChecksumAlgorithm checksumAlgorithm)
        {
            Code.AssertArgument(!resourceKey.IsEmpty, nameof(resourceKey), "The resource key should be non empty.");

            var algName = GetChecksumName(checksumAlgorithm);

            using (var stream = resourceKey.TryGetResourceStream())
            {
                if (stream == null)
                {
                    return(Array <byte> .Empty);
                }

                using (var hashAlgorithm = HashAlgorithm.Create(algName))
                {
                    // ReSharper disable once PossibleNullReferenceException
                    return(hashAlgorithm.ComputeHash(stream));
                }
            }
        }
コード例 #28
0
        /// <summary>
        /// Prints full data table
        /// </summary>
        /// <param name="writer">Instance of <see cref="TextWriter"/> to write to.</param>
        /// <param name="data">Data to write.</param>
        /// <param name="widths">Array of column widths</param>
        /// <param name="indent">The indent.</param>
        public static void Print(
            [NotNull] TextWriter writer,
            [NotNull, ItemNotNull] IEnumerable <string[]> data,
            [NotNull] int[] widths,
            [CanBeNull] string indent = null)
        {
            Code.NotNull(writer, nameof(writer));
            Code.NotNull(data, nameof(writer));
            Code.NotNull(widths, nameof(widths));
            Code.AssertArgument(widths.Length > 0, nameof(widths), "At least one column must be specified");
            Code.AssertArgument(widths.All(w => w > 0), nameof(widths), "Column width must be greater than 0");

            var first = true;

            foreach (var line in data)
            {
                Code.AssertState(line.Length <= widths.Length, $"{nameof(widths)} array to short.");
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.WriteLine();
                }
                if (indent != null)
                {
                    writer.Write(indent);
                }
                for (var i = 0; i < line.Length; i++)
                {
                    var val   = line[i];
                    var width = widths[i];
                    writer.Write(
                        val.Length > width
                                                        ? val.Substring(0, width)
                                                        : val.PadRight(width));
                }
            }
        }
コード例 #29
0
        private static bool TryFixBenchmarkXmlAnnotation(
            AnnotateContext annotateContext, string xmlFileName,
            CompetitionTarget competitionTarget,
            CompetitionState competitionState)
        {
            Code.AssertArgument(
                competitionTarget.CompetitionMetadata != null, nameof(competitionTarget),
                "Competition metadata cannot be null for xml annotations.");

            var xmlAnnotationDoc = annotateContext.TryGetXmlAnnotation(
                xmlFileName,
                competitionTarget.CompetitionMetadata.UseFullTypeName,
                competitionState);

            if (xmlAnnotationDoc == null)
            {
                return(false);
            }

            XmlAnnotations.AddOrUpdateXmlAnnotation(xmlAnnotationDoc, competitionTarget);
            annotateContext.MarkAsChanged(xmlFileName);

            return(true);
        }