|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +public class FloatSpecialValuesTests : IDisposable |
| 11 | +{ |
| 12 | + private string _ilasmFile; |
| 13 | + private string _ildasmFile; |
| 14 | + |
| 15 | + public FloatSpecialValuesTests() |
| 16 | + { |
| 17 | + string coreRoot = Environment.GetEnvironmentVariable("CORE_ROOT"); |
| 18 | + if (string.IsNullOrWhiteSpace(coreRoot)) |
| 19 | + { |
| 20 | + Console.WriteLine("Environment variable is not set: 'CORE_ROOT'"); |
| 21 | + throw new InvalidOperationException("Environment variable is not set: 'CORE_ROOT'"); |
| 22 | + } |
| 23 | + if (!Directory.Exists(coreRoot)) |
| 24 | + { |
| 25 | + Console.WriteLine($"Did not find CORE_ROOT directory: {coreRoot}"); |
| 26 | + throw new InvalidOperationException("Did not find CORE_ROOT directory"); |
| 27 | + } |
| 28 | + |
| 29 | + var nativeExeExtensions = new string[] { string.Empty, ".exe" }; |
| 30 | + bool found = false; |
| 31 | + string ilasmFile = null; |
| 32 | + string ildasmFile = null; |
| 33 | + foreach (string nativeExeExtension in nativeExeExtensions) |
| 34 | + { |
| 35 | + ilasmFile = Path.Combine(coreRoot, $"ilasm{nativeExeExtension}"); |
| 36 | + ildasmFile = Path.Combine(coreRoot, $"ildasm{nativeExeExtension}"); |
| 37 | + if (File.Exists(ilasmFile) && File.Exists(ildasmFile)) |
| 38 | + { |
| 39 | + found = true; |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + if (!found) |
| 44 | + { |
| 45 | + Console.WriteLine($"Did not find ilasm or ildasm in CORE_ROOT directory: {coreRoot}"); |
| 46 | + throw new InvalidOperationException("Did not find ilasm or ildasm in CORE_ROOT directory"); |
| 47 | + } |
| 48 | + |
| 49 | + _ilasmFile = ilasmFile; |
| 50 | + _ildasmFile = ildasmFile; |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void NaNOutputIsNormalizedAcrossPlatforms() |
| 55 | + { |
| 56 | + Console.WriteLine("NaNOutputIsNormalizedAcrossPlatforms"); |
| 57 | + |
| 58 | + string ilFileName = "FloatSpecialValues.il"; |
| 59 | + string disasmIlFileName; |
| 60 | + ProcessStartInfo ilasmPsi, ildasmPsi; |
| 61 | + GetIlasmProcessStartInfos(_ilasmFile, _ildasmFile, ilFileName, out disasmIlFileName, out ilasmPsi, out ildasmPsi); |
| 62 | + |
| 63 | + Process ilasmProcess = Process.Start(ilasmPsi); |
| 64 | + ilasmProcess.WaitForExit(); |
| 65 | + Assert.Equal(0, ilasmProcess.ExitCode); |
| 66 | + |
| 67 | + Process ildasmProcess = Process.Start(ildasmPsi); |
| 68 | + ildasmProcess.WaitForExit(); |
| 69 | + Assert.Equal(0, ildasmProcess.ExitCode); |
| 70 | + |
| 71 | + string disasmIl = File.ReadAllText(disasmIlFileName); |
| 72 | + |
| 73 | + // Verify that NaN values use the normalized format without platform-specific suffixes |
| 74 | + // The hex representation should be followed by "// -nan" without "(ind)" suffix |
| 75 | + var nanFloat64Regex = new Regex(@"float64\(0xFFF8000000000000\)\s+//\s+-nan\s", RegexOptions.Compiled); |
| 76 | + Assert.True(nanFloat64Regex.IsMatch(disasmIl), |
| 77 | + "NaN float64 should be output as '// -nan' without platform-specific suffixes like '(ind)'"); |
| 78 | + |
| 79 | + // Verify the output does NOT contain the Windows-specific "(ind)" suffix |
| 80 | + Assert.DoesNotContain("-nan(ind)", disasmIl); |
| 81 | + Assert.DoesNotContain("-nan(", disasmIl); |
| 82 | + } |
| 83 | + |
| 84 | + private static void GetIlasmProcessStartInfos( |
| 85 | + string ilasmFile, |
| 86 | + string ildasmFile, |
| 87 | + string ilFileName, |
| 88 | + out string disasmIlFileName, |
| 89 | + out ProcessStartInfo ilasmPsi, |
| 90 | + out ProcessStartInfo ildasmPsi) |
| 91 | + { |
| 92 | + if (!File.Exists(ilFileName)) |
| 93 | + { |
| 94 | + throw new FileNotFoundException( |
| 95 | + $"Did not find '{ilFileName}' in working directory '{Environment.CurrentDirectory}'"); |
| 96 | + } |
| 97 | + |
| 98 | + string currentDirectory = Environment.CurrentDirectory; |
| 99 | + |
| 100 | + ilasmPsi = new ProcessStartInfo(); |
| 101 | + ilasmPsi.UseShellExecute = false; |
| 102 | + ilasmPsi.WorkingDirectory = currentDirectory; |
| 103 | + ilasmPsi.FileName = ilasmFile; |
| 104 | + string asmDllFileName = $"{Path.GetFileNameWithoutExtension(ilFileName)}.dll"; |
| 105 | + ilasmPsi.Arguments = |
| 106 | + $"-nologo -dll -optimize -output={asmDllFileName} {ilFileName}"; |
| 107 | + |
| 108 | + ildasmPsi = new ProcessStartInfo(); |
| 109 | + ildasmPsi.UseShellExecute = false; |
| 110 | + ildasmPsi.WorkingDirectory = currentDirectory; |
| 111 | + ildasmPsi.FileName = ildasmFile; |
| 112 | + disasmIlFileName = $"{Path.GetFileNameWithoutExtension(ilFileName)}_dis{Path.GetExtension(ilFileName)}"; |
| 113 | + ildasmPsi.Arguments = $"-out={disasmIlFileName} {asmDllFileName}"; |
| 114 | + } |
| 115 | + |
| 116 | + public void Dispose() {} |
| 117 | +} |
0 commit comments