]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_delwin.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / test_delwin.c
1 /****************************************************************************
2  * Copyright 2022,2023 Thomas E. Dickey                                     *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30  * $Id: test_delwin.c,v 1.5 2023/05/27 20:34:51 tom Exp $
31  */
32 #include <test.priv.h>
33
34 #define STATUS 10
35
36 static SCREEN *my_screen;
37
38 static void
39 show_rc(const char *what, const char *explain, int rc)
40 {
41     printw("%s : %d (%s)\n", what, rc, explain);
42 }
43
44 static void
45 next_step(WINDOW *win)
46 {
47     int ch = wgetch(win);
48     if (ch == QUIT || ch == ESCAPE) {
49         endwin();
50         /* use this to verify if delscreen frees all memory */
51         delscreen(my_screen);
52         exit(EXIT_FAILURE);
53     }
54 }
55
56 static void
57 usage(int ok)
58 {
59     static const char *msg[] =
60     {
61         "Usage: test_delwin [options]"
62         ,""
63         ,USAGE_COMMON
64     };
65     size_t n;
66
67     for (n = 0; n < SIZEOF(msg); n++)
68         fprintf(stderr, "%s\n", msg[n]);
69
70     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
71 }
72 /* *INDENT-OFF* */
73 VERSION_COMMON()
74 /* *INDENT-ON* */
75
76 int
77 main(int argc, char **argv)
78 {
79     WINDOW *parent, *child1;
80     int rc;
81     int ch;
82
83     while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
84         switch (ch) {
85         case OPTS_VERSION:
86             show_version(argv);
87             ExitProgram(EXIT_SUCCESS);
88         default:
89             usage(ch == OPTS_USAGE);
90             /* NOTREACHED */
91         }
92     }
93     if (optind < argc)
94         usage(FALSE);
95
96     if ((my_screen = newterm(NULL, stdout, stdin)) == NULL)
97         ExitProgram(EXIT_FAILURE);
98
99     noecho();
100     cbreak();
101
102     refresh();
103     wsetscrreg(stdscr, 0, STATUS - 1);
104     scrollok(stdscr, TRUE);
105
106     parent = newwin(0, 0, STATUS, 0);
107     box(parent, 0, 0);
108     wrefresh(parent);
109     next_step(parent);
110
111     printw("New window %p    %s\n", (void *) parent, "Top window");
112     mvwprintw(parent, 1, 1, "Top window");
113     wrefresh(parent);
114     next_step(stdscr);
115
116     child1 = derwin(parent, LINES - STATUS - 4, COLS - 4, 2, 2);
117     box(child1, 0, 0);
118     mvwprintw(child1, 1, 1, "Sub window");
119     wrefresh(child1);
120
121     printw("Sub window %p    %s\n", (void *) child1, "Hello world!");
122     next_step(stdscr);
123
124     show_rc("Deleted parent",
125             "should fail, it still has a subwindow",
126             delwin(parent));
127     next_step(stdscr);
128     show_rc("Deleted child1",
129             "should succeed",
130             rc = delwin(child1));
131     next_step(stdscr);
132     if (rc == OK) {
133         wclrtobot(parent);
134         box(parent, 0, 0);
135         next_step(parent);
136     }
137     show_rc("Deleted parent",
138             "should succeed, it has no subwindow now",
139             rc = delwin(parent));
140     next_step(stdscr);
141     if (rc == OK) {
142         touchwin(stdscr);
143         next_step(stdscr);
144     }
145     show_rc("Deleted parent",
146             "should fail, may dump core",
147             delwin(parent));
148     next_step(stdscr);
149     endwin();
150     ExitProgram(EXIT_SUCCESS);
151 }