ByteTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // ByteTest.cs - NUnit Test Cases for the System.Byte struct
  2. //
  3. // Mario Martinez ([email protected])
  4. //
  5. // (C) Ximian, Inc. http://www.ximian.com
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Globalization;
  10. using System.Threading;
  11. namespace MonoTests.System
  12. {
  13. public class ByteTest : TestCase
  14. {
  15. private const Byte MyByte1 = 42;
  16. private const Byte MyByte2 = 0;
  17. private const Byte MyByte3 = 255;
  18. private const string MyString1 = "42";
  19. private const string MyString2 = "0";
  20. private const string MyString3 = "255";
  21. private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
  22. private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
  23. private string[] Results1 = { "",
  24. "0", "0.000000e+000", "0.00",
  25. "0", "0.00", "0.00 %", "0"};
  26. private string[] Results1_Nfi = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
  27. "0", "0.000000e+000", "0.00",
  28. "0", "0.00", "0.00 %", "0"};
  29. private string[] Results2 = {NumberFormatInfo.CurrentInfo.CurrencySymbol+"255.00000",
  30. "00255", "2.55000e+002", "255.00000",
  31. "255", "255.00000", "25,500.00000 %", "000ff"};
  32. private string[] Results2_Nfi = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"255.00000",
  33. "00255", "2.55000e+002", "255.00000",
  34. "255", "255.00000", "25,500.00000 %", "000ff"};
  35. private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
  36. public ByteTest() {}
  37. protected override void SetUp() {
  38. int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
  39. string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
  40. string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
  41. Results1[0] = csym + "0" + csuffix;
  42. }
  43. protected override void TearDown () {
  44. }
  45. public void TestMinMax()
  46. {
  47. AssertEquals(Byte.MinValue, MyByte2);
  48. AssertEquals(Byte.MaxValue, MyByte3);
  49. }
  50. public void TestCompareTo()
  51. {
  52. Assert(MyByte3.CompareTo(MyByte2) > 0);
  53. Assert(MyByte2.CompareTo(MyByte2) == 0);
  54. Assert(MyByte1.CompareTo((Byte)42) == 0);
  55. Assert(MyByte2.CompareTo(MyByte3) < 0);
  56. try {
  57. MyByte2.CompareTo(100);
  58. Fail("Should raise a System.ArgumentException");
  59. }
  60. catch (Exception e) {
  61. Assert(typeof(ArgumentException) == e.GetType());
  62. }
  63. }
  64. public void TestEquals()
  65. {
  66. Assert(MyByte1.Equals(MyByte1));
  67. Assert(MyByte1.Equals((Byte)42));
  68. Assert(MyByte1.Equals((Int16)42) == false);
  69. Assert(MyByte1.Equals(MyByte2) == false);
  70. }
  71. public void TestGetHashCode()
  72. {
  73. try {
  74. MyByte1.GetHashCode();
  75. MyByte2.GetHashCode();
  76. MyByte3.GetHashCode();
  77. }
  78. catch {
  79. Fail("GetHashCode should not raise an exception here");
  80. }
  81. }
  82. public void TestParse()
  83. {
  84. //test Parse(string s)
  85. Assert("MyByte1="+MyByte1+", MyString1="+MyString1+", Parse="+Byte.Parse(MyString1) , MyByte1 == Byte.Parse(MyString1));
  86. Assert("MyByte2", MyByte2 == Byte.Parse(MyString2));
  87. Assert("MyByte3", MyByte3 == Byte.Parse(MyString3));
  88. try {
  89. Byte.Parse(null);
  90. Fail("Should raise a System.ArgumentNullException");
  91. }
  92. catch (Exception e) {
  93. Assert("Should get ArgumentNullException", typeof(ArgumentNullException) == e.GetType());
  94. }
  95. try {
  96. Byte.Parse("not-a-number");
  97. Fail("Should raise a System.FormatException");
  98. }
  99. catch (Exception e) {
  100. Assert("not-a-number", typeof(FormatException) == e.GetType());
  101. }
  102. try {
  103. int OverInt = Byte.MaxValue + 1;
  104. Byte.Parse(OverInt.ToString());
  105. Fail("Should raise a System.OverflowException");
  106. }
  107. catch (Exception e) {
  108. Assert("OverflowException", typeof(OverflowException) == e.GetType());
  109. }
  110. //test Parse(string s, NumberStyles style)
  111. AssertEquals(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ",
  112. (byte)42, Byte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ",
  113. NumberStyles.Currency));
  114. try {
  115. Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
  116. Fail("Should raise a System.FormatException");
  117. }
  118. catch (Exception e) {
  119. Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer", typeof(FormatException) == e.GetType());
  120. }
  121. //test Parse(string s, IFormatProvider provider)
  122. Assert(" 42 and Nfi", 42 == Byte.Parse(" 42 ", Nfi));
  123. try {
  124. Byte.Parse("%42", Nfi);
  125. Fail("Should raise a System.FormatException");
  126. }
  127. catch (Exception e) {
  128. Assert("%42 and Nfi", typeof(FormatException) == e.GetType());
  129. }
  130. //test Parse(string s, NumberStyles style, IFormatProvider provider)
  131. Assert("NumberStyles.HexNumber", 16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
  132. try {
  133. Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
  134. Fail("Should raise a System.FormatException");
  135. }
  136. catch (Exception e) {
  137. Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi", typeof(FormatException) == e.GetType());
  138. }
  139. }
  140. public void TestToString()
  141. {
  142. //test ToString()
  143. AssertEquals("Compare failed for MyString1 and MyByte1", MyString1, MyByte1.ToString());
  144. AssertEquals("Compare failed for MyString2 and MyByte2", MyString2, MyByte2.ToString());
  145. AssertEquals("Compare failed for MyString3 and MyByte3", MyString3, MyByte3.ToString());
  146. //test ToString(string format)
  147. for (int i=0; i < Formats1.Length; i++) {
  148. AssertEquals("Compare failed for Formats1["+i.ToString()+"]", Results1[i], MyByte2.ToString(Formats1[i]));
  149. AssertEquals("Compare failed for Formats2["+i.ToString()+"]", Results2[i], MyByte3.ToString(Formats2[i]));
  150. }
  151. //test ToString(string format, IFormatProvider provider);
  152. for (int i=0; i < Formats1.Length; i++) {
  153. AssertEquals("Compare failed for Formats1["+i.ToString()+"] with Nfi", Results1_Nfi[i], MyByte2.ToString(Formats1[i], Nfi));
  154. AssertEquals("Compare failed for Formats2["+i.ToString()+"] with Nfi", Results2_Nfi[i], MyByte3.ToString(Formats2[i], Nfi));
  155. }
  156. try {
  157. MyByte1.ToString("z");
  158. Fail("Should raise a System.FormatException");
  159. }
  160. catch (Exception e) {
  161. AssertEquals("Exception is the wrong type", typeof(FormatException), e.GetType());
  162. }
  163. }
  164. }
  165. }