check_money.6.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. START_TEST(test_money_create)
  24. {
  25. Money *m;
  26. m = money_create(5, "USD");
  27. ck_assert_int_eq(money_amount(m), 5);
  28. ck_assert_str_eq(money_currency(m), "USD");
  29. money_free(m);
  30. }
  31. END_TEST
  32. START_TEST(test_money_create_neg)
  33. {
  34. Money *m = money_create(-1, "USD");
  35. ck_assert_msg(m == NULL,
  36. "NULL should be returned on attempt to create with "
  37. "a negative amount");
  38. }
  39. END_TEST
  40. START_TEST(test_money_create_zero)
  41. {
  42. Money *m = money_create(0, "USD");
  43. if (money_amount(m) != 0)
  44. {
  45. ck_abort_msg("Zero is a valid amount of money");
  46. }
  47. }
  48. END_TEST
  49. Suite * money_suite(void)
  50. {
  51. Suite *s;
  52. TCase *tc_core;
  53. TCase *tc_limits;
  54. s = suite_create("Money");
  55. /* Core test case */
  56. tc_core = tcase_create("Core");
  57. tcase_add_test(tc_core, test_money_create);
  58. suite_add_tcase(s, tc_core);
  59. /* Limits test case */
  60. tc_limits = tcase_create("Limits");
  61. tcase_add_test(tc_limits, test_money_create_neg);
  62. tcase_add_test(tc_limits, test_money_create_zero);
  63. suite_add_tcase(s, tc_limits);
  64. return s;
  65. }
  66. int main(void)
  67. {
  68. int number_failed;
  69. Suite *s;
  70. SRunner *sr;
  71. s = money_suite();
  72. sr = srunner_create(s);
  73. srunner_run_all(sr, CK_NORMAL);
  74. number_failed = srunner_ntests_failed(sr);
  75. srunner_free(sr);
  76. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  77. }