From dadfff98d9c50c4c98edc3cc50ab8afa08e7d47e Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Wed, 2 Dec 2020 04:26:14 +0100 Subject: [PATCH] Add BloomFilter test --- tests/BloomFilter.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++ tests/Makefile | 8 ++-- 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 tests/BloomFilter.cpp diff --git a/tests/BloomFilter.cpp b/tests/BloomFilter.cpp new file mode 100644 index 0000000..26db9b4 --- /dev/null +++ b/tests/BloomFilter.cpp @@ -0,0 +1,109 @@ +#include "../src/BloomFilter.h" + +#include +#include +#include +#include +#include + +/* From Wikipedia */ +std::vector commonHeaders = { + "A-IM", + "Accept", + "Accept-Charset", + "Accept-Datetime", + "Accept-Encoding", + "Accept-Language", + "Access-Control-Request-Method", + "Access-Control-Request-Headers", + "Authorization", + "Cache-Control", + "Connection", + "Content-Encoding", + "Content-Length", + "Content-MD5", + "Content-Type", + "Cookie", + "Date", + "Expect", + "Forwarded", + "From", + "Host", + "HTTP2-Settings", + "If-Match", + "If-Modified-Since", + "If-None-Match", + "If-Range", + "If-Unmodified-Since", + "Max-Forwards", + "Origin", + "Pragma", + "Proxy-Authorization", + "Range", + "Referer", + "TE", + "Trailer", + "Transfer-Encoding", + "User-Agent", + "Upgrade", + "Via", + "Warning", + + /* Put common non-standard ones here */ +}; + +int main() { + + /* Lowercase everything */ + std::transform(commonHeaders.begin(), commonHeaders.end(), commonHeaders.begin(), [](std::string &header) { + std::transform(header.begin(), header.end(), header.begin(), ::tolower); + return header; + }); + + uWS::BloomFilter bf; + unsigned int totalCollisions = 0; + + /* One on one */ + for (int i = 0; i < commonHeaders.size(); i++) { + bf.reset(); + assert(bf.mightHave(commonHeaders[i]) == false); + + bf.add(commonHeaders[i]); + assert(bf.mightHave(commonHeaders[i]) == true); + + for (int j = i + 1; j < commonHeaders.size(); j++) { + if (bf.mightHave(commonHeaders[j])) { + std::cout << commonHeaders[i] << " collides with " << commonHeaders[j] << std::endl; + totalCollisions++; + } + } + } + + /* We don't want any direct one-one-one collisions (please) */ + std::cout << "Total collisions: " << totalCollisions << std::endl; + assert(totalCollisions == 0); + + unsigned int totalFalsePositives = 0; + + /* Add all except the one we test */ + for (int i = 0; i < commonHeaders.size(); i++) { + bf.reset(); + + /* Add all headers but our */ + for (int j = 0; j < commonHeaders.size(); j++) { + if (j != i) { + bf.add(commonHeaders[j]); + } + } + + /* Do we have false positives? */ + if (bf.mightHave(commonHeaders[i])) { + std::cout << commonHeaders[i] << " has false positives" << std::endl; + totalFalsePositives++; + } + } + + /* It is totally fine to have a few false positives */ + std::cout << "Total false positives: " << totalFalsePositives << std::endl; + assert(totalFalsePositives == 1); +} \ No newline at end of file diff --git a/tests/Makefile b/tests/Makefile index 7c9acff..238a7e8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,7 @@ default: - $(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree - ./TopicTree + #$(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree + #./TopicTree $(CXX) -std=c++17 -fsanitize=address HttpRouter.cpp -o HttpRouter - ./HttpRouter \ No newline at end of file + ./HttpRouter + $(CXX) -std=c++17 -fsanitize=address BloomFilter.cpp -o BloomFilter + ./BloomFilter \ No newline at end of file