示例#1
0
        public void CompareTo_Other_ReturnsExpected(UInt128 i, object value, int expected)
        {
            if (value is UInt128 UInt128Value)
            {
                Assert.Equal(expected, Math.Sign(i.CompareTo(UInt128Value)));
            }

            Assert.Equal(expected, Math.Sign(i.CompareTo(value)));
        }
示例#2
0
        public void UInt128_CompareTo_InvalidType_Throws()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };


            Assert.Throws <ArgumentException>(() =>
                                              baseValue.CompareTo((object)0));

            Assert.Throws <ArgumentException>(() =>
                                              baseValue.CompareTo(new object()));
        }
        public int CompareTo(CosmosArray cosmosArray)
        {
            UInt128 hash1 = DistinctHash.GetHash(this);
            UInt128 hash2 = DistinctHash.GetHash(cosmosArray);

            return(hash1.CompareTo(hash2));
        }
示例#4
0
 public int CompareTo(UInt128 other)
 {
     if (IsNegative)
     {
         return(-1);
     }
     return(v.CompareTo(other));
 }
示例#5
0
        public void UInt128_CompareTo_Null_ReturnsGreaterThanZero()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };


            Assert.True(
                baseValue.CompareTo((object)null) > 0);
        }
示例#6
0
        public void UInt128_CompareTo_UInt128_InequivalentLess_ReturnsGreaterThanZero()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };
            var testValue = new UInt128()
            {
                High = 2, Low = 0
            };


            Assert.True(
                baseValue.CompareTo(testValue) > 0);
        }
示例#7
0
        public void UInt128_CompareTo_InequivalentLess_ReturnsLessThanZero()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };
            var testValue = (object)new UInt128()
            {
                High = 0, Low = 0
            };


            Assert.True(
                baseValue.CompareTo(testValue) < 0);
        }
示例#8
0
        public void UInt128_CompareTo_UInt128_Equivalent_ReturnsZero()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };

            var testValues = new[] {
                baseValue,
                new UInt128()
                {
                    High = 1, Low = 2
                }
            };


            foreach (var testValue in testValues)
            {
                Assert.Equal(
                    0,
                    baseValue.CompareTo(testValue));
            }
        }
        /// <summary>
        /// Compares to objects and returns their partial sort relationship.
        /// </summary>
        /// <param name="element1">The first element to compare.</param>
        /// <param name="element2">The second element to compare.</param>
        /// <returns>
        /// Less than zero if obj1 comes before obj2 in the sort order.
        /// Zero if obj1 and obj2 are interchangeable in the sort order.
        /// Greater than zero if obj2 comes before obj1 in the sort order.
        /// </returns>
        public int Compare(CosmosElement element1, CosmosElement element2)
        {
            if (object.ReferenceEquals(element1, element2))
            {
                return(0);
            }

            if (object.ReferenceEquals(element1, MinValueItem.Singleton))
            {
                return(-1);
            }

            if (object.ReferenceEquals(element2, MinValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element1, MaxValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element2, MaxValueItem.Singleton))
            {
                return(-1);
            }

            if (element1 == Undefined)
            {
                return(-1);
            }

            if (element2 == Undefined)
            {
                return(1);
            }

            CosmosElementType type1 = element1.Type;

            int cmp = CompareTypes(element1, element2);

            if (cmp == 0)
            {
                // If they are the same type then you need to break the tie.
                switch (type1)
                {
                case CosmosElementType.Boolean:
                    cmp = Comparer <bool> .Default.Compare(
                        (element1 as CosmosBoolean).Value,
                        (element2 as CosmosBoolean).Value);

                    break;

                case CosmosElementType.Null:
                    // All nulls are the same.
                    cmp = 0;
                    break;

                case CosmosElementType.Number:
                    CosmosNumber number1 = element1 as CosmosNumber;
                    CosmosNumber number2 = element2 as CosmosNumber;
                    if (number1.NumberType == CosmosNumberType.Number64)
                    {
                        // Both are Number64, so compare as Number64
                        cmp = number1.Value.CompareTo(number2.Value);
                    }
                    else
                    {
                        // We have a number with precision
                        if (number2.Value.IsInteger)
                        {
                            // compare as longs, since all types have an implicit conversion with full fidelity.
                            long integer1 = Number64.ToLong(number1.Value);
                            long integer2 = Number64.ToLong(number2.Value);
                            cmp = Comparer <long> .Default.Compare(integer1, integer2);
                        }
                        else
                        {
                            // compare as doubles, since all types have an implicit conversion with full fidelity.
                            double double1 = Number64.ToDouble(number1.Value);
                            double double2 = Number64.ToDouble(number2.Value);
                            cmp = Comparer <double> .Default.Compare(double1, double2);
                        }
                    }

                    break;

                case CosmosElementType.String:
                    CosmosString string1 = element1 as CosmosString;
                    CosmosString string2 = element2 as CosmosString;
                    cmp = string.CompareOrdinal(
                        string1.Value,
                        string2.Value);
                    break;

                case CosmosElementType.Guid:
                    CosmosGuid guid1 = element1 as CosmosGuid;
                    CosmosGuid guid2 = element2 as CosmosGuid;
                    cmp = guid1.Value.CompareTo(guid2.Value);
                    break;

                case CosmosElementType.Binary:
                    CosmosBinary binary1 = element1 as CosmosBinary;
                    CosmosBinary binary2 = element2 as CosmosBinary;
                    cmp = ItemComparer.CompareTo(binary1, binary2);
                    break;

                case CosmosElementType.Array:
                case CosmosElementType.Object:
                {
                    UInt128 hash1 = DistinctHash.GetHash(element1);
                    UInt128 hash2 = DistinctHash.GetHash(element2);
                    return(hash1.CompareTo(hash2));
                }

                default:
                    throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {type1}");
                }
            }

            return(cmp);
        }
示例#10
0
文件: int128.cs 项目: sgf/SCTP
 public int CompareTo(UInt128 other)
 {
     if (IsNegative)
         return -1;
     return v.CompareTo(other);
 }
        public void UInt128_CompareTo_UInt128_Equivalent_ReturnsZero()
        {
            var baseValue = new UInt128() { High = 1, Low = 2 };

            var testValues = new[] { 
                baseValue,
                new UInt128() { High = 1, Low = 2 }
            };


            foreach (var testValue in testValues)
            {
                Assert.Equal(
                    0,
                    baseValue.CompareTo(testValue));
            }

        }
        public void UInt128_CompareTo_UInt128_InequivalentLess_ReturnsGreaterThanZero()
        {
            var baseValue = new UInt128() { High = 1, Low = 2 };
            var testValue = new UInt128() { High = 2, Low = 0 };


            Assert.True(
                baseValue.CompareTo(testValue) > 0);
        }
        public void UInt128_CompareTo_InequivalentLess_ReturnsLessThanZero()
        {
            var baseValue = new UInt128() { High = 1, Low = 2 };
            var testValue = (object) new UInt128() { High = 0, Low = 0 };


            Assert.True(
                baseValue.CompareTo(testValue) < 0);
        }
        public void UInt128_CompareTo_InvalidType_Throws()
        {
            var baseValue = new UInt128() { High = 1, Low = 2 };


            Assert.Throws<ArgumentException>(() => 
                baseValue.CompareTo((object) 0));

            Assert.Throws<ArgumentException>(() =>
                baseValue.CompareTo(new object()));
        }
        public void UInt128_CompareTo_Null_ReturnsGreaterThanZero()
        {
            var baseValue = new UInt128() { High = 1, Low = 2 };


            Assert.True(
                baseValue.CompareTo((object) null) > 0);
        }
示例#16
0
        /// <summary>
        /// Compares to objects and returns their partial sort relationship.
        /// </summary>
        /// <param name="element1">The first element to compare.</param>
        /// <param name="element2">The second element to compare.</param>
        /// <returns>
        /// Less than zero if obj1 comes before obj2 in the sort order.
        /// Zero if obj1 and obj2 are interchangeable in the sort order.
        /// Greater than zero if obj2 comes before obj1 in the sort order.
        /// </returns>
        public int Compare(CosmosElement element1, CosmosElement element2)
        {
            if (object.ReferenceEquals(element1, element2))
            {
                return(0);
            }

            if (object.ReferenceEquals(element1, MinValueItem.Singleton))
            {
                return(-1);
            }

            if (object.ReferenceEquals(element2, MinValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element1, MaxValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element2, MaxValueItem.Singleton))
            {
                return(-1);
            }

            if (element1 == Undefined)
            {
                return(-1);
            }

            if (element2 == Undefined)
            {
                return(1);
            }

            CosmosElementType type1 = element1.Type;

            int cmp = CompareTypes(element1, element2);

            if (cmp == 0)
            {
                // If they are the same type then you need to break the tie.
                switch (type1)
                {
                case CosmosElementType.Boolean:
                    cmp = Comparer <bool> .Default.Compare(
                        (element1 as CosmosBoolean).Value,
                        (element2 as CosmosBoolean).Value);

                    break;

                case CosmosElementType.Null:
                    // All nulls are the same.
                    cmp = 0;
                    break;

                case CosmosElementType.Number:
                    CosmosNumber number1 = element1 as CosmosNumber;
                    CosmosNumber number2 = element2 as CosmosNumber;
                    if (number1.NumberType == CosmosNumberType.Number64)
                    {
                        double double1;
                        if (number1.IsFloatingPoint)
                        {
                            double1 = number1.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double1 = number1.AsInteger().Value;
                        }

                        double double2;
                        if (number2.IsFloatingPoint)
                        {
                            double2 = number2.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double2 = number2.AsInteger().Value;
                        }

                        cmp = Comparer <double> .Default.Compare(
                            double1,
                            double2);
                    }
                    else if (number1.IsFloatingPoint)
                    {
                        double double1 = number1.AsFloatingPoint().Value;
                        double double2 = number2.AsFloatingPoint().Value;
                        cmp = Comparer <double> .Default.Compare(double1, double2);
                    }
                    else
                    {
                        long integer1 = number1.AsInteger().Value;
                        long integer2 = number2.AsInteger().Value;
                        cmp = Comparer <long> .Default.Compare(integer1, integer2);
                    }

                    break;

                case CosmosElementType.String:
                    CosmosString string1 = element1 as CosmosString;
                    CosmosString string2 = element2 as CosmosString;
                    cmp = string.CompareOrdinal(
                        string1.Value,
                        string2.Value);
                    break;

                case CosmosElementType.Guid:
                    CosmosGuid guid1 = element1 as CosmosGuid;
                    CosmosGuid guid2 = element2 as CosmosGuid;
                    cmp = guid1.Value.CompareTo(guid2.Value);
                    break;

                case CosmosElementType.Binary:
                    CosmosBinary binary1 = element1 as CosmosBinary;
                    CosmosBinary binary2 = element2 as CosmosBinary;
                    cmp = ItemComparer.CompareTo(binary1, binary2);
                    break;

                case CosmosElementType.Array:
                case CosmosElementType.Object:
                {
                    UInt128 hash1 = DistinctHash.GetHash(element1);
                    UInt128 hash2 = DistinctHash.GetHash(element2);
                    return(hash1.CompareTo(hash2));
                }

                default:
                    throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {type1}");
                }
            }

            return(cmp);
        }
示例#17
0
 public int CompareTo(BoardState other)
 {
     return(_state.CompareTo(other._state));
 }