check_money.7.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <stdlib.h>
  21. #include <check.h>
  22. #include "../src/money.h"
  23. Money *five_dollars;
  24. void setup(void)
  25. {
  26. five_dollars = money_create(5, "USD");
  27. }
  28. void teardown(void)
  29. {
  30. money_free(five_dollars);
  31. }
  32. START_TEST(test_money_create)
  33. {
  34. ck_assert_int_eq(money_amount(five_dollars), 5);
  35. ck_assert_str_eq(money_currency(five_dollars), "USD");
  36. }
  37. END_TEST
  38. START_TEST(test_money_create_neg)
  39. {
  40. Money *m = money_create(-1, "USD");
  41. ck_assert_msg(m == NULL,
  42. "NULL should be returned on attempt to create with "
  43. "a negative amount");
  44. }
  45. END_TEST
  46. START_TEST(test_money_create_zero)
  47. {
  48. Money *m = money_create(0, "USD");
  49. if (money_amount(m) != 0)
  50. {
  51. ck_abort_msg("Zero is a valid amount of money");
  52. }
  53. }
  54. END_TEST
  55. Suite * money_suite(void)
  56. {
  57. Suite *s;
  58. TCase *tc_core;
  59. TCase *tc_limits;
  60. s = suite_create("Money");
  61. /* Core test case */
  62. tc_core = tcase_create("Core");
  63. tcase_add_checked_fixture(tc_core, setup, teardown);
  64. tcase_add_test(tc_core, test_money_create);
  65. suite_add_tcase(s, tc_core);
  66. /* Limits test case */
  67. tc_limits = tcase_create("Limits");
  68. tcase_add_test(tc_limits, test_money_create_neg);
  69. tcase_add_test(tc_limits, test_money_create_zero);
  70. suite_add_tcase(s, tc_limits);
  71. return s;
  72. }
  73. int main(void)
  74. {
  75. int number_failed;
  76. Suite *s;
  77. SRunner *sr;
  78. s = money_suite();
  79. sr = srunner_create(s);
  80. srunner_run_all(sr, CK_NORMAL);
  81. number_failed = srunner_ntests_failed(sr);
  82. srunner_free(sr);
  83. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  84. }