Skip to content

Commit

Permalink
Bumped CSV version and fixed Topology conversion exception
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Apr 9, 2021
1 parent 5d277a6 commit d0e2cba
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
40 changes: 40 additions & 0 deletions src/Acoustics.Shared/Csv/TopologyEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TopologyEnumConverter.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Acoustics.Shared.Csv
{
using System;
using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;

public class TopologyEnumConverter : DefaultTypeConverter
{
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
{
if (Enum.TryParse<Topology>(text, ignoreCase: true, out var value))
{
return value;
}
else
{
return base.ConvertFromString(text, row, memberMapData);
}
}

public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
{
return value switch
{
Topology.Open => nameof(Topology.Exclusive),
Topology.LeftClosedRightOpen => nameof(Topology.MinimumInclusiveMaximumExclusive),
Topology.LeftOpenRightClosed => nameof(Topology.MinimumExclusiveMaximumInclusive),
Topology.Closed => nameof(Topology.Inclusive),
_ => throw new ArgumentException($"`{value}` is not a valid Interval Topology"),
};
}
}
}
6 changes: 3 additions & 3 deletions src/Acoustics.Shared/Extensions/IntervalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,19 @@ public static double ClampvValue(this Interval<double> range, double value)
return value;
}

public static Interval<T> AsInterval<T>(this (T Minimum, T Maximum) pair, Topology topology = Topology.Default)
public static Interval<T> AsInterval<T>(this (T Minimum, T Maximum) pair, Topology topology = default)
where T : struct, IComparable<T>, IFormattable
{
return new Interval<T>(pair.Minimum, pair.Maximum, topology);
}

public static Interval<T> AsIntervalTo<T>(this T minimum, T maximum, Topology topology = Topology.Default)
public static Interval<T> AsIntervalTo<T>(this T minimum, T maximum, Topology topology = default)
where T : struct, IComparable<T>, IFormattable
{
return new Interval<T>(minimum, maximum, topology);
}

public static Interval<T> AsIntervalFromZero<T>(this T maximum, Topology topology = Topology.Default)
public static Interval<T> AsIntervalFromZero<T>(this T maximum, Topology topology = default)
where T : struct, IComparable<T>, IFormattable
{
return new Interval<T>(default, maximum, topology);
Expand Down
11 changes: 4 additions & 7 deletions src/Acoustics.Shared/Interval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// <copyright file="Interval.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>
// <summary>
// Range of Min-Max.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Acoustics.Shared
Expand Down Expand Up @@ -58,7 +55,7 @@ public Interval(T minimum, T maximum)

this.Minimum = minimum;
this.Maximum = maximum;
this.Topology = Topology.Default;
this.Topology = default;
}

public Interval(T minimum, T maximum, Topology topology)
Expand Down Expand Up @@ -144,7 +141,7 @@ public static implicit operator Interval<T>((T Minimum, T Maximum, Topology Topo
return !(first == second);
}

public bool Contains(T scalar, Topology type = Topology.Default)
public bool Contains(T scalar, Topology type = default)
{
return ScalarEqualOrGreaterThanAnchor(scalar, this.Minimum, this.IsMinimumInclusive) &&
ScalarEqualOrLessThanAnchor(scalar, this.Maximum, this.IsMaximumInclusive);
Expand Down Expand Up @@ -247,7 +244,7 @@ public override int GetHashCode()

/// <summary>
/// Gets string representation of the Interval.
/// technially incorrectly representing this value.
/// technically incorrectly representing this value.
/// </summary>
/// <returns>
/// String representation.
Expand All @@ -259,7 +256,7 @@ public override string ToString()

/// <summary>
/// Gets string representation of the Interval.
/// technially incorrectly representing this value.
/// technically incorrectly representing this value.
/// </summary>
/// <param name="suppressName">
/// If true only prints interval data and not type name.
Expand Down
32 changes: 23 additions & 9 deletions tests/Acoustics.Test/Shared/CsvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Acoustics.Test.Shared
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class CsvTests
public class CsvTests : OutputDirectoryTest
{
private static readonly double[,] TestMatrix =
{
Expand All @@ -41,8 +41,7 @@ public class CsvTests
{ 17.0, 18.0, 19.0, 20.0 },
};

private DirectoryInfo outputDirectory;
private FileInfo testFile;
private readonly FileInfo testFile;

static CsvTests()
{
Expand All @@ -53,18 +52,14 @@ static CsvTests()
_ = new ImportedEvent();
}

[TestInitialize]
public void Setup()
public CsvTests()
{
this.outputDirectory = PathHelper.GetTempDir();

this.testFile = PathHelper.GetTempFile(".csv");
this.testFile = PathHelper.GetTempFile(this.TestOutputDirectory, ".csv");
}

[TestCleanup]
public void Cleanup()
{
this.outputDirectory.Delete();
this.testFile.Delete();
}

Expand Down Expand Up @@ -508,6 +503,18 @@ public void IntervalHasATypeConverter()
Assert.AreEqual("Property\r\n\"[0.5, 3)\"\r\n", storage.ToString());
}

public void TestIntervalTopologyRoundTrip()
{
var topology = Topology.Exclusive;

var file = this.TestOutputDirectory.CombineFile("timespan_roundtrip.csv");
Csv.WriteToCsv(file, new[] { new CsvTestClass2() { SomeNumber = 3, SomeTopology = topology } });

var actual = Csv.ReadFromCsv<CsvTestClass2>(file).ToArray();

Assert.AreEqual(Topology.Exclusive, actual.First().SomeTopology);
}

private static void AssertCsvEqual(string expected, FileInfo actual)
{
var lines = File.ReadAllText(actual.FullName);
Expand Down Expand Up @@ -543,6 +550,13 @@ public class CsvTestClass
public TimeSpan SomeTimeSpan { get; set; }
}

public class CsvTestClass2
{
public int SomeNumber { get; set; }

public Topology SomeTopology { get; set; }
}

public class CultureDataTester
{
public double Value { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions tests/Acoustics.Test/Shared/IntervalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void DoubleGrowWorks(double a1, double a2, double b1, double b2, int? rou
[DataRow(100, 300, 300, Topology.LeftOpenRightClosed, true)]
public void DoubleContainsWorks(double a1, double a2, double scalar, Topology? type, bool result)
{
var range = a1.AsIntervalTo(a2, type ?? Topology.Default);
var range = a1.AsIntervalTo(a2, type ?? default);

var actual = type.HasValue ? range.Contains(scalar, type.Value) : range.Contains(scalar);

Expand All @@ -327,7 +327,7 @@ public void DoubleContainsWorks(double a1, double a2, double scalar, Topology? t
[DataRow(100, 300, 300, 400, Topology.LeftOpenRightClosed, true)]
public void DoubleIntersectsWithWorks(double a1, double a2, double b1, double b2, Topology? type, bool result)
{
var a = a1.AsIntervalTo(a2, type ?? Topology.Default);
var a = a1.AsIntervalTo(a2, type ?? default);
var b = b1.AsIntervalTo(b2);

var actual = a.IntersectsWith(b);
Expand Down Expand Up @@ -363,7 +363,7 @@ public void DoubleIntersectsWithWorks(double a1, double a2, double b1, double b2
[DataRow(100, 300, 101, 300, Topology.LeftOpenRightClosed, true, false)]
public void DoubleContainsIntervalWorks(double a1, double a2, double b1, double b2, Topology? type, bool result, bool reverseResult)
{
var a = a1.AsIntervalTo(a2, type ?? Topology.Default);
var a = a1.AsIntervalTo(a2, type ?? default);
var b = b1.AsIntervalTo(b2);

var actual = a.Contains(b);
Expand Down Expand Up @@ -435,7 +435,7 @@ public void DefaultTopologyWorks()
var actual = default(Interval<double>);
Assert.AreEqual(0, actual.Minimum);
Assert.AreEqual(0, actual.Maximum);
Assert.AreEqual(Topology.Default, actual.Topology);
Assert.AreEqual(default, actual.Topology);
Assert.IsTrue(actual.IsEmpty);
Assert.IsTrue(actual.IsMinimumInclusive);
Assert.IsFalse(actual.IsMaximumInclusive);
Expand Down
2 changes: 1 addition & 1 deletion tests/Acoustics.Test/TestHelpers/OutputDirectoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected FileInfo SaveTestOutput(Func<DirectoryInfo, FileInfo> callback)
var newPath = this
.DailyOutputDirectory
.CombinePath(newName);
savedFile.CopyTo(newPath);
savedFile.CopyTo(newPath, overwrite: true);
}

return savedFile;
Expand Down

0 comments on commit d0e2cba

Please sign in to comment.