1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using ICSharpCode.NRefactory.Semantics;
21
using NUnit.Framework;
22
using ICSharpCode.NRefactory.TypeSystem;
24
namespace ICSharpCode.NRefactory.CSharp.Resolver
27
public class SizeOfTests : ResolverTestBase
30
public void SizeOfPrimitiveTypes()
32
foreach (var t in new[] { new { t = "sbyte", n = 1 },
33
new { t = "byte", n = 1 },
34
new { t = "short", n = 2 },
35
new { t = "ushort", n = 2 },
36
new { t = "int", n = 4 },
37
new { t = "uint", n = 4 },
38
new { t = "long", n = 8 },
39
new { t = "ulong", n = 8 },
40
new { t = "char", n = 2 },
41
new { t = "float", n = 4 },
42
new { t = "double", n = 8 },
43
new { t = "bool", n = 1 }
45
string program = @"using System;
48
public int s = $sizeof(" + t.t + @")$;
51
var rr = Resolve<SizeOfResolveResult>(program);
52
Assert.IsTrue(rr.Type.IsKnownType(KnownTypeCode.Int32));
53
Assert.IsFalse(rr.IsError);
54
Assert.AreEqual(t.n, rr.ConstantValue);
55
Assert.IsTrue(Type.GetType(rr.ReferencedType.FullName).IsPrimitive);
60
public void SizeOfEnum()
66
int s = $sizeof(TestEnum)$;
69
var rr = Resolve<SizeOfResolveResult>(program);
70
Assert.IsTrue(rr.Type.IsKnownType(KnownTypeCode.Int32));
71
Assert.IsFalse(rr.IsError);
72
Assert.AreEqual(4, rr.ConstantValue);
73
Assert.AreEqual("TestEnum", rr.ReferencedType.Name);
76
enum TestEnum2 : short {}
79
int s = $sizeof(TestEnum2)$;
82
rr = Resolve<SizeOfResolveResult>(program);
83
Assert.IsTrue(rr.Type.IsKnownType(KnownTypeCode.Int32));
84
Assert.IsFalse(rr.IsError);
85
Assert.AreEqual(2, rr.ConstantValue);
86
Assert.AreEqual("TestEnum2", rr.ReferencedType.Name);
90
public void SizeOfStructIsNotAConstant()
96
int s = $sizeof(MyStruct)$;
99
var rr = Resolve<SizeOfResolveResult>(program);
100
Assert.IsTrue(rr.Type.IsKnownType(KnownTypeCode.Int32));
101
Assert.IsFalse(rr.IsError);
102
Assert.IsNull(rr.ConstantValue);
103
Assert.AreEqual("MyStruct", rr.ReferencedType.Name);
107
public void SizeOfReferenceTypeIsAnError() {
112
int s = $sizeof(MyClass)$;
115
var rr = Resolve<SizeOfResolveResult>(program);
116
Assert.IsTrue(rr.Type.IsKnownType(KnownTypeCode.Int32));
117
Assert.IsTrue(rr.IsError);
118
Assert.IsNull(rr.ConstantValue);
119
Assert.AreEqual("MyClass", rr.ReferencedType.Name);