1#ifndef DEJAGNU_GTEST_H 2#define DEJAGNU_GTEST_H 1 3 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7#include <assert.h> 8#ifdef __cplusplus 9#include <string> 10#endif 11 12struct dejagnu_gtest_test 13{ 14 const char *name; 15 void (*fn) (void); 16 struct dejagnu_gtest_test *next; 17}; 18struct dejagnu_gtest_test *dejagnu_gtest_test_first, *dejagnu_gtest_test_last; 19int dejagnu_gtest_test_death_num, dejagnu_gtest_test_death_cur_num; 20 21#define TEST(cond, name) \ 22static void cond##_##name##_fn (void); \ 23static struct dejagnu_gtest_test cond##_##name##_struct \ 24 = { #cond "_" #name, cond##_##name##_fn, NULL }; \ 25static __attribute__((__constructor__)) void \ 26cond##_##name##_ctor (void) \ 27{ \ 28 if (strncmp (#name, "DISABLED_", 9) == 0) \ 29 return; \ 30 if (dejagnu_gtest_test_first == NULL) \ 31 dejagnu_gtest_test_first = &cond##_##name##_struct; \ 32 else \ 33 dejagnu_gtest_test_last->next = &cond##_##name##_struct; \ 34 dejagnu_gtest_test_last = &cond##_##name##_struct; \ 35} \ 36static void \ 37cond##_##name##_fn (void) 38 39#ifndef __cplusplus 40# define DEJAGNU_GTEST_TOCSTR(regex) (regex) 41#else 42static inline const char *DEJAGNU_GTEST_TOCSTR(const char *x) { return x; } 43static inline const char *DEJAGNU_GTEST_TOCSTR(const std::string &x) { return x.c_str (); } 44#endif 45 46#define EXPECT_DEATH(statement, regex) \ 47do \ 48 { \ 49 ++dejagnu_gtest_test_death_cur_num; \ 50 if (dejagnu_gtest_test_death_num == 0) \ 51 { \ 52 fprintf (stderr, "DEJAGNU_GTEST_EXPECT_DEATH%d %s " \ 53 "DEJAGNU_GTEST_EXPECT_DEATH%d %s " \ 54 "DEJAGNU_GTEST_EXPECT_DEATH%d\n", \ 55 dejagnu_gtest_test_death_cur_num, #statement, \ 56 dejagnu_gtest_test_death_cur_num, \ 57 DEJAGNU_GTEST_TOCSTR (regex), \ 58 dejagnu_gtest_test_death_cur_num); \ 59 } \ 60 else if (dejagnu_gtest_test_death_cur_num \ 61 == dejagnu_gtest_test_death_num) \ 62 { \ 63 statement; \ 64 } \ 65 } \ 66while (0) 67 68#define EXPECT_TRUE(condition) \ 69 if (!(condition)) \ 70 { \ 71 fprintf (stderr, "EXPECT_TRUE failed: " #condition "\n"); \ 72 exit (1); \ 73 } 74#define EXPECT_FALSE(condition) EXPECT_TRUE (!condition) 75#define EXPECT_EQ(expected, actual) EXPECT_TRUE ((expected) == (actual)) 76#define EXPECT_NE(expected, actual) EXPECT_TRUE ((expected) != (actual)) 77#define EXPECT_LT(expected, actual) EXPECT_TRUE ((expected) < (actual)) 78#define EXPECT_LE(expected, actual) EXPECT_TRUE ((expected) <= (actual)) 79#define EXPECT_GT(expected, actual) EXPECT_TRUE ((expected) > (actual)) 80#define EXPECT_GE(expected, actual) EXPECT_TRUE ((expected) >= (actual)) 81#define ASSERT_DEATH(statement, regex) EXPECT_DEATH (statement, regex) 82#define ASSERT_TRUE(condition) EXPECT_TRUE (condition) 83#define ASSERT_FALSE(condition) EXPECT_FALSE (condition) 84#define ASSERT_EQ(expected, actual) EXPECT_EQ (expected, actual) 85#define ASSERT_NE(expected, actual) EXPECT_NE (expected, actual) 86#define ASSERT_LT(expected, actual) EXPECT_LT (expected, actual) 87#define ASSERT_LE(expected, actual) EXPECT_LE (expected, actual) 88#define ASSERT_GT(expected, actual) EXPECT_GT (expected, actual) 89#define ASSERT_GE(expected, actual) EXPECT_GE (expected, actual) 90 91int 92main (int argc, const char **argv) 93{ 94 const char *test = NULL; 95 struct dejagnu_gtest_test *t; 96 if (argc > 1) 97 test = argv[1]; 98 else 99 test = getenv ("DEJAGNU_GTEST_ARG"); 100 if (test == NULL) 101 for (t = dejagnu_gtest_test_first; t; t = t->next) 102 fprintf (stderr, "DEJAGNU_GTEST_TEST %s\n", t->name); 103 else 104 { 105 const char *p = strchr (test, ':'); 106 if (p != NULL) 107 dejagnu_gtest_test_death_num = atoi (p + 1); 108 for (t = dejagnu_gtest_test_first; t; t = t->next) 109 if (p != NULL 110 ? (strncmp (test, t->name, p - test) == 0 111 && t->name[p - test] == '\0') 112 : (strcmp (test, t->name) == 0)) 113 break; 114 EXPECT_TRUE (t != NULL); 115 t->fn (); 116 } 117 return 0; 118} 119 120#endif 121