📄 check.c
字号:
#include "config.h"#include <string.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include "check.h"#include "check_error.h"#include "check_list.h"#include "check_impl.h"#include "check_msg.h"int check_major_version = CHECK_MAJOR_VERSION;int check_minor_version = CHECK_MINOR_VERSION;int check_micro_version = CHECK_MICRO_VERSION;static int non_pass (int val);static Fixture *fixture_create (SFun fun, int ischecked);static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown, int ischecked);static void tr_init (TestResult *tr);static void suite_free (Suite *s);static void tcase_free (TCase *tc);Suite *suite_create (const char *name){ Suite *s; s = emalloc (sizeof(Suite)); /* freed in suite_free */ if (name == NULL) s->name = ""; else s->name = name; s->tclst = list_create(); return s;}static void suite_free (Suite *s){ List *l; if (s == NULL) return; l = s->tclst; for (list_front(l); !list_at_end(l); list_advance (l)) { tcase_free (list_val(l)); } list_free (s->tclst); free(s);}TCase *tcase_create (const char *name){ TCase *tc = emalloc (sizeof(TCase)); /*freed in tcase_free */ if (name == NULL) tc->name = ""; else tc->name = name; tc->tflst = list_create(); tc->unch_sflst = list_create(); tc->ch_sflst = list_create(); tc->unch_tflst = list_create(); tc->ch_tflst = list_create(); return tc;}static void tcase_free (TCase *tc){ list_apply (tc->tflst, free); list_apply (tc->unch_sflst, free); list_apply (tc->ch_sflst, free); list_apply (tc->unch_tflst, free); list_apply (tc->ch_tflst, free); list_free(tc->tflst); list_free(tc->unch_sflst); list_free(tc->ch_sflst); list_free(tc->unch_tflst); list_free(tc->ch_tflst); free(tc);}void suite_add_tcase (Suite *s, TCase *tc){ if (s == NULL || tc == NULL) return; list_add_end (s->tclst, tc);}void _tcase_add_test (TCase *tc, TFun fn, const char *name){ TF * tf; if (tc == NULL || fn == NULL || name == NULL) return; tf = emalloc (sizeof(TF)); /* freed in tcase_free */ tf->fn = fn; tf->name = name; list_add_end (tc->tflst, tf);}static Fixture *fixture_create (SFun fun, int ischecked){ Fixture *f; f = emalloc (sizeof(Fixture)); f->fun = fun; f->ischecked = ischecked; return f;}void tcase_add_unchecked_fixture (TCase *tc, SFun setup, SFun teardown){ tcase_add_fixture(tc,setup,teardown,0);}void tcase_add_checked_fixture (TCase *tc, SFun setup, SFun teardown){ tcase_add_fixture (tc,setup,teardown,1);}static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown, int ischecked){ if (setup) { if (ischecked) list_add_end (tc->ch_sflst, fixture_create(setup, ischecked)); else list_add_end (tc->unch_sflst, fixture_create(setup, ischecked)); } /* Add teardowns at front so they are run in reverse order. */ if (teardown) { if (ischecked) list_add_front (tc->ch_tflst, fixture_create(teardown, ischecked)); else list_add_front (tc->unch_tflst, fixture_create(teardown, ischecked)); }}void tcase_fn_start (const char *fname, const char *file, int line){ send_ctx_info (get_send_key(),CK_CTX_TEST); send_loc_info (get_send_key(),file, line);}void _mark_point (const char *file, int line){ send_loc_info (get_send_key(), file, line);}void _fail_unless (int result, const char *file, int line, const char * msg, ...){ if (msg == NULL) eprintf ("_fail_unless() called with NULL msg",__FILE__,__LINE__); send_loc_info (get_send_key(), file, line); if (!result) { va_list ap; char buf[BUFSIZ]; va_start(ap,msg); vsnprintf(buf, BUFSIZ, msg, ap); va_end(ap); send_failure_info (get_send_key(), buf); if (cur_fork_status() == CK_FORK) exit(1); }}SRunner *srunner_create (Suite *s){ SRunner *sr = emalloc (sizeof(SRunner)); /* freed in srunner_free */ sr->slst = list_create(); if (s != NULL) list_add_end(sr->slst, s); sr->stats = emalloc (sizeof(TestStats)); /* freed in srunner_free */ sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0; sr->resultlst = list_create(); sr->log_fname = NULL; sr->xml_fname = NULL; sr->loglst = NULL; sr->fstat = CK_FORK_UNSPECIFIED; return sr;}void srunner_add_suite (SRunner *sr, Suite *s){ list_add_end(sr->slst, s);}void srunner_free (SRunner *sr){ List *l; TestResult *tr; if (sr == NULL) return; free (sr->stats); l = sr->slst; for (list_front(l); !list_at_end(l); list_advance(l)) { suite_free(list_val(l)); } list_free(sr->slst); l = sr->resultlst; for (list_front(l); !list_at_end(l); list_advance(l)) { tr = list_val(l); free(tr->file); free(tr->msg); free(tr); } list_free (sr->resultlst); free (sr);}int srunner_ntests_failed (SRunner *sr){ return sr->stats->n_failed + sr->stats->n_errors;}int srunner_ntests_run (SRunner *sr){ return sr->stats->n_checked;}TestResult **srunner_failures (SRunner *sr){ int i = 0; TestResult **trarray; List *rlst; trarray = malloc (sizeof(trarray[0]) * srunner_ntests_failed (sr)); rlst = sr->resultlst; for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) { TestResult *tr = list_val(rlst); if (non_pass(tr->rtype)) trarray[i++] = tr; } return trarray;}TestResult **srunner_results (SRunner *sr){ int i = 0; TestResult **trarray; List *rlst; trarray = malloc (sizeof(trarray[0]) * srunner_ntests_run (sr)); rlst = sr->resultlst; for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) { trarray[i++] = list_val(rlst); } return trarray;}static int non_pass (int val){ return val != CK_PASS;}TestResult *tr_create(void){ TestResult *tr; tr = emalloc (sizeof(TestResult)); tr_init (tr); return tr;}void tr_reset(TestResult *tr){ tr_init(tr);}static void tr_init (TestResult *tr){ tr->ctx = -1; tr->line = -1; tr->rtype = -1; tr->msg = NULL; tr->file = NULL; tr->tcname = NULL; tr->tname = NULL;}const char *tr_msg (TestResult *tr){ return tr->msg;}int tr_lno (TestResult *tr){ return tr->line;}const char *tr_lfile (TestResult *tr){ return tr->file;}int tr_rtype (TestResult *tr){ return tr->rtype;}enum ck_result_ctx tr_ctx (TestResult *tr){ return tr->ctx;}const char *tr_tcname (TestResult *tr){ return tr->tcname;}static int _fstat = CK_FORK;void set_fork_status (enum fork_status fstat){ if (fstat == CK_FORK || fstat == CK_NOFORK) _fstat = fstat; else eprintf ("Bad status in set_fork_status", __FILE__, __LINE__);}enum fork_status cur_fork_status (void){ return _fstat;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -