LSL_TypesTestLSLInteger.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Collections.Generic;
  28. using NUnit.Framework;
  29. using OpenSim.Tests.Common;
  30. using OpenSim.Region.ScriptEngine.Shared;
  31. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  32. {
  33. [TestFixture]
  34. public class LSL_TypesTestLSLInteger
  35. {
  36. private Dictionary<double, int> m_doubleIntSet;
  37. private Dictionary<string, int> m_stringIntSet;
  38. /// <summary>
  39. /// Sets up dictionaries and arrays used in the tests.
  40. /// </summary>
  41. [TestFixtureSetUp]
  42. public void SetUpDataSets()
  43. {
  44. m_doubleIntSet = new Dictionary<double, int>();
  45. m_doubleIntSet.Add(2.0, 2);
  46. m_doubleIntSet.Add(-2.0, -2);
  47. m_doubleIntSet.Add(0.0, 0);
  48. m_doubleIntSet.Add(1.0, 1);
  49. m_doubleIntSet.Add(-1.0, -1);
  50. m_doubleIntSet.Add(999999999.0, 999999999);
  51. m_doubleIntSet.Add(-99999999.0, -99999999);
  52. m_stringIntSet = new Dictionary<string, int>();
  53. m_stringIntSet.Add("2", 2);
  54. m_stringIntSet.Add("-2", -2);
  55. m_stringIntSet.Add("0", 0);
  56. m_stringIntSet.Add("1", 1);
  57. m_stringIntSet.Add("-1", -1);
  58. m_stringIntSet.Add("123.9", 123);
  59. m_stringIntSet.Add("999999999", 999999999);
  60. m_stringIntSet.Add("-99999999", -99999999);
  61. m_stringIntSet.Add("", 0);
  62. m_stringIntSet.Add("aa", 0);
  63. m_stringIntSet.Add("56foo", 56);
  64. m_stringIntSet.Add("42", 42);
  65. m_stringIntSet.Add("42 is the answer", 42);
  66. m_stringIntSet.Add(" 42", 42);
  67. m_stringIntSet.Add("42,123,456", 42);
  68. m_stringIntSet.Add("0xff", 255);
  69. m_stringIntSet.Add("12345678900000", -1);
  70. }
  71. /// <summary>
  72. /// Tests LSLFloat is correctly cast explicitly to LSLInteger.
  73. /// </summary>
  74. [Test]
  75. public void TestExplicitCastLSLFloatToLSLInteger()
  76. {
  77. LSL_Types.LSLInteger testInteger;
  78. foreach (KeyValuePair<double, int> number in m_doubleIntSet)
  79. {
  80. testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLFloat(number.Key);
  81. Assert.AreEqual(testInteger.value, number.Value);
  82. }
  83. }
  84. /// <summary>
  85. /// Tests string is correctly cast explicitly to LSLInteger.
  86. /// </summary>
  87. [Test]
  88. public void TestExplicitCastStringToLSLInteger()
  89. {
  90. LSL_Types.LSLInteger testInteger;
  91. foreach (KeyValuePair<string, int> number in m_stringIntSet)
  92. {
  93. testInteger = (LSL_Types.LSLInteger) number.Key;
  94. Assert.AreEqual(testInteger.value, number.Value);
  95. }
  96. }
  97. /// <summary>
  98. /// Tests LSLString is correctly cast explicitly to LSLInteger.
  99. /// </summary>
  100. [Test]
  101. public void TestExplicitCastLSLStringToLSLInteger()
  102. {
  103. LSL_Types.LSLInteger testInteger;
  104. foreach (KeyValuePair<string, int> number in m_stringIntSet)
  105. {
  106. testInteger = (LSL_Types.LSLInteger) new LSL_Types.LSLString(number.Key);
  107. Assert.AreEqual(testInteger.value, number.Value);
  108. }
  109. }
  110. /// <summary>
  111. /// Tests boolean correctly cast implicitly to LSLInteger.
  112. /// </summary>
  113. [Test]
  114. public void TestImplicitCastBooleanToLSLInteger()
  115. {
  116. LSL_Types.LSLInteger testInteger;
  117. testInteger = (1 == 0);
  118. Assert.AreEqual(0, testInteger.value);
  119. testInteger = (1 == 1);
  120. Assert.AreEqual(1, testInteger.value);
  121. testInteger = false;
  122. Assert.AreEqual(0, testInteger.value);
  123. testInteger = true;
  124. Assert.AreEqual(1, testInteger.value);
  125. }
  126. }
  127. }