From 6a883a625283c72e2cfaeaa6a1db5ff52fab34fd Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sun, 21 Jul 2019 09:10:45 +0100 Subject: [PATCH] First pass at LR35902 fuse test runner code analysis Signed-off-by: Adrian Conlon --- .../LR35902.FuseTest/LR35902.FuseTest.csproj | 2 ++ LR35902/LR35902.FuseTest/Lines.cs | 12 ++++++------ LR35902/LR35902.FuseTest/MemoryDatum.cs | 17 +++++++++++++++-- LR35902/LR35902.FuseTest/RegisterState.cs | 15 ++++++++++----- LR35902/LR35902.FuseTest/Result.cs | 12 ++++++++++-- LR35902/LR35902.FuseTest/Test.cs | 12 ++++++++++-- LR35902/LR35902.FuseTest/TestEvent.cs | 5 +++++ LR35902/LR35902.FuseTest/TestEvents.cs | 7 +++++-- LR35902/LR35902.FuseTest/TestRunner.cs | 10 +++++----- LR35902/LR35902.FuseTest/stylecop.json | 19 +++++++++++++++++++ 10 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 LR35902/LR35902.FuseTest/stylecop.json diff --git a/LR35902/LR35902.FuseTest/LR35902.FuseTest.csproj b/LR35902/LR35902.FuseTest/LR35902.FuseTest.csproj index 1c14afc..cf1e497 100644 --- a/LR35902/LR35902.FuseTest/LR35902.FuseTest.csproj +++ b/LR35902/LR35902.FuseTest/LR35902.FuseTest.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + AllRules.ruleset AnyCPU @@ -59,6 +60,7 @@ + diff --git a/LR35902/LR35902.FuseTest/Lines.cs b/LR35902/LR35902.FuseTest/Lines.cs index 31e30c8..8ee525d 100644 --- a/LR35902/LR35902.FuseTest/Lines.cs +++ b/LR35902/LR35902.FuseTest/Lines.cs @@ -1,5 +1,6 @@ namespace Fuse { + using System; using System.Collections.Generic; using System.IO; @@ -15,22 +16,21 @@ public void Read() { - using (var reader = new StreamReader(this.path)) + using (var reader = File.OpenText(this.path)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); - var ignored = line.StartsWith(";"); + var ignored = line.StartsWith(";", StringComparison.OrdinalIgnoreCase); if (!ignored) { this.lines.Add(line); } } } - if (this.lines.Count > 0) - { - this.position = 0; - } + + // Users should check EndOfFile before using a bad position... + this.position = 0; } public string ReadLine() diff --git a/LR35902/LR35902.FuseTest/MemoryDatum.cs b/LR35902/LR35902.FuseTest/MemoryDatum.cs index 7cd836c..6dafb9c 100644 --- a/LR35902/LR35902.FuseTest/MemoryDatum.cs +++ b/LR35902/LR35902.FuseTest/MemoryDatum.cs @@ -2,21 +2,34 @@ { using System; using System.Collections.Generic; + using System.Collections.ObjectModel; public class MemoryDatum { + private readonly List bytes = new List(); + public ushort Address { get; private set; } = (ushort)EightBit.Mask.Mask16; - public List Bytes { get; } = new List(); + public ReadOnlyCollection Bytes => this.bytes.AsReadOnly(); public void Parse(string line) { + if (string.IsNullOrWhiteSpace(line)) + { + throw new ArgumentNullException(nameof(line)); + } + var tokens = line.Split(new char[] { ' ', '\t' }); this.Parse(tokens); } public void Parse(string[] tokens) { + if (tokens == null) + { + throw new ArgumentNullException(nameof(tokens)); + } + this.Address = Convert.ToUInt16(tokens[0], 16); var finished = false; @@ -26,7 +39,7 @@ finished = token == "-1"; if (!finished) { - this.Bytes.Add(Convert.ToByte(token, 16)); + this.bytes.Add(Convert.ToByte(token, 16)); } } } diff --git a/LR35902/LR35902.FuseTest/RegisterState.cs b/LR35902/LR35902.FuseTest/RegisterState.cs index 9281274..384fb55 100644 --- a/LR35902/LR35902.FuseTest/RegisterState.cs +++ b/LR35902/LR35902.FuseTest/RegisterState.cs @@ -2,7 +2,8 @@ { using System; using System.Collections.Generic; - + using System.Collections.ObjectModel; + using System.Globalization; using EightBit; public class RegisterState @@ -12,8 +13,12 @@ AF, BC, DE, HL, SP, PC }; - public List Registers { get; } = new List(); + private readonly List registers = new List(); + + public ReadOnlyCollection Registers => this.registers.AsReadOnly(); + public bool Halted { get; private set; } = false; + public int TStates { get; private set; } = -1; public void Parse(Lines lines) @@ -32,8 +37,8 @@ private void ParseInternalState(string[] tokens) { - this.Halted = Convert.ToInt32(tokens[0]) == 1; - this.TStates = Convert.ToInt32(tokens[1]); + this.Halted = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture) == 1; + this.TStates = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture); } private void ParseExternalState(Lines lines) @@ -41,7 +46,7 @@ var line = lines.ReadLine(); foreach (var token in line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)) { - this.Registers.Add(new Register16(Convert.ToUInt16(token, 16))); + this.registers.Add(new Register16(Convert.ToUInt16(token, 16))); } } } diff --git a/LR35902/LR35902.FuseTest/Result.cs b/LR35902/LR35902.FuseTest/Result.cs index 1db4900..23d69c4 100644 --- a/LR35902/LR35902.FuseTest/Result.cs +++ b/LR35902/LR35902.FuseTest/Result.cs @@ -1,10 +1,13 @@ namespace Fuse { + using System; using System.Collections.Generic; + using System.Collections.ObjectModel; public class Result { private readonly TestEvents events = new TestEvents(); + private readonly List memoryData = new List(); public bool Valid => !string.IsNullOrEmpty(this.Description); @@ -12,10 +15,15 @@ public RegisterState RegisterState { get; } = new RegisterState(); - public List MemoryData { get; } = new List(); + public ReadOnlyCollection MemoryData => this.memoryData.AsReadOnly(); public void Parse(Lines lines) { + if (lines == null) + { + throw new ArgumentNullException(nameof(lines)); + } + while (!lines.EndOfFile && !this.Valid) { this.Description = lines.ReadLine(); @@ -38,7 +46,7 @@ { var datum = new MemoryDatum(); datum.Parse(line); - this.MemoryData.Add(datum); + this.memoryData.Add(datum); } } while (!finished); diff --git a/LR35902/LR35902.FuseTest/Test.cs b/LR35902/LR35902.FuseTest/Test.cs index 5674937..b59f7fa 100644 --- a/LR35902/LR35902.FuseTest/Test.cs +++ b/LR35902/LR35902.FuseTest/Test.cs @@ -1,19 +1,27 @@ namespace Fuse { + using System; using System.Collections.Generic; public class Test { + private List memoryData = new List(); + public bool Valid => !string.IsNullOrEmpty(this.Description); public string Description { get; private set; } public RegisterState RegisterState { get; } = new RegisterState(); - public List MemoryData { get; } = new List(); + public IReadOnlyCollection MemoryData => this.memoryData.AsReadOnly(); public void Parse(Lines lines) { + if (lines == null) + { + throw new ArgumentNullException(nameof(lines)); + } + while (!lines.EndOfFile && !this.Valid) { this.Description = lines.ReadLine(); @@ -35,7 +43,7 @@ { var datum = new MemoryDatum(); datum.Parse(line); - this.MemoryData.Add(datum); + this.memoryData.Add(datum); } } while (!finished); diff --git a/LR35902/LR35902.FuseTest/TestEvent.cs b/LR35902/LR35902.FuseTest/TestEvent.cs index 3897a8d..a133646 100644 --- a/LR35902/LR35902.FuseTest/TestEvent.cs +++ b/LR35902/LR35902.FuseTest/TestEvent.cs @@ -19,6 +19,11 @@ public void Parse(Lines lines) { + if (lines == null) + { + throw new ArgumentNullException(nameof(lines)); + } + this.ParseLine(lines.ReadLine()); if (!this.Valid) { diff --git a/LR35902/LR35902.FuseTest/TestEvents.cs b/LR35902/LR35902.FuseTest/TestEvents.cs index 5d85337..3a788cd 100644 --- a/LR35902/LR35902.FuseTest/TestEvents.cs +++ b/LR35902/LR35902.FuseTest/TestEvents.cs @@ -1,10 +1,13 @@ namespace Fuse { using System.Collections.Generic; + using System.Collections.ObjectModel; public class TestEvents { - public List Container { get; } = new List(); + private readonly List container = new List(); + + public ReadOnlyCollection Container => this.container.AsReadOnly(); public void Parse(Lines lines) { @@ -16,7 +19,7 @@ complete = !e.Valid; if (!complete) { - this.Container.Add(e); + this.container.Add(e); } } while (!complete); diff --git a/LR35902/LR35902.FuseTest/TestRunner.cs b/LR35902/LR35902.FuseTest/TestRunner.cs index 3f48792..7154b2b 100644 --- a/LR35902/LR35902.FuseTest/TestRunner.cs +++ b/LR35902/LR35902.FuseTest/TestRunner.cs @@ -3,13 +3,13 @@ public class TestRunner : EightBit.GameBoy.Bus { private readonly Test test; - private readonly Result expected; + private readonly Result result; private readonly EightBit.Ram ram = new EightBit.Ram(0x10000); - public TestRunner(Test test, Result expected) + public TestRunner(Test test, Result result) { this.test = test; - this.expected = expected; + this.result = result; } public bool Failed { get; private set; } = false; @@ -87,7 +87,7 @@ private void Checkregisters() { - var expectedState = this.expected.RegisterState; + var expectedState = this.result.RegisterState; var expectedRegisters = expectedState.Registers; var af = this.CPU.AF.Word == expectedRegisters[(int)RegisterState.Register.AF].Word; @@ -152,7 +152,7 @@ { var first = true; - foreach (var memoryDatum in this.expected.MemoryData) + foreach (var memoryDatum in this.result.MemoryData) { var address = memoryDatum.Address; foreach (var expected in memoryDatum.Bytes) diff --git a/LR35902/LR35902.FuseTest/stylecop.json b/LR35902/LR35902.FuseTest/stylecop.json new file mode 100644 index 0000000..3af08b4 --- /dev/null +++ b/LR35902/LR35902.FuseTest/stylecop.json @@ -0,0 +1,19 @@ +{ + // ACTION REQUIRED: This file was automatically added to your project, but it + // will not take effect until additional steps are taken to enable it. See the + // following page for additional information: + // + // https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md + + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "documentationRules": { + "documentInterfaces": false, + "documentExposedElements": false, + "documentInternalElements": false, + "documentPrivateElements": false, + "documentPrivateFields": false, + "companyName": "Adrian Conlon" + } + } +}