From f6fa4ada4ea8680e49a47e2b9a5741c8d33ef74d Mon Sep 17 00:00:00 2001 From: Mike Mazur Date: Mon, 28 Jul 2008 07:46:53 +0000 Subject: When casting strings to int, use double.Parse() as strings may be floats. With this commit, issue 1822 should be fixed. --- OpenSim/Region/ScriptEngine/Common/LSL_Types.cs | 6 ++- OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs | 6 ++- .../ScriptEngine/Common/LSL_TypesTestLSLInteger.cs | 43 +++++++++++++++++++++- .../ScriptEngine/Shared/LSL_TypesTestLSLInteger.cs | 43 +++++++++++++++++++++- 4 files changed, 92 insertions(+), 6 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs index 74c9935..d0a5079 100644 --- a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs @@ -1172,7 +1172,8 @@ namespace OpenSim.Region.ScriptEngine.Common 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) @@ -1283,7 +1284,8 @@ namespace OpenSim.Region.ScriptEngine.Common 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) diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs index 57394d9..c42e3e6 100644 --- a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs @@ -1172,7 +1172,8 @@ namespace OpenSim.Region.ScriptEngine.Shared 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) @@ -1283,7 +1284,8 @@ namespace OpenSim.Region.ScriptEngine.Shared 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) diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLInteger.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLInteger.cs index 08f80d3..3f01d09 100644 --- a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLInteger.cs +++ b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLInteger.cs @@ -36,6 +36,7 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests public class LSL_TypesTestLSLInteger { private Dictionary m_doubleIntSet; + private Dictionary m_stringIntSet; /// /// 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(999999999.0, 999999999); m_doubleIntSet.Add(-99999999.0, -99999999); + + m_stringIntSet = new Dictionary(); + 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); } /// - /// Tests LSLInteger is correctly cast explicitly to LSLFloat. + /// Tests LSLFloat is correctly cast explicitly to LSLInteger. /// [Test] public void TestExplicitCastLSLFloatToLSLInteger() @@ -67,5 +78,35 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests Assert.AreEqual(testInteger.value, number.Value); } } + + /// + /// Tests string is correctly cast explicitly to LSLInteger. + /// + [Test] + public void TestExplicitCastStringToLSLInteger() + { + LSL_Types.LSLInteger testInteger; + + foreach (KeyValuePair number in m_stringIntSet) + { + testInteger = (LSL_Types.LSLInteger) number.Key; + Assert.AreEqual(testInteger.value, number.Value); + } + } + + /// + /// Tests LSLString is correctly cast explicitly to LSLInteger. + /// + [Test] + public void TestExplicitCastLSLStringToLSLInteger() + { + LSL_Types.LSLInteger testInteger; + + foreach (KeyValuePair number in m_stringIntSet) + { + testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLString(number.Key); + Assert.AreEqual(testInteger.value, number.Value); + } + } } } diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/LSL_TypesTestLSLInteger.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/LSL_TypesTestLSLInteger.cs index a4e7bbc..d158084 100644 --- a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/LSL_TypesTestLSLInteger.cs +++ b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/LSL_TypesTestLSLInteger.cs @@ -36,6 +36,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests public class LSL_TypesTestLSLInteger { private Dictionary m_doubleIntSet; + private Dictionary m_stringIntSet; /// /// 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(999999999.0, 999999999); m_doubleIntSet.Add(-99999999.0, -99999999); + + m_stringIntSet = new Dictionary(); + 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); } /// - /// Tests LSLInteger is correctly cast explicitly to LSLFloat. + /// Tests LSLFloat is correctly cast explicitly to LSLInteger. /// [Test] public void TestExplicitCastLSLFloatToLSLInteger() @@ -67,5 +78,35 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests Assert.AreEqual(testInteger.value, number.Value); } } + + /// + /// Tests string is correctly cast explicitly to LSLInteger. + /// + [Test] + public void TestExplicitCastStringToLSLInteger() + { + LSL_Types.LSLInteger testInteger; + + foreach (KeyValuePair number in m_stringIntSet) + { + testInteger = (LSL_Types.LSLInteger) number.Key; + Assert.AreEqual(testInteger.value, number.Value); + } + } + + /// + /// Tests LSLString is correctly cast explicitly to LSLInteger. + /// + [Test] + public void TestExplicitCastLSLStringToLSLInteger() + { + LSL_Types.LSLInteger testInteger; + + foreach (KeyValuePair number in m_stringIntSet) + { + testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLString(number.Key); + Assert.AreEqual(testInteger.value, number.Value); + } + } } } -- cgit v1.1