瀏覽代碼

Implemented GetHashCode for constraint classes. Added tests and continued
work on DataColumn

svn path=/trunk/mcs/; revision=7299

Franklin Wise 23 年之前
父節點
當前提交
5fda660239

+ 10 - 0
mcs/class/System.Data/ChangeLog

@@ -1,3 +1,13 @@
+2002-09-06  Franklin Wise <[email protected]>
+	
+	* System.Data/DataColumn.cs: More flushing out.
+
+	* System.Data/ForeignKeyConstraint.cs: Implemented GetHashCode()
+
+	* System.Data/UniqueKeyConstraint.cs: Implemented GetHashCode()
+	
+	* Tests:  See Changelog for System.Data/Test
+	
 2002-09-04  Franklin Wise <[email protected]>
 	
 	* Tests:  See Changelog for System.Data/Test

+ 35 - 8
mcs/class/System.Data/System.Data/DataColumn.cs

@@ -48,12 +48,12 @@ namespace System.Data
 		private MappingType _columnMapping = MappingType.Element;
 		private string _columnName = null;
 		private Type _dataType = null;
-		private object defaultValue = null;
+		private object _defaultValue = null;
 		private string expression = null;
-		private PropertyCollection extendedProperties = null;
-		private int maxLength = -1;
+		private PropertyCollection _extendedProperties = null;
+		private int maxLength = -1; //-1 represents no length limit
 		private string nameSpace = null;
-		private int ordinal = -1;
+		private int _ordinal = -1; //-1 represents not part of a collection
 		private string prefix = null;
 		private bool readOnly = false;
 		private DataTable _table = null;
@@ -244,13 +244,32 @@ namespace System.Data
 		/// 
 		/// </summary>
 		/// <remarks>When AutoIncrement is set to true, there can be no default value.</remarks>
+		/// <exception cref="System.InvalidCastException"></exception>
+		/// <exception cref="System.ArgumentException"></exception>
 		public object DefaultValue
 		{
 			get {
-				return defaultValue;
+				return _defaultValue;
 			}
 			set {
-				defaultValue = value;
+				
+				//If autoIncrement == true throw
+				if (AutoIncrement) 
+				{
+					throw new ArgumentException("Can not set default value while" +
+							" AutoIncrement is true on this column.");
+				}
+					
+				//Will throw invalid cast exception
+				//if value is not the correct type
+				//FIXME: some types can be casted
+				if (value.GetType() != _dataType)
+				{
+					throw new InvalidCastException("Default Value type is not compatible with" + 
+							" column type.");
+				}
+					
+				_defaultValue = value;
 			}
 		}
 
@@ -269,16 +288,18 @@ namespace System.Data
 		public PropertyCollection ExtendedProperties
 		{
 			get {
-				return extendedProperties;
+				return _extendedProperties;
 			}
 		}
 
 		public int MaxLength
 		{
 			get {
+				//Default == -1 no max length
 				return maxLength;
 			}
 			set {
+				//only applies to string columns
 				maxLength = value;
 			}
 		}
@@ -297,10 +318,16 @@ namespace System.Data
 		public int Ordinal
 		{
 			get {
-				return ordinal;
+				//value is -1 if not part of a collection
+				return _ordinal;
 			}
 		}
 
+		internal void SetOrdinal(int ordinal)
+		{
+			_ordinal = ordinal;
+		}
+
 		public string Prefix
 		{
 			get {

+ 33 - 3
mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs

@@ -296,9 +296,39 @@ namespace System.Data
 			return true;
 		}
 
-		[MonoTODO]
-		public override int GetHashCode() {
-			throw new NotImplementedException ();
+		public override int GetHashCode()
+		{
+			//initialize hash1 and hash2 with default hashes
+			//any two DIFFERENT numbers will do here
+			int hash1 = 32, hash2 = 88;
+			int i;
+
+			//derive the hash code from the columns that way
+			//Equals and GetHashCode return Equal objects to be the
+			//same
+
+			//Get the first parent column hash
+			if (this.Columns.Length > 0)
+				hash1 ^= this.Columns[0].GetHashCode();
+			
+			//get the rest of the parent column hashes if there any
+			for (i = 1; i < this.Columns.Length; i++)
+			{
+				hash1 ^= this.Columns[1].GetHashCode();
+				
+			}
+			
+			//Get the child column hash
+			if (this.RelatedColumns.Length > 0)
+				hash2 ^= this.Columns[0].GetHashCode();
+			
+			for (i = 1; i < this.RelatedColumns.Length; i++)
+			{
+				hash2 ^= this.RelatedColumns[1].GetHashCode();
+			}
+
+			//combine the two hashes
+			return hash1 ^ hash2;
 		}
 
 		internal override void AddToConstraintCollectionSetup(

+ 22 - 3
mcs/class/System.Data/System.Data/UniqueConstraint.cs

@@ -288,9 +288,28 @@ namespace System.Data
 
 		}
 
-		[MonoTODO]
-		public override int GetHashCode() {
-			throw new NotImplementedException ();
+		public override int GetHashCode() 
+		{
+			//initialize hash with default value 
+			int hash = 42;
+			int i;
+
+			//derive the hash code from the columns that way
+			//Equals and GetHashCode return Equal objects to be the
+			//same
+
+			//Get the first column hash
+			if (this.Columns.Length > 0)
+				hash ^= this.Columns[0].GetHashCode();
+			
+			//get the rest of the column hashes if there any
+			for (i = 1; i < this.Columns.Length; i++)
+			{
+				hash ^= this.Columns[1].GetHashCode();
+				
+			}
+			
+			return hash ;
 		}
 		
 	

+ 7 - 0
mcs/class/System.Data/Test/ChangeLog

@@ -1,3 +1,10 @@
+2002-09-06  Franklin Wise <[email protected]>
+
+	*  System.Data\ForeignKeyConstraintTest.cs: added
+	  TestEqualAndHashCode() test.
+	
+	*  System.Data\UniqueConstraint.cs: Added testing for HashCode
+	
 2002-09-04  Franklin Wise <[email protected]>
 	
 	* New Files: 

+ 45 - 2
mcs/class/System.Data/Test/System.Data/DataColumnTest.cs

@@ -144,10 +144,10 @@ namespace MonoTests.System.Data
 			DataColumn col = new DataColumn();
 			DataColumn col2 = new DataColumn();
 			
+			col.ColumnName = "abc";
+
 			_tbl.Columns.Add(col);
 			_tbl.Columns.Add(col2);
-
-			col.ColumnName = "abc";
 			
 			//Duplicate name exception
 			try
@@ -176,6 +176,49 @@ namespace MonoTests.System.Data
 				Assertion.Fail("AE: Wrong exception type. " + exc.ToString());
 			}
 			
+		}
+
+		public void TestDefaultValue()
+		{
+			DataTable tbl = new DataTable();
+			tbl.Columns.Add("MyCol", typeof(int));
+			
+			//Set default Value if Autoincrement is true
+			tbl.Columns[0].AutoIncrement = true;
+			try
+			{
+				tbl.Columns[0].DefaultValue = 2;
+				Assertion.Fail("Failed to throw ArgumentException.");
+			}
+			catch (ArgumentException){}
+			catch (AssertionFailedError exc) {throw  exc;}
+			catch (Exception exc)
+			{
+				Assertion.Fail("WET1: Wrong exception type. " + exc.ToString());
+			}
+
+
+			tbl.Columns[0].AutoIncrement = false;
+
+			//Set default value to an incompatible datatype
+			try
+			{
+				tbl.Columns[0].DefaultValue = "hello";
+				Assertion.Fail("Failed to throw InvalidCastException.");
+			}
+			catch (InvalidCastException){}
+			catch (AssertionFailedError exc) {throw  exc;}
+			catch (Exception exc)
+			{
+				Assertion.Fail("WET2: Wrong exception type. " + exc.ToString());
+			}
+
+			//TODO: maybe add tests for setting default value for types that can implict
+			//cast
+
+
+
+
 		}
 
 		public void TestSetDataType()

+ 28 - 2
mcs/class/System.Data/Test/System.Data/ForeignKeyConstraintTest.cs

@@ -194,9 +194,35 @@ namespace MonoTests.System.Data
 
 		}
 
-		public void TestEquals()
+		public void TestEqualsAndHashCode()
 		{
-			//TODO:
+			DataTable tbl = _ds.Tables[0];
+			DataTable tbl2 = _ds.Tables[1];
+
+			ForeignKeyConstraint fkc = new ForeignKeyConstraint( 
+				new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
+				new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
+
+			ForeignKeyConstraint fkc2 = new ForeignKeyConstraint( 
+				new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
+				new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
+
+			ForeignKeyConstraint fkcDiff = 
+				new ForeignKeyConstraint( tbl.Columns[1], tbl.Columns[2]);
+		
+			Assertion.Assert( "Equals failed. 1" , fkc.Equals(fkc2));
+			Assertion.Assert( "Equals failed. 2" , fkc2.Equals(fkc));
+			Assertion.Assert( "Equals failed. 3" , fkc.Equals(fkc));
+
+			Assertion.Assert( "Equals failed diff. 1" , fkc.Equals(fkcDiff) == false);
+
+			Assertion.Assert( "Hash Code Failed. 1", fkc.GetHashCode() == fkc2.GetHashCode() );
+			Assertion.Assert( "Hash Code Failed. 2", fkc.GetHashCode() != fkcDiff.GetHashCode() );
+
+
+
+
+	
 		}
 	}
 }

+ 8 - 1
mcs/class/System.Data/Test/System.Data/UniqueConstraintTest.cs

@@ -154,7 +154,7 @@ namespace MonoTests.System.Data
 		
 		}
 
-		public void TestEquals() {
+		public void TestEqualsAndHashCode() {
 			UniqueConstraint cst = new UniqueConstraint( new DataColumn [] {
 					_table.Columns[0], _table.Columns[1]});
 			UniqueConstraint cst2 = new UniqueConstraint( new DataColumn [] {
@@ -172,6 +172,13 @@ namespace MonoTests.System.Data
 			Assertion.Assert("A3", cst3.Equals(cst) == false);
 			Assertion.Assert("A4", cst.Equals(cst4) == false);
 
+			//true
+			Assertion.Assert("HashEquals", cst.GetHashCode() == cst2.GetHashCode());
+
+			//false
+			Assertion.Assert("Hash Not Equals", (cst.GetHashCode() == cst3.GetHashCode()) == false);
+
+
 		}