TypeConverterTests.cs 3.3 KB

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