Beispiel #1
0
        public void GetHashCode_Success(Expression expression, int expectedHashCode)
        {
            ExpressionEqualityComparer comparer = new ExpressionEqualityComparer();
            int hashCode = comparer.GetHashCode(expression);

            Assert.Equal(expectedHashCode, hashCode);
        }
Beispiel #2
0
        /// <summary>
        /// Generates test data that can be used as expected data in subsequent runs.
        /// The generated file (ExpressionHashCodeTest_Generated.tsv) is put into the build directory and
        /// must be copied to the project folder in case of an update).
        /// </summary>
        /// <returns>The generated test data set.</returns>
        private static TestRecord[] GenerateTestData()
        {
            // generate the expected set of hash codes (file can be used as expected data later on)
            ExpressionEqualityComparer comparer = new ExpressionEqualityComparer();
            List <TestRecord>          records  = new List <TestRecord>();

            foreach (var expression in TestExpressions)
            {
                int hashCode = comparer.GetHashCode(expression);
                records.Add(
                    new TestRecord
                {
                    Expression = expression.ToString(),
                    HashCode   = hashCode
                });
            }

#if WRITE_GENERATED_HASH_CODES_TO_FILE
            // write expected data set
            string path = Path.GetFullPath("ExpressionHashCodeTest_Generated.tsv");
            Helpers.WriteTestData(path, records);
#endif // WRITE_GENERATED_HASH_CODES_TO_FILE

            // check whether the generated hash codes are different
            // (different expressions may have the same hash code, but it should not happen too often...)
            Dictionary <int, string> set = new Dictionary <int, string>();
            int duplicateHashCodeCount   = 0;
            foreach (var record in records)
            {
                if (set.TryGetValue(record.HashCode, out var other))
                {
                    Console.WriteLine("Detected duplicate hash code ({0}) for expression ({1}) and expression ({2})", record.HashCode, other, record.Expression);
                    duplicateHashCodeCount++;
                    continue;
                }

                set.Add(record.HashCode, record.Expression);
            }

            // check whether the number of hash code duplicates is below the threshold of 5%
            double duplicateHashCodeRatio = (double)duplicateHashCodeCount / records.Count;
            Assert.True(duplicateHashCodeRatio < 0.05);

            return(records.ToArray());
        }
Beispiel #3
0
        public void Equals_Success()
        {
            ExpressionEqualityComparer comparer = new ExpressionEqualityComparer();

            var expressions = TestExpressions.ToArray();

            for (int i = 0; i < expressions.Length; i++)
            {
                for (int j = 0; j < expressions.Length; j++)
                {
                    bool isEqual = comparer.Equals(expressions[i], expressions[j]);
                    if (i == j)
                    {
                        Assert.True(isEqual, $"Expression {i} {expressions[i]} compared with itself should be equal, but Equals() returns false.");
                    }
                    else
                    {
                        Assert.False(isEqual, $"Expression {i} {expressions[i]} compared with expression {j} ({expressions[j]}) should not be equal, but Equals() returns true.");
                    }
                }
            }
        }