TypeConverterTests.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Jint.Native;
  2. using Jint.Runtime;
  3. using Xunit.Abstractions;
  4. namespace Jint.Tests.Runtime;
  5. public class TypeConverterTests
  6. {
  7. private readonly Engine _engine;
  8. public TypeConverterTests(ITestOutputHelper output)
  9. {
  10. _engine = new Engine()
  11. .SetValue("log", new Action<object>(o => output.WriteLine(o.ToString())))
  12. .SetValue("assert", new Action<bool>(Assert.True))
  13. .SetValue("equal", new Action<object, object>(Assert.Equal))
  14. ;
  15. }
  16. public static readonly IEnumerable<object[]> ConvertNumberToInt32AndUint32TestData = new TheoryData<double, int>()
  17. {
  18. { 0.0, 0 },
  19. { -0.0, 0 },
  20. { double.Epsilon, 0 },
  21. { 0.5, 0 },
  22. { -0.5, 0 },
  23. { 0.9999999999999999, 0 },
  24. { 1.0, 1 },
  25. { 1.5, 1 },
  26. { 10.0, 10 },
  27. { -12.3, -12 },
  28. { 1485772.6, 1485772 },
  29. { -984737183.8, -984737183 },
  30. { Math.Pow(2, 31) - 1.0, int.MaxValue },
  31. { Math.Pow(2, 31) - 0.5, int.MaxValue },
  32. { Math.Pow(2, 32) - 1.0, -1 },
  33. { Math.Pow(2, 32) - 0.5, -1 },
  34. { Math.Pow(2, 32), 0 },
  35. { -Math.Pow(2, 32), 0 },
  36. { -Math.Pow(2, 32) - 0.5, 0 },
  37. { Math.Pow(2, 32) + 1.0, 1 },
  38. { Math.Pow(2, 45) + 17.56, 17 },
  39. { Math.Pow(2, 45) - 17.56, -18 },
  40. { -Math.Pow(2, 45) + 17.56, 18 },
  41. { Math.Pow(2, 51) + 17.5, 17 },
  42. { Math.Pow(2, 51) - 17.5, -18 },
  43. { Math.Pow(2, 53) - 1.0, -1 },
  44. { -Math.Pow(2, 53) + 1.0, 1 },
  45. { Math.Pow(2, 53), 0 },
  46. { -Math.Pow(2, 53), 0 },
  47. { Math.Pow(2, 53) + 12.0, 12 },
  48. { -Math.Pow(2, 53) - 12.0, -12 },
  49. { (Math.Pow(2, 53) - 1.0) * Math.Pow(2, 1), -2 },
  50. { -(Math.Pow(2, 53) - 1.0) * Math.Pow(2, 3), 8 },
  51. { -(Math.Pow(2, 53) - 1.0) * Math.Pow(2, 11), 1 << 11 },
  52. { (Math.Pow(2, 53) - 1.0) * Math.Pow(2, 20), -(1 << 20) },
  53. { (Math.Pow(2, 53) - 1.0) * Math.Pow(2, 31), int.MinValue },
  54. { -(Math.Pow(2, 53) - 1.0) * Math.Pow(2, 31), int.MinValue },
  55. { (Math.Pow(2, 53) - 1.0) * Math.Pow(2, 32), 0 },
  56. { -(Math.Pow(2, 53) - 1.0) * Math.Pow(2, 32), 0 },
  57. { (Math.Pow(2, 53) - 1.0) * Math.Pow(2, 36), 0 },
  58. { double.MaxValue, 0 },
  59. { double.MinValue, 0 },
  60. { double.PositiveInfinity, 0 },
  61. { double.NegativeInfinity, 0 },
  62. { double.NaN, 0 },
  63. };
  64. [Theory]
  65. [MemberData(nameof(ConvertNumberToInt32AndUint32TestData))]
  66. public void ConvertNumberToInt32AndUint32(double value, int expectedResult)
  67. {
  68. JsValue jsval = value;
  69. Assert.Equal(expectedResult, TypeConverter.ToInt32(jsval));
  70. Assert.Equal((uint)expectedResult, TypeConverter.ToUint32(jsval));
  71. }
  72. [Fact]
  73. public void ToPrimitiveShouldEvaluateOnlyOnceDuringInExpression()
  74. {
  75. _engine.Execute(@"
  76. var b = {};
  77. var bval = 0;
  78. b[Symbol.toPrimitive] = function(hint) { return bval++; };
  79. b in {};
  80. equal(1, bval);
  81. ");
  82. }
  83. }