Changeset 134ac5d in mainline for uspace/lib/pcut/README.rst
- Timestamp:
- 2014-06-06T07:54:24Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8db09e4
- Parents:
- eeb23f2d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/pcut/README.rst
reeb23f2d r134ac5d 22 22 safe against unexpected crashes, such as null pointer dereference. 23 23 24 More details can be found on PCUT wiki on GitHub. 25 26 https://github.com/vhotspur/pcut/wiki 24 More details can be found on PCUT wiki on GitHub: 25 https://github.com/vhotspur/pcut/wiki 27 26 28 27 29 Main goal - simple to use30 ------------------- ------28 Quick-start example 29 ------------------- 31 30 32 Let's illustrate how PCUT aims to be simple when creating unit tests for 33 function ``intmin`` that ought to return smaller of its two arguments.:: 31 The following code tests the standard ``atoi`` function:: 34 32 35 int intmin(int a, int b) { 36 return a > b ? b : a; 33 #include <pcut/pcut.h> 34 #include <stdlib.h> 35 36 PCUT_INIT 37 38 PCUT_TEST(atoi_zero) { 39 PCUT_ASSERT_INT_EQUALS(0, atoi("0")); 37 40 } 38 41 39 Individual test-cases for such method could cover following cases: getting 40 minimal of 41 42 - two same numbers 43 - negative and positive number 44 - two negative numbers 45 - two positive numbers 46 - "corner case" numbers, such as minimal and maximal represented number 47 48 In PCUT, that would be translated to the following code::: 49 50 #include <pcut/test.h> 51 #include <limits.h> 52 /* Other include to have declaration of intmin */ 53 54 PCUT_INIT 55 56 PCUT_TEST_SUITE(intmin_tests); 57 58 PCUT_TEST(same_number) { 59 PCUT_ASSERT_INT_EQUALS(719, intmin(719, 719) ); 60 PCUT_ASSERT_INT_EQUALS(-4589, intmin(-4589, -4589) ); 42 PCUT_TEST(atoi_positive) { 43 PCUT_ASSERT_INT_EQUALS(42, atoi("42")); 61 44 } 62 45 63 PCUT_TEST(positive_and_negative) { 64 PCUT_ASSERT_INT_EQUALS(-5, intmin(-5, 71) ); 65 PCUT_ASSERT_INT_EQUALS(-17, intmin(423, -17) ); 46 PCUT_TEST(atoi_negative) { 47 PCUT_ASSERT_INT_EQUALS(-273, atoi("-273")); 66 48 } 67 49 68 PCUT_TEST(same_sign) {69 PCUT_ASSERT_INT_EQUALS(22, intmin(129, 22) );70 PCUT_ASSERT_INT_EQUALS(-37, intmin(-37, -1) );71 }72 73 PCUT_TEST(corner_cases) {74 PCUT_ASSERT_INT_EQUALS(INT_MIN, intmin(INT_MIN, -1234) );75 PCUT_ASSERT_INT_EQUALS(9876, intmin(9876, INT_MAX) );76 }77 78 50 PCUT_MAIN() 79 51 80 And that's all. 81 You do not need to manually specify which tests to run etc., 82 everything is done magically via the ``PCUT_INIT``, ``PCUT_MAIN`` and 83 ``PCUT_TEST`` macros. 84 All you need to do is to compile this code and link it with ``libpcut``. 85 Result of the linking would be an executable that runs the tests and 86 reports the results. 52 As you can see, there is no manual listing of tests that form the test 53 suite etc, only the tests and ``PCUT_INIT`` at the beginning and 54 ``PCUT_MAIN`` at the end. 87 55 88 89 Examples 90 -------- 56 This code has to be linked with ``libpcut`` to get an executable that runs 57 the tests and reports the results. 91 58 92 59 More examples, in the form of self-tests, are available in the ``tests/`` 93 60 subdirectory. 61 Other examples can be found on the Wiki. 94 62 95 63
Note:
See TracChangeset
for help on using the changeset viewer.