]> rtime.felk.cvut.cz Git - wvtest.git/blob - cpp/wvtestmain.cc
Import basic C++ wvtest from wvstreams project.
[wvtest.git] / cpp / wvtestmain.cc
1 #include "wvtest.h"
2 #include "wvcrash.h"
3 #include "wvstring.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #ifdef _WIN32
7 #include <io.h>
8 #include <windows.h>
9 #else
10 #include <unistd.h>
11 #include <fcntl.h>
12 #endif
13
14 static bool fd_is_valid(int fd)
15 {
16 #ifdef _WIN32
17     if ((HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) return true;
18 #endif    
19     int nfd = dup(fd);
20     if (nfd >= 0)
21     {
22         close(nfd);
23         return true;
24     }
25     return false;
26
27 }
28
29
30 static int fd_count(const char *when)
31 {
32     int count = 0;
33     
34     printf("fds open at %s:", when);
35     
36     for (int fd = 0; fd < 1024; fd++)
37     {
38         if (fd_is_valid(fd))
39         {
40             count++;
41             printf(" %d", fd);
42             fflush(stdout);
43         }
44     }
45     printf("\n");
46     
47     return count;
48 }
49
50
51 int main(int argc, char **argv)
52 {
53 #ifdef _WIN32
54     setup_console_crash();
55 #endif
56
57     // test wvtest itself.  Not very thorough, but you have to draw the
58     // line somewhere :)
59     WVPASS(true);
60     WVPASS(1);
61     WVFAIL(false);
62     WVFAIL(0);
63     int startfd, endfd;
64     char * const *prefixes = NULL;
65     
66     if (argc > 1)
67         prefixes = argv + 1;
68     
69     startfd = fd_count("start");
70     int ret = WvTest::run_all(prefixes);
71     
72     if (ret == 0) // don't pollute the strace output if we failed anyway
73     {
74         endfd = fd_count("end");
75     
76         WVPASS(startfd == endfd);
77 #ifndef _WIN32
78         if (startfd != endfd)
79             system(WvString("ls -l /proc/%s/fd", getpid()));
80 #endif    
81     }
82     
83     // keep 'make' from aborting if this environment variable is set
84     if (getenv("WVTEST_NO_FAIL"))
85         return 0;
86     else
87         return ret;
88 }