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