implement missing function in buffer.c
[libdht.git] / regress / buf.c
blob1ee41cb52e69b13fec5c91ed531dc172ac7550b4
1 /*
2 * Copyright (c) 2016 Mohamed Aslan <maslan@sce.carleton.ca>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <buffer.h>
21 #include <sys/param.h>
23 int
24 main(int argc, char **argv)
26 struct rbuffer *rb;
27 char *str;
28 int i;
30 rb = rbuffer_new(10);
31 if (rb == NULL)
32 return 1;
33 rbuffer_write(rb, "1234567890XY", 12);
35 /* test: insertion */
36 if (strncmp(rb->rb_data, "XY34567890", 10)) {
37 printf("FAILED: ");
38 for (i = 0 ; i < rb->rb_capacity ; i++)
39 printf("%c ", rb->rb_data[i]);
40 printf(" [%c]\n", *rb->rb_rp);
41 return 1;
44 rbuffer_write(rb, "ABCDEFGHI", 9);
45 if (strncmp(rb->rb_data, "IYABCDEFGH", 10)) {
46 printf("FAILED: ");
47 for (i = 0 ; i < rb->rb_capacity ; i++)
48 printf("%c ", rb->rb_data[i]);
49 printf(" [%c]\n", *rb->rb_rp);
50 return 1;
53 rbuffer_write(rb, "J", 1);
54 if (strncmp(rb->rb_data, "IJABCDEFGH", 10)) {
55 printf("FAILED: ");
56 for (i = 0 ; i < rb->rb_capacity ; i++)
57 printf("%c ", rb->rb_data[i]);
58 printf(" [%c]\n", *rb->rb_rp);
59 return 1;
62 /* test: read */
63 str = rbuffer_read(rb, 5);
64 if (str == NULL)
65 return 1;
66 if (strncmp(str, "ABCDE", 5)) {
67 printf("FAILED: ");
68 for (i = 0 ; i < 5 ; i++)
69 printf("%c ", str[i]);
70 printf("\n");
71 return 1;
73 free(str);
75 str = rbuffer_read(rb, 5);
76 if (str == NULL)
77 return 1;
78 if (strncmp(str, "FGHIJ", 5)) {
79 printf("FAILED: ");
80 for (i = 0 ; i < 5 ; i++)
81 printf("%c ", str[i]);
82 printf("\n");
83 return 1;
85 free(str);
87 /* test: read null */
88 str = rbuffer_read(rb, 2);
89 if (str != NULL)
90 return 1;
92 /* test: reset */
93 rbuffer_write(rb, "mnopqrs", 7);
94 str = rbuffer_read(rb, 5);
95 if (str == NULL)
96 return 1;
97 if (strncmp(str, "mnopq", 5)) {
98 printf("FAILED: ");
99 for (i = 0 ; i < 5 ; i++)
100 printf("%c ", str[i]);
101 printf("\n");
102 return 1;
104 free(str);
106 /* test: readline */
107 rbuffer_write(rb, "QWERTYUIOP\n", 11);
108 str = rbuffer_readline(rb);
109 if (str == NULL)
110 return 1;
111 if (strncmp(str, "WERTYUIOP", 10)) {
112 printf("FAILED: ");
113 for (i = 0 ; i < 10 ; i++)
114 printf("%c ", str[i]);
115 printf("\n");
116 return 1;
118 free(str);
120 rbuffer_write(rb, "\n", 1);
121 str = rbuffer_readline(rb);
122 if (str == NULL)
123 return 1;
124 if (strcmp(str, "")) {
125 printf("FAILED\n");
126 return 1;
129 rbuffer_free(rb);
131 return 0;