File: external_module_test.py

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 (66 lines) | stat: -rw-r--r-- 1,772 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
# 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.

import unittest

import faiss

import faiss.faiss_example_external_module as external_module

import numpy as np


class TestCustomIDSelector(unittest.TestCase):
    """test if we can construct a custom IDSelector"""

    def test_IDSelector(self):
        ids = external_module.IDSelectorModulo(3)
        self.assertFalse(ids.is_member(1))
        self.assertTrue(ids.is_member(3))


class TestArrayConversions(unittest.TestCase):

    def test_idx_array(self):
        tab = np.arange(10).astype("int64")
        new_sum = external_module.sum_of_idx(len(tab), faiss.swig_ptr(tab))
        self.assertEqual(new_sum, tab.sum())

    def do_array_test(self, ty):
        tab = np.arange(10).astype(ty)
        func = getattr(external_module, "sum_of_" + ty)
        print("perceived type", faiss.swig_ptr(tab))
        new_sum = func(len(tab), faiss.swig_ptr(tab))
        self.assertEqual(new_sum, tab.sum())

    def test_sum_uint8(self):
        self.do_array_test("uint8")

    def test_sum_uint16(self):
        self.do_array_test("uint16")

    def test_sum_uint32(self):
        self.do_array_test("uint32")

    def test_sum_uint64(self):
        self.do_array_test("uint64")

    def test_sum_int8(self):
        self.do_array_test("int8")

    def test_sum_int16(self):
        self.do_array_test("int16")

    def test_sum_int32(self):
        self.do_array_test("int32")

    def test_sum_int64(self):
        self.do_array_test("int64")

    def test_sum_float32(self):
        self.do_array_test("float32")

    def test_sum_float64(self):
        self.do_array_test("float64")