< Summary

Information
Class: Morpho25.Utility.Util
Assembly: Morpho25
File(s): D:\a\Morpho\Morpho\project\Morpho\Morpho25\Utility\Util.cs
Line coverage
11%
Covered lines: 8
Uncovered lines: 62
Coverable lines: 70
Total lines: 151
Line coverage: 11.4%
Branch coverage
9%
Covered branches: 2
Total branches: 22
Branch coverage: 9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Accumulate()100%2100%
ChunkBy(...)0%60%
ConvertToNumber(...)100%10%
FilterByMinMax(...)0%60%
ClosestValue(...)0%20%
CreateXmlSection(...)0%60%

File(s)

D:\a\Morpho\Morpho\project\Morpho\Morpho25\Utility\Util.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Xml;
 5
 6namespace Morpho25.Utility
 7{
 8    /// <summary>
 9    /// Util class.
 10    /// </summary>
 11    public static class Util
 12    {
 13        /// <summary>
 14        /// From °C to Kelvin.
 15        /// </summary>
 16        public const double TO_KELVIN = 273.15;
 17
 18        /// <summary>
 19        /// Accumulate list of double.
 20        /// </summary>
 21        /// <param name="sequence">List of double.</param>
 22        /// <returns>New list of value with partial sums.</returns>
 23        public static IEnumerable<double> Accumulate(IEnumerable<double> sequence)
 2224        {
 2225            double sum = 0;
 129626            foreach (var item in sequence)
 61527            {
 61528                sum += item;
 61529                yield return sum;
 61530            }
 2231        }
 32
 33        /// <summary>
 34        /// Divide a list into chunks.
 35        /// </summary>
 36        /// <typeparam name="T">Type to use</typeparam>
 37        /// <param name="source">List to transform.</param>
 38        /// <param name="chunkSize">Size of the chunk.</param>
 39        /// <returns>List of chunks.</returns>
 40        public static List<List<T>> ChunkBy<T>(this List<T> source,
 41            int chunkSize)
 042        {
 043            return source
 044                .Select((x, i) => new { Index = i, Value = x })
 045                .GroupBy(x => x.Index / chunkSize)
 046                .Select(x => x.Select(v => v.Value).ToList())
 047                .ToList();
 048        }
 49
 50        /// <summary>
 51        /// Convert a cell to number.
 52        /// </summary>
 53        /// <param name="cell">Cell to convert.</param>
 54        /// <returns>List of values.</returns>
 55        public static List<double> ConvertToNumber(string cell)
 056        {
 057            var spacing = cell.Split(',')
 058              .Select(v => Convert.ToDouble(v))
 059              .ToList();
 60
 061            return spacing;
 062        }
 63
 64        /// <summary>
 65        /// Return a sublist of an array using upper and lower limits.
 66        /// </summary>
 67        /// <param name="sequence">List to use.</param>
 68        /// <param name="upperLimit">Upper limit.</param>
 69        /// <param name="lowerLimit">Lower limit.</param>
 70        /// <returns>New list of values.</returns>
 71        public static IEnumerable<double> FilterByMinMax(double[] sequence,
 72            double upperLimit, double lowerLimit)
 073        {
 074            List<double> values = new List<double>();
 75
 076            foreach (double value in sequence)
 077            {
 078                if (value < upperLimit && value > lowerLimit)
 079                    values.Add(value);
 080            }
 81
 082            return values;
 083        }
 84
 85        /// <summary>
 86        /// Using an array get the index of the closest value
 87        /// by a number.
 88        /// </summary>
 89        /// <param name="sequence">Array to use.</param>
 90        /// <param name="number">Number to use to get the
 91        /// closest value.</param>
 92        /// <returns>Return the index of the closest value.</returns>
 93        public static int ClosestValue(double[] sequence,
 94            double number)
 095        {
 096            double value = sequence.Aggregate((a, b) => Math.Abs(
 097                a - number) < Math.Abs(b - number) ? a : b);
 098            return Array.IndexOf(sequence, value);
 099        }
 100
 101        /// <summary>
 102        /// Utility method to create envimet XML part.
 103        /// </summary>
 104        /// <param name="w">XML writer.</param>
 105        /// <param name="sectionTitle">Title of the section.</param>
 106        /// <param name="tags">Tags of the section.</param>
 107        /// <param name="values">Valued of the section.</param>
 108        /// <param name="flag">0, 1, 2 for section type.</param>
 109        /// <param name="attributes">Attributes of the root.</param>
 110        public static void CreateXmlSection(XmlTextWriter w,
 111            string sectionTitle, string[] tags, string[] values,
 112            int flag, string[] attributes)
 0113        {
 0114            w.WriteStartElement(sectionTitle);
 0115            w.WriteString("\n ");
 0116            foreach (var item in tags.Zip(values, (a, b) => new { A = a, B = b }))
 0117            {
 0118                if (flag == 0)
 0119                {
 0120                    w.WriteStartElement(item.A);
 0121                    w.WriteString(item.B);
 0122                    w.WriteEndElement();
 0123                }
 0124                else if (flag == 1)
 0125                {
 0126                    w.WriteStartElement(item.A);
 0127                    w.WriteAttributeString("", "type", null, attributes[0]);
 0128                    w.WriteAttributeString("", "dataI", null, attributes[1]);
 0129                    w.WriteAttributeString("", "dataJ", null, attributes[2]);
 0130                    w.WriteString(item.B);
 0131                    w.WriteEndElement();
 0132                }
 133                else
 0134                {
 0135                    w.WriteStartElement(item.A);
 0136                    w.WriteAttributeString("", "type", null, attributes[0]);
 0137                    w.WriteAttributeString("", "dataI", null, attributes[1]);
 0138                    w.WriteAttributeString("", "dataJ", null, attributes[2]);
 0139                    w.WriteAttributeString("", "zlayers", null, attributes[3]);
 0140                    w.WriteAttributeString("", "defaultValue", null, attributes[4]);
 0141                    w.WriteString(item.B);
 0142                    w.WriteEndElement();
 0143                }
 0144                w.WriteString("\n ");
 145
 0146            }
 0147            w.WriteEndElement();
 0148            w.WriteString("\n");
 0149        }
 150    }
 151}