@@ -9,7 +9,7 @@ import Testing
9
9
struct BloomFilterTests {
10
10
#if os(macOS)
11
11
@Test ( " Bloom Filter's init " , . enabled( if: ProcessInfo . processInfo. operatingSystemVersionString == " 14.0 " ) )
12
- func testInitType ( ) throws {
12
+ func initType ( ) throws {
13
13
// hashValue: 0x1dd382138
14
14
// 1 &<< (value &>> 0x10): 1 &<< 0x38 -> 0x0100_0000_0000_0000
15
15
// 1 &<< (value &>> 0x0a): 1 &<< 0x08 -> 0x0000_0000_0000_0100
@@ -29,20 +29,66 @@ struct BloomFilterTests {
29
29
#expect( BloomFilter ( type: type) . value == expectedValue)
30
30
}
31
31
32
+ #if arch(x86_64) || arch(arm64)
32
33
@Test
33
- func testInitHashValue ( ) throws {
34
+ func initHashValue ( ) throws {
34
35
// hashValue: 0
35
36
// 1 &<< (value &>> 0x10): 1 &<< 0 -> 0x0000_0000_0000_0001
36
37
// 1 &<< (value &>> 0x0a): 1 &<< 0 -> 0x0000_0000_0000_0001
37
38
// 1 &<< (value &>> 0x04): 1 &<< 0 -> 0x0000_0000_0000_0001
38
39
#expect( BloomFilter ( hashValue: 0 ) . value == 0x0000_0000_0000_0001 )
39
40
40
- #if arch(x86_64) || arch(arm64)
41
41
// hashValue: 0x00000001dfa19ae0
42
42
// 1 &<< (value &>> 0x10): 1 &<< 0x21 -> 0x0000_0002_0000_0000
43
43
// 1 &<< (value &>> 0x0a): 1 &<< 0x26 -> 0x0000_0040_0000_0000
44
44
// 1 &<< (value &>> 0x04): 1 &<< 0x2e -> 0x0000_4000_0000_0000
45
45
#expect( BloomFilter ( hashValue: 0x0000_0001_DFA1_9AE0 ) . value == 0x0000_4042_0000_0000 )
46
- #endif
47
46
}
47
+
48
+ @Test
49
+ func union( ) throws {
50
+ // Test union of two filters
51
+ let filter1 = BloomFilter ( hashValue: 0 ) // 0x0000_0000_0000_0001
52
+ let filter2 = BloomFilter ( hashValue: 0x0000_0001_DFA1_9AE0 ) // 0x0000_4042_0000_0000
53
+
54
+ // Test union operation
55
+ let unionResult = filter1. union ( filter2)
56
+ #expect( unionResult. value == 0x0000_4042_0000_0001 )
57
+
58
+ // Test formUnion operation
59
+ var mutableFilter = filter1
60
+ mutableFilter. formUnion ( filter2)
61
+ #expect( mutableFilter. value == 0x0000_4042_0000_0001 )
62
+
63
+ // Test union with empty filter
64
+ let emptyFilter = BloomFilter ( )
65
+ #expect( filter1. union ( emptyFilter) . value == filter1. value)
66
+ #expect( emptyFilter. union ( filter1) . value == filter1. value)
67
+ }
68
+
69
+ @Test
70
+ func mayContain( ) throws {
71
+ // Create filters with known bits
72
+ let filter1 = BloomFilter ( hashValue: 0 ) // 0x0000_0000_0000_0001
73
+ let filter2 = BloomFilter ( hashValue: 0x0000_0001_DFA1_9AE0 ) // 0x0000_4042_0000_0000
74
+ let unionFilter = filter1. union ( filter2) // 0x0000_4042_0000_0001
75
+ let emptyFilter = BloomFilter ( ) // 0x0000_0000_0000_0000
76
+
77
+ // Test mayContain functionality
78
+ #expect( unionFilter. mayContain ( filter1) , " Union may contain filter1 " )
79
+ #expect( unionFilter. mayContain ( filter2) , " Union may contain filter2 " )
80
+ #expect( !filter1. mayContain ( filter2) , " filter1 does not contain filter2 " )
81
+ #expect( !filter2. mayContain ( filter1) , " filter2 does not contain filter1 " )
82
+
83
+ // Empty filter cases
84
+ #expect( filter1. mayContain ( emptyFilter) , " Any non-empty filter should contain empty filter " )
85
+ #expect( filter2. mayContain ( emptyFilter) , " Any non-empty filter should contain empty filter " )
86
+ #expect( !emptyFilter. mayContain ( filter1) , " Empty filter does not contain any non-empty filter " )
87
+
88
+ // Self-containment
89
+ #expect( filter1. mayContain ( filter1) , " Filter should contain itself " )
90
+ #expect( filter2. mayContain ( filter2) , " Filter should contain itself " )
91
+ #expect( emptyFilter. mayContain ( emptyFilter) , " Empty filter should contain itself " )
92
+ }
93
+ #endif
48
94
}
0 commit comments