]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_delwin.c
450dad1e62f08988349899fef96d3da31ad8a87b
[ncurses.git] / test / test_delwin.c
1 /****************************************************************************
2  * Copyright 2022 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.3 2022/08/14 00:12:52 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 int
57 main(void)
58 {
59     WINDOW *parent, *child1;
60     int rc;
61
62     if ((my_screen = newterm(NULL, stdout, stdin)) == NULL)
63         ExitProgram(EXIT_FAILURE);
64
65     noecho();
66     cbreak();
67
68     refresh();
69     wsetscrreg(stdscr, 0, STATUS - 1);
70     scrollok(stdscr, TRUE);
71
72     parent = newwin(0, 0, STATUS, 0);
73     box(parent, 0, 0);
74     wrefresh(parent);
75     next_step(parent);
76
77     printw("New window %p    %s\n", (void *) parent, "Top window");
78     mvwprintw(parent, 1, 1, "Top window");
79     wrefresh(parent);
80     next_step(stdscr);
81
82     child1 = derwin(parent, LINES - STATUS - 4, COLS - 4, 2, 2);
83     box(child1, 0, 0);
84     mvwprintw(child1, 1, 1, "Sub window");
85     wrefresh(child1);
86
87     printw("Sub window %p    %s\n", (void *) child1, "Hello world!");
88     next_step(stdscr);
89
90     show_rc("Deleted parent",
91             "should fail, it still has a subwindow",
92             rc = delwin(parent));
93     next_step(stdscr);
94     show_rc("Deleted child1",
95             "should succeed",
96             rc = delwin(child1));
97     next_step(stdscr);
98     if (rc == OK) {
99         wclrtobot(parent);
100         box(parent, 0, 0);
101         next_step(parent);
102     }
103     show_rc("Deleted parent",
104             "should succeed, it has no subwindow now",
105             rc = delwin(parent));
106     next_step(stdscr);
107     if (rc == OK) {
108         touchwin(stdscr);
109         next_step(stdscr);
110     }
111     show_rc("Deleted parent",
112             "should fail, may dump core",
113             delwin(parent));
114     next_step(stdscr);
115     endwin();
116     ExitProgram(EXIT_SUCCESS);
117 }