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