forked from nietras/1brc.cs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrcTest.cs
103 lines (86 loc) · 3.31 KB
/
BrcTest.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace nietras.Test;
[TestClass]
public class BrcTest
{
public record Test(string InputFilePath, string ExpectedOutputFilePath);
static readonly Test[] s_tests = null!;
static BrcTest()
{
s_tests = GetTests();
}
public static IEnumerable<object[]> Tests => s_tests.Select(t => new object[] { t });
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_MemoryMapped_Threads_1(Test test) =>
RunAndAssert(Brc.RunMemoryMapped, test, threadCount: 1);
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_MemoryMapped_Threads_8(Test test) =>
RunAndAssert(Brc.RunMemoryMapped, test, threadCount: 8);
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_MemoryMapped_Threads_ProcessorCount(Test test) =>
RunAndAssert(Brc.RunMemoryMapped, test, threadCount: Environment.ProcessorCount);
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_RandomAccess_Threads_1(Test test) =>
RunAndAssert(Brc.RunRandomAccess, test, threadCount: 1);
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_RandomAccess_Threads_8(Test test) =>
RunAndAssert(Brc.RunRandomAccess, test, threadCount: 8);
[TestMethod]
[DynamicData(nameof(Tests))]
public void BrcTest_E2E_RandomAccess_Threads_ProcessorCount(Test test) =>
RunAndAssert(Brc.RunRandomAccess, test, threadCount: Environment.ProcessorCount);
static void RunAndAssert(Func<string, int, (string Result, Brc.Timings Timings)> run, Test test, int threadCount)
{
Trace.WriteLine($"{test} {nameof(threadCount)} = {threadCount}");
var actual = run(test.InputFilePath, threadCount).Result;
var expected = Encoding.UTF8.GetString(File.ReadAllBytes(test.ExpectedOutputFilePath));
Trace.WriteLine(expected);
Trace.WriteLine(actual);
if (!string.Equals(expected, actual, StringComparison.Ordinal))
{
var index = IndexOfNotEqual(expected, actual);
Assert.AreEqual(expected, actual, $"{index} {expected.Length} {actual.Length} {test}");
}
}
static int IndexOfNotEqual(string str1, string str2)
{
var minLength = Math.Min(str1.Length, str2.Length);
for (var i = 0; i < minLength; i++)
{
if (str1[i] != str2[i])
{
return i;
}
}
if (str1.Length != str2.Length)
{
return minLength;
}
return -1;
}
static Test[] GetTests()
{
var sourceDirectory = GetSourceDirectory();
var testDirectory = Path.Combine(sourceDirectory, "../../test/");
var inputFiles = Directory.GetFiles(testDirectory, "measurements*.txt").Select(Path.GetFullPath);
var tests = inputFiles.Select(f => new Test(f, f.Replace(".txt", ".out"))).ToArray();
return tests;
}
static string GetSourceDirectory([CallerFilePath] string sourceFilePath = "")
{
var sourceDirectory = Path.GetDirectoryName(sourceFilePath)!;
return sourceDirectory;
}
}