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 
12 struct dejagnu_gtest_test
13 {
14   const char *name;
15   void (*fn) (void);
16   struct dejagnu_gtest_test *next;
17 };
18 struct dejagnu_gtest_test *dejagnu_gtest_test_first, *dejagnu_gtest_test_last;
19 int dejagnu_gtest_test_death_num, dejagnu_gtest_test_death_cur_num;
20 
21 #define TEST(cond, name) \
22 static void cond##_##name##_fn (void);				\
23 static struct dejagnu_gtest_test cond##_##name##_struct		\
24   = { #cond "_" #name, cond##_##name##_fn, NULL };		\
25 static __attribute__((__constructor__)) void			\
26 cond##_##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 }								\
36 static void							\
37 cond##_##name##_fn (void)
38 
39 #ifndef __cplusplus
40 # define DEJAGNU_GTEST_TOCSTR(regex) (regex)
41 #else
DEJAGNU_GTEST_TOCSTR(const char * x)42 static inline const char *DEJAGNU_GTEST_TOCSTR(const char *x) { return x; }
DEJAGNU_GTEST_TOCSTR(const std::string & x)43 static inline const char *DEJAGNU_GTEST_TOCSTR(const std::string &x) { return x.c_str (); }
44 #endif
45 
46 #define EXPECT_DEATH(statement, regex) \
47 do								\
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   }								\
66 while (0)
67 
68 #define EXPECT_TRUE(condition) \
69   if (!(condition))						\
70     {								\
71       fprintf (stderr, "%s",					\
72 	       "EXPECT_TRUE failed: " #condition "\n");		\
73       exit (1);							\
74     }
75 #define EXPECT_FALSE(condition) EXPECT_TRUE (!condition)
76 #define EXPECT_EQ(expected, actual) EXPECT_TRUE ((expected) == (actual))
77 #define EXPECT_NE(expected, actual) EXPECT_TRUE ((expected) != (actual))
78 #define EXPECT_LT(expected, actual) EXPECT_TRUE ((expected) < (actual))
79 #define EXPECT_LE(expected, actual) EXPECT_TRUE ((expected) <= (actual))
80 #define EXPECT_GT(expected, actual) EXPECT_TRUE ((expected) > (actual))
81 #define EXPECT_GE(expected, actual) EXPECT_TRUE ((expected) >= (actual))
82 #define ASSERT_DEATH(statement, regex) EXPECT_DEATH (statement, regex)
83 #define ASSERT_TRUE(condition) EXPECT_TRUE (condition)
84 #define ASSERT_FALSE(condition) EXPECT_FALSE (condition)
85 #define ASSERT_EQ(expected, actual) EXPECT_EQ (expected, actual)
86 #define ASSERT_NE(expected, actual) EXPECT_NE (expected, actual)
87 #define ASSERT_LT(expected, actual) EXPECT_LT (expected, actual)
88 #define ASSERT_LE(expected, actual) EXPECT_LE (expected, actual)
89 #define ASSERT_GT(expected, actual) EXPECT_GT (expected, actual)
90 #define ASSERT_GE(expected, actual) EXPECT_GE (expected, actual)
91 
92 int
main(int argc,const char ** argv)93 main (int argc, const char **argv)
94 {
95   const char *test = NULL;
96   struct dejagnu_gtest_test *t;
97   if (argc > 1)
98     test = argv[1];
99   else
100     test = getenv ("DEJAGNU_GTEST_ARG");
101   if (test == NULL)
102     for (t = dejagnu_gtest_test_first; t; t = t->next)
103       fprintf (stderr, "DEJAGNU_GTEST_TEST %s\n", t->name);
104   else
105     {
106       const char *p = strchr (test, ':');
107       if (p != NULL)
108 	dejagnu_gtest_test_death_num = atoi (p + 1);
109       for (t = dejagnu_gtest_test_first; t; t = t->next)
110 	if (p != NULL
111 	    ? (strncmp (test, t->name, p - test) == 0
112 	       && t->name[p - test] == '\0')
113 	    : (strcmp (test, t->name) == 0))
114 	  break;
115       EXPECT_TRUE (t != NULL);
116       t->fn ();
117     }
118   return 0;
119 }
120 
121 #endif
122