cunit.c

00001 /*
00002  *  Simple example of a CUnit unit test.
00003  *
00004  *  This program (crudely) demonstrates a very simple "black box"
00005  *  test of the standard library functions fprintf() and fread().
00006  *  It uses suite initialization and cleanup functions to open
00007  *  and close a common temporary file used by the test functions.
00008  *  The test functions then write to and read from the temporary
00009  *  file in the course of testing the library functions.
00010  *
00011  *  The 2 test functions are added to a single CUnit suite, and
00012  *  then run using the CUnit Basic interface.  The output of the
00013  *  program (on CUnit version 2.0-2) is:
00014  *
00015  *           CUnit : A Unit testing framework for C.
00016  *           http://cunit.sourceforge.net/
00017  *
00018  *       Suite: Suite_1
00019  *         Test: test of fprintf() ... passed
00020  *         Test: test of fread() ... passed
00021  *
00022  *       --Run Summary: Type      Total     Ran  Passed  Failed
00023  *                      suites        1       1     n/a       0
00024  *                      tests         2       2       2       0
00025  *                      asserts       5       5       5       0
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include "CUnit/Basic.h"
00031 
00032 /* Pointer to the file used by the tests. */
00033 static FILE* temp_file = NULL;
00034 
00035 /* The suite initialization function.
00036  * Opens the temporary file used by the tests.
00037  * Returns zero on success, non-zero otherwise.
00038  */
00039 int init_suite1(void)
00040 {
00041    if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
00042       return -1;
00043    }
00044    else {
00045       return 0;
00046    }
00047 }
00048 
00049 /* The suite cleanup function.
00050  * Closes the temporary file used by the tests.
00051  * Returns zero on success, non-zero otherwise.
00052  */
00053 int clean_suite1(void)
00054 {
00055    if (0 != fclose(temp_file)) {
00056       return -1;
00057    }
00058    else {
00059       temp_file = NULL;
00060       return 0;
00061    }
00062 }
00063 
00064 /* Simple test of fprintf().
00065  * Writes test data to the temporary file and checks
00066  * whether the expected number of bytes were written.
00067  */
00068 void testFPRINTF(void)
00069 {
00070    int i1 = 10;
00071 
00072    if (NULL != temp_file) {
00073       CU_ASSERT(0 == fprintf(temp_file, ""));
00074       CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
00075       CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
00076    }
00077 }
00078 
00079 /* Simple test of fread().
00080  * Reads the data previously written by testFPRINTF()
00081  * and checks whether the expected characters are present.
00082  * Must be run after testFPRINTF().
00083  */
00084 void testFREAD(void)
00085 {
00086    unsigned char buffer[20];
00087 
00088    if (NULL != temp_file) {
00089       rewind(temp_file);
00090       CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
00091       CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
00092    }
00093 }
00094 
00095 /* The main() function for setting up and running the tests.
00096  * Returns a CUE_SUCCESS on successful running, another
00097  * CUnit error code on failure.
00098  */
00099 int main()
00100 {
00101    CU_pSuite pSuite = NULL;
00102 
00103    /* initialize the CUnit test registry */
00104    if (CUE_SUCCESS != CU_initialize_registry())
00105       return CU_get_error();
00106 
00107    /* add a suite to the registry */
00108    pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
00109    if (NULL == pSuite) {
00110       CU_cleanup_registry();
00111       return CU_get_error();
00112    }
00113 
00114    /* add the tests to the suite */
00115    /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
00116    if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
00117        (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
00118    {
00119       CU_cleanup_registry();
00120       return CU_get_error();
00121    }
00122 
00123    /* Run all tests using the CUnit Basic interface */
00124    CU_basic_set_mode(CU_BRM_VERBOSE);
00125    CU_basic_run_tests();
00126    CU_cleanup_registry();
00127    return CU_get_error();
00128 }
Generato il Fri Jan 28 22:16:31 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3