console: correctly resize lines
[platform/upstream/kmscon.git] / tests / test_buffer.c
1 /*
2  * test_buffer - Test Buffer
3  *
4  * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /*
28  * Test Buffer
29  * This runs some stress tests on the buffer implementation. It can also be used
30  * to run some performance benchmarks and similar.
31  * This does not access the display output or perform any other _useful_ action.
32  *
33  * Use this as playground for new buffer features.
34  */
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include "console.h"
41 #include "log.h"
42
43 /* print buffer with boundary */
44 static void print_buf(struct kmscon_buffer *buf)
45 {
46         unsigned int width, height, x, y;
47         struct kmscon_char *ch;
48         int ret;
49         const char *c;
50
51         ret = kmscon_char_new(&ch);
52         if (ret) {
53                 log_warning("Cannot allocate character\n");
54                 return;
55         }
56
57         width = kmscon_buffer_get_width(buf);
58         height = kmscon_buffer_get_height(buf);
59
60         log_info("Buffer: %ux%u\n", width, height);
61
62         printf("x");
63         for (x = 0; x < width; ++x)
64                 printf("x");
65         printf("x\n");
66
67         for (y = 0; y < height; ++y) {
68                 printf("x");
69                 for (x = 0; x < width; ++x) {
70                         kmscon_buffer_read(buf, x, y, ch);
71                         c = kmscon_char_get_u8(ch);
72                         printf("%c", c ? *c : ' ');
73                 }
74                 printf("x\n");
75         }
76
77         printf("x");
78         for (x = 0; x < width; ++x)
79                 printf("x");
80         printf("x\n");
81
82         kmscon_char_free(ch);
83 }
84
85 static void test1(struct kmscon_buffer *buf)
86 {
87         int ret;
88         struct kmscon_char *ch;
89
90         log_info("Test1:\n");
91
92         ret = kmscon_char_new_u8(&ch, "?", 1);
93         if (ret) {
94                 log_err("Cannot create character\n");
95                 return;
96         }
97
98         kmscon_buffer_write(buf, 0, 0, ch);
99         kmscon_buffer_write(buf, 9, 2, ch);
100         kmscon_buffer_write(buf, 4, 4, ch);
101         kmscon_buffer_rotate(buf);
102         print_buf(buf);
103         kmscon_buffer_resize(buf, 5, 3);
104         print_buf(buf);
105         kmscon_buffer_resize(buf, 20, 5);
106         print_buf(buf);
107         kmscon_buffer_write(buf, 15, 1, ch);
108         print_buf(buf);
109         kmscon_buffer_rotate(buf);
110         print_buf(buf);
111
112         kmscon_char_free(ch);
113 }
114
115 int main(int argc, char **argv)
116 {
117         struct kmscon_buffer *buf;
118         int ret;
119
120         ret = kmscon_buffer_new(&buf, 10, 5);
121         if (ret) {
122                 log_err("Cannot create buffer object\n");
123                 goto err_out;
124         }
125
126         test1(buf);
127
128         kmscon_buffer_unref(buf);
129 err_out:
130         return abs(ret);
131 }