adds array interface and tests feature/array

Thu, 04 Jul 2019 20:07:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Jul 2019 20:07:31 +0200
branch
feature/array
changeset 334
bc81faa9afda
parent 333
b3ad9d1a20b7
child 335
872ae61c8945

adds array interface and tests

src/Makefile.am file | annotate | diff | comparison | revisions
src/array.c file | annotate | diff | comparison | revisions
src/ucx/array.h file | annotate | diff | comparison | revisions
src/ucx/ucx.h file | annotate | diff | comparison | revisions
test/Makefile.am file | annotate | diff | comparison | revisions
test/array_tests.c file | annotate | diff | comparison | revisions
test/array_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/Makefile.am	Fri Dec 28 17:20:23 2018 +0100
     1.2 +++ b/src/Makefile.am	Thu Jul 04 20:07:31 2019 +0200
     1.3 @@ -29,6 +29,7 @@
     1.4  lib_LTLIBRARIES = libucx.la
     1.5  libucx_la_LDFLAGS = -version-info $(UCX_LIB_VERSION)
     1.6  libucx_la_SOURCES = utils.c
     1.7 +libucx_la_SOURCES += array.c
     1.8  libucx_la_SOURCES += list.c
     1.9  libucx_la_SOURCES += map.c
    1.10  libucx_la_SOURCES += avl.c
    1.11 @@ -44,6 +45,7 @@
    1.12  
    1.13  ucxdir = $(includedir)/ucx
    1.14  ucx_HEADERS = ucx/allocator.h
    1.15 +ucx_HEADERS += ucx/array.h
    1.16  ucx_HEADERS += ucx/avl.h
    1.17  ucx_HEADERS += ucx/buffer.h
    1.18  ucx_HEADERS += ucx/list.h
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/array.c	Thu Jul 04 20:07:31 2019 +0200
     2.3 @@ -0,0 +1,107 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     2.8 + *
     2.9 + * Redistribution and use in source and binary forms, with or without
    2.10 + * modification, are permitted provided that the following conditions are met:
    2.11 + *
    2.12 + *   1. Redistributions of source code must retain the above copyright
    2.13 + *      notice, this list of conditions and the following disclaimer.
    2.14 + *
    2.15 + *   2. Redistributions in binary form must reproduce the above copyright
    2.16 + *      notice, this list of conditions and the following disclaimer in the
    2.17 + *      documentation and/or other materials provided with the distribution.
    2.18 + *
    2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.29 + * POSSIBILITY OF SUCH DAMAGE.
    2.30 + */
    2.31 +
    2.32 +#include "ucx/array.h"
    2.33 +
    2.34 +
    2.35 +UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
    2.36 +    return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
    2.37 +}
    2.38 +
    2.39 +UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
    2.40 +        UcxAllocator* allocator) {
    2.41 +    UcxArray array;
    2.42 +    
    2.43 +    return array;
    2.44 +}
    2.45 +
    2.46 +UcxArray ucx_array_clone(UcxArray array) {
    2.47 +    UcxArray clone;
    2.48 +    
    2.49 +    return clone;
    2.50 +}
    2.51 +
    2.52 +int ucx_array_equals(UcxArray array1, UcxArray array2,
    2.53 +        cmp_func cmpfnc, void* data) {
    2.54 +    
    2.55 +    return 1;
    2.56 +}
    2.57 +
    2.58 +void ucx_array_free(UcxArray *array) {
    2.59 +    
    2.60 +}
    2.61 +
    2.62 +int ucx_array_append(UcxArray *array, void *data) {
    2.63 +    return 1;
    2.64 +}
    2.65 +
    2.66 +int ucx_array_prepend(UcxArray *array, void *data) {
    2.67 +    return 1;
    2.68 +}
    2.69 +
    2.70 +int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
    2.71 +    return 1;
    2.72 +}
    2.73 +
    2.74 +void *ucx_array_at(UcxArray array, size_t index) {
    2.75 +    return NULL;
    2.76 +}
    2.77 +
    2.78 +size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
    2.79 +    
    2.80 +    return 0;
    2.81 +}
    2.82 +
    2.83 +int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
    2.84 +    return ucx_array_find(array, elem, cmpfnc, data) != array.size;
    2.85 +}
    2.86 +
    2.87 +int ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) {
    2.88 +    return 1;
    2.89 +}
    2.90 +
    2.91 +void ucx_array_remove(UcxArray *array, size_t index) {
    2.92 +    
    2.93 +}
    2.94 +
    2.95 +void ucx_array_remove_fast(UcxArray *array, size_t index) {
    2.96 +    
    2.97 +}
    2.98 +
    2.99 +int ucx_array_shrink(UcxArray* array) {
   2.100 +    return 1;
   2.101 +}
   2.102 +
   2.103 +int ucx_array_resize(UcxArray* array, size_t capacity) {
   2.104 +    return 1;
   2.105 +}
   2.106 +
   2.107 +int ucx_array_reserve(UcxArray* array, size_t capacity) {
   2.108 +    return 1;
   2.109 +}
   2.110 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/ucx/array.h	Thu Jul 04 20:07:31 2019 +0200
     3.3 @@ -0,0 +1,356 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +/**
    3.32 + * Dynamically allocated array implementation.
    3.33 + * 
    3.34 + * @file   array.h
    3.35 + * @author Mike Becker
    3.36 + * @author Olaf Wintermann
    3.37 + */
    3.38 +
    3.39 +#ifndef UCX_ARRAY_H
    3.40 +#define	UCX_ARRAY_H
    3.41 +
    3.42 +#include "ucx.h"
    3.43 +#include "allocator.h"
    3.44 +
    3.45 +#ifdef	__cplusplus
    3.46 +extern "C" {
    3.47 +#endif
    3.48 +
    3.49 +/**
    3.50 + * UCX array type.
    3.51 + */
    3.52 +typedef struct {
    3.53 +    /**
    3.54 +     * The current capacity of the array.
    3.55 +     */
    3.56 +    size_t capacity;
    3.57 +    /**
    3.58 +     * The actual number of elements in the array.
    3.59 +     */
    3.60 +    size_t size;
    3.61 +    /**
    3.62 +     * The size of an individual element in bytes.
    3.63 +     */
    3.64 +    size_t elemsize;
    3.65 +    /**
    3.66 +     * A pointer to the data.
    3.67 +     */
    3.68 +    void* data;
    3.69 +    /**
    3.70 +     * The allocator used for the data.
    3.71 +     */
    3.72 +    UcxAllocator* allocator;
    3.73 +} UcxArray;
    3.74 +
    3.75 +
    3.76 +/**
    3.77 + * Creates a new UCX array with the given capacity and element size.
    3.78 + * @param capacity the initial capacity
    3.79 + * @param elemsize the element size
    3.80 + * @return a new UCX array structure
    3.81 + */
    3.82 +UcxArray ucx_array_new(size_t capacity, size_t elemsize);
    3.83 +
    3.84 +/**
    3.85 + * Creates a new UCX array using the specified allocator.
    3.86 + * 
    3.87 + * @param capacity the initial capacity
    3.88 + * @param elemsize the element size
    3.89 + * @param allocator the allocator to use
    3.90 + * @return a new UCX array structure
    3.91 + */
    3.92 +UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
    3.93 +        UcxAllocator* allocator);
    3.94 +
    3.95 +/**
    3.96 + * Creates an shallow copy of an array.
    3.97 + * 
    3.98 + * This function clones the specified array by using memcpy().
    3.99 + * 
   3.100 + * @param array the array to copy
   3.101 + * @return the copy (may be an empty array on allocation errors)
   3.102 + */
   3.103 +UcxArray ucx_array_clone(UcxArray array);
   3.104 +
   3.105 +
   3.106 +/**
   3.107 + * Compares two UCX arrays element-wise by using a compare function.
   3.108 + *
   3.109 + * Elements of the two specified arrays are compared by using the specified
   3.110 + * compare function and the additional data. The type and content of this
   3.111 + * additional data depends on the cmp_func() used.
   3.112 + * 
   3.113 + * This function always returns zero, if the element sizes of the arrays do
   3.114 + * not match and performs no comparisons in this case.
   3.115 + * 
   3.116 + * @param array1 the first array
   3.117 + * @param array2 the second array
   3.118 + * @param cmpfnc the compare function
   3.119 + * @param data additional data for the compare function
   3.120 + * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
   3.121 + */
   3.122 +int ucx_array_equals(UcxArray array1, UcxArray array2,
   3.123 +        cmp_func cmpfnc, void* data);
   3.124 +
   3.125 +/**
   3.126 + * Destroys the array.
   3.127 + * 
   3.128 + * The data is freed and both capacity and count are reset to zero.
   3.129 + * If the array structure itself has been dynamically allocated, it has to be
   3.130 + * freed separately.
   3.131 + * 
   3.132 + * @param array the array to free
   3.133 + */
   3.134 +void ucx_array_free(UcxArray *array);
   3.135 +
   3.136 +/**
   3.137 + * Inserts an element at the end of the array.
   3.138 + * 
   3.139 + * This is an O(1) operation.
   3.140 + * The array will automatically grow, if the capacity is exceeded.
   3.141 + * If a pointer to data is provided, the data is copied into the array with
   3.142 + * memcpy(). Otherwise the new element is completely zeroed.
   3.143 + * 
   3.144 + * @param array a pointer the array where to append the data
   3.145 + * @param data a pointer to the data to insert (may be <code>NULL</code>)
   3.146 + * @return zero on success, non-zero if a reallocation was necessary but failed
   3.147 + */
   3.148 +int ucx_array_append(UcxArray *array, void *data);
   3.149 +
   3.150 +
   3.151 +/**
   3.152 + * Inserts an element at the beginning of the array.
   3.153 + * 
   3.154 + * This is an expensive operation, because the contents must be moved.
   3.155 + * If there is no particular reason to prepend data, you should use
   3.156 + * ucx_array_append() instead.
   3.157 + * 
   3.158 + * @param array a pointer the array where to prepend the data
   3.159 + * @param data a pointer to the data to insert (may be <code>NULL</code>)
   3.160 + * @return zero on success, non-zero if a reallocation was necessary but failed
   3.161 + */
   3.162 +int ucx_array_prepend(UcxArray *array, void *data);
   3.163 +
   3.164 +/**
   3.165 + * Concatenates two arrays.
   3.166 + * 
   3.167 + * The contents of the second array are appended to the first array in one
   3.168 + * single operation. The second array is otherwise left untouched.
   3.169 + * 
   3.170 + * The first array may grow automatically. If this fails, both arrays remain
   3.171 + * unmodified.
   3.172 + * 
   3.173 + * @param array1 first array
   3.174 + * @param array2 second array
   3.175 + * @return zero on success, non-zero if reallocation was necessary but failed 
   3.176 + * or the element size does not match
   3.177 + */
   3.178 +int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
   3.179 +
   3.180 +/**
   3.181 + * Returns a pointer to the array element at the specified index.
   3.182 + * 
   3.183 + * @param array the array to retrieve the element from
   3.184 + * @param index index of the element to return
   3.185 + * @return a pointer to the element at the specified index or <code>NULL</code>,
   3.186 + * if the index is greater than the array size
   3.187 + */
   3.188 +void *ucx_array_at(UcxArray array, size_t index);
   3.189 +
   3.190 +/**
   3.191 + * Returns an element of the specified type by value.
   3.192 + * 
   3.193 + * This expression can also be assigned to.
   3.194 + * 
   3.195 + * If <code>sizeof(type)</code> does not equal the array's element size, the
   3.196 + * behavior is undefined.
   3.197 + * If the index is out of bounds, the behavior is undefined.
   3.198 + * 
   3.199 + * @param type the type of the element
   3.200 + * @param array the array to retrieve the element from
   3.201 + * @param index index of the element to return
   3.202 + * @return the requested element
   3.203 + * @see ucx_array_at()
   3.204 + */
   3.205 +#define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
   3.206 +
   3.207 +/**
   3.208 + * Shorthand for ucx_array_at_typed().
   3.209 + */
   3.210 +#define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
   3.211 +
   3.212 +/**
   3.213 + * Shorthand for ucx_array_at_typed().
   3.214 + */
   3.215 +#define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
   3.216 +
   3.217 +/**
   3.218 + * Shorthand for ucx_array_at_typed().
   3.219 + */
   3.220 +#define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
   3.221 +
   3.222 +/**
   3.223 + * Shorthand for ucx_array_at_typed().
   3.224 + */
   3.225 +#define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
   3.226 +
   3.227 +/**
   3.228 + * Shorthand for ucx_array_at_typed().
   3.229 + */
   3.230 +#define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
   3.231 +
   3.232 +/**
   3.233 + * Shorthand for ucx_array_at_typed().
   3.234 + */
   3.235 +#define ucx_array_at_ulongint(arr, i) \
   3.236 +    ucx_array_at_typed(unsigned long int, arr, i)
   3.237 +
   3.238 +/**
   3.239 + * Shorthand for ucx_array_get_typed().
   3.240 + */
   3.241 +#define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
   3.242 +
   3.243 +/**
   3.244 + * Shorthand for ucx_array_get_typed().
   3.245 + */
   3.246 +#define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
   3.247 +
   3.248 +/**
   3.249 + * Returns the index of an element containing the specified data.
   3.250 + *
   3.251 + * This function uses a cmp_func() to compare the data of each list element
   3.252 + * with the specified data. If no cmp_func is provided, memcmp() is used.
   3.253 + * 
   3.254 + * If the array contains the data more than once, the index of the first
   3.255 + * occurrence is returned.
   3.256 + * If the array does not contain the data, the size of array is returned.
   3.257 + *  
   3.258 + * @param array the array where to search for the data
   3.259 + * @param elem the element data
   3.260 + * @param cmpfnc the compare function
   3.261 + * @param data additional data for the compare function
   3.262 + * @return the index of the element containing the specified data or the size of
   3.263 + * the array, if the data is not found in this array
   3.264 + */
   3.265 +size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
   3.266 +
   3.267 +/**
   3.268 + * Checks, if an array contains a specific element.
   3.269 + * 
   3.270 + * An element is found, if ucx_array_find() returns a value less than the size.
   3.271 + * 
   3.272 + * @param array the array where to search for the data
   3.273 + * @param elem the element data
   3.274 + * @param cmpfnc the compare function
   3.275 + * @param data additional data for the compare function
   3.276 + * @return 1, if and only if the array contains the specified element data
   3.277 + * @see ucx_array_find()
   3.278 + */
   3.279 +int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
   3.280 +
   3.281 +/**
   3.282 + * Sorts a UcxArray with natural merge sort.
   3.283 + * 
   3.284 + * This function uses O(n) additional temporary memory for merge operations
   3.285 + * that is automatically freed after each merge.
   3.286 + * 
   3.287 + * @param the array to sort
   3.288 + * @param cmpfnc the function that shall be used to compare the element data
   3.289 + * @param data additional data for the cmp_func()
   3.290 + * @return zero on success, non-zero if allocation of temporary memory failed
   3.291 + */
   3.292 +int ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
   3.293 +
   3.294 +/**
   3.295 + * Removes an element from the array.
   3.296 + * 
   3.297 + * This is in general an expensive operation, because several elements may
   3.298 + * be moved. If the order of the elements is not relevant, use
   3.299 + * ucx_array_remove_fast() instead.
   3.300 + * 
   3.301 + * @param array pointer to the array from which the element shall be removed
   3.302 + * @param index the index of the element to remove
   3.303 + */
   3.304 +void ucx_array_remove(UcxArray *array, size_t index);
   3.305 +
   3.306 +/**
   3.307 + * Removes an element from the array.
   3.308 + * 
   3.309 + * This is an O(1) operation, but does not maintain the order of the elements.
   3.310 + * The last element in the array is moved to the location of the removed
   3.311 + * element.
   3.312 + * 
   3.313 + * @param array pointer to the array from which the element shall be removed
   3.314 + * @param index the index of the element to remove
   3.315 + */
   3.316 +void ucx_array_remove_fast(UcxArray *array, size_t index);
   3.317 +
   3.318 +/**
   3.319 + * Shrinks the memory to exactly fit the contents.
   3.320 + * 
   3.321 + * After this operation, the capacity equals the size.
   3.322 + * 
   3.323 + * @param array a pointer to the array
   3.324 + * @return zero on success, non-zero if reallocation failed
   3.325 + */
   3.326 +int ucx_array_shrink(UcxArray* array);
   3.327 +
   3.328 +/**
   3.329 + * Sets the capacity of the array.
   3.330 + * 
   3.331 + * If the new capacity is smaller than the size of the array, the elements
   3.332 + * are removed and the size is adjusted accordingly.
   3.333 + * 
   3.334 + * @param array a pointer to the array
   3.335 + * @param capacity the new capacity
   3.336 + * @return zero on success, non-zero if reallocation failed
   3.337 + */
   3.338 +int ucx_array_resize(UcxArray* array, size_t capacity);
   3.339 +
   3.340 +/**
   3.341 + * Resizes the array only, if the capacity is insufficient.
   3.342 + * 
   3.343 + * If the requested capacity is smaller than the current capacity, this
   3.344 + * function does nothing.
   3.345 + * 
   3.346 + * @param array a pointer to the array
   3.347 + * @param capacity the guaranteed capacity
   3.348 + * @return zero on success, non-zero if reallocation failed
   3.349 + */
   3.350 +int ucx_array_reserve(UcxArray* array, size_t capacity);
   3.351 +
   3.352 +
   3.353 +
   3.354 +#ifdef	__cplusplus
   3.355 +}
   3.356 +#endif
   3.357 +
   3.358 +#endif	/* UCX_ARRAY_H */
   3.359 +
     4.1 --- a/src/ucx/ucx.h	Fri Dec 28 17:20:23 2018 +0100
     4.2 +++ b/src/ucx/ucx.h	Thu Jul 04 20:07:31 2019 +0200
     4.3 @@ -1,7 +1,7 @@
     4.4  /*
     4.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6   *
     4.7 - * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     4.8 + * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     4.9   *
    4.10   * Redistribution and use in source and binary forms, with or without
    4.11   * modification, are permitted provided that the following conditions are met:
    4.12 @@ -40,7 +40,7 @@
    4.13  #define UCX_VERSION_MAJOR   2
    4.14  
    4.15  /** Minor UCX version as integer constant. */
    4.16 -#define UCX_VERSION_MINOR   0
    4.17 +#define UCX_VERSION_MINOR   1
    4.18  
    4.19  /** Version constant which ensures to increase monotonically. */
    4.20  #define UCX_VERSION (((UCX_VERSION_MAJOR)<<16)|UCX_VERSION_MINOR)
     5.1 --- a/test/Makefile.am	Fri Dec 28 17:20:23 2018 +0100
     5.2 +++ b/test/Makefile.am	Thu Jul 04 20:07:31 2019 +0200
     5.3 @@ -31,6 +31,7 @@
     5.4  ucxtest_CFLAGS = -I$(top_srcdir)/src
     5.5  ucxtest_SOURCES = main.c
     5.6  ucxtest_SOURCES += allocator_tests.c
     5.7 +ucxtest_SOURCES += array_tests.c
     5.8  ucxtest_SOURCES += list_tests.c
     5.9  ucxtest_SOURCES += avl_tests.c
    5.10  ucxtest_SOURCES += mpool_tests.c
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/array_tests.c	Thu Jul 04 20:07:31 2019 +0200
     6.3 @@ -0,0 +1,448 @@
     6.4 +/*
     6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 + *
     6.7 + * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     6.8 + *
     6.9 + * Redistribution and use in source and binary forms, with or without
    6.10 + * modification, are permitted provided that the following conditions are met:
    6.11 + *
    6.12 + *   1. Redistributions of source code must retain the above copyright
    6.13 + *      notice, this list of conditions and the following disclaimer.
    6.14 + *
    6.15 + *   2. Redistributions in binary form must reproduce the above copyright
    6.16 + *      notice, this list of conditions and the following disclaimer in the
    6.17 + *      documentation and/or other materials provided with the distribution.
    6.18 + *
    6.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    6.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    6.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    6.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    6.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    6.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    6.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    6.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    6.29 + * POSSIBILITY OF SUCH DAMAGE.
    6.30 + */
    6.31 +
    6.32 +#include "array_tests.h"
    6.33 +#include <ucx/utils.h>
    6.34 +
    6.35 +UCX_TEST(test_ucx_array_free) {
    6.36 +    UcxArray array = ucx_array_new(16, sizeof(int));
    6.37 +    
    6.38 +    UCX_TEST_BEGIN
    6.39 +    ucx_array_free(&array);
    6.40 +    UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free");
    6.41 +    UCX_TEST_ASSERT(array.size == 0, "size not zero after free");
    6.42 +    UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free");
    6.43 +    UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
    6.44 +            "allocator corrupted during free");
    6.45 +    UCX_TEST_END
    6.46 +}
    6.47 +
    6.48 +UCX_TEST(test_ucx_array_new) {
    6.49 +    UcxArray array = ucx_array_new(16, 47);
    6.50 +    
    6.51 +    UCX_TEST_BEGIN
    6.52 +    UCX_TEST_ASSERT(array.data, "no memory allocated");
    6.53 +    UCX_TEST_ASSERT(array.size == 0, "size not initially zero");
    6.54 +    UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested");
    6.55 +    UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested");
    6.56 +    UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
    6.57 +            "array not using the default allocator");
    6.58 +    UCX_TEST_END
    6.59 +    ucx_array_free(&array);
    6.60 +}
    6.61 +
    6.62 +UCX_TEST(test_ucx_array_append) {
    6.63 +    UcxArray array = ucx_array_new(16, sizeof(int));
    6.64 +    
    6.65 +    int x = 42;
    6.66 +    ucx_array_append(&array, &x);
    6.67 +    UCX_TEST_BEGIN
    6.68 +    
    6.69 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
    6.70 +    
    6.71 +    x = 13;
    6.72 +    ucx_array_append(&array, &x);
    6.73 +    
    6.74 +    UCX_TEST_ASSERT(array.size == 2, "incorrect size after append");
    6.75 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
    6.76 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
    6.77 +            "append corrupted previously inserted data");
    6.78 +    
    6.79 +    ucx_array_append(&array, NULL);
    6.80 +    
    6.81 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append");
    6.82 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed");
    6.83 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
    6.84 +            "NULL append corrupted previously inserted data");
    6.85 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
    6.86 +            "NULL append corrupted previously inserted data");
    6.87 +    
    6.88 +    UCX_TEST_END
    6.89 +    
    6.90 +    ucx_array_free(&array);
    6.91 +}
    6.92 +
    6.93 +UCX_TEST(test_ucx_array_prepend) {
    6.94 +    UcxArray array = ucx_array_new(16, sizeof(int));
    6.95 +    
    6.96 +    int x = 42;
    6.97 +    ucx_array_prepend(&array, &x);
    6.98 +    UCX_TEST_BEGIN
    6.99 +    
   6.100 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
   6.101 +    
   6.102 +    x = 13;
   6.103 +    ucx_array_prepend(&array, &x);
   6.104 +    
   6.105 +    UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend");
   6.106 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed");
   6.107 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42,
   6.108 +            "prepend corrupted previously inserted data");
   6.109 +    
   6.110 +    ucx_array_prepend(&array, NULL);
   6.111 +    
   6.112 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend");
   6.113 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed");
   6.114 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
   6.115 +            "NULL prepend corrupted previously inserted data");
   6.116 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42,
   6.117 +            "NULL prepend corrupted previously inserted data");
   6.118 +    
   6.119 +    UCX_TEST_END
   6.120 +    
   6.121 +    ucx_array_free(&array);
   6.122 +}
   6.123 +
   6.124 +UCX_TEST(test_ucx_array_equals) {
   6.125 +    UcxArray a1 = ucx_array_new(16, sizeof(int));
   6.126 +    UcxArray a2 = ucx_array_new(16, sizeof(int));
   6.127 +    UcxArray a3 = ucx_array_new(16, sizeof(long int));
   6.128 +    UcxArray a4 = ucx_array_new(16, sizeof(int));
   6.129 +    
   6.130 +    a1.size = 5;
   6.131 +    ucx_array_at_int(a1, 0) = 47;
   6.132 +    ucx_array_at_int(a1, 1) = 11;
   6.133 +    ucx_array_at_int(a1, 2) = 0;
   6.134 +    ucx_array_at_int(a1, 3) = 8;
   6.135 +    ucx_array_at_int(a1, 4) = 15;
   6.136 +    a2.size = 5;
   6.137 +    ucx_array_at_int(a2, 0) = 47;
   6.138 +    ucx_array_at_int(a2, 1) = 11;
   6.139 +    ucx_array_at_int(a2, 2) = 0;
   6.140 +    ucx_array_at_int(a2, 3) = 8;
   6.141 +    ucx_array_at_int(a2, 4) = 15;
   6.142 +    a3.size = 5;
   6.143 +    ucx_array_at_longint(a3, 0) = 47;
   6.144 +    ucx_array_at_longint(a3, 1) = 11;
   6.145 +    ucx_array_at_longint(a3, 2) = 0;
   6.146 +    ucx_array_at_longint(a3, 3) = 8;
   6.147 +    ucx_array_at_longint(a3, 4) = 15;
   6.148 +    a4.size = 5;
   6.149 +    ucx_array_at_int(a4, 0) = 47;
   6.150 +    ucx_array_at_int(a4, 1) = 11;
   6.151 +    ucx_array_at_int(a4, 2) = -6;
   6.152 +    ucx_array_at_int(a4, 3) = 8;
   6.153 +    ucx_array_at_int(a4, 4) = 15;
   6.154 +    
   6.155 +    UCX_TEST_BEGIN
   6.156 +    
   6.157 +    UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int, NULL) == 0, "failed");
   6.158 +    UCX_TEST_ASSERT(ucx_array_equals(a1, a4, ucx_cmp_int, NULL) > 0, "failed");
   6.159 +    UCX_TEST_ASSERT(ucx_array_equals(a4, a1, ucx_cmp_int, NULL) < 0, "failed");
   6.160 +    UCX_TEST_ASSERT(ucx_array_equals(a1, a3, ucx_cmp_int, NULL) < 0,
   6.161 +            "comparing arrays of different element size failed");
   6.162 +    UCX_TEST_ASSERT(ucx_array_equals(a3, a1, ucx_cmp_int, NULL) > 0,
   6.163 +            "comparing arrays of different element size failed");
   6.164 +    
   6.165 +    UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL) == 0,
   6.166 +            "compare using memcmp() failed");
   6.167 +    UCX_TEST_ASSERT(ucx_array_equals(a1, a4, NULL, NULL) > 0,
   6.168 +            "compare using memcmp() failed");
   6.169 +    UCX_TEST_ASSERT(ucx_array_equals(a4, a1, NULL, NULL) < 0,
   6.170 +            "compare using memcmp() failed");
   6.171 +    
   6.172 +    UCX_TEST_END
   6.173 +    ucx_array_free(&a1);
   6.174 +    ucx_array_free(&a2);
   6.175 +    ucx_array_free(&a3);
   6.176 +    ucx_array_free(&a4);
   6.177 +}
   6.178 +
   6.179 +UCX_TEST(test_ucx_array_concat) {
   6.180 +    UcxArray a1 = ucx_array_new(16, sizeof(int));
   6.181 +    UcxArray a2 = ucx_array_new(16, sizeof(int));
   6.182 +    
   6.183 +    a1.size = 2;
   6.184 +    ucx_array_at_int(a1, 0) = 47;
   6.185 +    ucx_array_at_int(a1, 1) = 11;
   6.186 +    a2.size = 3;
   6.187 +    ucx_array_at_int(a2, 0) = 0;
   6.188 +    ucx_array_at_int(a2, 1) = 8;
   6.189 +    ucx_array_at_int(a2, 2) = 15;
   6.190 +    
   6.191 +    UCX_TEST_BEGIN
   6.192 +    
   6.193 +    UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed");
   6.194 +    UCX_TEST_ASSERT(a1.size == 5, "failed");
   6.195 +    UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed");
   6.196 +    UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed");
   6.197 +    UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed");
   6.198 +    UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed");
   6.199 +    UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed");
   6.200 +    
   6.201 +    a1.elemsize *= 2;
   6.202 +    UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2),
   6.203 +            "arrays of different element size must not be concatenated");
   6.204 +    UCX_TEST_ASSERT(a1.size == 5,
   6.205 +            "arrays of different element size must not be concatenated");
   6.206 +    
   6.207 +    UCX_TEST_END
   6.208 +    ucx_array_free(&a1);
   6.209 +    ucx_array_free(&a2);    
   6.210 +}
   6.211 +
   6.212 +UCX_TEST(test_ucx_array_at) {
   6.213 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.214 +    
   6.215 +    int x = 42;
   6.216 +    ucx_array_append(&array, &x);
   6.217 +    x = 13;
   6.218 +    ucx_array_append(&array, &x);
   6.219 +    x = 5;
   6.220 +    ucx_array_append(&array, &x);
   6.221 +    
   6.222 +    UCX_TEST_BEGIN
   6.223 +    
   6.224 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
   6.225 +    ucx_array_at_int(array, 1) = 80;
   6.226 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed");
   6.227 +    
   6.228 +    
   6.229 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data");
   6.230 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data");
   6.231 +    
   6.232 +    UCX_TEST_END
   6.233 +    
   6.234 +    ucx_array_free(&array);
   6.235 +}
   6.236 +
   6.237 +UCX_TEST(test_ucx_array_find) {
   6.238 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.239 +    
   6.240 +    array.size = 5;
   6.241 +    ucx_array_at_int(array, 0) = 47;
   6.242 +    ucx_array_at_int(array, 1) = 11;
   6.243 +    ucx_array_at_int(array, 2) = 0;
   6.244 +    ucx_array_at_int(array, 3) = 8;
   6.245 +    ucx_array_at_int(array, 4) = 15;
   6.246 +    
   6.247 +    int x = 8;
   6.248 +    int y = 90;
   6.249 +    
   6.250 +    UCX_TEST_BEGIN
   6.251 +    
   6.252 +    UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3,
   6.253 +        "doesn't find element");
   6.254 +    UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5,
   6.255 +        "finds non-existing element");
   6.256 +    
   6.257 +    UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3,
   6.258 +        "failed using memcmp()");
   6.259 +    UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5,
   6.260 +        "failed using memcmp()");
   6.261 +    
   6.262 +    UCX_TEST_END
   6.263 +    ucx_array_free(&array);
   6.264 +}
   6.265 +
   6.266 +UCX_TEST(test_ucx_array_contains) {
   6.267 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.268 +    
   6.269 +    array.size = 5;
   6.270 +    ucx_array_at_int(array, 0) = 47;
   6.271 +    ucx_array_at_int(array, 1) = 11;
   6.272 +    ucx_array_at_int(array, 2) = 0;
   6.273 +    ucx_array_at_int(array, 3) = 8;
   6.274 +    ucx_array_at_int(array, 4) = 15;
   6.275 +    
   6.276 +    int x = 8;
   6.277 +    int y = 90;
   6.278 +    
   6.279 +    UCX_TEST_BEGIN
   6.280 +    
   6.281 +    UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL),
   6.282 +        "false negative");
   6.283 +    UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL),
   6.284 +        "false positive");
   6.285 +    
   6.286 +    UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL),
   6.287 +        "false negative using memcmp()");
   6.288 +    UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL),
   6.289 +        "false positive using memcmp()");
   6.290 +    
   6.291 +    UCX_TEST_END
   6.292 +    ucx_array_free(&array);
   6.293 +}
   6.294 +
   6.295 +UCX_TEST(test_ucx_array_remove) {
   6.296 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.297 +    
   6.298 +    array.size = 5;
   6.299 +    ucx_array_at_int(array, 0) = 47;
   6.300 +    ucx_array_at_int(array, 1) = 11;
   6.301 +    ucx_array_at_int(array, 2) = 0;
   6.302 +    ucx_array_at_int(array, 3) = 8;
   6.303 +    ucx_array_at_int(array, 4) = 15;
   6.304 +        
   6.305 +    UCX_TEST_BEGIN
   6.306 +    
   6.307 +    ucx_array_remove(&array, 2);
   6.308 +    UCX_TEST_ASSERT(
   6.309 +            ucx_array_at_int(array, 0) == 47 &&
   6.310 +            ucx_array_at_int(array, 1) == 11 &&
   6.311 +            ucx_array_at_int(array, 2) == 8 &&
   6.312 +            ucx_array_at_int(array, 3) == 15,
   6.313 +            "wrong contents after remove");
   6.314 +    UCX_TEST_ASSERT(array.size == 4, "wrong size after remove");
   6.315 +    
   6.316 +    ucx_array_remove_fast(&array, 1);
   6.317 +    UCX_TEST_ASSERT(
   6.318 +            ucx_array_at_int(array, 0) == 47 &&
   6.319 +            ucx_array_at_int(array, 1) == 15 &&
   6.320 +            ucx_array_at_int(array, 2) == 8,
   6.321 +            "wrong contents after fast remove");
   6.322 +    UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove");
   6.323 +    
   6.324 +    UCX_TEST_END
   6.325 +    ucx_array_free(&array);
   6.326 +}
   6.327 +
   6.328 +UCX_TEST(test_ucx_array_clone) {
   6.329 +   
   6.330 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.331 +    
   6.332 +    array.size = 5;
   6.333 +    ucx_array_at_int(array, 0) = 47;
   6.334 +    ucx_array_at_int(array, 1) = 11;
   6.335 +    ucx_array_at_int(array, 2) = 0;
   6.336 +    ucx_array_at_int(array, 3) = 8;
   6.337 +    ucx_array_at_int(array, 4) = 15;
   6.338 +    
   6.339 +    UcxArray copy = ucx_array_clone(array);
   6.340 +    UCX_TEST_BEGIN
   6.341 +
   6.342 +    UCX_TEST_ASSERT(array.data != copy.data, "no true copy");
   6.343 +    UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed");
   6.344 +    UCX_TEST_ASSERT(array.size == copy.size, "size mismatch");
   6.345 +    UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch");
   6.346 +    UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch");
   6.347 +    UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch");
   6.348 +    
   6.349 +    UCX_TEST_END
   6.350 +
   6.351 +    ucx_array_free(&array);
   6.352 +    ucx_array_free(&copy);
   6.353 +}
   6.354 +
   6.355 +UCX_TEST(test_ucx_array_sort) {
   6.356 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.357 +    array.size = 5;
   6.358 +    ucx_array_at_int(array, 0) = 47;
   6.359 +    ucx_array_at_int(array, 1) = 11;
   6.360 +    ucx_array_at_int(array, 2) = 0;
   6.361 +    ucx_array_at_int(array, 3) = 8;
   6.362 +    ucx_array_at_int(array, 4) = 15;
   6.363 +    
   6.364 +    UcxArray expected = ucx_array_new(16, sizeof(int));
   6.365 +    expected.size = 5;
   6.366 +    ucx_array_at_int(expected, 0) = 0;
   6.367 +    ucx_array_at_int(expected, 1) = 8;
   6.368 +    ucx_array_at_int(expected, 2) = 11;
   6.369 +    ucx_array_at_int(expected, 3) = 15;
   6.370 +    ucx_array_at_int(expected, 4) = 47;
   6.371 +    
   6.372 +
   6.373 +    UCX_TEST_BEGIN
   6.374 +    void* original_ptr = array.data;
   6.375 +    UCX_TEST_ASSERT(!ucx_array_sort(array, ucx_cmp_int, NULL), "failed");
   6.376 +    UCX_TEST_ASSERT(!ucx_array_equals(array, expected, NULL, NULL), "failed");
   6.377 +    UCX_TEST_ASSERT(array.size == 5, "size corrupted");
   6.378 +    UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate");
   6.379 +    UCX_TEST_END
   6.380 +
   6.381 +    ucx_array_free(&expected);
   6.382 +    ucx_array_free(&array);
   6.383 +}
   6.384 +
   6.385 +UCX_TEST(test_ucx_array_autogrow) {
   6.386 +    UcxArray array = ucx_array_new(4, sizeof(int));
   6.387 +    array.size = 3;
   6.388 +    ucx_array_at_int(array, 0) = 47;
   6.389 +    ucx_array_at_int(array, 1) = 11;
   6.390 +    int x = 5;
   6.391 +    
   6.392 +    UCX_TEST_BEGIN
   6.393 +
   6.394 +    void* oldptr = array.data;
   6.395 +    
   6.396 +    ucx_array_append(&array, &x);
   6.397 +    UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
   6.398 +            "array should not grow too early");
   6.399 +    ucx_array_append(&array, &x);
   6.400 +    UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
   6.401 +    UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
   6.402 +    UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 &&
   6.403 +        ucx_array_at_int(array, 3) == 5, "corrupt data");
   6.404 +    
   6.405 +    UCX_TEST_END
   6.406 +    ucx_array_free(&array);
   6.407 +}
   6.408 +
   6.409 +UCX_TEST(test_ucx_array_shrink) {
   6.410 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.411 +    array.size = 4;
   6.412 +    
   6.413 +    UCX_TEST_BEGIN
   6.414 +    UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed");
   6.415 +    UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink");
   6.416 +    UCX_TEST_END
   6.417 +    ucx_array_free(&array);
   6.418 +}
   6.419 +
   6.420 +UCX_TEST(test_ucx_array_resize) {
   6.421 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.422 +    array.size = 8;
   6.423 +    
   6.424 +    UCX_TEST_BEGIN
   6.425 +
   6.426 +    UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed");
   6.427 +    UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize");
   6.428 +    UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize");
   6.429 +    
   6.430 +    UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed");
   6.431 +    UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize");
   6.432 +    UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize");
   6.433 +    
   6.434 +    UCX_TEST_END
   6.435 +    ucx_array_free(&array);
   6.436 +}
   6.437 +
   6.438 +UCX_TEST(test_ucx_array_reserve) {
   6.439 +    UcxArray array = ucx_array_new(16, sizeof(int));
   6.440 +    
   6.441 +    UCX_TEST_BEGIN
   6.442 +
   6.443 +    UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed");
   6.444 +    UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink");
   6.445 +            
   6.446 +    UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed");
   6.447 +    UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve");    
   6.448 +    
   6.449 +    UCX_TEST_END
   6.450 +    ucx_array_free(&array);
   6.451 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/array_tests.h	Thu Jul 04 20:07:31 2019 +0200
     7.3 @@ -0,0 +1,61 @@
     7.4 +/*
     7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6 + *
     7.7 + * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     7.8 + *
     7.9 + * Redistribution and use in source and binary forms, with or without
    7.10 + * modification, are permitted provided that the following conditions are met:
    7.11 + *
    7.12 + *   1. Redistributions of source code must retain the above copyright
    7.13 + *      notice, this list of conditions and the following disclaimer.
    7.14 + *
    7.15 + *   2. Redistributions in binary form must reproduce the above copyright
    7.16 + *      notice, this list of conditions and the following disclaimer in the
    7.17 + *      documentation and/or other materials provided with the distribution.
    7.18 + *
    7.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    7.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    7.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    7.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    7.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    7.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    7.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    7.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    7.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    7.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    7.29 + * POSSIBILITY OF SUCH DAMAGE.
    7.30 + */
    7.31 +
    7.32 +#ifndef ARRAY_TESTS_H
    7.33 +#define	ARRAY_TESTS_H
    7.34 +
    7.35 +#include <ucx/array.h>
    7.36 +#include <ucx/test.h>
    7.37 +
    7.38 +#ifdef	__cplusplus
    7.39 +extern "C" {
    7.40 +#endif
    7.41 +
    7.42 +UCX_TEST(test_ucx_array_free);
    7.43 +UCX_TEST(test_ucx_array_new);
    7.44 +UCX_TEST(test_ucx_array_at);
    7.45 +UCX_TEST(test_ucx_array_append);
    7.46 +UCX_TEST(test_ucx_array_prepend);
    7.47 +UCX_TEST(test_ucx_array_autogrow);
    7.48 +UCX_TEST(test_ucx_array_equals);
    7.49 +UCX_TEST(test_ucx_array_concat);
    7.50 +UCX_TEST(test_ucx_array_find);
    7.51 +UCX_TEST(test_ucx_array_contains);
    7.52 +UCX_TEST(test_ucx_array_remove);
    7.53 +UCX_TEST(test_ucx_array_clone);
    7.54 +UCX_TEST(test_ucx_array_sort);
    7.55 +UCX_TEST(test_ucx_array_shrink);
    7.56 +UCX_TEST(test_ucx_array_resize);
    7.57 +UCX_TEST(test_ucx_array_reserve);
    7.58 +
    7.59 +#ifdef	__cplusplus
    7.60 +}
    7.61 +#endif
    7.62 +
    7.63 +#endif	/* ARRAY_TESTS_H */
    7.64 +
     8.1 --- a/test/main.c	Fri Dec 28 17:20:23 2018 +0100
     8.2 +++ b/test/main.c	Thu Jul 04 20:07:31 2019 +0200
     8.3 @@ -33,6 +33,7 @@
     8.4  
     8.5  #include "main.h"
     8.6  
     8.7 +#include "array_tests.h"
     8.8  #include "allocator_tests.h"
     8.9  #include "logging_tests.h"
    8.10  #include "list_tests.h"
    8.11 @@ -141,6 +142,24 @@
    8.12          ucx_test_register(suite, test_ucx_logger_new);
    8.13          ucx_test_register(suite, test_ucx_logger_log);
    8.14          
    8.15 +        /* UcxArray Tests */
    8.16 +        ucx_test_register(suite, test_ucx_array_free);
    8.17 +        ucx_test_register(suite, test_ucx_array_new);
    8.18 +        ucx_test_register(suite, test_ucx_array_at);
    8.19 +        ucx_test_register(suite, test_ucx_array_append);
    8.20 +        ucx_test_register(suite, test_ucx_array_prepend);
    8.21 +        ucx_test_register(suite, test_ucx_array_autogrow);
    8.22 +        ucx_test_register(suite, test_ucx_array_equals);
    8.23 +        ucx_test_register(suite, test_ucx_array_concat);
    8.24 +        ucx_test_register(suite, test_ucx_array_find);
    8.25 +        ucx_test_register(suite, test_ucx_array_contains);
    8.26 +        ucx_test_register(suite, test_ucx_array_remove);
    8.27 +        ucx_test_register(suite, test_ucx_array_clone);
    8.28 +        ucx_test_register(suite, test_ucx_array_sort);
    8.29 +        ucx_test_register(suite, test_ucx_array_shrink);
    8.30 +        ucx_test_register(suite, test_ucx_array_resize);
    8.31 +        ucx_test_register(suite, test_ucx_array_reserve);
    8.32 +        
    8.33          /* UcxList Tests */
    8.34          ucx_test_register(suite, test_ucx_list_append);
    8.35          ucx_test_register(suite, test_ucx_list_prepend);

mercurial