m = money_create (5, "USD");
\layout Code
- fail_unless (money_amount(m) == 5,
+ fail_unless (money_amount (m) == 5,
\layout Code
"Amount not set correctly on creation");
\layout Code
- fail_unless (strcmp(money_currency(m),"USD") == 0,
+ fail_unless (strcmp (money_currency (m), "USD") == 0,
\layout Code
"Currency not set correctly on creation");
\layout Code
- money_free(m);
+ money_free (m);
\layout Code
}
The second test above can be rewritten as follows:
\layout Code
-if (strcmp(money_currency(m), "USD") != 0) {
+if (strcmp (money_currency (m), "USD") != 0) {
\layout Code
- fail ("Currency not set correctly on creation");
+ fail ("Currency not set correctly on creation");
\layout Code
}
\layout Standard
+For your convenience the "fail_unless" function also accepts NULL as the
+ msg argument and substitutes a suitable message for you.
+ So you could also write a test as follows:
+\layout Code
+
+fail_unless (money_amount (m) == 5, NULL);
+\layout Standard
+
+This is equivalent to the line:
+\layout Code
+
+fail_unless (money_amount (m) == 5, "Assertion 'money_amount (m) == 5' failed");
+\layout Standard
+
When we try to compile and run the test suite now, we get a whole host of
compilation errors.
It may seem a bit strange to deliberately write code that won't compile,
\layout Code
-Money *money_create(int amount, char *currency);
+Money *money_create (int amount, char *currency);
\layout Code
int money_amount (Money *m);
char *money_currency (Money *m);
\layout Code
-void money_free(Money *m);
+void money_free (Money *m);
\layout Standard
and our code now compiles, but fails to link, since we haven't implemented
#include "money.h"
\layout Code
-Money *money_create(int amount, char *currency)
+Money *money_create (int amount, char *currency)
\layout Code
{
}
\layout Code
-void money_free(Money *m)
+void money_free (Money *m)
\layout Code
{
{
\layout Code
- Suite *s = suite_create("Money");
+ Suite *s = suite_create ("Money");
\layout Code
- TCase *tc_core = tcase_create("Core");
+ TCase *tc_core = tcase_create ("Core");
\layout Code
int nf;
\layout Code
- Suite *s = money_suite();
+ Suite *s = money_suite ();
\layout Code
- SRunner *sr = srunner_create(s);
+ SRunner *sr = srunner_create (s);
\layout Code
srunner_run_all (sr, CK_NORMAL);
\layout Code
- nf = srunner_ntests_failed(sr);
+ nf = srunner_ntests_failed (sr);
\layout Code
- srunner_free(sr);
+ srunner_free (sr);
\layout Code
- suite_free(s);
+ suite_free (s);
\layout Code
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
E.g., if we wrote some test code as follows:
\layout Code
-stuff_that_works();
+stuff_that_works ();
\layout Code
-mark_point();
+mark_point ();
\layout Code
-stuff_that_dies();
+stuff_that_dies ();
\layout Standard
then the point returned will be that marked by mark_point.
money with amount 0:
\layout Code
-START_TEST(test_neg_create)
+START_TEST (test_neg_create)
\layout Code
{
\layout Code
- Money *m = money_create(-1, "USD");
+ Money *m = money_create (-1, "USD");
\layout Code
fail_unless (m == NULL, "NULL should be returned on attempt to create
END_TEST
\layout Code
-START_TEST(test_zero_create)
+START_TEST (test_zero_create)
\layout Code
{
\layout Code
- Money *m = money_create(0, "USD");
+ Money *m = money_create (0, "USD");
\layout Code
- fail_unless (money_amount(m) == 0, "Zero is a valid amount of money");
+ fail_unless (money_amount (m) == 0, "Zero is a valid amount of money");
\layout Code
Suite *money_suite (void) {
\layout Code
- Suite *s = suite_create("Money");
+ Suite *s = suite_create ("Money");
\layout Code
- TCase *tc_core = tcase_create("Core");
+ TCase *tc_core = tcase_create ("Core");
\layout Code
- TCase *tc_limits = tcase_create("Limits");
+ TCase *tc_limits = tcase_create ("Limits");
\layout Code
suite_add_tcase (s, tc_core);
void setup (void) {
\layout Code
- five_dollars = money_create(5, "USD");
+ five_dollars = money_create (5, "USD");
\layout Code
}
{
\layout Code
- fail_unless (money_amount(five_dollars) == 5,
+ fail_unless (money_amount (five_dollars) == 5,
\layout Code
"Amount not set correctly on creation");
\layout Code
- fail_unless (strcmp(money_currency(five_dollars),"USD") == 0,
+ fail_unless (strcmp (money_currency (five_dollars), "USD") == 0,
\layout Code
"Currency not set correctly on creation");
declares suite creation functions for all the module tests:
\layout Code
-Suite *make_sub_suite(void);
+Suite *make_sub_suite (void);
\layout Code
-Suite *make_sub2_suite(void);
+Suite *make_sub2_suite (void);
\layout Code
-Suite *make_master_suite(void);
+Suite *make_master_suite (void);
\layout Code
-Suite *make_list_suite(void);
+Suite *make_list_suite (void);
\layout Code
-Suite *make_msg_suite(void);
+Suite *make_msg_suite (void);
\layout Code
-Suite *make_log_suite(void);
+Suite *make_log_suite (void);
\layout Standard
The function srunner_add_suite is used to add additional suites to an SRunner.
SRunner *sr;
\layout Code
-sr = srunner_create (make_master_suite());
+sr = srunner_create (make_master_suite ());
\layout Code
-srunner_add_suite(sr, make_list_suite());
+srunner_add_suite (sr, make_list_suite ());
\layout Code
-srunner_add_suite(sr, make_msg_suite());
+srunner_add_suite (sr, make_msg_suite ());
\layout Code
-srunner_add_suite(sr, make_log_suite());
+srunner_add_suite (sr, make_log_suite ());
\layout Subsection
SRunner *sr;
\layout Code
-sr = srunner_create(make_s1_suite());
+sr = srunner_create (make_s1_suite ());
\layout Code
-srunner_add_suite(sr, make_s2_suite());
+srunner_add_suite (sr, make_s2_suite ());
\layout Code
-srunner_set_log(sr, "test.log");
+srunner_set_log (sr, "test.log");
\layout Code
-srunner_run_all(sr, CRNORMAL);
+srunner_run_all (sr, CRNORMAL);
\layout Standard
Check will write the results of the run to test.log.