Skip to content
Snippets Groups Projects
average.c 2.54 KiB
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
/* average.c
 *
 * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

Bruce Cowan's avatar
Bruce Cowan committed
#include <math.h>
#include <stdbool.h>
Bruce Cowan's avatar
Bruce Cowan committed
#include <stdio.h>
#include <stdlib.h>

Bruce Cowan's avatar
Bruce Cowan committed
#include <array.h>
Bruce Cowan's avatar
Bruce Cowan committed

#define INT_TO_PTR(x) ((void *) (size_t) (x))
#define PTR_TO_INT(x) ((int) (size_t) (x))

Bruce Cowan's avatar
Bruce Cowan committed
get_total (const Array *array,
           ValueFunc    func)
{
    double total = 0;
    size_t length = array_get_length (array);

    for (size_t i = 0; i < length; i++)
        total += func (array_get (array, i));

    return total;
}

static double
Bruce Cowan's avatar
Bruce Cowan committed
get_average (const Array *array,
Bruce Cowan's avatar
Bruce Cowan committed
    return get_total (array, func) / array_get_length (array);
Bruce Cowan's avatar
Bruce Cowan committed
static double
array_to_int (const void *data)
{
    return (double) PTR_TO_INT (data);
}

static Array *
create_test_array (size_t start,
                   size_t end)
{
    Array *array = array_new (NULL);

    for (size_t i = start; i < end; i++)
        array_add (array, INT_TO_PTR (i));

    return array;
}

Bruce Cowan's avatar
Bruce Cowan committed
static bool
is_close (double a,
          double b)
{
    return fabs (a - b) <= (1e-8 + 1e-5 * b); // Values from numpy
}

Bruce Cowan's avatar
Bruce Cowan committed
int
main (void)
{
Bruce Cowan's avatar
Bruce Cowan committed
    int start = 1;
    int end = 100;

    Array *array = create_test_array (start, end);

    int n = end - start;
    double expected_total = (n * (n+1)) / 2; // Only valid if starting from 1
    double expected_average = expected_total / n;

    double total = get_total (array, array_to_int);
    if (!is_close (total, expected_total))
    {
        fprintf (stderr, "Expected total %lf, value returned was %lf\n",
                 expected_total, total);
        return EXIT_FAILURE;
    }

    double average = get_average (array, array_to_int);
    if (!is_close (average, expected_average))
    {
        fprintf (stderr, "Expected average %lf, value returned was %lf\n",
                 expected_average, average);
        return EXIT_FAILURE;
    }
Bruce Cowan's avatar
Bruce Cowan committed

    return EXIT_SUCCESS;
}