Browse Source

Don't cast from long to int when converting base.

Daniel Lo Nigro 11 years ago
parent
commit
b68cd0649e
2 changed files with 14 additions and 1 deletions
  1. 13 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 1 1
      Jint/Native/Number/NumberPrototype.cs

+ 13 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -706,5 +706,18 @@ namespace Jint.Tests.Runtime
 
 
             Assert.Throws<ArgumentException>(() => x.Invoke(1, 2));
             Assert.Throws<ArgumentException>(() => x.Invoke(1, 2));
         }
         }
+
+        [Theory]
+        [InlineData("0", 0, 16)]
+        [InlineData("1", 1, 16)]
+        [InlineData("100", 100, 10)]
+        [InlineData("1100100", 100, 2)]
+        [InlineData("2s", 100, 36)]
+        [InlineData("2qgpckvng1s", 10000000000000000L, 36)]
+        public void ShouldConvertNumbersToDifferentBase(string expected, long number, int radix)
+        {
+          var result = NumberPrototype.ToBase(number, radix);
+          Assert.Equal(expected, result);
+        }
     }
     }
 }
 }

+ 1 - 1
Jint/Native/Number/NumberPrototype.cs

@@ -235,7 +235,7 @@ namespace Jint.Native.Number
             var result = new StringBuilder();
             var result = new StringBuilder();
             while (n > 0)
             while (n > 0)
             {
             {
-                var digit = (int)n % radix;
+                var digit = (int)(n % radix);
                 n = n / radix;
                 n = n / radix;
                 result.Insert(0, digits[digit].ToString());
                 result.Insert(0, digits[digit].ToString());
             }
             }