unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
2.15k stars 130 forks source link

Bug: `usearch_get` returns wrong value in C99 interface #222

Closed alexbarev closed 1 year ago

alexbarev commented 1 year ago

Describe the bug

The function usearch_get consistently returns 1, regardless of the positive number of vectors actually copied to the buffer. I observed that in C# and then went down to C99 implementation.

I am using the newest main-dev branch.

Steps to reproduce

That function assumed to be inserted into c/test.c and called directly from main()

void test_get_multiple_vectors_from_one_key() {
    // initialization
    usearch_error_t error = NULL;
    size_t vector_dimension = 2;
    usearch_init_options_t opts = create_options(vector_dimension);
    opts.multi = true;
    usearch_index_t idx = usearch_init(&opts, &error);
    ASSERT(!error, error);

    // adding vectors
    usearch_reserve(idx, 4, &error);
    ASSERT(!error, error);
    float vector[] = {1, 2, 3, 4, 5, 6, 7, 8};
    usearch_key_t key = 1;
    for (size_t i = 0; i < 4; ++i) {
        usearch_add(idx, key, &vector[i * vector_dimension], usearch_scalar_f32_k, &error);
        ASSERT(!error, error);
    }

    // retrieve vectors from index
    float* vector_buffer = (float*)malloc(10 * sizeof(float));
    size_t count = usearch_get(idx, key, 4, vector_buffer, usearch_scalar_f32_k, &error);

    // 1 will be printed
    printf("count: %d\n", (int)count);

    // Though this prints 7, 8, 5, 6, 3, 4, 1, 2
    for (int i = 0; i < 8; ++i)
        printf("%.f ", vector_buffer[i]);

    usearch_free(idx, &error);
    ASSERT(!error, error);
    free(vector_buffer);
}

Expected behavior

usearch_get() return the correct number of vectors copied to the buffer.

USearch version

v1.1.0

Operating System

Ubuntu 22.04

Hardware architecture

x86

Which interface are you using?

Other bindings

Contact Details

No response

Is there an existing issue for this?

Code of Conduct

ashvardanian commented 1 year ago

@mgevor, Can you add this to the test cases for #207, in case it is not already there?

mgevor commented 1 year ago

The test added and the bug fixed in #207 PR