Commit a169e258 authored by HankG's avatar HankG

Combine PostComparison ascending/descending into one comparer type

parent f20e5558
......@@ -3,16 +3,22 @@ using MySocialPortalLib.Model;
namespace MySocialPortalLib.Util
{
public class PostComparisonAscending: IComparer<Post>
public class PostComparison: IComparer<Post>
{
private readonly int _multiplier;
public PostComparison(bool ascending)
{
_multiplier = ascending ? 1 : -1;
}
public int Compare(Post x, Post y)
{
if (x == null || y == null)
{
return 0;
}
return x.PostDateTime.CompareTo(y.PostDateTime);
return _multiplier * x.PostDateTime.CompareTo(y.PostDateTime);
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using MySocialPortalLib.Model;
using MySocialPortalLib.Util;
using Xunit;
using Xunit.Abstractions;
namespace MySocialPortalLibTest.Util
{
public class PostComparisonDescendingTest
{
private readonly ITestOutputHelper _testOutputHelper;
public PostComparisonDescendingTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void TestComparison()
{
var random = new Random();
var count = 10;
var list = new List<Post>();
for (var i = 0; i < count; i++)
{
var post = new Post
{
PostDateTime = DateTimeOffset.Now.AddDays(random.Next(0,100))
};
list.Add(post);
}
list.Sort(new PostComparisonDescending());
for (var i = 0; i < count - 1; i++)
{
_testOutputHelper.WriteLine(list[i].PostDateTime.ToString());
Assert.True(list[i].PostDateTime >= list[i+1].PostDateTime);
}
}
}
}
\ No newline at end of file
......@@ -7,17 +7,17 @@ using Xunit.Abstractions;
namespace MySocialPortalLibTest.Util
{
public class PostComparisonAscendingTest
public class PostComparisonTest
{
private readonly ITestOutputHelper _testOutputHelper;
public PostComparisonAscendingTest(ITestOutputHelper testOutputHelper)
public PostComparisonTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void TestComparison()
public void TestAscendingComparison()
{
var random = new Random();
var count = 10;
......@@ -31,7 +31,7 @@ namespace MySocialPortalLibTest.Util
list.Add(post);
}
list.Sort(new PostComparisonAscending());
list.Sort(new PostComparison(true));
for (var i = 0; i < count - 1; i++)
{
_testOutputHelper.WriteLine(list[i].PostDateTime.ToString());
......@@ -39,5 +39,27 @@ namespace MySocialPortalLibTest.Util
}
}
[Fact]
public void TestDescending()
{
var random = new Random();
var count = 10;
var list = new List<Post>();
for (var i = 0; i < count; i++)
{
var post = new Post
{
PostDateTime = DateTimeOffset.Now.AddDays(random.Next(0,100))
};
list.Add(post);
}
list.Sort(new PostComparison(false));
for (var i = 0; i < count - 1; i++)
{
_testOutputHelper.WriteLine(list[i].PostDateTime.ToString());
Assert.True(list[i].PostDateTime >= list[i+1].PostDateTime);
}
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment