FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added SortByLastNameAsyncWithAction_ReturnsTheRightSortedList_WhenCor… · ByteDecoder/RoyalLibrary@0170774 · GitHub

Repository navigation

Commit 0170774

Browse files
committed
Added SortByLastNameAsyncWithAction_ReturnsTheRightSortedList_WhenCorrectSourceIsProvided xUnit test
1 parent 5821cf2 commit 0170774

2 files changed

Lines changed: 66 additions & 120 deletions

File tree

‎src/RoyalLibrary.Tests/SortingExtensionTests.cs‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ namespace RoyalLibrary.Tests
88
{
99
public class SortingExtensionTests
1010
{
11+
// Arrange
12+
private readonly List<string> _unsortedInputList = new List<string>()
13+
{
14+
"Ai Bi Bu",
15+
"Ai Bi Az",
16+
"Na Za",
17+
"Xa Ma Co",
18+
"AA bb"
19+
};
20+
private readonly List<string> _expectedOutputList = new List<string>()
21+
{
22+
"Ai Bi Az",
23+
"AA bb",
24+
"Ai Bi Bu",
25+
"Xa Ma Co",
26+
"Na Za",
27+
};
28+
1129
[Fact]
1230
public async void SortByLastNameAsync_ThrowArgumentNullException_WhenSourceIsNull()
1331
{
@@ -36,29 +54,25 @@ public async void SortByLastNameAsync_ReturnsSourceCOllections_WhenSourceSizeIsZ
3654
public async void SortByLastNameAsync_ReturnsTheRightSortedList_WhenCorrectSourceIsProvided()
3755
{
3856
// Arrange
39-
var unSortedlist = new List<string>()
40-
{
41-
"Ai Bi Bu",
42-
"Ai Bi Az",
43-
"Na Za",
44-
"Xa Ma Co",
45-
"AA bb"
46-
};
47-
var expected = new List<string>()
48-
{
49-
"Ai Bi Az",
50-
"AA bb",
51-
"Ai Bi Bu",
52-
"Xa Ma Co",
53-
"Na Za",
57+
// Act
58+
var actual = await _unsortedInputList.SortByLastNameAsync();
59+
60+
// Assert
61+
Assert.True(_expectedOutputList[1] == actual.ElementAt(1));
62+
}
5463

55-
};
64+
[Fact]
65+
public async void SortByLastNameAsyncWithAction_ReturnsTheRightSortedList_WhenCorrectSourceIsProvided()
66+
{
67+
// Arrange
68+
var count = 0;
5669

5770
// Act
58-
var actual = await unSortedlist.SortByLastNameAsync();
71+
var actual = await _unsortedInputList.SortByLastNameAsync(_ => count++);
5972

6073
// Assert
61-
Assert.True(expected[1] == actual.ElementAt(1));
74+
Assert.True(_expectedOutputList[1] == actual.ElementAt(1));
75+
Assert.Equal(5, count);
6276
}
6377
}
6478
}

‎src/RoyalLibrary/SortingExtensions.cs‎

Lines changed: 34 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace ByteDecoder.RoyalLibrary
77
{
8-
98
/// <summary>
109
/// Advanced sorting for collections
1110
/// </summary>
@@ -16,131 +15,59 @@ public static class SortingExtensions
1615
/// </summary>
1716
/// <param name="source"></param>
1817
/// <returns>An Enumerable sorted collection by LastName</returns>
19-
public async static Task<IEnumerable<string>> SortByLastNameAsync(this IEnumerable<string> source)
18+
public async static Task<ICollection<string>> SortByLastNameAsync(this IEnumerable<string> source)
2019
{
2120
if (source == null)
2221
throw new ArgumentNullException(nameof(source));
2322

2423
int rowsCount = source.Count();
2524

26-
if (rowsCount == 0) return source;
25+
if (rowsCount == 0) return (ICollection<string>)source;
2726

2827
byte lastspaceIndex = 0;
2928
string firstName, lastName = null;
30-
var tempDic = new MultiKeysValue() { Capacity = rowsCount };
31-
var tempList = new List<string>(rowsCount);
32-
var tempResult = new List<string>(rowsCount);
29+
var orderedList = new SortedList<string, string>() { Capacity = rowsCount };
3330

3431
await Task.Run(() =>
3532
{
3633
foreach (var item in source)
3734
{
38-
3935
var spaces = item.Count(char.IsWhiteSpace);
4036
lastspaceIndex = (byte)item.IndexOfNth(' ', spaces - 1);
4137

42-
//get the whole firstname(s) , then the lastName, add them to tempDic which will be used
43-
//to get firstname(s) by lastName
44-
//lastname will be added to the temp list and sorted everytime
4538
firstName = item.Substring(0, lastspaceIndex);
4639
var lastNameLength = item.Length - firstName.Length;
4740
lastName = item.Substring(lastspaceIndex, lastNameLength);
4841

49-
tempDic.Add(firstName, lastName);
50-
tempList.Add(lastName);
51-
}
52-
tempList.Sort();
53-
54-
}).ContinueWith(async (filtering) =>
55-
{
56-
foreach (var LastName in tempList)
57-
{
58-
//get the firstName(s) from tempDic by lastName
59-
60-
var dicfirstName = tempDic.AsParallel().First(x => x.Value == LastName).Key;
61-
await Task.Delay(0);//fake await to run everything smoothly
62-
//combine names again with sorted
63-
tempResult.Add(dicfirstName + LastName);
64-
42+
orderedList.Add(lastName, firstName);
6543
}
6644
});
67-
tempDic = null;
68-
tempList = null;
69-
return tempResult;
70-
71-
72-
73-
7445

46+
return orderedList.AsParallel()
47+
.AsOrdered()
48+
.Select((fullName) => fullName.Value + fullName.Key).ToList();
7549
}
50+
7651
/// <summary>
7752
/// returns a new sorted ICollection
7853
/// </summary>
7954
/// <param name="source"></param>
8055
/// <param name="action"></param>
8156
/// <returns>An Enumerable sorted collection by LastName</returns>
82-
public async static Task<ICollection<string>> RoyalSortLastAsync(this IEnumerable<string> source, Action<string> action)
57+
public async static Task<IEnumerable<string>> SortByLastNameAsync(this IEnumerable<string> source, Action<string> action)
8358
{
84-
int rowsCount = source.Count();
85-
byte lastspaceIndex = 0;
86-
string firstName, lastName = null;
87-
var tempDic = new MultiKeysValue() { Capacity = rowsCount };
88-
var tempList = new List<string>(rowsCount);
89-
var tempResult = new List<string>(rowsCount);
90-
91-
await Task.Run(() =>
92-
{
93-
foreach (var item in source)
94-
{
95-
96-
var spaces = item.Count(char.IsWhiteSpace);
97-
lastspaceIndex = (byte)item.IndexOfNth(' ', spaces - 1);
98-
99-
//get the whole firstname(s) , then the lastName, add them to tempDic which will be used
100-
//to get firstname(s) by lastName
101-
//lastname will be added to the temp list and sorted everytime
102-
firstName = item.Substring(0, lastspaceIndex);
103-
var lastNameLength = item.Length - firstName.Length;
104-
lastName = item.Substring(lastspaceIndex, lastNameLength);
105-
106-
tempDic.Add(firstName, lastName);
107-
tempList.Add(lastName);
108-
109-
110-
111-
}
112-
tempList.Sort();
113-
114-
}).ContinueWith(async (filtering) =>
115-
{
116-
foreach (var LastName in tempList)
117-
{
118-
//get the firstName from tempDic by lastName
119-
120-
var dicfirstName = tempDic.AsParallel().First(x => x.Value == LastName).Key;
121-
await Task.Delay(0);//fake await to run everything smoothly
122-
//combine names again with sorted
123-
tempResult.Add(dicfirstName + LastName);
124-
125-
}
126-
});
127-
tempDic = null;
128-
tempList = null;
129-
130-
foreach (var item in tempResult.AsParallel())
131-
{
132-
action.Invoke(item);
133-
}
134-
135-
return tempResult;
136-
137-
138-
139-
140-
59+
var sortedList = await source.SortByLastNameAsync();
60+
sortedList.AsParallel().ForAll(action);
61+
return sortedList;
14162
}
14263

143-
64+
/// <summary>
65+
///
66+
/// </summary>
67+
/// <param name="str"></param>
68+
/// <param name="value"></param>
69+
/// <param name="nth"></param>
70+
/// <returns></returns>
14471
private static int IndexOfNth(this string str, char value, int nth)
14572
{
14673
if (nth < 0)
@@ -156,16 +83,21 @@ private static int IndexOfNth(this string str, char value, int nth)
15683
return offset;
15784
}
15885

159-
160-
}
161-
class MultiKeysValue : List<KeyValuePair<string, string>>
162-
{
163-
164-
165-
public void Add(string key, string value)
86+
/// <summary>
87+
///
88+
/// </summary>
89+
class MultiKeysValue : List<KeyValuePair<string, string>>
16690
{
167-
var element = new KeyValuePair<string, string>(key, value);
168-
this.Add(element);
91+
/// <summary>
92+
///
93+
/// </summary>
94+
/// <param name="key"></param>
95+
/// <param name="value"></param>
96+
public void Add(string key, string value)
97+
{
98+
var element = new KeyValuePair<string, string>(key, value);
99+
this.Add(element);
100+
}
169101
}
170102
}
171-
}
103+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL