OpenSSL::Test: Fix directory calculations in __cwd()
[openssl.git] / test / testutil.h
1 /*
2  * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef HEADER_TESTUTIL_H
11 # define HEADER_TESTUTIL_H
12
13 /*-
14  * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
15  *
16  * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
17  * object called "fixture". It will also allocate the "result" variable used
18  * by EXECUTE_TEST. set_up() should take a const char* specifying the test
19  * case name and return a TEST_FIXTURE_TYPE by value.
20  *
21  * EXECUTE_TEST will pass fixture to execute_func() by value, call
22  * tear_down(), and return the result of execute_func(). execute_func() should
23  * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
24  * failure.
25  *
26  * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
27  * variations like so:
28  *
29  * #define SETUP_FOOBAR_TEST_FIXTURE()\
30  *   SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
31  *
32  * #define EXECUTE_FOOBAR_TEST()\
33  *   EXECUTE_TEST(execute_foobar, tear_down_foobar)
34  *
35  * Then test case functions can take the form:
36  *
37  * static int test_foobar_feature()
38  *      {
39  *      SETUP_FOOBAR_TEST_FIXTURE();
40  *      [...set individual members of fixture...]
41  *      EXECUTE_FOOBAR_TEST();
42  *      }
43  */
44 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
45     TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
46     int result = 0
47
48 # define EXECUTE_TEST(execute_func, tear_down)\
49         result = execute_func(fixture);\
50         tear_down(fixture);\
51         return result
52
53 /*
54  * TEST_CASE_NAME is defined as the name of the test case function where
55  * possible; otherwise we get by with the file name and line number.
56  */
57 # if __STDC_VERSION__ < 199901L
58 #  if defined(_MSC_VER)
59 #   define TEST_CASE_NAME __FUNCTION__
60 #  else
61 #   define testutil_stringify_helper(s) #s
62 #   define testutil_stringify(s) testutil_stringify_helper(s)
63 #   define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
64 #  endif                        /* _MSC_VER */
65 # else
66 #  define TEST_CASE_NAME __func__
67 # endif                         /* __STDC_VERSION__ */
68
69 /*
70  * In main(), call ADD_TEST to register each test case function, then call
71  * run_tests() to execute all tests and report the results. The result
72  * returned from run_tests() should be used as the return value for main().
73  */
74 # define ADD_TEST(test_function) add_test(#test_function, test_function)
75
76 /*
77  * Simple parameterized tests. Adds a test_function(idx) test for each
78  * 0 <= idx < num.
79  */
80 # define ADD_ALL_TESTS(test_function, num) \
81   add_all_tests(#test_function, test_function, num)
82
83 void add_test(const char *test_case_name, int (*test_fn) ());
84 void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num);
85 int run_tests(const char *test_prog_name);
86
87 #endif                          /* HEADER_TESTUTIL_H */