Blame doc/example/tests/check_money.6.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <check.h>
Packit 0b5880
#include "../src/money.h"
Packit 0b5880
Packit 0b5880
START_TEST(test_money_create)
Packit 0b5880
{
Packit 0b5880
    Money *m;
Packit 0b5880
Packit 0b5880
    m = money_create(5, "USD");
Packit 0b5880
    ck_assert_int_eq(money_amount(m), 5);
Packit 0b5880
    ck_assert_str_eq(money_currency(m), "USD");
Packit 0b5880
    money_free(m);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_money_create_neg)
Packit 0b5880
{
Packit 0b5880
    Money *m = money_create(-1, "USD");
Packit 0b5880
Packit 0b5880
    ck_assert_msg(m == NULL,
Packit 0b5880
                  "NULL should be returned on attempt to create with "
Packit 0b5880
                  "a negative amount");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_money_create_zero)
Packit 0b5880
{
Packit 0b5880
    Money *m = money_create(0, "USD");
Packit 0b5880
Packit 0b5880
    if (money_amount(m) != 0)
Packit 0b5880
    {
Packit 0b5880
        ck_abort_msg("Zero is a valid amount of money");
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
Suite * money_suite(void)
Packit 0b5880
{
Packit 0b5880
    Suite *s;
Packit 0b5880
    TCase *tc_core;
Packit 0b5880
    TCase *tc_limits;
Packit 0b5880
Packit 0b5880
    s = suite_create("Money");
Packit 0b5880
Packit 0b5880
    /* Core test case */
Packit 0b5880
    tc_core = tcase_create("Core");
Packit 0b5880
Packit 0b5880
    tcase_add_test(tc_core, test_money_create);
Packit 0b5880
    suite_add_tcase(s, tc_core);
Packit 0b5880
Packit 0b5880
    /* Limits test case */
Packit 0b5880
    tc_limits = tcase_create("Limits");
Packit 0b5880
Packit 0b5880
    tcase_add_test(tc_limits, test_money_create_neg);
Packit 0b5880
    tcase_add_test(tc_limits, test_money_create_zero);
Packit 0b5880
    suite_add_tcase(s, tc_limits);
Packit 0b5880
Packit 0b5880
    return s;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int main(void)
Packit 0b5880
{
Packit 0b5880
    int number_failed;
Packit 0b5880
    Suite *s;
Packit 0b5880
    SRunner *sr;
Packit 0b5880
Packit 0b5880
    s = money_suite();
Packit 0b5880
    sr = srunner_create(s);
Packit 0b5880
Packit 0b5880
    srunner_run_all(sr, CK_NORMAL);
Packit 0b5880
    number_failed = srunner_ntests_failed(sr);
Packit 0b5880
    srunner_free(sr);
Packit 0b5880
    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
Packit 0b5880
}