0
|
1 |
/*
|
|
2 |
* Simple example of a CUnit unit test.
|
|
3 |
*
|
|
4 |
* This program (crudely) demonstrates a very simple "black box"
|
|
5 |
* test of the standard library functions fprintf() and fread().
|
|
6 |
* It uses suite initialization and cleanup functions to open
|
|
7 |
* and close a common temporary file used by the test functions.
|
|
8 |
* The test functions then write to and read from the temporary
|
|
9 |
* file in the course of testing the library functions.
|
|
10 |
*
|
|
11 |
* The 2 test functions are added to a single CUnit suite, and
|
|
12 |
* then run using the CUnit Basic interface. The output of the
|
|
13 |
* program (on CUnit version 2.0-2) is:
|
|
14 |
*
|
|
15 |
* CUnit : A Unit testing framework for C.
|
|
16 |
* http://cunit.sourceforge.net/
|
|
17 |
*
|
|
18 |
* Suite: Suite_1
|
|
19 |
* Test: test of fprintf() ... passed
|
|
20 |
* Test: test of fread() ... passed
|
|
21 |
*
|
|
22 |
* --Run Summary: Type Total Ran Passed Failed
|
|
23 |
* suites 1 1 n/a 0
|
|
24 |
* tests 2 2 2 0
|
|
25 |
* asserts 5 5 5 0
|
|
26 |
*/
|
|
27 |
|
|
28 |
#include <stdio.h>
|
|
29 |
#include <string.h>
|
|
30 |
#include "CUnit/Basic.h"
|
|
31 |
|
|
32 |
/* Pointer to the file used by the tests. */
|
|
33 |
static FILE *temp_file = NULL;
|
|
34 |
|
|
35 |
/* The suite initialization function.
|
|
36 |
* Opens the temporary file used by the tests.
|
|
37 |
* Returns zero on success, non-zero otherwise.
|
|
38 |
*/
|
|
39 |
int init_suite1 (void)
|
|
40 |
{
|
|
41 |
if (NULL == (temp_file = fopen ("temp.txt", "w+"))) {
|
|
42 |
return -1;
|
|
43 |
} else {
|
|
44 |
return 0;
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
/* The suite cleanup function.
|
|
49 |
* Closes the temporary file used by the tests.
|
|
50 |
* Returns zero on success, non-zero otherwise.
|
|
51 |
*/
|
|
52 |
int clean_suite1 (void)
|
|
53 |
{
|
|
54 |
if (0 != fclose (temp_file)) {
|
|
55 |
return -1;
|
|
56 |
} else {
|
|
57 |
temp_file = NULL;
|
|
58 |
return 0;
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
/* Simple test of fprintf().
|
|
63 |
* Writes test data to the temporary file and checks
|
|
64 |
* whether the expected number of bytes were written.
|
|
65 |
*/
|
|
66 |
void testFPRINTF (void)
|
|
67 |
{
|
|
68 |
int i1 = 10;
|
|
69 |
|
|
70 |
if (NULL != temp_file) {
|
|
71 |
CU_ASSERT (0 == fprintf (temp_file, ""));
|
|
72 |
CU_ASSERT (2 == fprintf (temp_file, "Q\n"));
|
|
73 |
CU_ASSERT (7 == fprintf (temp_file, "i1 = %d", i1));
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/* Simple test of fread().
|
|
78 |
* Reads the data previously written by testFPRINTF()
|
|
79 |
* and checks whether the expected characters are present.
|
|
80 |
* Must be run after testFPRINTF().
|
|
81 |
*/
|
|
82 |
void testFREAD (void)
|
|
83 |
{
|
|
84 |
char buffer[20];
|
|
85 |
|
|
86 |
if (NULL != temp_file) {
|
|
87 |
rewind (temp_file);
|
|
88 |
CU_ASSERT (9 == fread (buffer, sizeof (unsigned char), 20, temp_file));
|
|
89 |
CU_ASSERT (0 == strncmp (buffer, "Q\ni1 = 10", 9));
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
/* The main() function for setting up and running the tests.
|
|
94 |
* Returns a CUE_SUCCESS on successful running, another
|
|
95 |
* CUnit error code on failure.
|
|
96 |
*/
|
|
97 |
int main ()
|
|
98 |
{
|
|
99 |
CU_pSuite pSuite = NULL;
|
|
100 |
|
|
101 |
/*
|
|
102 |
* initialize the CUnit test registry
|
|
103 |
*/
|
|
104 |
if (CUE_SUCCESS != CU_initialize_registry ())
|
|
105 |
return CU_get_error ();
|
|
106 |
|
|
107 |
/*
|
|
108 |
* add a suite to the registry
|
|
109 |
*/
|
|
110 |
pSuite = CU_add_suite ("Suite_1", init_suite1, clean_suite1);
|
|
111 |
if (NULL == pSuite) {
|
|
112 |
CU_cleanup_registry ();
|
|
113 |
return CU_get_error ();
|
|
114 |
}
|
|
115 |
|
|
116 |
/*
|
|
117 |
* add the tests to the suite
|
|
118 |
*/
|
|
119 |
/*
|
|
120 |
* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf()
|
|
121 |
*/
|
|
122 |
if ((NULL == CU_add_test (pSuite, "test of fprintf()", testFPRINTF)) ||
|
|
123 |
(NULL == CU_add_test (pSuite, "test of fread()", testFREAD))) {
|
|
124 |
CU_cleanup_registry ();
|
|
125 |
return CU_get_error ();
|
|
126 |
}
|
|
127 |
|
|
128 |
/*
|
|
129 |
* Run all tests using the CUnit Basic interface
|
|
130 |
*/
|
|
131 |
CU_basic_set_mode (CU_BRM_VERBOSE);
|
|
132 |
CU_basic_run_tests ();
|
|
133 |
CU_cleanup_registry ();
|
|
134 |
return CU_get_error ();
|
|
135 |
}
|