18a68b2009-09-17Martin Stjernholm // -*- pike -*- // Include file for use in test suites (start --self-test). // // Assumes: // // inherit "etc/test/tests/pike_test_common.pike"
5859dd2009-09-24Martin Stjernholm // Run EXPR and check that it produces a nonzero result. If the test // is logged then EXPR is printed. If ARGS is given then // sprintf(EXPR, ARGS) is printed. Returns the result of EXPR.
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_TRUE(EXPR, ARGS...) \ cpp_test_true (__FILE__, __LINE__, \ lambda() {return (EXPR);}, \ #EXPR, ({ARGS}))
18a68b2009-09-17Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_FALSE(EXPR, ARGS...) \ cpp_test_true (__FILE__, __LINE__, \ lambda() {return !(EXPR);}, \ #EXPR, ({ARGS}))
5a69712009-11-03Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_EQUAL(A, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_test_true (__FILE__, __LINE__, \ lambda() { \ return equal (args__[2] = (A), args__[5] = (B)); \ }, \ "%-*s (is %O) equals\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \
6633672011-04-18Martin Stjernholm  }()
18a68b2009-09-17Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_NOT_EQUAL(A, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_test_true (__FILE__, __LINE__, \ lambda() { \ return !equal (args__[2] = (A), args__[5] = (B)); \ }, \ "%-*s (is %O) does not equal\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \
6633672011-04-18Martin Stjernholm  }()
18a68b2009-09-17Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CMP(A, OP, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_test_true (__FILE__, __LINE__, \ lambda() { \ return (args__[2] = (A)) OP (args__[5] = (B)); \ }, \ "%-*s (is %O) " #OP "\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \
6633672011-04-18Martin Stjernholm  }()
5859dd2009-09-24Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_ERROR(CODE, ARGS...) \ cpp_test_true (__FILE__, __LINE__, \ lambda() {return catch {CODE;};}, \ #CODE, ({ARGS}))
5859dd2009-09-24Martin Stjernholm 
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_NOT_ERROR(CODE, ARGS...) \ cpp_test_true (__FILE__, __LINE__, \ lambda() {return !catch {CODE;};}, \ #CODE, ({ARGS}))
5859dd2009-09-24Martin Stjernholm 
92d7312018-02-27Karl Gustav Sterneberg #define ASSERT_TRUE(EXPR, ARGS...) \ cpp_assert_true (__FILE__, __LINE__, \ lambda() {return (EXPR);}, \ #EXPR, ({ARGS})) #define ASSERT_FALSE(EXPR, ARGS...) \ cpp_assert_true (__FILE__, __LINE__, \ lambda() {return !(EXPR);}, \ #EXPR, ({ARGS})) // ASSERT macros: // Work like their TEST_-equivalent above but do not swallow errors. #define ASSERT_EQUAL(A, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_assert_true (__FILE__, __LINE__, \ lambda() { \ return equal (args__[2] = (A), args__[5] = (B)); \ }, \ "%-*s (is %O) equals\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \ }() #define ASSERT_NOT_EQUAL(A, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_assert_true (__FILE__, __LINE__, \ lambda() { \ return !equal (args__[2] = (A), args__[5] = (B)); \ }, \ "%-*s (is %O) does not equal\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \ }() #define ASSERT_CMP(A, OP, B) \ lambda () { \ int len__ = min (max (sizeof (#A), sizeof (#B)), 40); \ array args__ = ({len__, #A, 0, len__, #B, 0}); \ cpp_assert_true (__FILE__, __LINE__, \ lambda() { \ return (args__[2] = (A)) OP (args__[5] = (B)); \ }, \ "%-*s (is %O) " #OP "\n" \ "%-*s (is %O)?", \ args__); \ args__ = 0; \ }() #define ASSERT_ERROR(CODE, ARGS...) \ cpp_assert_true (__FILE__, __LINE__, \ lambda() {return catch {CODE;};}, \ #CODE, ({ARGS})) #define ASSERT_NOT_ERROR(CODE, ARGS...) \ cpp_assert_true (__FILE__, __LINE__, \ lambda() {return !catch {CODE;};}, \ #CODE, ({ARGS}))
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL(FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test (({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_TRUE(FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_true (({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_FALSE(FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_false (({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_ERROR(FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_error (({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_EQUAL(VAL, FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_equal ((VAL), ({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_NOT_EQUAL(VAL, FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_not_equal ((VAL), ({__FILE__, __LINE__, (FN)}), ARGS)
7eaa822018-02-27Karl Gustav Sterneberg #define TEST_CALL_GENERIC(CHECK_FN, FN, ARGS...) \
5859dd2009-09-24Martin Stjernholm  test_generic ((CHECK_FN), ({__FILE__, __LINE__, (FN)}), ARGS)
92d7312018-02-27Karl Gustav Sterneberg  // ASSERT macros: // Work like their TEST_-equivalent but do not swallow errors. #define ASSERT_CALL(FN, ARGS...) \ assert (({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_TRUE(FN, ARGS...) \ assert_true (({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_FALSE(FN, ARGS...) \ assert_false (({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_ERROR(FN, ARGS...) \ assert_error (({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_EQUAL(VAL, FN, ARGS...) \ assert_equal ((VAL), ({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_NOT_EQUAL(VAL, FN, ARGS...) \ assert_not_equal ((VAL), ({__FILE__, __LINE__, (FN)}), ARGS) #define ASSERT_CALL_GENERIC(CHECK_FN, FN, ARGS...) \ assert_generic ((CHECK_FN), ({__FILE__, __LINE__, (FN)}), ARGS)