check_money.c 2.4 KB

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