Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / corlib / System / Int16.cs
1 //
2 // System.Int16.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004 Novell (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 using System.Globalization;
31
32 namespace System {
33         
34         [Serializable]
35         [System.Runtime.InteropServices.ComVisible (true)]
36         public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16>
37         {
38
39                 public const short MaxValue =  32767;
40                 public const short MinValue = -32768;
41                 
42                 internal short m_value;
43
44                 public int CompareTo (object value)
45                 {
46                         if (value == null)
47                                 return 1;
48
49                         if (!(value is System.Int16))
50                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
51
52                         short xv = (short) value;
53                         if (m_value == xv)
54                                 return 0;
55                         if (m_value > xv)
56                                 return 1;
57                         else
58                                 return -1;
59                 }
60
61                 public override bool Equals (object obj)
62                 {
63                         if (!(obj is System.Int16))
64                                 return false;
65
66                         return ((short) obj) == m_value;
67                 }
68
69                 public override int GetHashCode ()
70                 {
71                         return m_value;
72                 }
73
74                 public int CompareTo (short value)
75                 {
76                         if (m_value == value)
77                                 return 0;
78                         if (m_value > value)
79                                 return 1;
80                         else
81                                 return -1;
82                 }
83
84                 public bool Equals (short obj)
85                 {
86                         return obj == m_value;
87                 }
88
89                 internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
90                 {
91                         short val = 0;
92                         int len;
93                         int i, sign = 1;
94                         bool digits_seen = false;
95
96                         result = 0;
97                         exc = null;
98
99                         if (s == null) {
100                                 if (!tryParse)
101                                         exc = new ArgumentNullException ("s");
102                                 return false;
103                         }
104
105                         len = s.Length;
106
107                         char c;
108                         for (i = 0; i < len; i++){
109                                 c = s [i];
110                                 if (!Char.IsWhiteSpace (c))
111                                         break;
112                         }
113                         
114                         if (i == len) {
115                                 if (!tryParse)
116                                         exc = Int32.GetFormatException ();
117                                 return false;
118                         }
119
120                         c = s [i];
121                         if (c == '+')
122                                 i++;
123                         else if (c == '-'){
124                                 sign = -1;
125                                 i++;
126                         }
127                         
128                         for (; i < len; i++){
129                                 c = s [i];
130                                 if (c >= '0' && c <= '9'){
131                                         byte d = (byte) (c - '0');
132                                                 
133                                         if (val > (MaxValue/10))
134                                                 goto overflow;
135                                         
136                                         if (val == (MaxValue/10)){
137                                                 if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
138                                                         goto overflow;
139                                                 if (sign == -1)
140                                                         val = (short) ((val * sign * 10) - d);
141                                                 else
142                                                         val = (short) ((val * 10) + d);
143
144                                                 if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
145                                                         result = val;
146                                                         return true;
147                                                 }
148                                                 goto overflow;
149                                         } else 
150                                                 val = (short) (val * 10 + d);
151                                         
152                                         
153                                         digits_seen = true;
154                                 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
155                                         return false;
156                                         
157                         }
158                         if (!digits_seen) {
159                                 if (!tryParse)
160                                         exc = Int32.GetFormatException ();
161                                 return false;
162                         }
163                         
164                         if (sign == -1)
165                                 result = (short) (val * sign);
166                         else
167                                 result = val;
168
169                         return true;
170
171                 overflow:
172                         if (!tryParse)
173                                 exc = new OverflowException ("Value is too large");
174                         return false;
175                 }
176
177                 public static short Parse (string s, IFormatProvider provider)
178                 {
179                         return Parse (s, NumberStyles.Integer, provider);
180                 }
181
182                 public static short Parse (string s, NumberStyles style)
183                 {
184                         return Parse (s, style, null);
185                 }
186
187                 public static short Parse (string s, NumberStyles style, IFormatProvider provider)
188                 {
189                         int tmpResult = Int32.Parse (s, style, provider);
190                         if ((style & NumberStyles.AllowHexSpecifier) != 0) {
191                                 if (tmpResult >= 0 && tmpResult <= ushort.MaxValue)
192                                         return (short) tmpResult;
193                         } else if (tmpResult <= MaxValue && tmpResult >= MinValue) {
194                                 return (short) tmpResult;
195                         }
196
197                         throw new OverflowException ("Value too large or too small.");
198                 }
199
200                 public static short Parse (string s) 
201                 {
202                         Exception exc;
203                         short res;
204
205                         if (!Parse (s, false, out res, out exc))
206                                 throw exc;
207
208                         return res;
209                 }
210
211                 public static bool TryParse (string s, out short result) 
212                 {
213                         Exception exc;
214                         if (!Parse (s, true, out result, out exc)) {
215                                 result = 0;
216                                 return false;
217                         }
218
219                         return true;
220                 }
221
222                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) 
223                 {
224                         int tmpResult;
225                         result = 0;
226                                 
227                         if (!Int32.TryParse (s, style, provider, out tmpResult))
228                                 return false;
229                         
230                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
231                                 return false;
232                                 
233                         result = (short)tmpResult;
234                         return true;
235                 }
236
237                 public override string ToString ()
238                 {
239                         return NumberFormatter.NumberToString (m_value, null);
240                 }
241
242                 public string ToString (IFormatProvider provider)
243                 {
244                         return NumberFormatter.NumberToString (m_value, provider);
245                 }
246
247                 public string ToString (string format)
248                 {
249                         return ToString (format, null);
250                 }
251
252                 public string ToString (string format, IFormatProvider provider)
253                 {
254                         return NumberFormatter.NumberToString(format, m_value, provider);
255                 }
256
257                 // =========== IConvertible Methods =========== //
258
259                 public TypeCode GetTypeCode ()
260                 {
261                         return TypeCode.Int16;
262                 }
263
264                 bool IConvertible.ToBoolean (IFormatProvider provider)
265                 {
266                         return System.Convert.ToBoolean (m_value);
267                 }
268
269                 byte IConvertible.ToByte (IFormatProvider provider)
270                 {
271                         return System.Convert.ToByte (m_value);
272                 }
273
274                 char IConvertible.ToChar (IFormatProvider provider)
275                 {
276                         return System.Convert.ToChar (m_value);
277                 }
278
279                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
280                 {
281                         return System.Convert.ToDateTime (m_value);
282                 }
283
284                 decimal IConvertible.ToDecimal (IFormatProvider provider)
285                 {
286                         return System.Convert.ToDecimal (m_value);
287                 }
288
289                 double IConvertible.ToDouble (IFormatProvider provider)
290                 {
291                         return System.Convert.ToDouble (m_value);
292                 }
293
294                 short IConvertible.ToInt16 (IFormatProvider provider)
295                 {
296                         return System.Convert.ToInt16 (m_value);
297                 }
298
299                 int IConvertible.ToInt32 (IFormatProvider provider)
300                 {
301                         return System.Convert.ToInt32 (m_value);
302                 }
303
304                 long IConvertible.ToInt64 (IFormatProvider provider)
305                 {
306                         return System.Convert.ToInt64 (m_value);
307                 }
308
309                 sbyte IConvertible.ToSByte (IFormatProvider provider)
310                 {
311                         return System.Convert.ToSByte (m_value);
312                 }
313
314                 float IConvertible.ToSingle (IFormatProvider provider)
315                 {
316                         return System.Convert.ToSingle (m_value);
317                 }
318
319                 object IConvertible.ToType (Type targetType, IFormatProvider provider)
320                 {
321                         if (targetType == null)
322                                 throw new ArgumentNullException ("targetType");
323                         return System.Convert.ToType (m_value, targetType, provider, false);
324                 }
325
326                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
327                 {
328                         return System.Convert.ToUInt16 (m_value);
329                 }
330
331                 uint IConvertible.ToUInt32 (IFormatProvider provider)
332                 {
333                         return System.Convert.ToUInt32 (m_value);
334                 }
335
336                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
337                 {
338                         return System.Convert.ToUInt64 (m_value);
339                 }
340         }
341 }