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