Line data Source code
1 : /*
2 : Simple buffer UNIT test
3 :
4 : Basic buffer manipulation routines.
5 :
6 : Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "config.h"
21 : #include <stdlib.h>
22 : #include <stdio.h>
23 : #include <string.h>
24 : #include <unistd.h>
25 : #include <fcntl.h>
26 : #include <sys/stat.h>
27 : #define TRACE_HOME
28 : #include "trace.h"
29 : #include "simplebuffer.h"
30 :
31 :
32 : int verbose = 0;
33 :
34 : #define BOOUT(foo) \
35 : do { \
36 : if (verbose) foo; \
37 : } while(0)
38 :
39 :
40 1 : static int simple_test(void)
41 : {
42 1 : int error = EOK;
43 1 : struct simplebuffer *data = NULL;
44 1 : char str1[] = "test string 1";
45 1 : char str2[] = "test string 2";
46 1 : const char str3[] = "test string 3";
47 1 : uint32_t left = 0;
48 : int i;
49 : const unsigned char *buf;
50 :
51 1 : BOOUT(printf("Simple test start.\n"));
52 :
53 1 : error = simplebuffer_alloc(&data);
54 1 : if (error) {
55 0 : printf("Failed to allocate object %d\n", error);
56 0 : return error;
57 : }
58 :
59 1 : error = simplebuffer_add_raw(data,
60 : (void *)str1,
61 1 : strlen(str1),
62 : 1);
63 1 : if (error) {
64 0 : printf("Failed to add string to an object %d\n", error);
65 0 : simplebuffer_free(data);
66 0 : return error;
67 : }
68 :
69 1 : error = simplebuffer_add_cr(data);
70 1 : if (error) {
71 0 : printf("Failed to add CR to an object %d\n", error);
72 0 : simplebuffer_free(data);
73 0 : return error;
74 : }
75 :
76 1 : error = simplebuffer_add_raw(data,
77 : (void *)str2,
78 1 : strlen(str2),
79 : 1);
80 1 : if (error) {
81 0 : printf("Failed to add string to an object %d\n", error);
82 0 : simplebuffer_free(data);
83 0 : return error;
84 : }
85 :
86 1 : error = simplebuffer_add_cr(data);
87 1 : if (error) {
88 0 : printf("Failed to add CR to an object %d\n", error);
89 0 : simplebuffer_free(data);
90 0 : return error;
91 : }
92 :
93 1 : error = simplebuffer_add_str(data,
94 : str3,
95 : strlen(str3),
96 : 1);
97 1 : if (error) {
98 0 : printf("Failed to add string to an object %d\n", error);
99 0 : simplebuffer_free(data);
100 0 : return error;
101 : }
102 :
103 1 : left = simplebuffer_get_len(data);
104 1 : buf = simplebuffer_get_buf(data);
105 :
106 1 : BOOUT(for(i = 0; i < left; i++) {
107 : printf("%02d: %02X\n", i, buf[i]);
108 : });
109 :
110 1 : if (verbose) {
111 0 : while (left > 0) {
112 0 : error = simplebuffer_write(1, data, &left);
113 0 : if (error) {
114 0 : printf("Failed to write to output %d\n", error);
115 0 : simplebuffer_free(data);
116 0 : return error;
117 : }
118 : }
119 : }
120 :
121 1 : BOOUT(printf("\n[%s]\n", simplebuffer_get_buf(data)));
122 1 : BOOUT(printf("Length: %d\n", simplebuffer_get_len(data)));
123 :
124 :
125 1 : simplebuffer_free(data);
126 :
127 1 : BOOUT(printf("Simple test end.\n"));
128 1 : return error;
129 : }
130 :
131 1 : int main(int argc, char *argv[])
132 : {
133 1 : int error = EOK;
134 :
135 1 : if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
136 :
137 1 : BOOUT(printf("Start\n"));
138 :
139 1 : if ((error = simple_test())) {
140 0 : printf("Test failed! Error %d.\n", error);
141 0 : return -1;
142 : }
143 :
144 1 : BOOUT(printf("Success!\n"));
145 : return 0;
146 : }
|