improves array append/prepend/set interface feature/array

Sat, 10 Aug 2019 11:12:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 10 Aug 2019 11:12:49 +0200
branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a

improves array append/prepend/set interface

src/array.c file | annotate | diff | comparison | revisions
src/ucx/array.h 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/array.c	Sat Aug 10 09:47:59 2019 +0200
     1.2 +++ b/src/array.c	Sat Aug 10 11:12:49 2019 +0200
     1.3 @@ -55,6 +55,18 @@
     1.4  #define ucx_array_sort_impl ucx_mergesort
     1.5  #endif
     1.6  
     1.7 +static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
     1.8 +    size_t required_capacity = array->capacity;
     1.9 +    while (reqcap > required_capacity) {
    1.10 +        if (required_capacity * 2 < required_capacity)
    1.11 +            return 1;
    1.12 +        required_capacity <<= 1;
    1.13 +    }
    1.14 +    if (ucx_array_reserve(array, required_capacity)) {
    1.15 +        return 1;
    1.16 +    }
    1.17 +}
    1.18 +
    1.19  UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
    1.20      return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
    1.21  }
    1.22 @@ -127,62 +139,84 @@
    1.23      array->capacity = array->size = 0;
    1.24  }
    1.25  
    1.26 -int ucx_array_append(UcxArray *array, void *data) {
    1.27 -    if (array->size == array->capacity) {
    1.28 -        if (ucx_array_reserve(array, array->capacity*2)) {
    1.29 -            return 1;
    1.30 -        }
    1.31 +int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
    1.32 +    if (ucx_array_ensurecap(array, array->size + count))
    1.33 +        return 1;
    1.34 +    
    1.35 +    void* dest = ucx_array_at(*array, array->size);
    1.36 +    if (data) {
    1.37 +        memcpy(dest, data, array->elemsize*count);
    1.38 +    } else {
    1.39 +        memset(dest, 0, array->elemsize*count);
    1.40 +    }
    1.41 +    array->size += count;
    1.42 +    
    1.43 +    return 0;
    1.44 +}
    1.45 +
    1.46 +int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
    1.47 +    if (ucx_array_ensurecap(array, array->size + count))
    1.48 +        return 1;
    1.49 +    
    1.50 +    if (array->size > 0) {
    1.51 +        void *dest = ucx_array_at(*array, count);
    1.52 +        memmove(dest, array->data, array->elemsize*array->size);
    1.53      }
    1.54      
    1.55 -    void* dest = ucx_array_at(*array, array->size++);
    1.56      if (data) {
    1.57 -        memcpy(dest, data, array->elemsize);
    1.58 +        memcpy(array->data, data, array->elemsize*count);
    1.59      } else {
    1.60 -        memset(dest, 0, array->elemsize);
    1.61 +        memset(array->data, 0, array->elemsize*count);
    1.62 +    }
    1.63 +    array->size += count;
    1.64 +        
    1.65 +    return 0;
    1.66 +}
    1.67 +
    1.68 +int ucx_array_set_from(UcxArray *array, size_t index,
    1.69 +        void *data, size_t count) {
    1.70 +    if (ucx_array_ensurecap(array, index + count))
    1.71 +        return 1;
    1.72 +    
    1.73 +    if (index+count > array->size) {
    1.74 +        array->size = index+count;
    1.75 +    }
    1.76 +    
    1.77 +    void *dest = ucx_array_at(*array, index);
    1.78 +    if (data) {
    1.79 +        memcpy(dest, data, array->elemsize*count);
    1.80 +    } else {
    1.81 +        memset(dest, 0, array->elemsize*count);
    1.82      }
    1.83      
    1.84      return 0;
    1.85  }
    1.86  
    1.87 -int ucx_array_prepend(UcxArray *array, void *data) {
    1.88 -    if (array->size == array->capacity) {
    1.89 -        if (ucx_array_reserve(array, array->capacity*2)) {
    1.90 -            return 1;
    1.91 -        }
    1.92 -    }
    1.93 -    
    1.94 -    array->size++;
    1.95 -    
    1.96 -    if (array->size > 1) {
    1.97 -        void *dest = ucx_array_at(*array,1);
    1.98 -        memmove(dest, array->data, array->elemsize*array->size);
    1.99 -    }
   1.100 -    
   1.101 -    if (data) {
   1.102 -        memcpy(array->data, data, array->elemsize);
   1.103 -    } else {
   1.104 -        memset(array->data, 0, array->elemsize);
   1.105 -    }
   1.106 -        
   1.107 -    return 0;
   1.108 +int ucx_array_appendv(UcxArray *array, ...) {
   1.109 +    va_list ap;
   1.110 +    va_start(ap, array);
   1.111 +    int elem = va_arg(ap, int);
   1.112 +    int ret = ucx_array_append_from(array, &elem, 1);
   1.113 +    va_end(ap);
   1.114 +    return ret;
   1.115  }
   1.116  
   1.117 -int ucx_array_set(UcxArray *array, size_t index, void *data) {
   1.118 -    if (index >= array->size) {
   1.119 -        if (ucx_array_reserve(array, index+1)) {
   1.120 -            return 1;
   1.121 -        }
   1.122 -        array->size = index+1;
   1.123 -    }
   1.124 -    
   1.125 -    void *dest = ucx_array_at(*array, index);
   1.126 -    if (data) {
   1.127 -        memcpy(dest, data, array->elemsize);
   1.128 -    } else {
   1.129 -        memset(dest, 0, array->elemsize);
   1.130 -    }
   1.131 -    
   1.132 -    return 0;
   1.133 +int ucx_array_prependv(UcxArray *array, ...) {
   1.134 +    va_list ap;
   1.135 +    va_start(ap, array);
   1.136 +    int elem = va_arg(ap, int);
   1.137 +    int ret = ucx_array_prepend_from(array, &elem, 1);
   1.138 +    va_end(ap);
   1.139 +    return ret;
   1.140 +}
   1.141 +
   1.142 +int ucx_array_setv(UcxArray *array, size_t index, ...) {
   1.143 +    va_list ap;
   1.144 +    va_start(ap, index);
   1.145 +    int elem = va_arg(ap, int);
   1.146 +    int ret = ucx_array_set_from(array, index, &elem, 1);
   1.147 +    va_end(ap);
   1.148 +    return ret;
   1.149  }
   1.150  
   1.151  int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
     2.1 --- a/src/ucx/array.h	Sat Aug 10 09:47:59 2019 +0200
     2.2 +++ b/src/ucx/array.h	Sat Aug 10 11:12:49 2019 +0200
     2.3 @@ -131,18 +131,86 @@
     2.4  void ucx_array_destroy(UcxArray *array);
     2.5  
     2.6  /**
     2.7 + * Inserts elements at the end of the array.
     2.8 + * 
     2.9 + * This is an O(1) operation.
    2.10 + * The array will automatically grow, if the capacity is exceeded.
    2.11 + * If a pointer to data is provided, the data is copied into the array with
    2.12 + * memcpy(). Otherwise the new elements are completely zeroed.
    2.13 + * 
    2.14 + * @param array a pointer the array where to append the data
    2.15 + * @param data a pointer to the data to insert (may be <code>NULL</code>)
    2.16 + * @param count number of elements to copy from data (if data is
    2.17 + * <code>NULL</code>, zeroed elements are appended)
    2.18 + * @return zero on success, non-zero if a reallocation was necessary but failed
    2.19 + * @see ucx_array_set_from()
    2.20 + * @see ucx_array_append()
    2.21 + */
    2.22 +int ucx_array_append_from(UcxArray *array, void *data, size_t count);
    2.23 +
    2.24 +
    2.25 +/**
    2.26 + * Inserts elements at the beginning of the array.
    2.27 + * 
    2.28 + * This is an expensive operation, because the contents must be moved.
    2.29 + * If there is no particular reason to prepend data, you should use
    2.30 + * ucx_array_append_from() instead.
    2.31 + * 
    2.32 + * @param array a pointer the array where to prepend the data
    2.33 + * @param data a pointer to the data to insert (may be <code>NULL</code>)
    2.34 + * @param count number of elements to copy from data (if data is
    2.35 + * <code>NULL</code>, zeroed elements are inserted)
    2.36 + * @return zero on success, non-zero if a reallocation was necessary but failed
    2.37 + * @see ucx_array_append_from()
    2.38 + * @see ucx_array_set_from()
    2.39 + * @see ucx_array_prepend()
    2.40 + */
    2.41 +int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
    2.42 +
    2.43 +
    2.44 +/**
    2.45 + * Sets elements starting at the specified index.
    2.46 + * 
    2.47 + * If the any index is out of bounds, the array automatically grows.
    2.48 + * The pointer to the data may be NULL, in which case the elements are zeroed. 
    2.49 + * 
    2.50 + * @param array a pointer the array where to set the data
    2.51 + * @param index the index of the element to set
    2.52 + * @param data a pointer to the data to insert (may be <code>NULL</code>)
    2.53 + * @param count number of elements to copy from data (if data is
    2.54 + * <code>NULL</code>, the memory in the array is zeroed)
    2.55 + * @return zero on success, non-zero if a reallocation was necessary but failed
    2.56 + * @see ucx_array_append_from()
    2.57 + * @see ucx_array_set()
    2.58 + */
    2.59 +int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
    2.60 +
    2.61 +/**
    2.62   * Inserts an element at the end of the array.
    2.63   * 
    2.64   * This is an O(1) operation.
    2.65   * The array will automatically grow, if the capacity is exceeded.
    2.66 - * If a pointer to data is provided, the data is copied into the array with
    2.67 - * memcpy(). Otherwise the new element is completely zeroed.
    2.68 + * If the type of the argument has a different size than the element size of
    2.69 + * this array, the behavior is undefined.
    2.70   * 
    2.71   * @param array a pointer the array where to append the data
    2.72 - * @param data a pointer to the data to insert (may be <code>NULL</code>)
    2.73 + * @param elem the value to insert
    2.74   * @return zero on success, non-zero if a reallocation was necessary but failed
    2.75 + * @see ucx_array_append_from()
    2.76 + * @see ucx_array_set()
    2.77   */
    2.78 -int ucx_array_append(UcxArray *array, void *data);
    2.79 +#define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
    2.80 +
    2.81 +/**
    2.82 + * For internal use.
    2.83 + * Use ucx_array_append()
    2.84 + * 
    2.85 + * @param array
    2.86 + * @param ... 
    2.87 + * @return 
    2.88 + * @see ucx_array_append()
    2.89 + */
    2.90 +int ucx_array_appendv(UcxArray *array, ...);
    2.91  
    2.92  
    2.93  /**
    2.94 @@ -153,24 +221,51 @@
    2.95   * ucx_array_append() instead.
    2.96   * 
    2.97   * @param array a pointer the array where to prepend the data
    2.98 - * @param data a pointer to the data to insert (may be <code>NULL</code>)
    2.99 + * @param elem the value to insert
   2.100   * @return zero on success, non-zero if a reallocation was necessary but failed
   2.101 + * @see ucx_array_append()
   2.102 + * @see ucx_array_set_from()
   2.103 + * @see ucx_array_prepend_from()
   2.104   */
   2.105 -int ucx_array_prepend(UcxArray *array, void *data);
   2.106 +#define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
   2.107 +
   2.108 +/**
   2.109 + * For internal use.
   2.110 + * Use ucx_array_prepend()
   2.111 + * 
   2.112 + * @param array
   2.113 + * @param ... 
   2.114 + * @return 
   2.115 + * @see ucx_array_prepend()
   2.116 + */
   2.117 +int ucx_array_prependv(UcxArray *array, ...);
   2.118  
   2.119  
   2.120  /**
   2.121   * Sets an element at the specified index.
   2.122   * 
   2.123 - * If the index is out of bounds, the array automatically grows.
   2.124 - * The pointer to the data may be NULL, in which case the element is zeroed. 
   2.125 + * If the any index is out of bounds, the array automatically grows.
   2.126   * 
   2.127   * @param array a pointer the array where to set the data
   2.128   * @param index the index of the element to set
   2.129 - * @param data a pointer to the data to insert (may be <code>NULL</code>)
   2.130 + * @param elem the value to set
   2.131   * @return zero on success, non-zero if a reallocation was necessary but failed
   2.132 + * @see ucx_array_append()
   2.133 + * @see ucx_array_set_from()
   2.134   */
   2.135 -int ucx_array_set(UcxArray *array, size_t index, void *data);
   2.136 +#define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
   2.137 +
   2.138 +/**
   2.139 + * For internal use.
   2.140 + * Use ucx_array_set()
   2.141 + * 
   2.142 + * @param array
   2.143 + * @param index
   2.144 + * @param ... 
   2.145 + * @return 
   2.146 + * @see ucx_array_set()
   2.147 + */
   2.148 +int ucx_array_setv(UcxArray *array, size_t index, ...);
   2.149  
   2.150  /**
   2.151   * Concatenates two arrays.
     3.1 --- a/test/array_tests.c	Sat Aug 10 09:47:59 2019 +0200
     3.2 +++ b/test/array_tests.c	Sat Aug 10 11:12:49 2019 +0200
     3.3 @@ -56,35 +56,138 @@
     3.4      ucx_array_destroy(&array);
     3.5  }
     3.6  
     3.7 -UCX_TEST(test_ucx_array_append) {
     3.8 +UCX_TEST(test_ucx_array_append_from) {
     3.9      UcxArray array = ucx_array_new(16, sizeof(int));
    3.10      int *elements;
    3.11      
    3.12      int x = 42;
    3.13 -    ucx_array_append(&array, &x);
    3.14 +    ucx_array_append_from(&array, &x, 1);
    3.15      UCX_TEST_BEGIN
    3.16      
    3.17      elements = array.data;
    3.18      UCX_TEST_ASSERT(elements[0] == 42, "failed");
    3.19      
    3.20 -    x = 13;
    3.21 -    ucx_array_append(&array, &x);
    3.22 +    int y[2] = {13, 37};
    3.23 +    ucx_array_append_from(&array, y, 2);
    3.24      
    3.25      elements = array.data;
    3.26 -    UCX_TEST_ASSERT(array.size == 2, "incorrect size after append");
    3.27 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
    3.28      UCX_TEST_ASSERT(elements[1] == 13, "failed");
    3.29 +    UCX_TEST_ASSERT(elements[2] == 37, "failed");
    3.30      UCX_TEST_ASSERT(elements[0] == 42,
    3.31              "append corrupted previously inserted data");
    3.32      
    3.33 -    ucx_array_append(&array, NULL);
    3.34 +    ucx_array_append_from(&array, NULL, 2);
    3.35      
    3.36      elements = array.data;
    3.37 -    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append");
    3.38 -    UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed");
    3.39 +    UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL append");
    3.40 +    UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed");
    3.41 +    UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed");
    3.42      UCX_TEST_ASSERT(elements[0] == 42,
    3.43              "NULL append corrupted previously inserted data");
    3.44      UCX_TEST_ASSERT(elements[1] == 13,
    3.45              "NULL append corrupted previously inserted data");
    3.46 +    UCX_TEST_ASSERT(elements[2] == 37,
    3.47 +            "NULL append corrupted previously inserted data");
    3.48 +    
    3.49 +    UCX_TEST_END
    3.50 +    
    3.51 +    ucx_array_destroy(&array);
    3.52 +}
    3.53 +
    3.54 +UCX_TEST(test_ucx_array_prepend_from) {
    3.55 +    int *elems;
    3.56 +    UcxArray array = ucx_array_new(16, sizeof(int));
    3.57 +    
    3.58 +    int x = 42;
    3.59 +    ucx_array_prepend_from(&array, &x, 1);
    3.60 +    UCX_TEST_BEGIN
    3.61 +    
    3.62 +    elems = array.data;
    3.63 +    UCX_TEST_ASSERT(elems[0] == 42, "failed");
    3.64 +    
    3.65 +    int y[2] = {13, 37};
    3.66 +    ucx_array_prepend_from(&array, y, 2);
    3.67 +    
    3.68 +    elems = array.data;
    3.69 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
    3.70 +    UCX_TEST_ASSERT(elems[0] == 13, "failed");
    3.71 +    UCX_TEST_ASSERT(elems[1] == 37, "failed");
    3.72 +    UCX_TEST_ASSERT(elems[2] == 42,
    3.73 +            "prepend corrupted previously inserted data");
    3.74 +    
    3.75 +    ucx_array_prepend_from(&array, NULL, 2);
    3.76 +    
    3.77 +    elems = array.data;
    3.78 +    UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL prepend");
    3.79 +    UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
    3.80 +    UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed");
    3.81 +    UCX_TEST_ASSERT(elems[2] == 13,
    3.82 +            "NULL prepend corrupted previously inserted data");
    3.83 +    UCX_TEST_ASSERT(elems[3] == 37,
    3.84 +            "NULL prepend corrupted previously inserted data");
    3.85 +    UCX_TEST_ASSERT(elems[4] == 42,
    3.86 +            "NULL prepend corrupted previously inserted data");
    3.87 +    
    3.88 +    UCX_TEST_END
    3.89 +    
    3.90 +    ucx_array_destroy(&array);
    3.91 +}
    3.92 +
    3.93 +UCX_TEST(test_ucx_array_set_from) {
    3.94 +    int *elems;
    3.95 +    UcxArray array = ucx_array_new(16, sizeof(int));
    3.96 +    
    3.97 +    int x = 42;
    3.98 +
    3.99 +    UCX_TEST_BEGIN
   3.100 +
   3.101 +    ucx_array_set_from(&array, 7, &x, 1);
   3.102 +    
   3.103 +    elems = array.data;
   3.104 +    UCX_TEST_ASSERT(elems[7] == 42, "failed");
   3.105 +    UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
   3.106 +    UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
   3.107 +    
   3.108 +    int y[2] = {13, 37};
   3.109 +    ucx_array_set_from(&array, 27, y, 2);
   3.110 +    
   3.111 +    elems = array.data;
   3.112 +    UCX_TEST_ASSERT(elems[27] == 13, "failed");
   3.113 +    UCX_TEST_ASSERT(elems[28] == 37, "failed");
   3.114 +    UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
   3.115 +    UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
   3.116 +    
   3.117 +    ucx_array_set_from(&array, 7, NULL, 2);
   3.118 +    
   3.119 +    elems = array.data;
   3.120 +    UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
   3.121 +    UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set");
   3.122 +    
   3.123 +    UCX_TEST_END
   3.124 +    
   3.125 +    ucx_array_destroy(&array);
   3.126 +}
   3.127 +
   3.128 +UCX_TEST(test_ucx_array_append) {
   3.129 +    UcxArray array = ucx_array_new(16, sizeof(int));
   3.130 +    int *elements;
   3.131 +    
   3.132 +    ucx_array_append(&array, 42);
   3.133 +    UCX_TEST_BEGIN
   3.134 +    
   3.135 +    elements = array.data;
   3.136 +    UCX_TEST_ASSERT(elements[0] == 42, "failed");
   3.137 +    
   3.138 +    ucx_array_append(&array, 13);
   3.139 +    ucx_array_append(&array, 37);
   3.140 +    
   3.141 +    elements = array.data;
   3.142 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
   3.143 +    UCX_TEST_ASSERT(elements[1] == 13, "failed");
   3.144 +    UCX_TEST_ASSERT(elements[2] == 37, "failed");
   3.145 +    UCX_TEST_ASSERT(elements[0] == 42,
   3.146 +            "append corrupted previously inserted data");
   3.147      
   3.148      UCX_TEST_END
   3.149      
   3.150 @@ -95,32 +198,22 @@
   3.151      int *elems;
   3.152      UcxArray array = ucx_array_new(16, sizeof(int));
   3.153      
   3.154 -    int x = 42;
   3.155 -    ucx_array_prepend(&array, &x);
   3.156 +    ucx_array_prepend(&array, 42);
   3.157      UCX_TEST_BEGIN
   3.158      
   3.159      elems = array.data;
   3.160      UCX_TEST_ASSERT(elems[0] == 42, "failed");
   3.161      
   3.162 -    x = 13;
   3.163 -    ucx_array_prepend(&array, &x);
   3.164 +    ucx_array_prepend(&array, 37);
   3.165 +    ucx_array_prepend(&array, 13);
   3.166      
   3.167      elems = array.data;
   3.168 -    UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend");
   3.169 +    UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
   3.170      UCX_TEST_ASSERT(elems[0] == 13, "failed");
   3.171 -    UCX_TEST_ASSERT(elems[1] == 42,
   3.172 +    UCX_TEST_ASSERT(elems[1] == 37, "failed");
   3.173 +    UCX_TEST_ASSERT(elems[2] == 42,
   3.174              "prepend corrupted previously inserted data");
   3.175      
   3.176 -    ucx_array_prepend(&array, NULL);
   3.177 -    
   3.178 -    elems = array.data;
   3.179 -    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend");
   3.180 -    UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
   3.181 -    UCX_TEST_ASSERT(elems[1] == 13,
   3.182 -            "NULL prepend corrupted previously inserted data");
   3.183 -    UCX_TEST_ASSERT(elems[2] == 42,
   3.184 -            "NULL prepend corrupted previously inserted data");
   3.185 -    
   3.186      UCX_TEST_END
   3.187      
   3.188      ucx_array_destroy(&array);
   3.189 @@ -129,32 +222,25 @@
   3.190  UCX_TEST(test_ucx_array_set) {
   3.191      int *elems;
   3.192      UcxArray array = ucx_array_new(16, sizeof(int));
   3.193 -    
   3.194 -    
   3.195 -    int x = 42;
   3.196  
   3.197      UCX_TEST_BEGIN
   3.198  
   3.199 -    ucx_array_set(&array, 7, &x);
   3.200 +    ucx_array_set(&array, 7, 42);
   3.201      
   3.202      elems = array.data;
   3.203      UCX_TEST_ASSERT(elems[7] == 42, "failed");
   3.204 -    UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
   3.205 +    UCX_TEST_ASSERT(array.size == 8, "array not resized on set");
   3.206      UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
   3.207      
   3.208 -    x = 13;
   3.209 -    ucx_array_set(&array, 27, &x);
   3.210 +    ucx_array_set(&array, 27, 13);
   3.211 +    ucx_array_set(&array, 28, 37);
   3.212      
   3.213      elems = array.data;
   3.214      UCX_TEST_ASSERT(elems[27] == 13, "failed");
   3.215 -    UCX_TEST_ASSERT(array.size == 28, "array not resized on set");
   3.216 -    UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown");
   3.217 -    
   3.218 -    ucx_array_set(&array, 7, NULL);
   3.219 -    
   3.220 -    elems = array.data;
   3.221 -    UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
   3.222 -    
   3.223 +    UCX_TEST_ASSERT(elems[28] == 37, "failed");
   3.224 +    UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
   3.225 +    UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
   3.226 +        
   3.227      UCX_TEST_END
   3.228      
   3.229      ucx_array_destroy(&array);
   3.230 @@ -260,12 +346,8 @@
   3.231  UCX_TEST(test_ucx_array_at) {
   3.232      UcxArray array = ucx_array_new(16, sizeof(int));
   3.233      
   3.234 -    int x = 42;
   3.235 -    ucx_array_append(&array, &x);
   3.236 -    x = 13;
   3.237 -    ucx_array_append(&array, &x);
   3.238 -    x = 5;
   3.239 -    ucx_array_append(&array, &x);
   3.240 +    int x[3] = {42, 13, 5};
   3.241 +    ucx_array_append_from(&array, x, 3);
   3.242      
   3.243      UCX_TEST_BEGIN
   3.244      
   3.245 @@ -484,10 +566,10 @@
   3.246  
   3.247      void* oldptr = array.data;
   3.248      
   3.249 -    ucx_array_append(&array, &x);
   3.250 +    ucx_array_append(&array, 5);
   3.251      UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
   3.252              "array should not grow too early");
   3.253 -    ucx_array_append(&array, &x);
   3.254 +    ucx_array_append(&array, 5);
   3.255      elems = array.data;
   3.256      UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
   3.257      UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
     4.1 --- a/test/array_tests.h	Sat Aug 10 09:47:59 2019 +0200
     4.2 +++ b/test/array_tests.h	Sat Aug 10 11:12:49 2019 +0200
     4.3 @@ -39,6 +39,9 @@
     4.4  UCX_TEST(test_ucx_array_free);
     4.5  UCX_TEST(test_ucx_array_new);
     4.6  UCX_TEST(test_ucx_array_at);
     4.7 +UCX_TEST(test_ucx_array_append_from);
     4.8 +UCX_TEST(test_ucx_array_prepend_from);
     4.9 +UCX_TEST(test_ucx_array_set_from);
    4.10  UCX_TEST(test_ucx_array_append);
    4.11  UCX_TEST(test_ucx_array_prepend);
    4.12  UCX_TEST(test_ucx_array_set);
     5.1 --- a/test/main.c	Sat Aug 10 09:47:59 2019 +0200
     5.2 +++ b/test/main.c	Sat Aug 10 11:12:49 2019 +0200
     5.3 @@ -146,6 +146,9 @@
     5.4          ucx_test_register(suite, test_ucx_array_free);
     5.5          ucx_test_register(suite, test_ucx_array_new);
     5.6          ucx_test_register(suite, test_ucx_array_at);
     5.7 +        ucx_test_register(suite, test_ucx_array_append_from);
     5.8 +        ucx_test_register(suite, test_ucx_array_prepend_from);
     5.9 +        ucx_test_register(suite, test_ucx_array_set_from);
    5.10          ucx_test_register(suite, test_ucx_array_append);
    5.11          ucx_test_register(suite, test_ucx_array_prepend);
    5.12          ucx_test_register(suite, test_ucx_array_set);

mercurial