1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
* CUnit - A Unit testing framework library for C.
* Copyright (C) 2001 Anil Kumar
* Copyright (C) 2004, 2005 Anil Kumar, Jerry St.Clair
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "CUnit.h"
#include "ExampleTests.h"
/* WARNING - MAINTENANCE NIGHTMARE AHEAD
*
* If you change any of the tests & suites below, you also need
* to keep track of changes in the result statistics and reflect
* any changes in the result report counts in print_example_results().
*
* Yes, this could have been designed better using a more
* automated mechanism. No, it was not done that way.
*/
/* Suite initialization/cleanup functions */
static int suite_success_init(void) { return 0; }
static int suite_success_clean(void) { return 0; }
static int suite_failure_init(void) { return 1;}
static int suite_failure_clean(void) { return 1; }
static void testSuccess1(void) { CU_ASSERT(1); }
static void testSuccess2(void) { CU_ASSERT(2); }
static void testSuccess3(void) { CU_ASSERT(3); }
static void testSuiteFailure1(void) { CU_ASSERT(0); }
static void testSuiteFailure2(void) { CU_ASSERT(2); }
static void testFailure1(void) { CU_ASSERT(0); }
static void testFailure2(void) { CU_ASSERT(0); }
static void testFailure3(void) { CU_ASSERT(0); }
/* Test functions for CUnit assertions */
static void testSimpleAssert(void)
{
CU_ASSERT(1);
CU_ASSERT(!0);
CU_TEST(1);
CU_ASSERT(0);
CU_ASSERT(!1);
CU_TEST(0);
}
static void testFail(void)
{
CU_FAIL("This is a failure.");
CU_FAIL("This is another failure.");
}
static void testAssertTrue(void)
{
CU_ASSERT_TRUE(CU_TRUE);
CU_ASSERT_TRUE(!CU_FALSE);
CU_ASSERT_TRUE(!CU_TRUE);
CU_ASSERT_TRUE(CU_FALSE);
}
static void testAssertFalse(void)
{
CU_ASSERT_FALSE(CU_FALSE);
CU_ASSERT_FALSE(!CU_TRUE);
CU_ASSERT_FALSE(!CU_FALSE);
CU_ASSERT_FALSE(CU_TRUE);
}
static void testAssertEqual(void)
{
CU_ASSERT_EQUAL(10, 10);
CU_ASSERT_EQUAL(0, 0);
CU_ASSERT_EQUAL(0, -0);
CU_ASSERT_EQUAL(-12, -12);
CU_ASSERT_EQUAL(10, 11);
CU_ASSERT_EQUAL(0, 1);
CU_ASSERT_EQUAL(0, -1);
CU_ASSERT_EQUAL(-12, 12);
}
static void testAssertNotEqual(void)
{
CU_ASSERT_NOT_EQUAL(10, 11);
CU_ASSERT_NOT_EQUAL(0, -1);
CU_ASSERT_NOT_EQUAL(-12, -11);
CU_ASSERT_NOT_EQUAL(10, 10);
CU_ASSERT_NOT_EQUAL(0, -0);
CU_ASSERT_NOT_EQUAL(0, 0);
CU_ASSERT_NOT_EQUAL(-12, -12);
}
static void testAssertPtrEqual(void)
{
CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x101);
}
static void testAssertPtrNotEqual(void)
{
CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x100);
}
static void testAssertPtrNull(void)
{
CU_ASSERT_PTR_NULL((void*)(NULL));
CU_ASSERT_PTR_NULL((void*)(0x0));
CU_ASSERT_PTR_NULL((void*)0x23);
}
static void testAssertPtrNotNull(void)
{
CU_ASSERT_PTR_NOT_NULL((void*)0x100);
CU_ASSERT_PTR_NOT_NULL(NULL);
CU_ASSERT_PTR_NOT_NULL((void*)0x0);
}
static void testAssertStringEqual(void)
{
char str1[] = "test";
char str2[] = "test";
char str3[] = "suite";
CU_ASSERT_STRING_EQUAL(str1, str2);
CU_ASSERT_STRING_EQUAL(str1, str3);
CU_ASSERT_STRING_EQUAL(str3, str2);
}
static void testAssertStringNotEqual(void)
{
char str1[] = "test";
char str2[] = "test";
char str3[] = "suite";
CU_ASSERT_STRING_NOT_EQUAL(str1, str3);
CU_ASSERT_STRING_NOT_EQUAL(str3, str2);
CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
}
static void testAssertNStringEqual(void)
{
char str1[] = "test";
char str2[] = "testgfsg";
char str3[] = "tesgfsg";
CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
CU_ASSERT_NSTRING_EQUAL(str2, str3, 4);
CU_ASSERT_NSTRING_EQUAL(str1, str3, strlen(str1));
}
static void testAssertNStringNotEqual(void)
{
char str1[] = "test";
char str2[] = "tevt";
char str3[] = "testgfsg";
CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 2);
CU_ASSERT_NSTRING_NOT_EQUAL(str2, str3, 2);
}
static void testAssertDoubleEqual(void)
{
CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.00001);
CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.00001);
CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.00001);
CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.00001);
}
static void testAssertDoubleNotEqual(void)
{
CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.01);
CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.01);
CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.01);
CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.01);
}
static void testAbort(void)
{
CU_TEST_FATAL(CU_TRUE);
CU_TEST_FATAL(CU_FALSE);
fprintf(stderr, "\nFatal assertion failed to abort test in testAbortIndirect1\n");
exit(1);
}
static void testAbortIndirect(void)
{
testAbort();
fprintf(stderr, "\nFatal assertion failed to abort test in testAbortIndirect2\n");
exit(1);
}
static void testFatal(void)
{
CU_TEST_FATAL(CU_TRUE);
testAbortIndirect();
fprintf(stderr, "\nFatal assertion failed to abort test in testFatal\n");
exit(1);
}
static CU_TestInfo tests_success[] = {
{ "testSuccess1", testSuccess1 },
{ "testSuccess2", testSuccess2 },
{ "testSuccess3", testSuccess3 },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_failure[] = {
{ "testFailure1", testFailure1 },
{ "testFailure2", testFailure2 },
{ "testFailure3", testFailure3 },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_suitefailure[] = {
{ "testSuiteFailure1", testSuiteFailure1 },
{ "testSuiteFailure2", testSuiteFailure2 },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_simple[] = {
{ "testSimpleAssert", testSimpleAssert },
{ "testFail", testFail },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_bool[] = {
{ "testAssertTrue", testAssertTrue },
{ "testAssertFalse", testAssertFalse },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_equal[] = {
{ "testAssertEqual", testAssertEqual },
{ "testAssertNotEqual", testAssertNotEqual },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_ptr[] = {
{ "testAssertPtrEqual", testAssertPtrEqual },
{ "testAssertPtrNotEqual", testAssertPtrNotEqual },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_null[] = {
{ "testAssertPtrNull", testAssertPtrNull },
{ "testAssertPtrNotNull", testAssertPtrNotNull },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_string[] = {
{ "testAssertStringEqual", testAssertStringEqual },
{ "testAssertStringNotEqual", testAssertStringNotEqual },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_nstring[] = {
{ "testAssertNStringEqual", testAssertNStringEqual },
{ "testAssertNStringNotEqual", testAssertNStringNotEqual },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_double[] = {
{ "testAssertDoubleEqual", testAssertDoubleEqual },
{ "testAssertDoubleNotEqual", testAssertDoubleNotEqual },
CU_TEST_INFO_NULL,
};
static CU_TestInfo tests_fatal[] = {
{ "testFatal", testFatal },
CU_TEST_INFO_NULL,
};
static CU_SuiteInfo suites[] = {
{ "suite_success_both", suite_success_init, suite_success_clean, NULL, NULL, tests_success},
{ "suite_success_init", suite_success_init, NULL, NULL, NULL, tests_success},
{ "suite_success_clean", NULL, suite_success_clean, NULL, NULL, tests_success},
{ "test_failure", NULL, NULL, NULL, NULL, tests_failure},
{ "suite_failure_both", suite_failure_init, suite_failure_clean, NULL, NULL, tests_suitefailure}, /* tests should not run */
{ "suite_failure_init", suite_failure_init, NULL, NULL, NULL, tests_suitefailure}, /* tests should not run */
{ "suite_success_but_failure_clean", NULL, suite_failure_clean, NULL, NULL, tests_suitefailure}, /* tests will run, suite counted as running, but suite tagged as a failure */
{ "TestSimpleAssert", NULL, NULL, NULL, NULL, tests_simple},
{ "TestBooleanAssert", NULL, NULL, NULL, NULL, tests_bool},
{ "TestEqualityAssert", NULL, NULL, NULL, NULL, tests_equal},
{ "TestPointerAssert", NULL, NULL, NULL, NULL, tests_ptr},
{ "TestNullnessAssert", NULL, NULL, NULL, NULL, tests_null},
{ "TestStringAssert", NULL, NULL, NULL, NULL, tests_string},
{ "TestNStringAssert", NULL, NULL, NULL, NULL, tests_nstring},
{ "TestDoubleAssert", NULL, NULL, NULL, NULL, tests_double},
{ "TestFatal", NULL, NULL, NULL, NULL, tests_fatal},
CU_SUITE_INFO_NULL,
};
void AddTests(void)
{
assert(NULL != CU_get_registry());
assert(!CU_is_test_running());
/* Register suites. */
if (CU_register_suites(suites) != CUE_SUCCESS) {
fprintf(stderr, "suite registration failed - %s\n",
CU_get_error_msg());
exit(EXIT_FAILURE);
}
/* implementation without shortcut registration
CU_pSuite pSuite;
pSuite = CU_add_suite("suite_success_both", suite_success_init, suite_success_clean);
CU_add_test(pSuite, "testSuccess1", testSuccess1);
CU_add_test(pSuite, "testSuccess2", testSuccess2);
CU_add_test(pSuite, "testSuccess3", testSuccess3);
pSuite = CU_add_suite("suite_success_init", suite_success_init, NULL);
CU_add_test(pSuite, "testSuccess1", testSuccess1);
CU_add_test(pSuite, "testSuccess2", testSuccess2);
CU_add_test(pSuite, "testSuccess3", testSuccess3);
pSuite = CU_add_suite("suite_success_clean", NULL, suite_success_clean);
CU_add_test(pSuite, "testSuccess1", testSuccess1);
CU_add_test(pSuite, "testSuccess2", testSuccess2);
CU_add_test(pSuite, "testSuccess3", testSuccess3);
pSuite = CU_add_suite("test_failure", NULL, NULL);
CU_add_test(pSuite, "testFailure1", testFailure1);
CU_add_test(pSuite, "testFailure2", testFailure2);
CU_add_test(pSuite, "testFailure3", testFailure3);
/ * tests should not run * /
pSuite = CU_add_suite("suite_failure_both", suite_failure_init, suite_failure_clean);
CU_add_test(pSuite, "testSuiteFailure1", testSuiteFailure1);
CU_add_test(pSuite, "testSuiteFailure2", testSuiteFailure2);
/ * tests should not run * /
pSuite = CU_add_suite("suite_failure_init", suite_failure_init, NULL);
CU_add_test(pSuite, "testSuiteFailure1", testSuiteFailure1);
CU_add_test(pSuite, "testSuiteFailure2", testSuiteFailure2);
/ * tests will run, suite counted as running, but suite tagged as a failure * /
pSuite = CU_add_suite("suite_success_but_failure_clean", NULL, suite_failure_clean);
CU_add_test(pSuite, "testSuiteFailure1", testSuiteFailure1);
CU_add_test(pSuite, "testSuiteFailure2", testSuiteFailure2);
pSuite = CU_add_suite("TestSimpleAssert", NULL, NULL);
CU_add_test(pSuite, "testSimpleAssert", testSimpleAssert);
CU_add_test(pSuite, "testFail", testFail);
pSuite = CU_add_suite("TestBooleanAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertTrue", testAssertTrue);
CU_add_test(pSuite, "testAssertFalse", testAssertFalse);
pSuite = CU_add_suite("TestEqualityAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertEqual", testAssertEqual);
CU_add_test(pSuite, "testAssertNotEqual", testAssertNotEqual);
pSuite = CU_add_suite("TestPointerAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertPtrEqual", testAssertPtrEqual);
CU_add_test(pSuite, "testAssertPtrNotEqual", testAssertPtrNotEqual);
pSuite = CU_add_suite("TestNullnessAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertPtrNull", testAssertPtrNull);
CU_add_test(pSuite, "testAssertPtrNotNull", testAssertPtrNotNull);
pSuite = CU_add_suite("TestStringAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertStringEqual", testAssertStringEqual);
CU_add_test(pSuite, "testAssertStringNotEqual", testAssertStringNotEqual);
pSuite = CU_add_suite("TestNStringAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertNStringEqual", testAssertNStringEqual);
CU_add_test(pSuite, "testAssertNStringNotEqual", testAssertNStringNotEqual);
pSuite = CU_add_suite("TestDoubleAssert", NULL, NULL);
CU_add_test(pSuite, "testAssertDoubleEqual", testAssertDoubleEqual);
CU_add_test(pSuite, "testAssertDoubleNotEqual", testAssertDoubleNotEqual);
pSuite = CU_add_suite("TestFatal", NULL, NULL);
CU_add_test(pSuite, "testFatal", testFatal);
*/
}
void print_example_results(void)
{
fprintf(stdout, "\n\nExpected Test Results:"
"\n\n Error Handling Type # Run # Pass # Fail"
"\n\n ignore errors suites%9u%9u%9u"
"\n tests %9u%9u%9u"
"\n asserts%8u%9u%9u"
"\n\n stop on error suites%9u%9u%9u"
"\n tests %9u%9u%9u"
"\n asserts%8u%9u%9u\n\n",
14, 14, 3,
31, 10, 21,
89, 47, 42,
4, 4, 1,
12, 9, 3,
12, 9, 3);
}
|