From c7e8d71edbbc7525b2b2cd3521f8965a07a3b63c Mon Sep 17 00:00:00 2001 From: distributivgesetz Date: Sat, 23 Dec 2023 22:11:05 +0100 Subject: [PATCH] Move duplicate opcode check to a unit test (#1563) * move opcode unique validity check to unit tests * automatic code style changes :/ --- Content.Tests/RuntimeTests.cs | 16 ++++++++++++++++ DMCompiler/Bytecode/DreamProcOpcode.cs | 21 --------------------- DMCompiler/DMCompiler.cs | 10 +++------- 3 files changed, 19 insertions(+), 28 deletions(-) create mode 100644 Content.Tests/RuntimeTests.cs diff --git a/Content.Tests/RuntimeTests.cs b/Content.Tests/RuntimeTests.cs new file mode 100644 index 0000000000..81e6e9f46e --- /dev/null +++ b/Content.Tests/RuntimeTests.cs @@ -0,0 +1,16 @@ +using System; +using DMCompiler.Bytecode; +using NUnit.Framework; + +namespace Content.Tests; + +[TestFixture] +public sealed class RuntimeTests { + /// + /// Validates that the opcodes in DreamProcOpcode are all unique, such that none resolve to the same byte. + /// + [Test] + public void EnsureOpcodesUnique() { + Assert.That(Enum.GetValues(), Is.Unique); + } +} diff --git a/DMCompiler/Bytecode/DreamProcOpcode.cs b/DMCompiler/Bytecode/DreamProcOpcode.cs index 10bb98d707..766f8b9f41 100644 --- a/DMCompiler/Bytecode/DreamProcOpcode.cs +++ b/DMCompiler/Bytecode/DreamProcOpcode.cs @@ -363,27 +363,6 @@ public override string ToString() { // Dummy class-as-namespace because C# just kinda be like this public static class OpcodeVerifier { - /// - /// Validates that the opcodes in DreamProcOpcode are all unique, such that none resolve to the same byte. - /// - /// True if there are duplicate opcodes, false if not - // FIXME: Can this be made into something done during compiletime? Like, *this code's* compiletime? >:/ - public static bool AreOpcodesInvalid() { - // I'm not *too* satisfied with this boolean schtick, as opposed to throwing, - // but I want each executable to be able to do what they want with this information. - - HashSet bytePool = new(); - foreach (DreamProcOpcode usedInt in Enum.GetValues(typeof(DreamProcOpcode))) { - if(bytePool.Contains(usedInt)) { - return true; - } - - bytePool.Add(usedInt); - } - - return false; - } - /// /// Calculates a hash of all the DreamProcOpcodes for warning on incompatibilities. /// diff --git a/DMCompiler/DMCompiler.cs b/DMCompiler/DMCompiler.cs index 823d43ee7c..ab396e178a 100644 --- a/DMCompiler/DMCompiler.cs +++ b/DMCompiler/DMCompiler.cs @@ -1,3 +1,4 @@ +using DMCompiler.Bytecode; using DMCompiler.Compiler.DM; using DMCompiler.Compiler.DMM; using DMCompiler.Compiler.DMPreprocessor; @@ -5,17 +6,16 @@ using DMCompiler.DM.Visitors; using OpenDreamShared.Compiler; using OpenDreamShared.Json; +using Robust.Shared.Utility; using System; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Reflection; using System.Linq; +using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; -using DMCompiler.Bytecode; -using Robust.Shared.Utility; namespace DMCompiler; @@ -51,10 +51,6 @@ public static bool Compile(DMCompilerSettings settings) { ForcedWarning("Unimplemented proc & var warnings are currently suppressed"); } - if(OpcodeVerifier.AreOpcodesInvalid()) { - ForcedError("Some opcodes have the same byte value! Output assembly may be corrupted."); - } - DMPreprocessor preprocessor = Preprocess(settings.Files, settings.MacroDefines); bool successfulCompile = preprocessor is not null && Compile(preprocessor);