-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Pilcher
committed
Aug 30, 2016
1 parent
21b05b1
commit e6cfec4
Showing
7 changed files
with
80 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,76 @@ | ||
import XCTest | ||
|
||
/* Two hash functions, adapted from | ||
http://www.cse.yorku.ca/~oz/hash.html */ | ||
http://www.cse.yorku.ca/~oz/hash.html */ | ||
|
||
func djb2(x: String) -> Int { | ||
var hash = 5381 | ||
func djb2(_ x: String) -> Int { | ||
var hash = 5381 | ||
|
||
for char in x.characters { | ||
hash = ((hash << 5) &+ hash) &+ char.hashValue | ||
} | ||
for char in x.characters { | ||
hash = ((hash << 5) &+ hash) &+ char.hashValue | ||
} | ||
|
||
return Int(hash) | ||
return Int(hash) | ||
} | ||
|
||
func sdbm(x: String) -> Int { | ||
var hash = 0 | ||
func sdbm(_ x: String) -> Int { | ||
var hash = 0 | ||
|
||
for char in x.characters { | ||
hash = char.hashValue &+ (hash << 6) &+ (hash << 16) &- hash | ||
} | ||
for char in x.characters { | ||
hash = char.hashValue &+ (hash << 6) &+ (hash << 16) &- hash | ||
} | ||
|
||
return Int(hash) | ||
return Int(hash) | ||
} | ||
|
||
|
||
class BloomFilterTests: XCTestCase { | ||
|
||
func testSingleHashFunction() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2]) | ||
func testSingleHashFunction() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2]) | ||
|
||
bloom.insert("Hello world!") | ||
bloom.insert("Hello world!") | ||
|
||
let result_good = bloom.query("Hello world!") | ||
let result_bad = bloom.query("Hello world") | ||
let result_good = bloom.query("Hello world!") | ||
let result_bad = bloom.query("Hello world") | ||
|
||
XCTAssertTrue(result_good) | ||
XCTAssertFalse(result_bad) | ||
} | ||
XCTAssertTrue(result_good) | ||
XCTAssertFalse(result_bad) | ||
} | ||
|
||
func testEmptyFilter() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2]) | ||
func testEmptyFilter() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2]) | ||
|
||
let empty = bloom.isEmpty() | ||
let empty = bloom.isEmpty() | ||
|
||
XCTAssertTrue(empty) | ||
} | ||
XCTAssertTrue(empty) | ||
} | ||
|
||
func testMultipleHashFunctions() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2, sdbm]) | ||
func testMultipleHashFunctions() { | ||
let bloom = BloomFilter<String>(hashFunctions: [djb2, sdbm]) | ||
|
||
bloom.insert("Hello world!") | ||
bloom.insert("Hello world!") | ||
|
||
let result_good = bloom.query("Hello world!") | ||
let result_bad = bloom.query("Hello world") | ||
let result_good = bloom.query("Hello world!") | ||
let result_bad = bloom.query("Hello world") | ||
|
||
XCTAssertTrue(result_good) | ||
XCTAssertFalse(result_bad) | ||
} | ||
XCTAssertTrue(result_good) | ||
XCTAssertFalse(result_bad) | ||
} | ||
|
||
func testFalsePositive() { | ||
let bloom = BloomFilter<String>(size: 5, hashFunctions: [djb2, sdbm]) | ||
func testFalsePositive() { | ||
let bloom = BloomFilter<String>(size: 5, hashFunctions: [djb2, sdbm]) | ||
|
||
bloom.insert(["hello", "elloh", "llohe", "lohel", "ohell"]) | ||
bloom.insert(["hello", "elloh", "llohe", "lohel", "ohell"]) | ||
|
||
print("Inserted") | ||
print("Inserted") | ||
|
||
let query = bloom.query("This wasn't inserted!") | ||
let query = bloom.query("This wasn't inserted!") | ||
|
||
// This is true even though we did not insert the value in the Bloom filter; | ||
// the Bloom filter is capable of producing false positives but NOT | ||
// false negatives. | ||
// This is true even though we did not insert the value in the Bloom filter; | ||
// the Bloom filter is capable of producing false positives but NOT | ||
// false negatives. | ||
|
||
XCTAssertTrue(query) | ||
} | ||
XCTAssertTrue(query) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Bloom Filter/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters