Skip to content

Commit

Permalink
add new function to sort (qsort) for now stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
murilobsd committed Mar 20, 2020
1 parent fd6fc5f commit 93ed731
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/capybara/serie_int32.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
long double (*sum)(serie_int32_t *);
double (*variance)(serie_int32_t *);
double (*std_dev)(serie_int32_t *);
void (*sort)(serie_int32_t *);
} serie_int32_ops;

/* serie_int32 */
Expand Down
24 changes: 23 additions & 1 deletion src/serie_int32.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "capybara/utils.h"

#define SERIE ((struct serie_int32_impl *)s)
#define N(x) (sizeof(x) / sizeof(x[0]))

/* implementation */
struct serie_int32_impl {
Expand Down Expand Up @@ -52,6 +53,8 @@ static double mean(serie_int32_t *);
static long double sum(serie_int32_t *);
static double variance(serie_int32_t *);
static double std_dev(serie_int32_t *);
static void sort(serie_int32_t *);
static int cmp(const void *, const void *);

static serie_int32_ops int32_ops = {
set_name,
Expand All @@ -67,9 +70,28 @@ static serie_int32_ops int32_ops = {
mean,
sum,
variance,
std_dev
std_dev,
sort
};

static void
sort(serie_int32_t *s)
{
const size_t sz = size(s);
const size_t szint = sizeof(int32_t);

qsort(SERIE->data, sz, szint, cmp);
}

static int
cmp(const void *a, const void *b)
{
const int32_t *ia = (const int32_t *)a;
const int32_t *ib = (const int32_t *)b;

return *ia - *ib;
}

static double
std_dev(serie_int32_t *s)
{
Expand Down
21 changes: 21 additions & 0 deletions test/test_serie_int32.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ test_sd_dev_serie_int32(void)
s->ops->free_serie(s);
}

void
test_sort_serie_int32(void)
{
serie_int32_t *s = serie_int32_new();
double std_dev = 0;

s->ops->add(s, 5);
s->ops->add(s, 2);
s->ops->add(s, 1);
s->ops->add(s, 4);

s->ops->sort(s);

TEST_ASSERT_EQUAL(1, *s->ops->get(s, 0));
TEST_ASSERT_EQUAL(5, *s->ops->get(s, 3));

s->ops->free_serie(s);
}



int
main(void)
Expand All @@ -227,6 +247,7 @@ main(void)
RUN_TEST(test_sum_serie_int32);
RUN_TEST(test_variance_serie_int32);
RUN_TEST(test_sd_dev_serie_int32);
RUN_TEST(test_sort_serie_int32);

return UNITY_END();
}

0 comments on commit 93ed731

Please sign in to comment.