return 0;
int chars = 0;
- int result = 0;
+ uint result = 0;
int digitValue;
int i = 0;
i += 2;
}
+ uint max_value;
+ if ((flags & TreatAsI1) != 0) {
+ max_value = Byte.MaxValue;
+ } else if ((flags & TreatAsI2) != 0) {
+ max_value = UInt16.MaxValue;
+ } else {
+ max_value = UInt32.MaxValue;
+ }
+
while (i < len) {
char c = value [i++];
if (Char.IsNumber (c)) {
throw new FormatException ("Could not find any parsable digits.");
}
- result = fromBase * result + digitValue;
+ if ((flags & TreatAsUnsigned) != 0) {
+ result = (uint) fromBase * result + (uint) digitValue;
+ if (result > max_value)
+ throw new OverflowException ();
+ } else {
+ var res = (uint) fromBase * result + (uint) digitValue;
+ if (res < result || res > max_value)
+ throw new OverflowException ();
+
+ result = res;
+ }
+
chars++;
}
if (chars == 0)
throw new FormatException ("Could not find any parsable digits.");
- return negative ? -result : result;
+ return negative ? -(int)result : (int)result;
}
public static string LongToString (long value, int toBase, int width, char paddingChar, int flags)
ci.NumberFormat.NumberDecimalDigits = 3;
}
+ [Test]
public void TestChangeType() {
int iTest = 1;
try {
iTest++;
Assert.AreEqual ((float)1234, Convert.ChangeType(tryInt16, typeof(float), ci), "#A12");
iTest++;
- Assert.AreEqual (null, Convert.ChangeType(null, null, ci), "#A13");
+ try {
+ Assert.AreEqual (null, Convert.ChangeType(null, null, ci), "#A13");
+ Assert.Fail ("#F13");
+ } catch (ArgumentNullException) {
+ }
iTest++;
Assert.AreEqual ((decimal)0, Convert.ChangeType(tryByte, TypeCode.Decimal, ci), "#A14");
}
+ [Test]
public void TestGetTypeCode() {
int marker = 1;
try {
}
}
+ [Test]
public void TestIsDBNull() {
Assert.AreEqual (false, Convert.IsDBNull(tryInt32), "#C01");
Assert.AreEqual (true, Convert.IsDBNull(Convert.DBNull), "#C02");
Assert.AreEqual (false, Convert.IsDBNull(tryFloat), "#C05");
}
+ [Test]
public void TestToBoolean() {
tryObj = (object)tryDbl;
}
}
+ [Test]
public void TestToByte() {
Assert.AreEqual ((byte)1, Convert.ToByte(boolTrue), "#E01");
}
}
+ [Test]
public void TestToChar(){
tryByte = 58;
Assert.AreEqual (':', Convert.ToChar(tryByte), "#F01");
Convert.ToDateTime("20002-25-01");
} */
+ [Test]
public void TestToDateTime() {
string dateString = "01/01/2002";
int iTest = 1;
}
+ [Test]
public void TestToDecimal() {
Assert.AreEqual ((decimal)1, Convert.ToDecimal(boolTrue), "#H01");
Assert.AreEqual ((decimal)0, Convert.ToDecimal(boolFalse), "#H02");
}
}
+ [Test]
public void TestToDouble() {
int iTest = 1;
try {
}
}
+ [Test]
public void TestToInt16() {
Assert.AreEqual ((short)0, Convert.ToInt16(boolFalse), "#J01");
Assert.AreEqual ((short)1, Convert.ToInt16(boolTrue), "#J02");
}
}
+ [Test]
public void TestToInt32() {
long tryMax = long.MaxValue;
long tryMin = long.MinValue;
}
}
+ [Test]
public void TestToInt64() {
decimal longMax = long.MaxValue;
longMax += 1000000;
}
}
+ [Test]
public void TestToSByte() {
int iTest = 1;
try {
}
}
+ [Test]
public void TestToSingle() {
int iTest = 1;
try {
}
}
+ [Test]
public void TestToString() {
tryByte = 123;
}
}
+ [Test]
public void TestToUInt16() {
Assert.AreEqual ((ushort)0, Convert.ToUInt16(boolFalse), "#P01");
Assert.AreEqual ((ushort)1, Convert.ToUInt16(boolTrue), "#P02");
}
}
+ [Test]
public void TestSignedToInt() {
// String cannot contain a minus sign if the base is not 10.
// But can if it is ten, and + is allowed everywhere.
}
+ [Test]
public void TestToUInt32() {
Assert.AreEqual ((uint)1, Convert.ToUInt32(boolTrue), "#Q01");
Assert.AreEqual ((uint)0, Convert.ToUInt32(boolFalse), "#Q02");
}
}
+ [Test]
public void TestToUInt64()
{
int iTest = 1;
Assert.AreEqual (bb [i], b2 [i], "#0" + (i + 2));
}
+ [Test]
public void TestToBase64CharArray ()
{
byte[] byteArr = {33, 127, 255, 109, 170, 54};
Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, Int32.MaxValue);
}
+ [Test]
public void TestToBase64String() {
byte[] byteArr = {33, 127, 255, 109, 170, 54};
string expectedStr = "IX//bao2";
Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("1000000000000000000000000000000000000000000000000000000000000000", 2), "1000000000000000000000000000000000000000000000000000000000000000,2");
}
+ [Test]
+ public void MoreOverflows ()
+ {
+ try {
+ Convert.ToInt16 ("ffff7fff", 16);
+ Assert.Fail ("#1");
+ } catch (OverflowException) {
+ }
+
+ try {
+ Convert.ToSByte ("ffff7fff", 16);
+ Assert.Fail ("#2");
+ } catch (OverflowException) {
+ }
+
+ try {
+ Convert.ToUInt32 ("4294967298", 10);
+ Assert.Fail ("#3");
+ } catch (OverflowException) {
+ }
+ }
+
// signed types
[Test]