169fba94fSVolker Mische/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
269fba94fSVolker Mische
369fba94fSVolker Mische/**
469fba94fSVolker Mische * @copyright 2013 Couchbase, Inc.
569fba94fSVolker Mische *
669fba94fSVolker Mische * @author Volker Mische <volker@couchbase.com>
769fba94fSVolker Mische *
869fba94fSVolker Mische * Licensed under the Apache License, Version 2.0 (the "License"); you may not
969fba94fSVolker Mische * use this file except in compliance with the License. You may obtain a copy of
1069fba94fSVolker Mische * the License at
1169fba94fSVolker Mische *
1269fba94fSVolker Mische *  http://www.apache.org/licenses/LICENSE-2.0
1369fba94fSVolker Mische *
1469fba94fSVolker Mische * Unless required by applicable law or agreed to in writing, software
1569fba94fSVolker Mische * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1669fba94fSVolker Mische * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1769fba94fSVolker Mische * License for the specific language governing permissions and limitations under
1869fba94fSVolker Mische * the License.
1969fba94fSVolker Mische **/
2069fba94fSVolker Mische
2169fba94fSVolker Mische#include "spatial_tests.h"
2269fba94fSVolker Mische
2313d4a251SDave Rigby#include <platform/cb_malloc.h>
2469fba94fSVolker Mische
2569fba94fSVolker Mische/* Convert a binary number encoded as string to an uint32 */
2669fba94fSVolker Mischestatic uint32_t b2u(const char *binary)
2769fba94fSVolker Mische{
2869fba94fSVolker Mische    uint32_t result = 0;
2969fba94fSVolker Mische
3069fba94fSVolker Mische    while(*binary != '\0')
3169fba94fSVolker Mische    {
3269fba94fSVolker Mische        result <<= 1;
3369fba94fSVolker Mische        if (*binary == '1') {
3469fba94fSVolker Mische            result |= 1;
3569fba94fSVolker Mische        }
3669fba94fSVolker Mische        binary++;
3769fba94fSVolker Mische    }
3869fba94fSVolker Mische    return result;
3969fba94fSVolker Mische}
4069fba94fSVolker Mische
4169fba94fSVolker Mische
4269fba94fSVolker Mische/* Convert a binary number encoded as string to an unsigned char array
4369fba94fSVolker Mische * The size (in bytes) will be used for padding the result with leading
4469fba94fSVolker Mische * zeros. */
4569fba94fSVolker Mischestatic unsigned char *b2c(const char *binary, size_t size)
4669fba94fSVolker Mische{
4769fba94fSVolker Mische    int i = 0;
4869fba94fSVolker Mische    /* The offset is on the input string, hence it is in bits */
4969fba94fSVolker Mische    size_t offset = size*8 - strlen(binary);
5013d4a251SDave Rigby    unsigned char *result = (unsigned char *)cb_calloc(
5169fba94fSVolker Mische        size, sizeof(unsigned char));
5269fba94fSVolker Mische
5369fba94fSVolker Mische    for(i = 0; *binary != '\0'; i++, binary++) {
5469fba94fSVolker Mische        result[(i+offset)/8] <<= 1;
5569fba94fSVolker Mische        if (*binary == '1') {
5669fba94fSVolker Mische            result[(i+offset)/8] |= 1;
5769fba94fSVolker Mische        }
5869fba94fSVolker Mische    }
5969fba94fSVolker Mische    return result;
6069fba94fSVolker Mische}
6169fba94fSVolker Mische
6269fba94fSVolker Mische
6369fba94fSVolker Mische/* Return an assertable value: != 0 when the interleaving returned the
6469fba94fSVolker Mische * expected result */
6569fba94fSVolker Mischestatic int interleaving(uint32_t numbers[],
6669fba94fSVolker Mische                        const uint16_t size_numbers,
6769fba94fSVolker Mische                        const char *expected)
6869fba94fSVolker Mische{
6969fba94fSVolker Mische    unsigned char *interleaved;
7069fba94fSVolker Mische    unsigned char *expected_bin;
7169fba94fSVolker Mische    int ret = 0;
7269fba94fSVolker Mische
7369fba94fSVolker Mische    interleaved = interleave_uint32s(numbers, size_numbers/sizeof(uint32_t));
7469fba94fSVolker Mische    expected_bin = b2c(expected, size_numbers);
7569fba94fSVolker Mische
7669fba94fSVolker Mische    ret = memcmp((void *)interleaved, expected_bin, size_numbers) == 0;
7769fba94fSVolker Mische
7813d4a251SDave Rigby    cb_free(interleaved);
7913d4a251SDave Rigby    cb_free(expected_bin);
8069fba94fSVolker Mische
8169fba94fSVolker Mische    return ret;
8269fba94fSVolker Mische}
8369fba94fSVolker Mische
8469fba94fSVolker Mische
8569fba94fSVolker Mischevoid test_interleaving()
8669fba94fSVolker Mische{
8769fba94fSVolker Mische    uint32_t *numbers;
8869fba94fSVolker Mische    uint32_t numbers_size;
8969fba94fSVolker Mische
90ed955bb8STrond Norbye    fprintf(stderr, "Running spatial interleaving tests\n");
9169fba94fSVolker Mische
9269fba94fSVolker Mische    numbers_size = 2 * sizeof(uint32_t);
9313d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
9469fba94fSVolker Mische    numbers[0] = b2u("111111000000");
9569fba94fSVolker Mische    numbers[1] = b2u("000000111111");
9681a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "101010101010010101010101"));
9713d4a251SDave Rigby    cb_free(numbers);
9869fba94fSVolker Mische
9969fba94fSVolker Mische    numbers_size = 3 * sizeof(uint32_t);
10013d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
10169fba94fSVolker Mische    numbers[0] = b2u("1101");
10269fba94fSVolker Mische    numbers[1] = b2u("0101");
10369fba94fSVolker Mische    numbers[2] = b2u("1111");
10481a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "101111001111"));
10513d4a251SDave Rigby    cb_free(numbers);
10669fba94fSVolker Mische
10769fba94fSVolker Mische    numbers_size = 4 * sizeof(uint32_t);
10813d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
10969fba94fSVolker Mische    numbers[0] = b2u("0000");
11069fba94fSVolker Mische    numbers[1] = b2u("1111");
11169fba94fSVolker Mische    numbers[2] = b2u("1111");
11269fba94fSVolker Mische    numbers[3] = b2u("0000");
11381a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "0110011001100110"));
11413d4a251SDave Rigby    cb_free(numbers);
11569fba94fSVolker Mische
11669fba94fSVolker Mische    numbers_size = 5 * sizeof(uint32_t);
11713d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
11869fba94fSVolker Mische    numbers[0] = b2u("11");
11969fba94fSVolker Mische    numbers[1] = b2u("01");
12069fba94fSVolker Mische    numbers[2] = b2u("11");
12169fba94fSVolker Mische    numbers[3] = b2u("00");
12269fba94fSVolker Mische    numbers[4] = b2u("10");
12381a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "1010111100"));
12413d4a251SDave Rigby    cb_free(numbers);
12569fba94fSVolker Mische
12669fba94fSVolker Mische    numbers_size = 2 * sizeof(uint32_t);
12713d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
12869fba94fSVolker Mische    numbers[0] = b2u("11111111111111111111111111111111");
12969fba94fSVolker Mische    numbers[1] = b2u("00000000000000000000000000000000");
13081a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "1010101010101010101010101010101010101010101010101010101010101010"));
13113d4a251SDave Rigby    cb_free(numbers);
13269fba94fSVolker Mische
13369fba94fSVolker Mische    numbers_size = 12 * sizeof(uint32_t);
13413d4a251SDave Rigby    numbers = (uint32_t *)cb_malloc(numbers_size);
13569fba94fSVolker Mische    numbers[0] = b2u("11");
13669fba94fSVolker Mische    numbers[1] = b2u("00");
13769fba94fSVolker Mische    numbers[2] = b2u("11");
13869fba94fSVolker Mische    numbers[3] = b2u("11");
13969fba94fSVolker Mische    numbers[4] = b2u("00");
14069fba94fSVolker Mische    numbers[5] = b2u("00");
14169fba94fSVolker Mische    numbers[6] = b2u("11");
14269fba94fSVolker Mische    numbers[7] = b2u("11");
14369fba94fSVolker Mische    numbers[8] = b2u("11");
14469fba94fSVolker Mische    numbers[9] = b2u("00");
14569fba94fSVolker Mische    numbers[10] = b2u("00");
14669fba94fSVolker Mische    numbers[11] = b2u("00");
14781a8545eSTrond Norbye    cb_assert(interleaving(numbers, numbers_size, "101100111000101100111000"));
14813d4a251SDave Rigby    cb_free(numbers);
14969fba94fSVolker Mische}
15069fba94fSVolker Mische
15169fba94fSVolker Mische
15269fba94fSVolker Mischevoid test_spatial_scale_factor()
15369fba94fSVolker Mische{
15469fba94fSVolker Mische    double mbb[] = {1.0, 3.0, 30.33, 31.33, 15.4, 138.7, 7.8, 7.8};
15569fba94fSVolker Mische    uint16_t dim = (sizeof(mbb)/sizeof(double))/2;
15669fba94fSVolker Mische    uint32_t max = ZCODE_MAX_VALUE;
15769fba94fSVolker Mische    scale_factor_t *sf = NULL;
15869fba94fSVolker Mische
159ed955bb8STrond Norbye    fprintf(stderr, "Running spatial scale factor tests\n");
16069fba94fSVolker Mische
16169fba94fSVolker Mische    sf = spatial_scale_factor(mbb, dim, max);
16269fba94fSVolker Mische
16381a8545eSTrond Norbye    cb_assert(sf->offsets[0] == mbb[0]);
16481a8545eSTrond Norbye    cb_assert(sf->offsets[1] == mbb[2]);
16581a8545eSTrond Norbye    cb_assert(sf->offsets[2] == mbb[4]);
16681a8545eSTrond Norbye    cb_assert(sf->offsets[3] == mbb[6]);
16781a8545eSTrond Norbye    cb_assert((uint32_t)(sf->scales[0]*(mbb[1]-mbb[0])) == max);
16881a8545eSTrond Norbye    cb_assert((uint32_t)(sf->scales[1]*(mbb[3]-mbb[2])) == max);
16981a8545eSTrond Norbye    cb_assert((uint32_t)(sf->scales[2]*(mbb[5]-mbb[4])) == max);
17081a8545eSTrond Norbye    cb_assert((uint32_t)(sf->scales[3]) == 0);
17181a8545eSTrond Norbye    cb_assert(sf->dim == dim);
17269fba94fSVolker Mische
17369fba94fSVolker Mische    free_spatial_scale_factor(sf);
17469fba94fSVolker Mische}
17569fba94fSVolker Mische
17669fba94fSVolker Mische
17769fba94fSVolker Mischevoid test_spatial_center()
17869fba94fSVolker Mische{
17969fba94fSVolker Mische    double mbb[] = {1.0, 3.0, 30.33, 31.33, 15.4, 138.7, 7.8, 7.8};
18069fba94fSVolker Mische    double mbb2[] = {6.3, 18.7};
18169fba94fSVolker Mische    sized_mbb_t mbb_struct;
18269fba94fSVolker Mische    double *center;
18369fba94fSVolker Mische
184ed955bb8STrond Norbye    fprintf(stderr, "Running spatial scale factor tests\n");
18569fba94fSVolker Mische
18669fba94fSVolker Mische    mbb_struct.mbb = mbb;
18769fba94fSVolker Mische    mbb_struct.num = sizeof(mbb)/sizeof(double);
18869fba94fSVolker Mische    center = spatial_center(&mbb_struct);
18981a8545eSTrond Norbye    cb_assert(center[0] == 2.0);
19081a8545eSTrond Norbye    cb_assert(center[1] == 30.83);
19181a8545eSTrond Norbye    cb_assert(center[2] == 77.05);
19281a8545eSTrond Norbye    cb_assert(center[3] == 7.8);
19313d4a251SDave Rigby    cb_free(center);
19469fba94fSVolker Mische
19569fba94fSVolker Mische    mbb_struct.mbb = mbb2;
19669fba94fSVolker Mische    mbb_struct.num = sizeof(mbb2)/sizeof(double);
19769fba94fSVolker Mische    center = spatial_center(&mbb_struct);
19881a8545eSTrond Norbye    cb_assert(center[0] == 12.5);
19913d4a251SDave Rigby    cb_free(center);
20069fba94fSVolker Mische}
20169fba94fSVolker Mische
20269fba94fSVolker Mische
20369fba94fSVolker Mischevoid test_spatial_scale_point()
20469fba94fSVolker Mische{
20569fba94fSVolker Mische    double mbb[] = {1.0, 3.0, 30.33, 31.33, 15.4, 138.7, 7.8, 7.8};
20669fba94fSVolker Mische    double point[] = {2.0, 31.0, 42.02, 7.8};
20769fba94fSVolker Mische    uint16_t dim = (sizeof(mbb)/sizeof(double))/2;
20869fba94fSVolker Mische    uint32_t max = ZCODE_MAX_VALUE;
20969fba94fSVolker Mische    scale_factor_t *sf = NULL;
21069fba94fSVolker Mische    uint32_t *scaled;
21169fba94fSVolker Mische
212ed955bb8STrond Norbye    fprintf(stderr, "Running spatial scale point tests\n");
21369fba94fSVolker Mische
21469fba94fSVolker Mische    sf = spatial_scale_factor(mbb, dim, max);
21569fba94fSVolker Mische    scaled = spatial_scale_point(point, sf);
21681a8545eSTrond Norbye    cb_assert(scaled[0] == UINT32_MAX/2);
21781a8545eSTrond Norbye    cb_assert(scaled[1] > UINT32_MAX/2 && scaled[1] > 0);
21881a8545eSTrond Norbye    cb_assert(scaled[2] < UINT32_MAX/2 && scaled[2] > 0);
21981a8545eSTrond Norbye    cb_assert(scaled[3] == 0);
22069fba94fSVolker Mische
22169fba94fSVolker Mische    free_spatial_scale_factor(sf);
22213d4a251SDave Rigby    cb_free(scaled);
22369fba94fSVolker Mische}
22469fba94fSVolker Mische
22569fba94fSVolker Mische
22669fba94fSVolker Mischestatic int cmp_bytes(const unsigned char *bitmap,
227825b107fSVolker Mische                     const char *expected,
22869fba94fSVolker Mische                     const uint8_t size)
22969fba94fSVolker Mische{
23069fba94fSVolker Mische    int result = 0;
23169fba94fSVolker Mische    unsigned char *tmp = b2c(expected, size);
23269fba94fSVolker Mische
23369fba94fSVolker Mische    result = memcmp(bitmap, tmp, size) == 0;
23413d4a251SDave Rigby    cb_free(tmp);
23569fba94fSVolker Mische
23669fba94fSVolker Mische    return result;
23769fba94fSVolker Mische}
23869fba94fSVolker Mische
23969fba94fSVolker Mischevoid test_set_bit_sized()
24069fba94fSVolker Mische{
24169fba94fSVolker Mische    uint8_t size = 1;
24269fba94fSVolker Mische    unsigned char* bitmap = b2c("00010001", size);
24369fba94fSVolker Mische    unsigned char* bitmap2 = b2c("0000000000000000", 2);
24469fba94fSVolker Mische
245ed955bb8STrond Norbye    fprintf(stderr, "Running set bit sized tests\n");
24669fba94fSVolker Mische
24769fba94fSVolker Mische    set_bit_sized(bitmap, size, 1);
24881a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap, "00010011", size));
24969fba94fSVolker Mische
25069fba94fSVolker Mische    set_bit_sized(bitmap, size, 7);
25181a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap, "10010011", size));
25269fba94fSVolker Mische    set_bit_sized(bitmap, size, 2);
25381a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap, "10010111", size));
25469fba94fSVolker Mische    set_bit_sized(bitmap, size, 3);
25581a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap, "10011111", size));
25669fba94fSVolker Mische    /* Setting the same bit doesn't change the value */
25769fba94fSVolker Mische    set_bit_sized(bitmap, size, 3);
25881a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap, "10011111", size));
25969fba94fSVolker Mische
26069fba94fSVolker Mische    size = 2;
26169fba94fSVolker Mische    set_bit_sized(bitmap2, size, 0);
26281a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap2, "0000000000000001", size));
26369fba94fSVolker Mische    set_bit_sized(bitmap2, size, 13);
26481a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap2, "0010000000000001", size));
26569fba94fSVolker Mische    set_bit_sized(bitmap2, size, 7);
26681a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap2, "0010000010000001", size));
26769fba94fSVolker Mische    set_bit_sized(bitmap2, size, 3);
26881a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap2, "0010000010001001", size));
26969fba94fSVolker Mische    set_bit_sized(bitmap2, size, 12);
27081a8545eSTrond Norbye    cb_assert(cmp_bytes(bitmap2, "0011000010001001", size));
27169fba94fSVolker Mische
27213d4a251SDave Rigby    cb_free(bitmap);
27313d4a251SDave Rigby    cb_free(bitmap2);
27469fba94fSVolker Mische}
2759a8beb97SVolker Mische
2769a8beb97SVolker Mische
2779a8beb97SVolker Mischevoid test_encode_spatial_key()
2789a8beb97SVolker Mische{
2799a8beb97SVolker Mische    sized_mbb_t mbb_struct;
2809a8beb97SVolker Mische    char encoded[66];
2819a8beb97SVolker Mische    double mbb[] = {6.3, 18.7};
2829a8beb97SVolker Mische    double mbb2[] = {1.0, 3.0, 30.33, 31.33, 15.4, 138.7, 7.8, 7.8};
2839a8beb97SVolker Mische    unsigned char expected[] = {
2849a8beb97SVolker Mische        0x00, 0x02, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x19, 0x40,
2859a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x33, 0xb3, 0x32, 0x40
2869a8beb97SVolker Mische    };
2879a8beb97SVolker Mische    unsigned char expected2[] = {
2889a8beb97SVolker Mische        0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
2899a8beb97SVolker Mische        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x14, 0xae,
2909a8beb97SVolker Mische        0x47, 0xe1, 0x7a, 0x54, 0x3e, 0x40, 0x14, 0xae, 0x47, 0xe1,
2919a8beb97SVolker Mische        0x7a, 0x54, 0x3f, 0x40, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
2929a8beb97SVolker Mische        0x2e, 0x40, 0x66, 0x66, 0x66, 0x66, 0x66, 0x56, 0x61, 0x40,
2939a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x1f, 0x40, 0x33, 0x33,
2949a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x1f, 0x40
2959a8beb97SVolker Mische    };
2969a8beb97SVolker Mische
2979a8beb97SVolker Mische    fprintf(stderr, "Running encode spatial key tests\n");
2989a8beb97SVolker Mische
2999a8beb97SVolker Mische    mbb_struct.mbb = mbb;
3009a8beb97SVolker Mische    mbb_struct.num = sizeof(mbb)/sizeof(double);
3019a8beb97SVolker Mische    encode_spatial_key(&mbb_struct, (char *)&encoded, sizeof(encoded));
30281a8545eSTrond Norbye    cb_assert(memcmp(encoded, expected, 18) == 0);
3039a8beb97SVolker Mische
3049a8beb97SVolker Mische    mbb_struct.mbb = mbb2;
3059a8beb97SVolker Mische    mbb_struct.num = sizeof(mbb2)/sizeof(double);
3069a8beb97SVolker Mische    encode_spatial_key(&mbb_struct, (char *)&encoded, sizeof(encoded));
30781a8545eSTrond Norbye    cb_assert(memcmp(encoded, expected2, 66) == 0);
3089a8beb97SVolker Mische}
3099a8beb97SVolker Mische
3109a8beb97SVolker Mische
3119a8beb97SVolker Mische/* Compares two arrays of doubles. 1 if equal, 0 if not equal */
3129a8beb97SVolker Mischestatic bool is_double_array_equal(double *a, double *b, int len)
3139a8beb97SVolker Mische{
3149a8beb97SVolker Mische    int i;
3159a8beb97SVolker Mische
3169a8beb97SVolker Mische    for (i = 0; i < len; ++i) {
3179a8beb97SVolker Mische        if (a[i] != b[i]) {
3189a8beb97SVolker Mische            return false;
3199a8beb97SVolker Mische        }
3209a8beb97SVolker Mische    }
3219a8beb97SVolker Mische
3229a8beb97SVolker Mische    return true;
3239a8beb97SVolker Mische}
3249a8beb97SVolker Mische
3259a8beb97SVolker Mischevoid test_decode_spatial_key()
3269a8beb97SVolker Mische{
3279a8beb97SVolker Mische    sized_mbb_t decoded;
3289a8beb97SVolker Mische    unsigned char mbb[] = {
3299a8beb97SVolker Mische        0x00, 0x02, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x19, 0x40,
3309a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x33, 0xb3, 0x32, 0x40
3319a8beb97SVolker Mische    };
3329a8beb97SVolker Mische    unsigned char mbb2[] = {
3339a8beb97SVolker Mische        0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
3349a8beb97SVolker Mische        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x14, 0xae,
3359a8beb97SVolker Mische        0x47, 0xe1, 0x7a, 0x54, 0x3e, 0x40, 0x14, 0xae, 0x47, 0xe1,
3369a8beb97SVolker Mische        0x7a, 0x54, 0x3f, 0x40, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
3379a8beb97SVolker Mische        0x2e, 0x40, 0x66, 0x66, 0x66, 0x66, 0x66, 0x56, 0x61, 0x40,
3389a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x1f, 0x40, 0x33, 0x33,
3399a8beb97SVolker Mische        0x33, 0x33, 0x33, 0x33, 0x1f, 0x40
3409a8beb97SVolker Mische    };
3419a8beb97SVolker Mische    double expected[] = {6.3, 18.7};
3429a8beb97SVolker Mische    double expected2[] = {1.0, 3.0, 30.33, 31.33, 15.4, 138.7, 7.8, 7.8};
3439a8beb97SVolker Mische
3449a8beb97SVolker Mische    fprintf(stderr, "Running decode spatial key tests\n");
3459a8beb97SVolker Mische
3469a8beb97SVolker Mische    decode_spatial_key((char *)mbb, &decoded);
34781a8545eSTrond Norbye    cb_assert(decoded.num == 2);
34881a8545eSTrond Norbye    cb_assert(is_double_array_equal(decoded.mbb, expected, 2));
3499a8beb97SVolker Mische
3509a8beb97SVolker Mische    decode_spatial_key((char *)mbb2, &decoded);
35181a8545eSTrond Norbye    cb_assert(decoded.num == 8);
35281a8545eSTrond Norbye    cb_assert(is_double_array_equal(decoded.mbb, expected2, 8));
3539a8beb97SVolker Mische}
3549a8beb97SVolker Mische
3559a8beb97SVolker Mische
3569a8beb97SVolker Mischevoid test_expand_mbb()
3579a8beb97SVolker Mische{
3589a8beb97SVolker Mische    sized_mbb_t mbb_struct_a, mbb_struct_b;
3599a8beb97SVolker Mische    double mbb_a[] = {6.3, 18.7};
3609a8beb97SVolker Mische    double mbb_b[] = {10.1, 31.5};
3619a8beb97SVolker Mische    double expected_mbb[] = {6.3, 31.5};
3629a8beb97SVolker Mische    double mbb2_a[] = {3.0, 5.0, 100.1, 150.2, 12.5, 13.75};
3639a8beb97SVolker Mische    double mbb2_b[] = {2.9, 4.9, 80.0, 222.2, 13.0, 13.1};
3649a8beb97SVolker Mische    double expected2_mbb[] = {2.9, 5.0, 80.0, 222.2, 12.5, 13.75};
3659a8beb97SVolker Mische
3669a8beb97SVolker Mische    fprintf(stderr, "Running expand MBB tests\n");
3679a8beb97SVolker Mische
3689a8beb97SVolker Mische    mbb_struct_a.num = 2;
3699a8beb97SVolker Mische    mbb_struct_a.mbb = mbb_a;
3709a8beb97SVolker Mische    mbb_struct_b.num = 2;
3719a8beb97SVolker Mische    mbb_struct_b.mbb = mbb_b;
3729a8beb97SVolker Mische    expand_mbb(&mbb_struct_a, &mbb_struct_b);
37381a8545eSTrond Norbye    cb_assert(is_double_array_equal(mbb_struct_a.mbb, expected_mbb, 2));
3749a8beb97SVolker Mische
3759a8beb97SVolker Mische    mbb_struct_a.num = 6;
3769a8beb97SVolker Mische    mbb_struct_a.mbb = mbb2_a;
3779a8beb97SVolker Mische    mbb_struct_b.num = 6;
3789a8beb97SVolker Mische    mbb_struct_b.mbb = mbb2_b;
3799a8beb97SVolker Mische    expand_mbb(&mbb_struct_a, &mbb_struct_b);
38081a8545eSTrond Norbye    cb_assert(is_double_array_equal(mbb_struct_a.mbb, expected2_mbb, 6));
3819a8beb97SVolker Mische}
3826ef489f3Sharsha
3836ef489f3Sharshavoid test_view_spatial_reduce()
3846ef489f3Sharsha{
3856ef489f3Sharsha    sized_mbb_t original_mbb, expander_mbb;
3866ef489f3Sharsha
3876ef489f3Sharsha    original_mbb.num = expander_mbb.num = 6;
3886ef489f3Sharsha    double mbb1[] = {60, 60, 115, 115, 1, 6};
3896ef489f3Sharsha    double mbb2[] = {63, 63, 1506, 1506, 1, 6};
3906ef489f3Sharsha    original_mbb.mbb = mbb1;
3916ef489f3Sharsha    original_mbb.num = expander_mbb.num = 6;
3926ef489f3Sharsha    expander_mbb.mbb = mbb2;
3936ef489f3Sharsha
3946ef489f3Sharsha    nodelist root, child;
3956ef489f3Sharsha    char encoded1[128], encoded2[128], dst[128];
3966ef489f3Sharsha    size_t size;
3976ef489f3Sharsha
3986ef489f3Sharsha    encode_spatial_key(&original_mbb, (char *)&encoded1, sizeof(encoded1));
3996ef489f3Sharsha    encode_spatial_key(&expander_mbb, (char *)&encoded2, sizeof(encoded2));
4006ef489f3Sharsha
4016ef489f3Sharsha    root.key.buf = encoded1;
4026ef489f3Sharsha    child.key.buf = encoded2;
4036ef489f3Sharsha
4046ef489f3Sharsha    root.next = &child;
4056ef489f3Sharsha    child.next = NULL;
4066ef489f3Sharsha
4076ef489f3Sharsha    fprintf(stderr, "Running view_spatial_reduce test\n");
4086ef489f3Sharsha    view_spatial_reduce(dst, &size, &root, 2, NULL);
4096ef489f3Sharsha
4106ef489f3Sharsha}
4116ef489f3Sharsha
412