You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
5.8 KiB

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.CodeDom.Compiler;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Runtime.CompilerServices;
  23. using ICSharpCode.Decompiler.Tests.Helpers;
  24. using NUnit.Framework;
  25. namespace ICSharpCode.Decompiler.Tests
  26. {
  27. [TestFixture]
  28. public class CorrectnessTestRunner
  29. {
  30. const string TestCasePath = @"../../Tests/TestCases/Correctness";
  31. [Test]
  32. public void AllFilesHaveTests()
  33. {
  34. var testNames = typeof(CorrectnessTestRunner).GetMethods()
  35. .Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Any())
  36. .Select(m => m.Name)
  37. .ToArray();
  38. foreach (var file in new DirectoryInfo(TestCasePath).EnumerateFiles()) {
  39. if (file.Extension == ".txt" || file.Extension == ".exe")
  40. continue;
  41. var testName = Path.GetFileNameWithoutExtension(file.Name);
  42. Assert.Contains(testName, testNames);
  43. }
  44. }
  45. static readonly CompilerOptions[] defaultOptions =
  46. {
  47. CompilerOptions.None,
  48. CompilerOptions.Optimize,
  49. CompilerOptions.UseRoslyn,
  50. CompilerOptions.Optimize | CompilerOptions.UseRoslyn
  51. };
  52. [Test]
  53. public void Comparisons([ValueSource("defaultOptions")] CompilerOptions options)
  54. {
  55. RunCS(options: options);
  56. }
  57. [Test]
  58. public void Conversions([ValueSource("defaultOptions")] CompilerOptions options)
  59. {
  60. RunCS(options: options);
  61. }
  62. [Test]
  63. public void HelloWorld([ValueSource("defaultOptions")] CompilerOptions options)
  64. {
  65. RunCS(options: options);
  66. }
  67. [Test]
  68. public void ControlFlow([ValueSource("defaultOptions")] CompilerOptions options)
  69. {
  70. RunCS(options: options);
  71. }
  72. [Test]
  73. public void CompoundAssignment([ValueSource("defaultOptions")] CompilerOptions options)
  74. {
  75. RunCS(options: options);
  76. }
  77. [Test]
  78. public void PropertiesAndEvents([ValueSource("defaultOptions")] CompilerOptions options)
  79. {
  80. RunCS(options: options);
  81. }
  82. [Test]
  83. public void Switch([ValueSource("defaultOptions")] CompilerOptions options)
  84. {
  85. RunCS(options: options);
  86. }
  87. [Test]
  88. public void Generics([ValueSource("defaultOptions")] CompilerOptions options)
  89. {
  90. RunCS(options: options);
  91. }
  92. [Test]
  93. public void ValueTypeCall([ValueSource("defaultOptions")] CompilerOptions options)
  94. {
  95. RunCS(options: options);
  96. }
  97. [Test]
  98. public void InitializerTests([ValueSource("defaultOptions")] CompilerOptions options)
  99. {
  100. RunCS(options: options);
  101. }
  102. [Test]
  103. public void DecimalFields([ValueSource("defaultOptions")] CompilerOptions options)
  104. {
  105. RunCS(options: options);
  106. }
  107. [Test]
  108. public void UndocumentedExpressions([ValueSource("defaultOptions")] CompilerOptions options)
  109. {
  110. RunCS(options: options);
  111. }
  112. [Test]
  113. public void MemberLookup([ValueSource("defaultOptions")] CompilerOptions options)
  114. {
  115. RunCS(options: options);
  116. }
  117. [Test]
  118. public void BitNot()
  119. {
  120. RunIL("BitNot.il");
  121. RunIL("BitNot.il", CompilerOptions.UseDebug | CompilerOptions.Force32Bit, AssemblerOptions.Force32Bit);
  122. }
  123. [Test]
  124. public void UnsafeCode([ValueSource("defaultOptions")] CompilerOptions options)
  125. {
  126. RunCS(options: options);
  127. }
  128. void RunCS([CallerMemberName] string testName = null, CompilerOptions options = CompilerOptions.UseDebug)
  129. {
  130. string testFileName = testName + ".cs";
  131. CompilerResults outputFile = null, decompiledOutputFile = null;
  132. try {
  133. outputFile = Tester.CompileCSharp(Path.Combine(TestCasePath, testFileName), options);
  134. string decompiledCodeFile = Tester.DecompileCSharp(outputFile.PathToAssembly);
  135. decompiledOutputFile = Tester.CompileCSharp(decompiledCodeFile, options);
  136. Tester.RunAndCompareOutput(testFileName, outputFile.PathToAssembly, decompiledOutputFile.PathToAssembly, decompiledCodeFile);
  137. File.Delete(decompiledCodeFile);
  138. File.Delete(outputFile.PathToAssembly);
  139. File.Delete(decompiledOutputFile.PathToAssembly);
  140. } finally {
  141. if (outputFile != null)
  142. outputFile.TempFiles.Delete();
  143. if (decompiledOutputFile != null)
  144. decompiledOutputFile.TempFiles.Delete();
  145. }
  146. }
  147. void RunIL(string testFileName, CompilerOptions options = CompilerOptions.UseDebug, AssemblerOptions asmOptions = AssemblerOptions.None)
  148. {
  149. string outputFile = null;
  150. CompilerResults decompiledOutputFile = null;
  151. try {
  152. outputFile = Tester.AssembleIL(Path.Combine(TestCasePath, testFileName), asmOptions);
  153. string decompiledCodeFile = Tester.DecompileCSharp(outputFile);
  154. decompiledOutputFile = Tester.CompileCSharp(decompiledCodeFile, options);
  155. Tester.RunAndCompareOutput(testFileName, outputFile, decompiledOutputFile.PathToAssembly, decompiledCodeFile);
  156. File.Delete(decompiledCodeFile);
  157. File.Delete(decompiledOutputFile.PathToAssembly);
  158. } finally {
  159. if (decompiledOutputFile != null)
  160. decompiledOutputFile.TempFiles.Delete();
  161. }
  162. }
  163. }
  164. }