When casting strings to int, use double.Parse() as strings may be floats. With

this commit, issue 1822 should be fixed.
0.6.0-stable
Mike Mazur 2008-07-28 07:46:53 +00:00
parent d959c65b91
commit f6fa4ada4e
4 changed files with 92 additions and 6 deletions

View File

@ -1172,7 +1172,8 @@ namespace OpenSim.Region.ScriptEngine.Common
public static explicit operator LSLInteger(LSLString s) public static explicit operator LSLInteger(LSLString s)
{ {
return new LSLInteger(Convert.ToInt32(s.m_string)); // double.Parse() used because s could be "123.9" for example.
return new LSLInteger(double.Parse(s.m_string));
} }
public static explicit operator LSLString(double d) public static explicit operator LSLString(double d)
@ -1283,7 +1284,8 @@ namespace OpenSim.Region.ScriptEngine.Common
static public explicit operator LSLInteger(string s) static public explicit operator LSLInteger(string s)
{ {
return new LSLInteger(int.Parse(s)); // double.Parse() used because s could be "123.9" for example.
return new LSLInteger(double.Parse(s));
} }
static public implicit operator LSLInteger(uint u) static public implicit operator LSLInteger(uint u)

View File

@ -1172,7 +1172,8 @@ namespace OpenSim.Region.ScriptEngine.Shared
public static explicit operator LSLInteger(LSLString s) public static explicit operator LSLInteger(LSLString s)
{ {
return new LSLInteger(Convert.ToInt32(s.m_string)); // double.Parse() used because s could be "123.9" for example.
return new LSLInteger(double.Parse(s.m_string));
} }
public static explicit operator LSLString(double d) public static explicit operator LSLString(double d)
@ -1283,7 +1284,8 @@ namespace OpenSim.Region.ScriptEngine.Shared
static public explicit operator LSLInteger(string s) static public explicit operator LSLInteger(string s)
{ {
return new LSLInteger(int.Parse(s)); // double.Parse() used because s could be "123.9" for example.
return new LSLInteger(double.Parse(s));
} }
static public implicit operator LSLInteger(uint u) static public implicit operator LSLInteger(uint u)

View File

@ -36,6 +36,7 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests
public class LSL_TypesTestLSLInteger public class LSL_TypesTestLSLInteger
{ {
private Dictionary<double, int> m_doubleIntSet; private Dictionary<double, int> m_doubleIntSet;
private Dictionary<string, int> m_stringIntSet;
/// <summary> /// <summary>
/// Sets up dictionaries and arrays used in the tests. /// Sets up dictionaries and arrays used in the tests.
@ -51,10 +52,20 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests
m_doubleIntSet.Add(-1.0, -1); m_doubleIntSet.Add(-1.0, -1);
m_doubleIntSet.Add(999999999.0, 999999999); m_doubleIntSet.Add(999999999.0, 999999999);
m_doubleIntSet.Add(-99999999.0, -99999999); m_doubleIntSet.Add(-99999999.0, -99999999);
m_stringIntSet = new Dictionary<string, int>();
m_stringIntSet.Add("2", 2);
m_stringIntSet.Add("-2", -2);
m_stringIntSet.Add("0", 0);
m_stringIntSet.Add("1", 1);
m_stringIntSet.Add("-1", -1);
m_stringIntSet.Add("123.9", 123);
m_stringIntSet.Add("999999999", 999999999);
m_stringIntSet.Add("-99999999", -99999999);
} }
/// <summary> /// <summary>
/// Tests LSLInteger is correctly cast explicitly to LSLFloat. /// Tests LSLFloat is correctly cast explicitly to LSLInteger.
/// </summary> /// </summary>
[Test] [Test]
public void TestExplicitCastLSLFloatToLSLInteger() public void TestExplicitCastLSLFloatToLSLInteger()
@ -67,5 +78,35 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests
Assert.AreEqual(testInteger.value, number.Value); Assert.AreEqual(testInteger.value, number.Value);
} }
} }
/// <summary>
/// Tests string is correctly cast explicitly to LSLInteger.
/// </summary>
[Test]
public void TestExplicitCastStringToLSLInteger()
{
LSL_Types.LSLInteger testInteger;
foreach (KeyValuePair<string, int> number in m_stringIntSet)
{
testInteger = (LSL_Types.LSLInteger) number.Key;
Assert.AreEqual(testInteger.value, number.Value);
}
}
/// <summary>
/// Tests LSLString is correctly cast explicitly to LSLInteger.
/// </summary>
[Test]
public void TestExplicitCastLSLStringToLSLInteger()
{
LSL_Types.LSLInteger testInteger;
foreach (KeyValuePair<string, int> number in m_stringIntSet)
{
testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLString(number.Key);
Assert.AreEqual(testInteger.value, number.Value);
}
}
} }
} }

View File

@ -36,6 +36,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests
public class LSL_TypesTestLSLInteger public class LSL_TypesTestLSLInteger
{ {
private Dictionary<double, int> m_doubleIntSet; private Dictionary<double, int> m_doubleIntSet;
private Dictionary<string, int> m_stringIntSet;
/// <summary> /// <summary>
/// Sets up dictionaries and arrays used in the tests. /// Sets up dictionaries and arrays used in the tests.
@ -51,10 +52,20 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests
m_doubleIntSet.Add(-1.0, -1); m_doubleIntSet.Add(-1.0, -1);
m_doubleIntSet.Add(999999999.0, 999999999); m_doubleIntSet.Add(999999999.0, 999999999);
m_doubleIntSet.Add(-99999999.0, -99999999); m_doubleIntSet.Add(-99999999.0, -99999999);
m_stringIntSet = new Dictionary<string, int>();
m_stringIntSet.Add("2", 2);
m_stringIntSet.Add("-2", -2);
m_stringIntSet.Add("0", 0);
m_stringIntSet.Add("1", 1);
m_stringIntSet.Add("-1", -1);
m_stringIntSet.Add("123.9", 123);
m_stringIntSet.Add("999999999", 999999999);
m_stringIntSet.Add("-99999999", -99999999);
} }
/// <summary> /// <summary>
/// Tests LSLInteger is correctly cast explicitly to LSLFloat. /// Tests LSLFloat is correctly cast explicitly to LSLInteger.
/// </summary> /// </summary>
[Test] [Test]
public void TestExplicitCastLSLFloatToLSLInteger() public void TestExplicitCastLSLFloatToLSLInteger()
@ -67,5 +78,35 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests
Assert.AreEqual(testInteger.value, number.Value); Assert.AreEqual(testInteger.value, number.Value);
} }
} }
/// <summary>
/// Tests string is correctly cast explicitly to LSLInteger.
/// </summary>
[Test]
public void TestExplicitCastStringToLSLInteger()
{
LSL_Types.LSLInteger testInteger;
foreach (KeyValuePair<string, int> number in m_stringIntSet)
{
testInteger = (LSL_Types.LSLInteger) number.Key;
Assert.AreEqual(testInteger.value, number.Value);
}
}
/// <summary>
/// Tests LSLString is correctly cast explicitly to LSLInteger.
/// </summary>
[Test]
public void TestExplicitCastLSLStringToLSLInteger()
{
LSL_Types.LSLInteger testInteger;
foreach (KeyValuePair<string, int> number in m_stringIntSet)
{
testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLString(number.Key);
Assert.AreEqual(testInteger.value, number.Value);
}
}
} }
} }