-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathExternalMergeSorterTests.cs
66 lines (56 loc) · 1.87 KB
/
ExternalMergeSorterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using Algorithms.Sorters.External;
using Algorithms.Sorters.External.Storages;
using Algorithms.Tests.Helpers;
using NUnit.Framework;
using NUnit.Framework.Internal;
namespace Algorithms.Tests.Sorters.External;
public static class ExternalMergeSorterTests
{
[Test]
public static void ArraySorted(
[Random(0, 1000, 100, Distinct = true)]
int n)
{
// Arrange
var sorter = new ExternalMergeSorter<int>();
var intComparer = new IntComparer();
var (correctArray, testArray) = RandomHelper.GetArrays(n);
var main = new IntInMemoryStorage(testArray);
var temp = new IntInMemoryStorage(new int[testArray.Length]);
// Act
sorter.Sort(main, temp, intComparer);
Array.Sort(correctArray, intComparer);
// Assert
Assert.That(correctArray, Is.EqualTo(testArray));
}
[Test]
public static void ArraySorted_OnDisk(
[Random(0, 1000, 100, Distinct = true)]
int n)
{
// Arrange
var sorter = new ExternalMergeSorter<int>();
var intComparer = new IntComparer();
var (correctArray, testArray) = RandomHelper.GetArrays(n);
var randomizer = Randomizer.CreateRandomizer();
var main = new IntFileStorage($"sorted_{randomizer.GetString(100)}", n);
var temp = new IntFileStorage($"temp_{randomizer.GetString(100)}", n);
var writer = main.GetWriter();
for (var i = 0; i < n; i++)
{
writer.Write(correctArray[i]);
}
writer.Dispose();
// Act
sorter.Sort(main, temp, intComparer);
Array.Sort(correctArray, intComparer);
// Assert
var reader = main.GetReader();
for (var i = 0; i < n; i++)
{
testArray[i] = reader.Read();
}
Assert.That(correctArray, Is.EqualTo(testArray));
}
}