eloop: move prefix to "ev_" instead of "kmscon_"
[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 #include "unicode.h"
43
44 /* print buffer with boundary */
45 static void print_buf(struct kmscon_buffer *buf)
46 {
47         unsigned int width, height, x, y;
48         kmscon_symbol_t ch;
49
50         width = kmscon_buffer_get_width(buf);
51         height = kmscon_buffer_get_height(buf);
52
53         log_info("Buffer: %ux%u\n", width, height);
54
55         printf("x");
56         for (x = 0; x < width; ++x)
57                 printf("x");
58         printf("x\n");
59
60         for (y = 0; y < height; ++y) {
61                 printf("x");
62                 for (x = 0; x < width; ++x) {
63                         ch = kmscon_buffer_read(buf, x, y);
64                         printf("%c", ch ? ch : ' ');
65                 }
66                 printf("x\n");
67         }
68
69         printf("x");
70         for (x = 0; x < width; ++x)
71                 printf("x");
72         printf("x\n");
73 }
74
75 static void test1(struct kmscon_buffer *buf)
76 {
77         kmscon_symbol_t ch;
78
79         log_info("Test1:\n");
80
81         kmscon_buffer_set_margins(buf, 1, 1);
82         ch = kmscon_symbol_make('?');
83
84         kmscon_buffer_write(buf, 0, 0, ch);
85         kmscon_buffer_write(buf, 2, 0, ch);
86         kmscon_buffer_write(buf, 4, 0, ch);
87         kmscon_buffer_write(buf, 1, 4, ch);
88         kmscon_buffer_write(buf, 3, 4, ch);
89         kmscon_buffer_write(buf, 5, 4, ch);
90         kmscon_buffer_write(buf, 9, 1, ch);
91         kmscon_buffer_write(buf, 9, 2, ch);
92         print_buf(buf);
93         kmscon_buffer_resize(buf, 20, 3);
94         print_buf(buf);
95         kmscon_buffer_resize(buf, 20, 5);
96         print_buf(buf);
97         kmscon_buffer_write(buf, 15, 1, ch);
98         print_buf(buf);
99         kmscon_buffer_scroll_up(buf, 1);
100         print_buf(buf);
101 }
102
103 static void test2()
104 {
105         struct kmscon_symbol_table *st;
106         int ret;
107         kmscon_symbol_t sym, sym2, sym3, sym4;
108         const uint32_t *str;
109         size_t len, i;
110
111         log_info("Test2:\n");
112
113         ret = kmscon_symbol_table_new(&st);
114         if (ret)
115                 return;
116
117         sym = kmscon_symbol_make('a');
118         sym2 = kmscon_symbol_append(st, sym, '^');
119         sym3 = kmscon_symbol_append(st, sym2, '^');
120         sym4 = kmscon_symbol_append(st, sym, '^');
121
122         log_info("equality: %i %i %i\n", sym == sym2, sym2 == sym4,
123                                                                 sym3 == sym2);
124
125         str = kmscon_symbol_get(st, &sym3, &len);
126
127         printf("sym3: ");
128         for (i = 0; i < len; ++i)
129                 printf("%c", str[i]);
130         printf("\n");
131
132         kmscon_symbol_table_unref(st);
133 }
134
135 static void test3()
136 {
137         int ret, i;
138         struct kmscon_utf8_mach *mach;
139         char buf[] = { 'a', 'b', 0xE2, 0x82, 0xAC };
140         uint32_t val;;
141
142         log_info("Test3:\n");
143
144         ret = kmscon_utf8_mach_new(&mach);
145         if (ret)
146                 return;
147
148         for (i = 0; i < sizeof(buf); ++i) {
149                 ret = kmscon_utf8_mach_feed(mach, buf[i]);
150                 if (ret == KMSCON_UTF8_ACCEPT) {
151                         val = kmscon_utf8_mach_get(mach);
152                         printf("ret: %d 0x%X\n", ret, val);
153                 }
154         }
155
156         kmscon_utf8_mach_free(mach);
157 }
158
159 int main(int argc, char **argv)
160 {
161         struct kmscon_buffer *buf;
162         int ret;
163
164         ret = kmscon_buffer_new(&buf, 10, 5);
165         if (ret) {
166                 log_err("Cannot create buffer object\n");
167                 goto err_out;
168         }
169         kmscon_buffer_set_max_sb(buf, 128);
170
171         test1(buf);
172         test2();
173         test3();
174
175         kmscon_buffer_unref(buf);
176 err_out:
177         return abs(ret);
178 }