/NRefactory/ICSharpCode.NRefactory.Tests/Utils/CSharpPrimitiveCastTests.cs

http://github.com/icsharpcode/ILSpy · C# · 83 lines · 58 code · 6 blank · 19 comment · 14 complexity · a9f40de71c8f75efa5bc9eaa7fa380df MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using NUnit.Framework;
  20. namespace ICSharpCode.NRefactory.Utils
  21. {
  22. [TestFixture]
  23. public class CSharpPrimitiveCastTests
  24. {
  25. // I know, these tests aren't really clever, more of a way to fake code coverage...
  26. // Well, at least they should ensure the 'tables' in CSharpPrimitiveCast don't contain any typos.
  27. [Test]
  28. public void FloatToInteger()
  29. {
  30. for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
  31. for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
  32. object val = CSharpPrimitiveCast.Cast(to, 3.9f, checkedMode == 1);
  33. Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
  34. Assert.AreEqual(3, Convert.ToInt64(val));
  35. }
  36. }
  37. }
  38. [Test]
  39. public void DoubleToInteger()
  40. {
  41. for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
  42. for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
  43. object val = CSharpPrimitiveCast.Cast(to, 3.9, checkedMode == 1);
  44. Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
  45. Assert.AreEqual(3, Convert.ToInt64(val));
  46. }
  47. }
  48. }
  49. [Test]
  50. public void DecimalToInteger()
  51. {
  52. for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
  53. for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
  54. object val = CSharpPrimitiveCast.Cast(to, 3.9m, checkedMode == 1);
  55. Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
  56. Assert.AreEqual(3, Convert.ToInt64(val));
  57. }
  58. }
  59. }
  60. [Test]
  61. public void IntegerToInteger()
  62. {
  63. for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
  64. for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
  65. for (TypeCode to2 = TypeCode.Char; to2 <= TypeCode.Decimal; to2++) {
  66. object val = CSharpPrimitiveCast.Cast(to, 3, checkedMode == 1);
  67. Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
  68. Assert.AreEqual(3, Convert.ToInt64(val));
  69. object val2 = CSharpPrimitiveCast.Cast(to2, val, checkedMode == 1);
  70. Assert.AreEqual(to2, Type.GetTypeCode(val2.GetType()));
  71. Assert.AreEqual(3, Convert.ToInt64(val2));
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }