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 129 130 131 132 133 134 135 136 137
|
#!/bin/sh
. ../../dttools/test/test_runner_common.sh
exe="buffer.test"
prepare()
{
${CC} -I../src/ -g $CCTOOLS_TEST_CCFLAGS -o "$exe" -x c - -x none ../src/libdttools.a -lm <<EOF
#include "buffer.h"
#include "debug.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define check(cmp,expr) \
do {\
int rc = (expr);\
if (!(cmp rc))\
fatal("[%s:%d]: unexpected failure: %s %d '%s'", __FILE__, __LINE__, #cmp, rc, strerror(errno));\
} while (0)
int main (int argc, char *argv[])
{
buffer_t B;
int i;
size_t len;
static char test[1<<20];
char buf1[4];
char buf2[1<<12];
char buf3[1<<13] = "a";
/* test that buffer starts with empty string */
buffer_init(&B);
buffer_ubuf(&B, buf3, sizeof(buf3));
check(0 ==, strcmp(buffer_tostring(&B), ""));
buffer_free(&B);
buffer_init(&B);
buffer_ubuf(&B, buf1, sizeof(buf1));
check(1 ==, buffer_putliteral(&B, "a"));
check(1 ==, buffer_putstring(&B, "b"));
check(1 ==, buffer_putlstring(&B, "cd", 1));
check(2 ==, buffer_putstring(&B, "de"));
check(0 ==, strcmp(buffer_tostring(&B), "abcde"));
buffer_free(&B);
/* small buffer shouldn't be used */
buffer_init(&B);
buffer_ubuf(&B, buf1, sizeof(buf1));
strcpy(test, "");
for (i = 0; i < 1<<12; i++) {
check(1 ==, buffer_putstring(&B, "a"));
strcat(test, "a");
}
check(0 ==, strcmp(buffer_tolstring(&B, &len), test));
buffer_free(&B);
/* this buffer is equal to initial and won't be used */
buffer_init(&B);
buffer_ubuf(&B, buf2, sizeof(buf2));
strcpy(test, "");
for (i = 0; i < 1<<12; i++) {
check(1 ==, buffer_putstring(&B, "a"));
strcat(test, "a");
}
check(0 ==, strcmp(buffer_tolstring(&B, &len), test));
buffer_free(&B);
/* testing max */
buffer_init(&B);
buffer_ubuf(&B, buf2, sizeof(buf2));
buffer_max(&B, 1<<12);
for (i = 0; i < (1<<12)-1; i++) {
check(1 ==, buffer_putstring(&B, "a"));
}
check(-1 ==, buffer_putstring(&B, "a"));
buffer_free(&B);
/* this buffer should be used */
buffer_init(&B);
buffer_ubuf(&B, buf3, sizeof(buf3));
buffer_max(&B, 1<<13);
strcpy(test, "");
for (i = 0; i < 1<<12; i++) {
check(1 ==, buffer_putstring(&B, "a"));
strcat(test, "a");
}
check(0 ==, strcmp(buffer_tolstring(&B, &len), test));
buffer_free(&B);
/* test max again */
buffer_init(&B);
buffer_ubuf(&B, buf3, sizeof(buf3));
buffer_max(&B, 1<<14);
for (i = 0; i < (1<<14)-1; i++) {
check(1 ==, buffer_putstring(&B, "a"));
}
check(-1 ==, buffer_putstring(&B, "a"));
buffer_free(&B);
/* testing heap growth */
buffer_init(&B);
buffer_ubuf(&B, buf3, sizeof(buf3));
for (i = 0; i < 1<<20; i++)
test[i] = 'a';
for (i = 0; i < 1<<20; i++) {
check(1 ==, buffer_putstring(&B, "a"));
}
check(0 ==, strcmp(buffer_tolstring(&B, &len), test));
check(1<<20 ==, len);
buffer_free(&B);
return 0;
}
EOF
return $?
}
run()
{
./"$exe"
return $?
}
clean()
{
rm -f "$exe"
return 0
}
dispatch "$@"
# vim: set noexpandtab tabstop=4:
|