Null reference contracts
authorCryptoManiac <balthazar.ad@gmail.com>
Fri, 28 Aug 2015 10:31:20 +0000 (13:31 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Fri, 28 Aug 2015 10:31:20 +0000 (13:31 +0300)
Novacoin/Hash.cs
NovacoinTest/Program.cs

index 92ce2c7..5386cb3 100644 (file)
@@ -82,17 +82,14 @@ namespace Novacoin
 
         public bool Equals(Hash item)
         {
-            if (item == null)
-            {
-                return false;
-            }
-
+            Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
             return _hashBytes.SequenceEqual((byte[])item);
         }
 
         public override bool Equals(object o)
         {
-            throw new NotSupportedException();
+            Contract.Requires<NullReferenceException>(o != null, "Null reference is not allowed.");
+            return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
         }
 
         public override int GetHashCode()
@@ -102,8 +99,8 @@ namespace Novacoin
 
         public int CompareTo(Hash item)
         {
+            Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
-            Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
 
             if (this > item)
             {
@@ -119,6 +116,7 @@ namespace Novacoin
 
         public static bool operator <(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
             
             for (int i = a.hashSize - 1; i >= 0; i--)
@@ -134,6 +132,7 @@ namespace Novacoin
 
         public static bool operator <=(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
             for (int i = a.hashSize - 1; i >= 0; i--)
@@ -149,6 +148,7 @@ namespace Novacoin
 
         public static bool operator >(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
             for (int i = a.hashSize - 1; i >= 0; i--)
@@ -164,6 +164,7 @@ namespace Novacoin
 
         public static bool operator >=(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
             for (int i = a.hashSize - 1; i >= 0; i--)
@@ -179,6 +180,7 @@ namespace Novacoin
 
         public static bool operator ==(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
             return a._hashBytes.SequenceEqual(b._hashBytes);
@@ -186,9 +188,10 @@ namespace Novacoin
 
         public static bool operator !=(Hash a, Hash b)
         {
+            Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
-            return !a._hashBytes.SequenceEqual(b._hashBytes);
+            return !a.Equals(b);
         }
 
         public override string ToString()
index 6f2f8e1..933481a 100644 (file)
@@ -276,13 +276,13 @@ namespace NovacoinTest
             ScryptHash256 hash2 = b2.header.Hash;
             ScryptHash256 hash3 = veryBigBlock.header.Hash;
 
-            Console.WriteLine("{0} < {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 < hash2);
-            Console.WriteLine("{0} > {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 > hash2);
+            Console.WriteLine("{0} < {1} : {2}", hash1.ToString(), hash3.ToString(), hash1 < hash3);
+            Console.WriteLine("{0} > {1} : {2}", hash1.ToString(), hash3.ToString(), hash1 > hash3);
             Console.WriteLine("{0} <= {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 <= hash2);
             Console.WriteLine("{0} >= {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 >= hash2);
 
             Console.WriteLine("{0} != {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 != hash2);
-            Console.WriteLine("{0} == {1} : {2}", hash3.ToString(), hash3.ToString(), hash3 == hash3);
+            Console.WriteLine("{0} == {1} : {2}", hash2.ToString(), hash3.ToString(), hash2 == hash3);
 
             /* 
             Console.WriteLine("Reading the block file...");