~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/SizeOfTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 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
 
 
19
using System;
 
20
using ICSharpCode.NRefactory.Semantics;
 
21
using NUnit.Framework;
 
22
using ICSharpCode.NRefactory.TypeSystem;
 
23
 
 
24
namespace ICSharpCode.NRefactory.CSharp.Resolver
 
25
{
 
26
        [TestFixture]
 
27
        public class SizeOfTests : ResolverTestBase
 
28
        {
 
29
                [Test]
 
30
                public void SizeOfPrimitiveTypes()
 
31
                {
 
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 }
 
44
                                }) {
 
45
                                string program = @"using System;
 
46
                                        class TestClass {
 
47
                                                static void Main() {
 
48
                                                        public int s = $sizeof(" + t.t + @")$;
 
49
                                                }
 
50
                                        }";
 
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);
 
56
                        }
 
57
                }
 
58
                
 
59
                [Test]
 
60
                public void SizeOfEnum()
 
61
                {
 
62
                        string program = @"
 
63
enum TestEnum {}
 
64
class TestClass {
 
65
        static void Main() {
 
66
                int s = $sizeof(TestEnum)$;
 
67
        }
 
68
}";
 
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);
 
74
 
 
75
                        program = @"
 
76
enum TestEnum2 : short {}
 
77
class TestClass {
 
78
        static void Main() {
 
79
                int s = $sizeof(TestEnum2)$;
 
80
        }
 
81
}";
 
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);
 
87
                }
 
88
                
 
89
                [Test]
 
90
                public void SizeOfStructIsNotAConstant()
 
91
                {
 
92
                        string program = @"
 
93
struct MyStruct {}
 
94
class TestClass {
 
95
        static void Main() {
 
96
                int s = $sizeof(MyStruct)$;
 
97
        }
 
98
}";
 
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);
 
104
                }
 
105
 
 
106
                [Test]
 
107
                public void SizeOfReferenceTypeIsAnError() {
 
108
                        string program = @"
 
109
class MyClass {}
 
110
class TestClass {
 
111
        static void Main() {
 
112
                int s = $sizeof(MyClass)$;
 
113
        }
 
114
}";
 
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);
 
120
                }
 
121
        }
 
122
}