Merge remote-tracking branch 'local/msvc-updates' into msvc-updates
[mono.git] / mcs / class / corlib / Test / System.Runtime.Versioning / VersioningHelperTest.cs
1 //
2 // VersioningHelperTest.cs -
3 //      Unit tests for System.Runtime.Versioning.VersioningHelper
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Runtime.Versioning;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Runtime.Versioning {
38
39         [TestFixture]
40         public class VersioningHelperTest {
41
42                 [Test]
43                 public void Name_Null ()
44                 {
45                         string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, ResourceScope.AppDomain);
46                         Assert.AreEqual (String.Empty, s, "Result");
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentException))]
51                 public void From_Invalid ()
52                 {
53                         ResourceScope bad = (ResourceScope) Int32.MinValue;
54                         string s = VersioningHelper.MakeVersionSafeName (null, bad, ResourceScope.None);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentException))]
59                 public void To_Invalid ()
60                 {
61                         ResourceScope bad = (ResourceScope) Int32.MinValue;
62                         string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, bad);
63                 }
64
65                 [Test]
66                 public void Type_Null ()
67                 {
68                         string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, ResourceScope.AppDomain, null);
69                         Assert.AreEqual (String.Empty, s, "Result");
70                 }
71
72                 // ResourceScope is encoded 6 bits for "from" and 6 bits for "to"
73                 static int[] ValidValues = { 65, 66, 68, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 
74                         143, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 1089, 1090, 1092, 1105, 1106, 
75                         1108, 1121, 1122, 1124, 1137, 1138, 1140, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162,
76                         1163, 1164, 1165, 1166, 1167, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180,
77                         1181, 1182, 1183, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198,
78                         1199, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1284,
79                         1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1300, 1301, 1302, 1303, 1304,
80                         1305, 1306, 1307, 1308, 1309, 1310, 1311, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 
81                         1325, 1326, 1327, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343, 2113, 
82                         2114, 2116, 2145, 2146, 2148, 2178, 2179, 2180, 2181, 2182, 2183, 2184, 2185, 2186, 2187, 2188, 
83                         2189, 2190, 2191, 2210, 2211, 2212, 2213, 2214, 2215, 2216, 2217, 2218, 2219, 2220, 2221, 2222, 
84                         2223, 2308, 2309, 2310, 2311, 2312, 2313, 2314, 2315, 2316, 2317, 2318, 2319, 2340, 2341, 2342,
85                         2343, 2344, 2345, 2346, 2347, 2348, 2349, 2350, 2351 };
86
87                 private bool IsValid (int n)
88                 {
89                         return (Array.BinarySearch (ValidValues, n) >= 0);
90                 }
91
92                 [Test]
93                 // note: this one can be _very_ slow under a debugger due to the high number of exceptions
94                 public void MakeVersionSafeName ()
95                 {
96                         for (int i = 0; i < 64; i++) {
97                                 ResourceScope from = (ResourceScope) i;
98                                 for (int j = 0; j < 64; j++) {
99                                         ResourceScope to = (ResourceScope) j;
100                                         bool valid = IsValid ((i << 6) | j);
101                                         string from_to = String.Format ("From ({2}) {0} to ({3}) {1}", from, to, (int)from, (int)to);
102                                         try {
103                                                 string s = VersioningHelper.MakeVersionSafeName ("a", from, to);
104                                                 Assert.IsTrue (valid, from_to);
105                                         }
106                                         catch (ArgumentException) {
107                                                 Assert.IsFalse (valid, from_to);
108                                         }
109                                 }
110                         }
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))]
115                 public void MakeVersionSafeName_SingleUnWorking ()
116                 {
117                         VersioningHelper.MakeVersionSafeName ("a", ResourceScope.None, ResourceScope.None);
118                 }
119
120                 [Test]
121                 public void ConvertTo_AppDomain ()
122                 {
123                         // if appdomain is present in "from" then the ouput is unchanged
124                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.Library), "1a");
125                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain), "1b");
126                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain | ResourceScope.Machine), "1c");
127
128                         // if "from" doesn't have appdomain then the appdomain id is appended
129                         Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.AppDomain).StartsWith ("a_"), "2");
130                 }
131
132                 [Test]
133                 public void ConvertTo_Process ()
134                 {
135                         // if process is present in "from" then the ouput is unchanged
136                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Library), "1a");
137                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Process), "1b");
138                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Process | ResourceScope.Library), "1c");
139
140                         // if "from" doesn't have process then the process id is appended
141                         Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.Process).StartsWith ("a_"), "2");
142                 }
143
144                 [Test]
145                 public void ConvertTo_AppDomain_Process ()
146                 {
147                         // if the appdomain is present then the output is unchanged (process isn't appended)
148                         Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain | ResourceScope.Process), "1a");
149                         // if the process is present then appdomain is appended to the output
150                         Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.AppDomain | ResourceScope.Process).StartsWith ("a_"), "1b");
151
152                         // if "from" doesn't have process nor appdomain then both id are appended
153                         string s = VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.AppDomain);
154                         int p1 = s.IndexOf ("_");
155                         Assert.IsTrue ((p1 > 0), "first _");
156                         int p2 = s.LastIndexOf ("_");
157                         Assert.IsTrue (((p2 > 0) && (p2 != p1)), "second _");
158                 }
159         }
160 }
161
162 #endif