2020-11-11 01:19:49 +03:00
|
|
|
#include "sc_array.h"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int example()
|
|
|
|
{
|
|
|
|
int *p;
|
2021-01-31 02:52:06 +03:00
|
|
|
int val;
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
sc_array_create(p, 0);
|
|
|
|
|
|
|
|
sc_array_add(p, 0);
|
|
|
|
sc_array_add(p, 1);
|
|
|
|
sc_array_add(p, 3);
|
|
|
|
|
|
|
|
printf("\nRemoving first element \n\n");
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del(p, 0);
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
printf("Capacity %zu \n", sc_array_cap(p));
|
|
|
|
printf("Element count %zu \n", sc_array_size(p));
|
|
|
|
|
|
|
|
for (int i = 0; i < sc_array_size(p); i++) {
|
|
|
|
printf("Elem = %d \n", p[i]);
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_foreach (p, val) {
|
|
|
|
printf("Elem = %d \n", val);
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_array_destroy(p);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const int *x = a;
|
|
|
|
const int *y = b;
|
|
|
|
|
|
|
|
return *x - *y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test1(void)
|
|
|
|
{
|
|
|
|
int *arr, total = 0;
|
|
|
|
|
|
|
|
sc_array_create(arr, 5);
|
|
|
|
sc_array_add(arr, 3);
|
|
|
|
sc_array_add(arr, 4);
|
|
|
|
sc_array_add(arr, 5);
|
|
|
|
|
|
|
|
assert(sc_array_size(arr) == 3);
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del(arr, 0);
|
2020-11-11 01:19:49 +03:00
|
|
|
assert(arr[0] == 4);
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del_last(arr);
|
2020-11-11 01:19:49 +03:00
|
|
|
assert(arr[0] == 4);
|
|
|
|
|
|
|
|
sc_array_add(arr, 1);
|
|
|
|
sc_array_add(arr, 3);
|
|
|
|
sc_array_add(arr, 2);
|
|
|
|
sc_array_add(arr, 0);
|
|
|
|
|
2020-11-17 04:58:36 +03:00
|
|
|
assert(sc_array_last(arr) == 0);
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_array_sort(arr, compare);
|
|
|
|
|
|
|
|
for (int i = 0; i < sc_array_size(arr); i++) {
|
|
|
|
total += arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(total == 10);
|
|
|
|
|
|
|
|
for (int i = 0; i < sc_array_size(arr); i++) {
|
|
|
|
assert(arr[i] == i);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
void test2()
|
|
|
|
{
|
|
|
|
int *arr;
|
|
|
|
int val;
|
2021-02-01 01:57:58 +03:00
|
|
|
bool b;
|
|
|
|
|
|
|
|
b = sc_array_create(arr, 0);
|
|
|
|
assert(b);
|
2021-01-31 02:52:06 +03:00
|
|
|
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
|
2021-02-01 01:57:58 +03:00
|
|
|
b = sc_array_create(arr, 2);
|
|
|
|
assert(b);
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
|
2021-02-01 01:57:58 +03:00
|
|
|
b = sc_array_create(arr, 2);
|
|
|
|
assert(b);
|
|
|
|
|
|
|
|
b = sc_array_add(arr, 1);
|
|
|
|
assert(b);
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
assert(val == 1);
|
|
|
|
}
|
|
|
|
sc_array_del_last(arr);
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:57:58 +03:00
|
|
|
b = sc_array_add(arr, 1);
|
|
|
|
assert(b == true);
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del_unordered(arr, 0);
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:19:23 +03:00
|
|
|
void bounds_test()
|
|
|
|
{
|
|
|
|
int *arr, total = 0;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
sc_array_create(arr, 2);
|
|
|
|
sc_array_add(arr, 3);
|
|
|
|
sc_array_add(arr, 4);
|
|
|
|
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
total += val;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(total == 7);
|
|
|
|
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
sc_array_create(arr, 0);
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
total += val;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_array_foreach (arr, val) {
|
|
|
|
total += val;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(total == 0);
|
|
|
|
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#ifdef SC_HAVE_WRAP
|
|
|
|
|
|
|
|
bool fail_realloc = false;
|
|
|
|
void *__real_realloc(void *p, size_t size);
|
|
|
|
void *__wrap_realloc(void *p, size_t n)
|
|
|
|
{
|
|
|
|
if (fail_realloc) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __real_realloc(p, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fail_test()
|
|
|
|
{
|
2020-11-29 19:37:24 +03:00
|
|
|
int tmp;
|
2020-11-11 01:19:49 +03:00
|
|
|
int *arr, total = 0;
|
|
|
|
|
|
|
|
assert(sc_array_create(arr, SIZE_MAX) == false);
|
|
|
|
assert(arr == NULL);
|
|
|
|
assert(sc_array_create(arr, 0) == true);
|
|
|
|
assert(arr != NULL);
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
assert(arr == NULL);
|
|
|
|
assert(sc_array_create(arr, 0) == true);
|
|
|
|
|
|
|
|
size_t count = SC_SIZE_MAX / sizeof(*arr);
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < count + 5; i++) {
|
|
|
|
success = sc_array_add(arr, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!success);
|
|
|
|
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
sc_array_create(arr, 0);
|
|
|
|
assert(sc_array_size(arr) == 0);
|
|
|
|
|
|
|
|
fail_realloc = true;
|
|
|
|
success = sc_array_add(arr, 0);
|
|
|
|
assert(!success);
|
|
|
|
|
|
|
|
fail_realloc = false;
|
|
|
|
success = sc_array_add(arr, 222);
|
|
|
|
assert(success);
|
|
|
|
sc_array_destroy(arr);
|
|
|
|
|
|
|
|
fail_realloc = true;
|
|
|
|
assert(sc_array_create(arr, 222) == false);
|
|
|
|
fail_realloc = false;
|
|
|
|
|
|
|
|
assert(sc_array_create(arr, 0) == true);
|
|
|
|
fail_realloc = true;
|
|
|
|
success = sc_array_add(arr, 222);
|
|
|
|
assert(!success);
|
|
|
|
fail_realloc = false;
|
|
|
|
|
|
|
|
sc_array_add(arr, 3);
|
|
|
|
sc_array_add(arr, 4);
|
|
|
|
sc_array_add(arr, 5);
|
|
|
|
|
|
|
|
assert(sc_array_size(arr) == 3);
|
|
|
|
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del(arr, 0);
|
2020-11-11 01:19:49 +03:00
|
|
|
assert(arr[0] == 4);
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del_last(arr);
|
2020-11-11 01:19:49 +03:00
|
|
|
assert(arr[0] == 4);
|
|
|
|
|
|
|
|
sc_array_add(arr, 1);
|
|
|
|
sc_array_add(arr, 3);
|
|
|
|
sc_array_add(arr, 2);
|
|
|
|
sc_array_add(arr, 0);
|
|
|
|
|
|
|
|
sc_array_sort(arr, compare);
|
|
|
|
|
|
|
|
for (int i = 0; i < sc_array_size(arr); i++) {
|
|
|
|
total += arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(total == 10);
|
|
|
|
|
|
|
|
for (int i = 0; i < sc_array_size(arr); i++) {
|
|
|
|
assert(arr[i] == i);
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:37:24 +03:00
|
|
|
total = 0;
|
2020-12-27 08:00:22 +03:00
|
|
|
sc_array_foreach (arr, tmp) {
|
|
|
|
total += tmp;
|
2020-11-29 19:37:24 +03:00
|
|
|
}
|
|
|
|
assert(total == 10);
|
|
|
|
|
2020-11-29 23:21:25 +03:00
|
|
|
sc_array_sort(arr, compare);
|
2021-01-31 02:52:06 +03:00
|
|
|
sc_array_del_unordered(arr, 0);
|
2020-11-29 23:21:25 +03:00
|
|
|
assert(arr[0] == 4);
|
|
|
|
assert(sc_array_size(arr) == 4);
|
2021-01-24 17:56:18 +03:00
|
|
|
sc_array_clear(arr);
|
|
|
|
assert(sc_array_size(arr) == 0);
|
|
|
|
sc_array_add(arr, 10);
|
|
|
|
assert(sc_array_size(arr) == 1);
|
|
|
|
assert(arr[0] == 10);
|
2020-11-29 23:21:25 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_array_destroy(arr);
|
|
|
|
}
|
2020-12-27 08:07:14 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#else
|
|
|
|
void fail_test(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
example();
|
|
|
|
test1();
|
2021-01-31 02:52:06 +03:00
|
|
|
test2();
|
2020-11-11 01:19:49 +03:00
|
|
|
fail_test();
|
2020-12-27 08:00:22 +03:00
|
|
|
bounds_test();
|
2020-12-27 08:34:10 +03:00
|
|
|
|
|
|
|
return 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|