public void box_unbox_Test_1() { // Creates objects for testing of different casts. object o_enum = MyEnum.Value; object o_enum1 = MyEnum1.Value; object o_long = 24L; object o_class = new CastTestClass(); object o_guid = Guid.NewGuid(); // Try casts that shoud succeed. Any exception here means failure. // First we try casts that should succeed. // Casts between enums with the same basic type MyEnum2 e2 = (MyEnum2)o_enum; // line 2 // Cast from enum to primitive type that enum is based on short sv = (short)o_enum; Assert.Equal(sv, (short)MyEnum.Value); // Cast from enum to primitive type that enum is based on int iv = (int)o_enum1; Assert.Equal(iv, (short)MyEnum1.Value); int i_long = (int)(long)o_long; CastTestClass cls = (CastTestClass)o_class; Guid guid = (Guid)o_guid; // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { MyEnum1 e1 = (MyEnum1)o_enum; }, "Trying to cast incompatible enums - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_long; }, "Trying to cast long to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_class; }, "Trying to cast object to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_enum; }, "Trying to cast enum to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_guid; }, "Trying to cast Guid to int - should throw InvalidCastException"); }
public MFTestResults box_unbox_Test_1() { MFTestResults retVal = MFTestResults.Pass; // Creates objects for testing of different casts. object o_enum = MyEnum.Value; object o_enum1 = MyEnum1.Value; object o_long = 24L; object o_class = new CastTestClass(); object o_guid = Guid.NewGuid(); // Try casts that shoud succeed. Any exception here means failure. try { // First we try casts that should succeed. // Casts between enums with the same basic type MyEnum2 e2 = (MyEnum2)o_enum; // line 2 // Cast from enum to primitive type that enum is based on short sv = (short)o_enum; if (sv != (short)MyEnum.Value) { Log.Comment("Changed value during box/unbox from " + MyEnum.Value + " to " + sv); retVal = MFTestResults.Fail; } // Cast from enum to primitive type that enum is based on int iv = (int)o_enum1; if (iv != (short)MyEnum1.Value) { Log.Comment("Changed value during box/unbox from " + MyEnum1.Value + " to " + iv); retVal = MFTestResults.Fail; } int i_long = (int)(long)o_long; CastTestClass cls = (CastTestClass)o_class; Guid guid = (Guid)o_guid; } catch (Exception e) { Log.Comment("Exception thrown during unbox test" + e.Message); retVal = MFTestResults.Fail; } // Now casts that should throw exception. Any cast that does not throw - means error. try { MyEnum1 e1 = (MyEnum1)o_enum; Log.Comment("Error, cast succeded, but should have thrown exception"); retVal = MFTestResults.Fail; } catch (Exception ) { } // Now casts that should throw exception. Any cast that does not throw - means error. try { int i = (int)o_long; Log.Comment("Error, cast object with long to int succeded, but should have thrown exception"); retVal = MFTestResults.Fail; } catch (Exception ) { } // Now casts that should throw exception. Any cast that does not throw - means error. try { int i = (int)o_class; Log.Comment("Error, cast object class to int succeded, but should have thrown exception"); retVal = MFTestResults.Fail; } catch (Exception ) { } // Now casts that should throw exception. Any cast that does not throw - means error. try { int i = (int)o_enum; Log.Comment("Error, cast object class to int succeded, but should have thrown exception"); retVal = MFTestResults.Fail; } catch (Exception ) { } // Now casts that should throw exception. Any cast that does not throw - means error. try { int i = (int)o_guid; Log.Comment("Error, cast stuct Guid class to int succeded, but should have thrown exception"); retVal = MFTestResults.Fail; } catch (Exception) { } return retVal; }