blob: 6a84e7f3b012c7b2a5ab8e13e3c47029da74b6c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Security;
using System.Runtime.CompilerServices;
using TestLibrary;
#if WINCORESYS
[assembly: AllowPartiallyTrustedCallers]
#endif
// Class has two fields, x and y; we consider two instances to be equal (Equals returns true,
// and GetHashCode returns the same for both instances) iff x==x and y==y
class ClassWithEquivalence
{
int x;
int y;
public ClassWithEquivalence(int _x, int _y)
{
this.x = _x;
this.y = _y;
}
public override bool Equals(object b)
{
ClassWithEquivalence bProper = b as ClassWithEquivalence;
if (bProper != null)
return (x == bProper.x) && (y == bProper.y);
else
return false;
}
public override int GetHashCode()
{
return x.GetHashCode() + y.GetHashCode();
}
}
public class GetHashCode
{
public bool PosTest1()
{
bool retVal = true;
TestFramework.BeginScenario("PosTest1: RuntimeHelpers.GetHashCode() on two non-reference-equals objects returns non-equal codes");
Object a, b;
try
{
a = new ClassWithEquivalence(10, 20);
b = new ClassWithEquivalence(10, 20);
if (!(a.Equals(b) && (a.GetHashCode() == b.GetHashCode()))
|| (Object.ReferenceEquals(a, b)))
{
// Log: setup failed
return false;
}
if (RuntimeHelpers.GetHashCode(a) == RuntimeHelpers.GetHashCode(b))
{
// Log: RTH.GHC should have returned different hash codes since the
// objects are not reference-equals.
TestFramework.LogError("001", "a and b are not reference equal, and yet RuntimeHelpers.GetHashCode returned same value for each");
retVal = false;
}
}
catch (Exception e)
{
TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
public bool RunTests()
{
bool retVal = true;
retVal = PosTest1() && retVal;
return retVal;
}
public static int Main(string[] args)
{
GetHashCode ghc = new GetHashCode();
if (ghc.RunTests())
{
TestFramework.LogInformation("PASSED");
return 100;
}
else
{
TestFramework.LogInformation("FAILED");
return 99;
}
}
}
|