Skip to content

Commit

Permalink
File consolidation. Having a separate ctest_test.c/h was more confusi…
Browse files Browse the repository at this point in the history
…ng, not less.
  • Loading branch information
Scott Bronson committed May 23, 2008
1 parent 15d4291 commit 6695610
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TODO:
- Get setjmp.h and
- Add routine to parse cmdline args.
--test will initiate unit testing (optional arg).
--list lists unit tests,
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
COPTS=-g -Wall -Werror
# COPTS+=-ansi -pedantic

CSRC=main.c ctest_tests.c ctest.c test_assert.c
CHDR=ctest.h ct_assert.h ctest_test.h
CSRC=main.c ctest.c ct_assert.c
CHDR=ctest.h ct_assert.h

all: ctest

Expand Down
6 changes: 4 additions & 2 deletions test_assert.c → ct_assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
* 6 Mar 2006
*
* Tests the AssertEqual(a,b) style tests from ctest_Assert.h
*
* This file is 100% ct_assert unit tests. Don't include it in your
* app unless you want to call the unit tests yourself.
*
* Copyright (C) 2007 Scott Bronson
* This file is released under the MIT License.
* See http://www.opensource.org/licenses/mit-license.php
*/

#include <string.h>

#include "ct_assert.h"
#include <string.h>


void test_assert_int()
Expand Down
2 changes: 1 addition & 1 deletion ct_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#ifndef CTEST_ASSERT_H
#define CTEST_ASSERT_H

#include "ctest_test.h"
#include "ctest.h"

/*
* Use these macros to test your app.
Expand Down
1 change: 0 additions & 1 deletion ctest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <string.h>

#include "ctest.h"
#include "ctest_test.h"


/** @file ctest.c
Expand Down
103 changes: 96 additions & 7 deletions ctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

/* @file ctest.h
*
* This file contains the declarations and all the Assert macros
* required to use ctest in your own applications.
*
* To compile ctest to run its own unit tests, do this:
* To run ctest's unit tests:
*
* <pre>
* $ make
Expand All @@ -28,9 +25,6 @@
* 4 tests run, 4 successes (132 assertions).
* </pre>
*
* If your non-gcc compiler complains about a missing __func__ macro,
* add -D__func__='"test"' to the compiler's command line.
*
* See ::ctest_tests for instructions on how to add ctest's
* built-in unit tests to your application's test suite.
*/
Expand Down Expand Up @@ -76,4 +70,99 @@ void ctest_run_unit_tests();
/* TODO: this is a hack. Handle it better. */
void ctest_show_failures();




#include <setjmp.h>
#include <stdio.h>


/* This is sad: because there are setjmp implementations that store jmp_buf
* as an array instead of a struct, I have to wrap it in a struct to be able
* to reliably pass the whole thing between functions.
*/
struct ctest_jmp_wrapper {
jmp_buf jmp;
};


/** Starts a new test.
*
* This call nests just fine. A test failure always unwinds to the
* most recent test_start call. Therefore, if you have a lengthly
* test and don't want a failure in the middle of it to bail all the
* way out, just wrap the failing code in its own test_start.
*
* Example:
* <pre>
* ctest_start("mytest") {
* AssertEqual(1,1);
* AssertEqual(1,2);
* }
* </pre>
*/

/* The for loop is so that ctest_internal_test_finished() called
* when the flow of control exits the block that follows ctest_start.
*/
#define ctest_start(name) \
if(setjmp(ctest_internal_start_test(name, __FILE__,__LINE__)->jmp)) { \
ctest_internal_test_jumped(); \
} else for(; ctest_internal_test_finished(); )


/** Starts a new test with reversed results.
*
* i.e. if the assertion succeeds, the test fails because
* the assertion was actually supposed to fail. And if the
* assertion fails, the test succeeds.
*
* This is only used for testing ctest itself. You should never have
* a need to call this routine directly. That would be perverse.
*/

#define ctest_start_inverted(name) \
if(setjmp(ctest_internal_start_inverted_test(name, __FILE__,__LINE__)->jmp)) { \
ctest_internal_test_jumped(); \
} else for(; ctest_internal_test_finished(); )


/** Prepares ctest to check an assertion
*
* Mostly it just increments the numer of assertions counter
* and prints the assertion if verbose is enabled.
*
* You must make sure that ctest_assert_succeeded() or ctest_assert_failed()
* will be called before ctest_assert_prepare() or ctest_start() can possibly
* be called.
*/

void ctest_assert_prepare(const char *file, int line, const char *assertion);


/** Causes the current assertion to fail.
*
* You must have previously called ctest_assert_prepare().
* Pass the message that should be printed as a printf format string.
*/

void ctest_assert_failed(const char *msg, ...);


/** Causes the current assertion to finish with success.
*
* You must have previously called ctest_assert_prepare().
*/

void ctest_assert_succeeded();


/* The following routines are not meant to be called directly; they are used
* by the mutest_start() macro and highly subject to change.
*/
struct ctest_jmp_wrapper* ctest_internal_start_test(const char *name, const char *file, int line);
struct ctest_jmp_wrapper* ctest_internal_start_inverted_test(const char *name, const char *file, int line);
void ctest_internal_test_jumped();
int ctest_internal_test_finished();

#endif
111 changes: 0 additions & 111 deletions ctest_test.h

This file was deleted.

35 changes: 0 additions & 35 deletions ctest_tests.c

This file was deleted.

13 changes: 12 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "ctest.h"

#include "ct_assert.h"


int main(int argc, char **argv)
{
Expand All @@ -22,7 +24,16 @@ int main(int argc, char **argv)
ctest_show_failures();
}

ctest_run_unit_tests();
/* Ensure that we can hit asserts without first calling ctest_start. */
AssertEQ(1,1);

/* TODO: write a test to fork and make sure that a failing
* assert causes the app to exit immediately.
*/

/* Run unit tests for all assert files we know about */
ctest_test_asserts();

ctest_exit();

/* this will never be reached */
Expand Down

0 comments on commit 6695610

Please sign in to comment.