Exemplo n.º 1
0
        /** check which address is closed to this one
         * @return true if we are closer to the first than second
         */
        public bool IsCloserToFirst(AHAddress first, AHAddress sec)
        {
            uint pre0 = _prefix;
            uint pref = first._prefix;
            uint pres = sec._prefix;

            if (pref == pres)
            {
                //They could be the same:
                if (first.Equals(sec))
                {
                    return(false);
                }
                return(DistanceTo(first).abs() < DistanceTo(sec).abs());
            }
            //See if the upper and lower bounds can avoid doing big-int stuff
            uint udf = pre0 > pref ? pre0 - pref : pref - pre0;
            uint uds = pre0 > pres ? pre0 - pres : pres - pre0;

            if (udf > Int32.MaxValue)
            {
                //Wrap it around:
                udf = UInt32.MaxValue - udf;
            }
            if (uds > Int32.MaxValue)
            {
                uds = UInt32.MaxValue - uds;
            }
            uint upperbound_f = udf + 1;
            uint lowerbound_s = uds > 0 ? uds - 1 : 0;

            if (upperbound_f <= lowerbound_s)
            {
                //There is no way the exact value could make df bigger than ds:
                return(true);
            }
            uint upperbound_s = uds + 1;
            uint lowerbound_f = udf > 0 ? udf - 1 : 0;

            if (upperbound_s <= lowerbound_f)
            {
                //There is no way the exact value could make ds bigger than df:
                return(false);
            }
            //Else just do it the simple, but costly way
            BigInteger df = DistanceTo(first).abs();
            BigInteger ds = DistanceTo(sec).abs();

            return(df < ds);
        }
        public override bool Equals(object o)
        {
            if (o == this)
            {
                return(true);
            }

            SubringTransportAddress other = o as SubringTransportAddress;

            if (other == null)
            {
                return(false);
            }

            return(Target.Equals(other.Target));
        }
Exemplo n.º 3
0
 //returns true if addr is in a given range including boundary.
 /**
  * This returns true if addr is between start and end in a ring.
  * IsBetweenFrom*() excludes both start and end, but InRange() includes both.
  * @param addr, this node's address
  * @param start, the beginning address of range
  * @param end, the ending address of range
  */
 public bool InRange(AHAddress addr, AHAddress start, AHAddress end) {
   return addr.IsBetweenFromLeft(start, end) || addr.Equals(start) || addr.Equals(end);
 }
Exemplo n.º 4
0
    public void Test() {
      byte[]  buf1 = new byte[20];
      for (int i = 0; i <= 18; i++)
      {
        buf1[i] = 0x00;
      }
      buf1[19] = 0x0A;
      AHAddress test_address_1 = new AHAddress( MemBlock.Reference(buf1, 0, buf1.Length) );

      byte[] buf2 = new byte[20];
      for (int i = 0; i <= 18; i++) {
        buf2[i] = 0xFF;
      }
      buf2[19] = 0xFE;
      AHAddress test_address_2 = new AHAddress( MemBlock.Reference(buf2, 0, buf2.Length) );
      //test_address_1 is to the left of test_address_2
      //because it only a few steps in the clockwise direction:
      Assert.IsTrue( test_address_1.IsLeftOf( test_address_2 ), "IsLeftOf");
      Assert.IsTrue( test_address_2.IsRightOf( test_address_1 ), "IsRightOf");
      //This distance is twelve:
      Assert.AreEqual( test_address_2.DistanceTo( test_address_1),
                       new BigInteger(12), "DistanceTo");
      Assert.IsTrue( test_address_1.CompareTo(test_address_2) < 0, "CompareTo");
      Assert.IsTrue( test_address_2.CompareTo(test_address_1) > 0, "CompareTo");
      byte[] buf3 = new byte[Address.MemSize];
      test_address_2.CopyTo(buf3);
      AHAddress test3 = new AHAddress(MemBlock.Reference(buf3,0,buf3.Length)); 
      Assert.IsTrue( test3.CompareTo( test_address_2 ) == 0 , "CompareTo");
      Assert.IsTrue( test3.CompareTo( test3 ) == 0, "CompareTo");
      //As long as the address does not wrap around, adding should increase it:
      AHAddress a4 = new AHAddress( test_address_1.ToBigInteger() + 100 );
      Assert.IsTrue( a4.CompareTo( test_address_1 ) > 0, "adding increases");
      Assert.IsTrue( a4.CompareTo( test_address_2 ) < 0, "smaller than biggest");
      //Here are some consistency tests:
      for( int i = 0; i < 1000; i++) {
        System.Random r = new Random();
        byte[] b1 = new byte[Address.MemSize];
        r.NextBytes(b1);
        //Make sure it is class 0:
        Address.SetClass(b1, 0);
        byte[] b2 = new byte[Address.MemSize];
        r.NextBytes(b2);
        //Make sure it is class 0:
        Address.SetClass(b2, 0);
        byte[] b3 = new byte[Address.MemSize];
        r.NextBytes(b3);
        //Make sure it is class 0:
        Address.SetClass(b3, 0);
        AHAddress a5 = new AHAddress( MemBlock.Reference(b1,0,b1.Length) );
        AHAddress a6 = new AHAddress( MemBlock.Reference(b2,0,b2.Length) );
        AHAddress a7 = new AHAddress( MemBlock.Reference(b3,0,b3.Length) );
        Assert.IsTrue( a5.CompareTo(a6) == -1 * a6.CompareTo(a5), "consistency");
        //Nothing is between the same address:
        Assert.IsFalse( a5.IsBetweenFromLeft(a6, a6), "Empty Between Left");
        Assert.IsFalse( a5.IsBetweenFromRight(a7, a7), "Empty Between Right");
        //Endpoints are not between:
        Assert.IsFalse( a6.IsBetweenFromLeft(a6, a7), "End point Between Left");
        Assert.IsFalse( a6.IsBetweenFromRight(a6, a7), "End point Between Right");
        Assert.IsFalse( a7.IsBetweenFromLeft(a6, a7), "End point Between Left");
        Assert.IsFalse( a7.IsBetweenFromRight(a6, a7), "End point Between Right");

        if ( a5.IsBetweenFromLeft(a6, a7) ) {
          //Then the following must be true:
          Assert.IsTrue( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                         "BetweenLeft true");
        }
        else {
          //Then the following must be false:
          Assert.IsFalse( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                          "BetweenLeft false");
        }
        if ( a5.IsBetweenFromRight(a6, a7) ) {
          //Then the following must be true:
          Assert.IsTrue( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                         "BetweenRight true");
        }
        else {
          //Then the following must be false:
          Assert.IsFalse( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                          "BetweenRight false");
        }
        if( a5.IsCloserToFirst(a6, a7) ) {
          Assert.IsTrue( a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 1");
        }
        else {
          Assert.IsFalse( a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 2");
        }
        Assert.IsFalse(a5.IsCloserToFirst(a6, a7) && a5.IsCloserToFirst(a7,a6), "can only be closer to one!");
        if( false == a5.Equals(a6) ) {
          Assert.IsTrue(a5.IsCloserToFirst(a5, a6), "Always closer to self");
        }
        Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) ==
                            a5.IsBetweenFromRight(a6, a7),
                            "can't be between left and between right");
      }
    }
Exemplo n.º 5
0
 /** check which address is closed to this one
  * @return true if we are closer to the first than second
  */
 public bool IsCloserToFirst(AHAddress first, AHAddress sec) {
   uint pre0 = _prefix;
   uint pref = first._prefix;
   uint pres = sec._prefix;
   if( pref == pres ) {
     //They could be the same:
     if( first.Equals( sec ) ) { return false; }
     return DistanceTo(first).abs() < DistanceTo(sec).abs();
   }
   //See if the upper and lower bounds can avoid doing big-int stuff
   uint udf = pre0 > pref ? pre0 - pref : pref - pre0;
   uint uds = pre0 > pres ? pre0 - pres : pres - pre0;
   if( udf > Int32.MaxValue ) {
     //Wrap it around:
     udf = UInt32.MaxValue - udf;
   }
   if( uds > Int32.MaxValue ) {
     uds = UInt32.MaxValue - uds;
   }
   uint upperbound_f = udf + 1;
   uint lowerbound_s = uds > 0 ? uds - 1 : 0;
   if( upperbound_f <= lowerbound_s ) {
     //There is no way the exact value could make df bigger than ds:
     return true;
   }
   uint upperbound_s = uds + 1;
   uint lowerbound_f = udf > 0 ? udf - 1 : 0;
   if( upperbound_s <= lowerbound_f ) {
     //There is no way the exact value could make ds bigger than df:
     return false;
   }
   //Else just do it the simple, but costly way
   BigInteger df = DistanceTo(first).abs();
   BigInteger ds = DistanceTo(sec).abs();
   return (df < ds);
 }
Exemplo n.º 6
0
        public void Test()
        {
            byte[] buf1 = new byte[20];
            for (int i = 0; i <= 18; i++)
            {
                buf1[i] = 0x00;
            }
            buf1[19] = 0x0A;
            AHAddress test_address_1 = new AHAddress(MemBlock.Reference(buf1, 0, buf1.Length));

            byte[] buf2 = new byte[20];
            for (int i = 0; i <= 18; i++)
            {
                buf2[i] = 0xFF;
            }
            buf2[19] = 0xFE;
            AHAddress test_address_2 = new AHAddress(MemBlock.Reference(buf2, 0, buf2.Length));

            //test_address_1 is to the left of test_address_2
            //because it only a few steps in the clockwise direction:
            Assert.IsTrue(test_address_1.IsLeftOf(test_address_2), "IsLeftOf");
            Assert.IsTrue(test_address_2.IsRightOf(test_address_1), "IsRightOf");
            //This distance is twelve:
            Assert.AreEqual(test_address_2.DistanceTo(test_address_1),
                            new BigInteger(12), "DistanceTo");
            Assert.IsTrue(test_address_1.CompareTo(test_address_2) < 0, "CompareTo");
            Assert.IsTrue(test_address_2.CompareTo(test_address_1) > 0, "CompareTo");
            byte[] buf3 = new byte[Address.MemSize];
            test_address_2.CopyTo(buf3);
            AHAddress test3 = new AHAddress(MemBlock.Reference(buf3, 0, buf3.Length));

            Assert.IsTrue(test3.CompareTo(test_address_2) == 0, "CompareTo");
            Assert.IsTrue(test3.CompareTo(test3) == 0, "CompareTo");
            //As long as the address does not wrap around, adding should increase it:
            AHAddress a4 = new AHAddress(test_address_1.ToBigInteger() + 100);

            Assert.IsTrue(a4.CompareTo(test_address_1) > 0, "adding increases");
            Assert.IsTrue(a4.CompareTo(test_address_2) < 0, "smaller than biggest");
            //Here are some consistency tests:
            for (int i = 0; i < 1000; i++)
            {
                System.Random r  = new Random();
                byte[]        b1 = new byte[Address.MemSize];
                r.NextBytes(b1);
                //Make sure it is class 0:
                Address.SetClass(b1, 0);
                byte[] b2 = new byte[Address.MemSize];
                r.NextBytes(b2);
                //Make sure it is class 0:
                Address.SetClass(b2, 0);
                byte[] b3 = new byte[Address.MemSize];
                r.NextBytes(b3);
                //Make sure it is class 0:
                Address.SetClass(b3, 0);
                AHAddress a5 = new AHAddress(MemBlock.Reference(b1, 0, b1.Length));
                AHAddress a6 = new AHAddress(MemBlock.Reference(b2, 0, b2.Length));
                AHAddress a7 = new AHAddress(MemBlock.Reference(b3, 0, b3.Length));
                Assert.IsTrue(a5.CompareTo(a6) == -1 * a6.CompareTo(a5), "consistency");
                //Nothing is between the same address:
                Assert.IsFalse(a5.IsBetweenFromLeft(a6, a6), "Empty Between Left");
                Assert.IsFalse(a5.IsBetweenFromRight(a7, a7), "Empty Between Right");
                //Endpoints are not between:
                Assert.IsFalse(a6.IsBetweenFromLeft(a6, a7), "End point Between Left");
                Assert.IsFalse(a6.IsBetweenFromRight(a6, a7), "End point Between Right");
                Assert.IsFalse(a7.IsBetweenFromLeft(a6, a7), "End point Between Left");
                Assert.IsFalse(a7.IsBetweenFromRight(a6, a7), "End point Between Right");

                if (a5.IsBetweenFromLeft(a6, a7))
                {
                    //Then the following must be true:
                    Assert.IsTrue(a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                                  "BetweenLeft true");
                }
                else
                {
                    //Then the following must be false:
                    Assert.IsFalse(a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                                   "BetweenLeft false");
                }
                if (a5.IsBetweenFromRight(a6, a7))
                {
                    //Then the following must be true:
                    Assert.IsTrue(a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                                  "BetweenRight true");
                }
                else
                {
                    //Then the following must be false:
                    Assert.IsFalse(a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                                   "BetweenRight false");
                }
                if (a5.IsCloserToFirst(a6, a7))
                {
                    Assert.IsTrue(a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 1");
                }
                else
                {
                    Assert.IsFalse(a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 2");
                }
                Assert.IsFalse(a5.IsCloserToFirst(a6, a7) && a5.IsCloserToFirst(a7, a6), "can only be closer to one!");
                if (false == a5.Equals(a6))
                {
                    Assert.IsTrue(a5.IsCloserToFirst(a5, a6), "Always closer to self");
                }
                Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) ==
                               a5.IsBetweenFromRight(a6, a7),
                               "can't be between left and between right");
            }
        }