|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace TraceGenerator.Tests |
| 6 | +{ |
| 7 | + |
| 8 | + public class TraceGeneratorFixture : IDisposable |
| 9 | + { |
| 10 | + public int RamSizeInMB { get; set; } = 1_000; |
| 11 | + public TraceGenerator TraceGenerator { get; private set; } |
| 12 | + |
| 13 | + public TraceGeneratorFixture() |
| 14 | + { |
| 15 | + TraceGenerator = new TraceGenerator("medium"); |
| 16 | + } |
| 17 | + |
| 18 | + public void Dispose() |
| 19 | + { |
| 20 | + var filename = "instructions-*.trace"; |
| 21 | + var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); |
| 22 | + var files = dir.GetFiles(filename); |
| 23 | + |
| 24 | + foreach (var file in files) |
| 25 | + { |
| 26 | + File.Delete(file.FullName); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public class TraceGeneratorTests : IClassFixture<TraceGeneratorFixture> |
| 32 | + { |
| 33 | + readonly TraceGeneratorFixture fixture; |
| 34 | + |
| 35 | + private readonly int ramSizeInMB = 1_000; |
| 36 | + private readonly int dataBlockSize = 32; |
| 37 | + |
| 38 | + public TraceGeneratorTests(TraceGeneratorFixture fixture) |
| 39 | + { |
| 40 | + this.fixture = fixture; |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void Generating_Trace_File() |
| 45 | + { |
| 46 | + fixture.TraceGenerator.GenerateTraceFile(ramSizeInMB, dataBlockSize); |
| 47 | + var expectedNumberOfRamInstances = 1; |
| 48 | + var filename = "instructions-*.trace"; |
| 49 | + var actualNumberOfRamInstances = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, filename).Length; |
| 50 | + |
| 51 | + Assert.Equal(expectedNumberOfRamInstances, actualNumberOfRamInstances); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void Generating_Trace_File_Twice() |
| 56 | + { |
| 57 | + fixture.TraceGenerator.GenerateTraceFile(ramSizeInMB, dataBlockSize); |
| 58 | + fixture.TraceGenerator.GenerateTraceFile(ramSizeInMB, dataBlockSize); |
| 59 | + var filename = "instructions-*.trace"; |
| 60 | + var expectedNumberOfRamInstances = 1; |
| 61 | + var actualNumberOfRamInstances = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, filename).Length; |
| 62 | + |
| 63 | + Assert.Equal(expectedNumberOfRamInstances, actualNumberOfRamInstances); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Generating_Trace_File_With_Wrong_Size() |
| 68 | + { |
| 69 | + Assert.Throws<Exception>(() => new TraceGenerator("tiny")); |
| 70 | + } |
| 71 | + |
| 72 | + [Theory] |
| 73 | + [InlineData("small", 100)] |
| 74 | + [InlineData("medium", 1_000)] |
| 75 | + [InlineData("large", 10_000)] |
| 76 | + public void Checking_Trace_File_Line_Number(string size, int expectedNumberOfLines) |
| 77 | + { |
| 78 | + var trace = new TraceGenerator(size); |
| 79 | + trace.GenerateTraceFile(ramSizeInMB, dataBlockSize); |
| 80 | + |
| 81 | + using var stream = File.OpenRead(trace.FileName); |
| 82 | + |
| 83 | + var lineCount = 0; |
| 84 | + const char CR = '\r'; |
| 85 | + const char LF = '\n'; |
| 86 | + const char NULL = (char)0; |
| 87 | + var byteBuffer = new byte[1024 * 1024]; |
| 88 | + const int bytesAtTheTime = 4; |
| 89 | + var detectedEOL = NULL; |
| 90 | + var currentChar = NULL; |
| 91 | + |
| 92 | + int bytesRead; |
| 93 | + while ((bytesRead = stream.Read(byteBuffer, 0, byteBuffer.Length)) > 0) |
| 94 | + { |
| 95 | + var i = 0; |
| 96 | + for (; i <= bytesRead - bytesAtTheTime; i += bytesAtTheTime) |
| 97 | + { |
| 98 | + currentChar = (char)byteBuffer[i]; |
| 99 | + |
| 100 | + if (detectedEOL != NULL) |
| 101 | + { |
| 102 | + if (currentChar == detectedEOL) |
| 103 | + { lineCount++; } |
| 104 | + |
| 105 | + currentChar = (char)byteBuffer[i + 1]; |
| 106 | + if (currentChar == detectedEOL) |
| 107 | + { lineCount++; } |
| 108 | + |
| 109 | + currentChar = (char)byteBuffer[i + 2]; |
| 110 | + if (currentChar == detectedEOL) |
| 111 | + { lineCount++; } |
| 112 | + |
| 113 | + currentChar = (char)byteBuffer[i + 3]; |
| 114 | + if (currentChar == detectedEOL) |
| 115 | + { lineCount++; } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + if (currentChar == LF || currentChar == CR) |
| 120 | + { |
| 121 | + detectedEOL = currentChar; |
| 122 | + lineCount++; |
| 123 | + } |
| 124 | + i -= bytesAtTheTime - 1; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + for (; i < bytesRead; i++) |
| 129 | + { |
| 130 | + currentChar = (char)byteBuffer[i]; |
| 131 | + |
| 132 | + if (detectedEOL != NULL) |
| 133 | + { |
| 134 | + if (currentChar == detectedEOL) |
| 135 | + { lineCount++; } |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + if (currentChar == LF || currentChar == CR) |
| 140 | + { |
| 141 | + detectedEOL = currentChar; |
| 142 | + lineCount++; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (currentChar != LF && currentChar != CR && currentChar != NULL) |
| 149 | + { |
| 150 | + lineCount++; |
| 151 | + } |
| 152 | + |
| 153 | + Assert.Equal(expectedNumberOfLines, lineCount); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
0 commit comments