PageRenderTime 37ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/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
19using System;
20using NUnit.Framework;
21
22namespace ICSharpCode.NRefactory.Utils
23{
24	[TestFixture]
25	public class CSharpPrimitiveCastTests
26	{
27		// I know, these tests aren't really clever, more of a way to fake code coverage...
28		// Well, at least they should ensure the 'tables' in CSharpPrimitiveCast don't contain any typos.
29		
30		[Test]
31		public void FloatToInteger()
32		{
33			for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
34				for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
35					object val = CSharpPrimitiveCast.Cast(to, 3.9f, checkedMode == 1);
36					Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
37					Assert.AreEqual(3, Convert.ToInt64(val));
38				}
39			}
40		}
41		
42		[Test]
43		public void DoubleToInteger()
44		{
45			for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
46				for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
47					object val = CSharpPrimitiveCast.Cast(to, 3.9, checkedMode == 1);
48					Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
49					Assert.AreEqual(3, Convert.ToInt64(val));
50				}
51			}
52		}
53		
54		[Test]
55		public void DecimalToInteger()
56		{
57			for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
58				for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
59					object val = CSharpPrimitiveCast.Cast(to, 3.9m, checkedMode == 1);
60					Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
61					Assert.AreEqual(3, Convert.ToInt64(val));
62				}
63			}
64		}
65		
66		[Test]
67		public void IntegerToInteger()
68		{
69			for (int checkedMode = 0; checkedMode < 2; checkedMode++) {
70				for (TypeCode to = TypeCode.Char; to <= TypeCode.UInt64; to++) {
71					for (TypeCode to2 = TypeCode.Char; to2 <= TypeCode.Decimal; to2++) {
72						object val = CSharpPrimitiveCast.Cast(to, 3, checkedMode == 1);
73						Assert.AreEqual(to, Type.GetTypeCode(val.GetType()));
74						Assert.AreEqual(3, Convert.ToInt64(val));
75						object val2 = CSharpPrimitiveCast.Cast(to2, val, checkedMode == 1);
76						Assert.AreEqual(to2, Type.GetTypeCode(val2.GetType()));
77						Assert.AreEqual(3, Convert.ToInt64(val2));
78					}
79				}
80			}
81		}
82	}
83}