1
+ // Test generic equality implementation
1
2
type MyType {
2
3
MySingleCase()
3
4
}
4
5
5
6
record EmptyRecord()
6
7
8
+ record Person(name: String, age: Int)
9
+
10
+ // Binary tree with values in nodes
11
+ type Tree[T] {
12
+ Leaf();
13
+ Node(value: T, left: Tree[T], right: Tree[T])
14
+ }
15
+
16
+ // Create a simple tree for testing
17
+ def createTree[T](depth: Int, value: T): Tree[T] = {
18
+ if (depth <= 0) {
19
+ Leaf()
20
+ } else {
21
+ Node(value, createTree(depth - 1, value), createTree(depth - 1, value))
22
+ }
23
+ }
24
+
25
+ // Create a more complex tree with different values
26
+ def createMixedTree(): Tree[Int] = {
27
+ Node(1,
28
+ Node(2, Leaf(), Leaf()),
29
+ Node(3,
30
+ Node(4, Leaf(), Leaf()),
31
+ Leaf()
32
+ )
33
+ )
34
+ }
35
+
7
36
def main() = {
37
+ // Original tests
38
+ println("=== Basic equality tests ===")
8
39
println(MySingleCase().equals(MySingleCase())) // ~> true
9
40
println(EmptyRecord().equals(EmptyRecord())) // ~> true
10
-
11
41
println(Some(MySingleCase()).equals(Some(MySingleCase()))) // ~> true
12
42
println(Some(EmptyRecord()).equals(Some(EmptyRecord()))) // ~> true
13
-
14
43
println([Some(EmptyRecord()), Some(EmptyRecord()), None()].equals([Some(EmptyRecord()), Some(EmptyRecord()), None()]))
44
+
45
+ // Enhanced tests with records and nested structures
46
+ println("\n=== Record equality tests ===")
47
+ val person1 = Person("Alice", 30)
48
+ val person2 = Person("Alice", 30)
49
+ val person3 = Person("Bob", 25)
50
+ println(person1.equals(person2)) // ~> true
51
+ println(person1.equals(person3)) // ~> false
52
+ println(Person("Alice", 30).equals(Person("Alice", 30))) // ~> true
53
+
54
+ // Test nested records
55
+ println("\n=== Nested structure tests ===")
56
+ println(Some(person1).equals(Some(person2))) // ~> true
57
+ println(Some(person1).equals(Some(person3))) // ~> false
58
+ println([person1, person2].equals([person1, person2])) // ~> true
59
+ println([person1, person3].equals([person1, person2])) // ~> false
60
+
61
+ // Tree equality tests
62
+ println("\n=== Tree equality tests ===")
63
+ val tree1 = createTree(3, 42)
64
+ val tree2 = createTree(3, 42)
65
+ val tree3 = createTree(2, 42)
66
+ val tree4 = createTree(3, 100)
67
+
68
+ println(tree1.equals(tree2)) // ~> true
69
+ println(tree1.equals(tree3)) // ~> false
70
+ println(tree1.equals(tree4)) // ~> false
71
+
72
+ val mixedTree1 = createMixedTree()
73
+ val mixedTree2 = createMixedTree()
74
+ println(mixedTree1.equals(mixedTree2)) // ~> true
75
+
76
+ // Even more complex nested structures
77
+ println("\n=== Complex nested structure tests ===")
78
+ val nestedList1 = [Some(tree1), None(), Some(mixedTree1)]
79
+ val nestedList2 = [Some(tree2), None(), Some(mixedTree2)]
80
+ val nestedList3 = [Some(tree3), None(), Some(mixedTree1)]
81
+
82
+ println(nestedList1.equals(nestedList2)) // ~> true
83
+ println(nestedList1.equals(nestedList3)) // ~> false
84
+
85
+ val complexRecord = Person("Charlie", 35)
86
+ val recordWithTree = [complexRecord, Person("Dave", 40), Person("Charlie", 35)]
87
+ println(recordWithTree.equals([complexRecord, Person("Dave", 40), Person("Charlie", 35)])) // ~> true
88
+ println(recordWithTree.equals([complexRecord, Person("Dave", 40), Person("Charlie", 36)])) // ~> false
15
89
}
0 commit comments