public void MaxTest()
        {
            DateTime earlier = new DateTime(2012, 04, 05);
            DateTime later = new DateTime(2012, 04, 10);

            Assert.AreEqual(later, earlier.Max(later));
            Assert.AreEqual(later, later.Max(earlier));
        }
Example #2
0
        private static IEnumerable<string> GenerateDateXTicks(int howMany, DateTime[] dates)
        {
            var max = dates.Max();
            var min = dates.Min();
            var minInterval = (max - min).TotalSeconds / howMany;
            var result = new List<DateTime>();
            for (int i = 0; i < howMany; ++i)
            {
                result.Add(min.AddSeconds(minInterval * i));
            }

            return result.Select(s => s.ToString("yyyy-MM-dd")).ToArray();
        }
Example #3
0
        public static bool Export(DateTime[] timestamps, Series[] series, TimeSpanOption aggregationPeriod, string filename, BackgroundWorker worker = null, GroupingType groupingType = GroupingType.Centered)
        {
            var progress = 0;
            var firstTimeStamp = timestamps.Min().RoundDown(aggregationPeriod.TimeSpan);
            var finalTimeStamp = timestamps.Max().RoundUp(aggregationPeriod.TimeSpan);
            var totalTimeSpan = finalTimeStamp - firstTimeStamp;
            var sortedTimestamps = timestamps.OrderBy(x => x).ToArray();

            var halfOfTimeSpan = aggregationPeriod.TimeSpan.Subtract(new TimeSpan(aggregationPeriod.TimeSpan.Ticks / 2));

            var aggregatedTimestamps = new List<DateTime>();
            var currentTimestampIndex = 0;
            for (var timestamp = firstTimeStamp; timestamp <= finalTimeStamp; timestamp = timestamp.Add(aggregationPeriod.TimeSpan))
            {
                aggregatedTimestamps.Add(timestamp);
                var inclusiveStart = DateTime.MinValue;
                var exclusiveEnd = DateTime.MaxValue;

                switch (groupingType)
                {
                    case GroupingType.Centered:
                        inclusiveStart = timestamp - halfOfTimeSpan;
                        exclusiveEnd = timestamp + halfOfTimeSpan;
                        break;
                    case GroupingType.Forward:
                        inclusiveStart = timestamp;
                        exclusiveEnd = timestamp.Add(aggregationPeriod.TimeSpan);
                        break;
                }

                var timestampsList = new List<DateTime>();

                while (currentTimestampIndex < sortedTimestamps.Length && sortedTimestamps[currentTimestampIndex] < inclusiveStart)
                    currentTimestampIndex++;

                while (currentTimestampIndex < sortedTimestamps.Length && sortedTimestamps[currentTimestampIndex] < exclusiveEnd)
                {
                    timestampsList.Add(sortedTimestamps[currentTimestampIndex]);
                    currentTimestampIndex++;
                }

                var includedTimestamps = timestampsList.ToArray();

                foreach (var x in series)
                {
                    x.AggregatedValues[timestamp] =
                        x.AggregationModel.AggregationMethod.Aggregate((from includedTimestamp in includedTimestamps where x.Values.ContainsKey(includedTimestamp) select new KeyValuePair<DateTime, float>(includedTimestamp, x.Values[includedTimestamp])).OrderBy(y => y.Key).ToArray(), inclusiveStart, exclusiveEnd, timestamp);
                }

                if (worker == null || !worker.WorkerReportsProgress) continue;

                var currentTimeSpan = timestamp - firstTimeStamp;
                var currentProgress = (int)(currentTimeSpan.TotalSeconds / totalTimeSpan.TotalSeconds * 100);

                if (currentProgress <= progress) continue;

                worker.ReportProgress(currentProgress);
                progress = currentProgress;
            }
            try
            {
                using (var writer = File.CreateText(filename))
                {
                    writer.Write("YYYY/MM/DD hh:mm");
                    foreach (var x in series)
                    {
                        writer.Write("," + x.Name);
                    }

                    foreach (var aggregatedTimestamp in aggregatedTimestamps)
                    {
                        writer.Write(writer.NewLine);
                        writer.Write(aggregatedTimestamp.ToString("yyyy/MM/dd HH:mm"));
                        foreach (var x in series)
                        {
                            writer.Write(",");
                            if (x.AggregatedValues.ContainsKey(aggregatedTimestamp) && !float.IsNaN(x.AggregatedValues[aggregatedTimestamp]))
                            {
                                writer.Write(x.AggregatedValues[aggregatedTimestamp]);
                            }
                        }
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
Example #4
0
        public void TestNormalSequence()
        {
            Assert.That (new int[]{3, 1, 4, 1, 5, 9, 2}.Max (), Is.EqualTo (9));
            Assert.That (new int?[]{3, 1, 4, 1, 5, 9, 2}.Max (), Is.EqualTo (9));
            Assert.That (new long[]{3, 1, 4, 1, 5, 9, 2}.Max (), Is.EqualTo (9L));
            Assert.That (new long?[]{3, 1, 4, 1, 5, 9, 2}.Max (), Is.EqualTo (9L));
            Assert.That (new float[]{3.0F, 1.0F, 4.0F, 1.0F, 5.0F, 9.0F, 2.0F}.Max (), Is.EqualTo (9.0F));
            Assert.That (new float?[]{3.0F, 1.0F, 4.0F, 1.0F, 5.0F, 9.0F, 2.0F}.Max (), Is.EqualTo (9.0F));
            Assert.That (new double[]{3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0}.Max (), Is.EqualTo (9.0));
            Assert.That (new double?[]{3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0}.Max (), Is.EqualTo (9.0));
            Assert.That (new decimal[]{3.0M, 1.0M, 4.0M, 1.0M, 5.0M, 9.0M, 2.0M}.Max (), Is.EqualTo (9.0M));
            Assert.That (new decimal?[]{3.0M, 1.0M, 4.0M, 1.0M, 5.0M, 9.0M, 2.0M}.Max (), Is.EqualTo (9.0M));

            IEnumerable<string> strings = new string[] {
                "Cube",
                "Sphere",
                "Capsule",
                "Cylinder",
                "Plane",
                "Quad"
            };
            Assert.That (strings.Max (), Is.EqualTo ("Sphere"));

            IEnumerable<DateTime> dateTimes = new DateTime[]{
                new DateTime (2014, 3, 21),
                new DateTime (2014, 6, 21),
                new DateTime (2014, 9, 23),
                new DateTime (2014, 12, 22),
            };
            Assert.That (dateTimes.Max (), Is.EqualTo (new DateTime (2014, 12, 22)));
        }
Example #5
0
        public void TestTSourceWithSelector()
        {
            // NonNullable -> Nullable
            IEnumerable<DateTime> dateTimes = new DateTime[]{
                new DateTime (2014, 3, 21),
                new DateTime (2014, 6, 21),
                new DateTime (2014, 9, 23),
                new DateTime (2014, 12, 22),
            };
            Assert.That (dateTimes.Max (it => it.ToString ()), Is.EqualTo ("12/22/2014 00:00:00"));

            // NonNullable -> NonNullable
            IEnumerable<long> dateTimeTics = dateTimes.Select (it => it.Ticks);
            Assert.That (dateTimeTics.Max (it => new DateTime (it)), Is.EqualTo (new DateTime (2014, 12, 22)));

            IEnumerable<string> strings = new string[] {
                "Cube",
                "Sphere",
                "Capsule",
                "Cylinder",
                "Plane",
                "Quad"
            };
            // Nullable -> NonNullable
            Assert.That (strings.Max (it => it.Length), Is.EqualTo (8));
            // Nullable -> Nullable
            Assert.That (strings.Max (it => it.ToUpper ()), Is.EqualTo ("SPHERE"));
        }
Example #6
0
        public void TestNullSelector()
        {
            IEnumerable<string> strings = new string[] {
                "Cube",
                "Sphere",
                "Capsule",
                "Cylinder",
                "Plane",
                "Quad"
            };
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, int>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, int?>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, long>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, long?>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, float>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, float?>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, double>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, double?>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, decimal>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, decimal?>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, string>)null));
            Assert.Throws<ArgumentNullException> (() => strings.Max ((Func<string, DateTime>)null));

            IEnumerable<DateTime> dateTimes = new DateTime[]{
                new DateTime (2014, 3, 21),
                new DateTime (2014, 6, 21),
                new DateTime (2014, 9, 23),
                new DateTime (2014, 12, 22),
            };
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, int>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, int?>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, long>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, long?>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, float>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, float?>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, double>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, double?>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, decimal>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, decimal?>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, string>)null));
            Assert.Throws<ArgumentNullException> (() => dateTimes.Max ((Func<DateTime, DateTime>)null));
        }