-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataGathering.cs
More file actions
104 lines (102 loc) · 3.03 KB
/
DataGathering.cs
File metadata and controls
104 lines (102 loc) · 3.03 KB
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
104
using Perfy.Testing;
using System.Text.Json;
namespace Perfy.DataGathering
{
interface IDataSource
{
public TestCase? GetNextTest();
public event Action? EndOfData;
public IDataSource Clone();
public int ItemsLeft();
}
class InvalidSourceException(string filename, string message) : Exception
{
readonly string Filename = filename;
readonly string ErrorMessage = message;
public override string ToString()
{
return $"Error encountered with file \"{Filename}\":\"{ErrorMessage}\". Developer info:\n{base.ToString()}";
}
}
class SingularSource(TestCase test) : IDataSource
{
TestCase Test = test;
bool hasReturned = false;
public event Action? EndOfData;
public TestCase? GetNextTest()
{
if (!hasReturned)
{
hasReturned = true;
EndOfData?.Invoke();
return Test;
}
else
return null;
}
public int ItemsLeft() => 1;
public IDataSource Clone()
{
return new SingularSource(Test);
}
}
class QueueSource(Queue<TestCase> data) : IDataSource
{
readonly Queue<TestCase> Data = data;
public event Action? EndOfData;
public TestCase? GetNextTest()
{
if (Data.Count == 0)
return null;
TestCase next = Data.Dequeue();
if (Data.Count == 0)
EndOfData?.Invoke();
return Data.Dequeue();
}
public IDataSource Clone()
{
Queue<TestCase> recollect = [];
TestCase[] read = [.. Data];
for (int i = 0; i < Data.Count; i++)
recollect.Enqueue(read[i]);
return new QueueSource(recollect);
}
public int ItemsLeft()
{
return Data.Count;
}
}
class JSDataFile : IDataSource
{
readonly Queue<TestCase> Data;
public event Action? EndOfData;
public JSDataFile(string filePath)
{
if(!File.Exists(filePath))
throw new InvalidSourceException(filePath, "File doesn't exist");
Data = JsonSerializer.Deserialize<Queue<TestCase>>(File.ReadAllText(filePath)) ?? throw new InvalidSourceException(filePath, "Invalid JSON syntax");
}
public TestCase? GetNextTest()
{
if(Data.Count == 0)
return null;
TestCase next = Data.Dequeue();
if (Data.Count == 0)
EndOfData?.Invoke();
return Data.Dequeue();
}
public IDataSource Clone()
{
Queue<TestCase> recollect = [];
TestCase[] read = [.. Data];
for (int i = 0; i < Data.Count; i++)
recollect.Enqueue(read[i]);
return new QueueSource(recollect);
}
public int ItemsLeft()
{
return Data.Count;
}
}
}