File: test_cppcontrib_uintreader.cpp

package info (click to toggle)
faiss 1.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,572 kB
  • sloc: cpp: 85,627; python: 27,889; sh: 905; ansic: 425; makefile: 41
file content (114 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * 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.
 */

// This test was designed to be run using valgrind or ASAN to test the
// correctness of memory accesses.

#include <gtest/gtest.h>

#include <cstddef>
#include <cstdint>
#include <memory>
#include <random>

#include <faiss/utils/hamming.h>

#include <faiss/cppcontrib/detail/UintReader.h>

template <intptr_t N_ELEMENTS, intptr_t CODE_BITS, intptr_t CPOS>
struct TestLoop {
    static void test(
            const uint8_t* const container,
            faiss::BitstringReader& br) {
        // validate
        const intptr_t uintreader_data = faiss::cppcontrib::detail::
                UintReaderRaw<N_ELEMENTS, CODE_BITS, CPOS>::get(container);
        const intptr_t bitstringreader_data = br.read(CODE_BITS);

        ASSERT_EQ(uintreader_data, bitstringreader_data)
                << "Mismatch between BitstringReader (" << bitstringreader_data
                << ") and UintReader (" << uintreader_data
                << ") for N_ELEMENTS=" << N_ELEMENTS
                << ", CODE_BITS=" << CODE_BITS << ", CPOS=" << CPOS;

        //
        TestLoop<N_ELEMENTS, CODE_BITS, CPOS + 1>::test(container, br);
    }
};

template <intptr_t N_ELEMENTS, intptr_t CODE_BITS>
struct TestLoop<N_ELEMENTS, CODE_BITS, N_ELEMENTS> {
    static void test(
            const uint8_t* const container,
            faiss::BitstringReader& br) {}
};

template <intptr_t N_ELEMENTS, intptr_t CODE_BITS>
void TestUintReader() {
    constexpr intptr_t CODE_BYTES = (CODE_BITS * N_ELEMENTS + 7) / 8;

    std::default_random_engine rng;
    std::uniform_int_distribution<uint64_t> u(0, 1 << CODE_BITS);

    // do several attempts
    for (size_t attempt = 0; attempt < 10; attempt++) {
        // allocate a buffer. This way, not std::vector
        std::unique_ptr<uint8_t[]> container(new uint8_t[CODE_BYTES]);
        // make it empty
        for (size_t i = 0; i < CODE_BYTES; i++) {
            container.get()[i] = 0;
        }

        // populate it
        faiss::BitstringWriter bw(container.get(), CODE_BYTES);
        for (size_t i = 0; i < N_ELEMENTS; i++) {
            bw.write(u(rng), CODE_BITS);
        }

        // read it back and verify against bitreader
        faiss::BitstringReader br(container.get(), CODE_BYTES);

        TestLoop<N_ELEMENTS, CODE_BITS, 0>::test(container.get(), br);
    }
}

template <intptr_t CODE_BITS>
void TestUintReaderBits() {
    TestUintReader<1, CODE_BITS>();
    TestUintReader<2, CODE_BITS>();
    TestUintReader<3, CODE_BITS>();
    TestUintReader<4, CODE_BITS>();
    TestUintReader<5, CODE_BITS>();
    TestUintReader<6, CODE_BITS>();
    TestUintReader<7, CODE_BITS>();
    TestUintReader<8, CODE_BITS>();
    TestUintReader<9, CODE_BITS>();
    TestUintReader<10, CODE_BITS>();
    TestUintReader<11, CODE_BITS>();
    TestUintReader<12, CODE_BITS>();
    TestUintReader<13, CODE_BITS>();
    TestUintReader<14, CODE_BITS>();
    TestUintReader<15, CODE_BITS>();
    TestUintReader<16, CODE_BITS>();
    TestUintReader<17, CODE_BITS>();
}

TEST(testCppcontribUintreader, Test8bit) {
    TestUintReaderBits<8>();
}

TEST(testCppcontribUintreader, Test10bit) {
    TestUintReaderBits<10>();
}

TEST(testCppcontribUintreader, Test12bit) {
    TestUintReaderBits<12>();
}

TEST(testCppcontribUintreader, Test16bit) {
    TestUintReaderBits<16>();
}