Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} facebookresearch / faiss Public Notifications You must be signed in to change notification settings Fork 4.4k Star 40.3k Code Issues 222 Pull requests 57 Discussions Actions Projects Wiki Security and quality 0 Insights Additional navigation options Code Issues Pull requests Discussions Actions Projects Wiki Security and quality Insights FilesExpand file tree mainBreadcrumbsfaiss/tests/test_binary_hash.cppCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory107 lines (91 loc) · 3.03 KB mainBreadcrumbsfaiss/tests/test_binary_hash.cppCopy pathTopFile metadata and controlsCodeBlame107 lines (91 loc) · 3.03 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #include <cstdint>#include <vector> #include <gtest/gtest.h> #include <faiss/IndexBinaryHash.h> // These use a single 2-byte vector (d=16, code_size=2) so that the// resulting index data is not an even multiple of sizeof(uint64_t),// testing that the uint64_t sized hash key extraction at the end of// the allocation boundary doesn't trigger a heap-buffer-overflow under// ASAN.//// NOTE: These are not python based tests because we have more control// over allocation behavior in C++, ensuring these tests// deterministically fail without proper handling of access near// the end of the allocation. TEST(BinaryHash, SmallCodeSizeRoundTrip) { int d = 16; int b = 5; // hash on 5 bits faiss::IndexBinaryHash idx(d, b); idx.nflip = 1; int n = 1; std::vector<uint8_t> data(n * idx.code_size); data[0] = 0xAA; data[1] = 0x55; idx.add(n, data.data()); EXPECT_EQ(idx.ntotal, n); int k = 1; std::vector<int32_t> distances(k); std::vector<faiss::idx_t> labels(k); idx.search(1, data.data(), k, distances.data(), labels.data()); EXPECT_EQ(distances[0], 0); EXPECT_EQ(labels[0], 0);} TEST(BinaryHash, MultiHashSmallCodeSizeRoundTrip) { int d = 16; int nhash = 2; int b = 4; // 2 hashes * 4 bits = 8 bits <= d=16 faiss::IndexBinaryMultiHash idx(d, nhash, b); idx.nflip = 1; int n = 1; std::vector<uint8_t> data(n * idx.code_size); data[0] = 0xAA; data[1] = 0x55; idx.add(n, data.data()); EXPECT_EQ(idx.ntotal, n); int k = 1; std::vector<int32_t> distances(k); std::vector<faiss::idx_t> labels(k); idx.search(1, data.data(), k, distances.data(), labels.data()); EXPECT_EQ(distances[0], 0); EXPECT_EQ(labels[0], 0);} TEST(BinaryHash, MultiHashResetClearsMaps) { int d = 16; int nhash = 2; int b = 4; faiss::IndexBinaryMultiHash idx(d, nhash, b); idx.nflip = 0; // Add a vector int n = 1; std::vector<uint8_t> data(n * idx.code_size); data[0] = 0xAA; data[1] = 0x55; idx.add(n, data.data()); EXPECT_EQ(idx.ntotal, 1); EXPECT_GT(idx.hashtable_size(), 0u); // Reset should clear everything idx.reset(); EXPECT_EQ(idx.ntotal, 0); EXPECT_EQ(idx.hashtable_size(), 0u); // Searching for the old vector after reset should not find it int k = 1; std::vector<int32_t> distances(k); std::vector<faiss::idx_t> labels(k); idx.search(1, data.data(), k, distances.data(), labels.data()); EXPECT_EQ(labels[0], -1); // After reset, add a new vector and verify the index is functional std::vector<uint8_t> data2(n * idx.code_size); data2[0] = 0x55; data2[1] = 0xAA; idx.add(n, data2.data()); EXPECT_EQ(idx.ntotal, 1); idx.search(1, data2.data(), k, distances.data(), labels.data()); EXPECT_EQ(distances[0], 0); EXPECT_EQ(labels[0], 0);} You can’t perform that action at this time.