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
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stringhelper.h"
void test_substr_replace_unmoving_insertion(void) {
char *source = calloc(1, 1024);
char *dest = calloc(1, 1024);
int slen = -1, dlen = -1, dalloc = 1024;
sprintf(source, "world");
sprintf(dest, "hello there");
substr_replace(&dest, &dlen, &dalloc, 6, strlen(dest), source, slen);
CU_ASSERT(!strcmp(dest, "hello world"));
CU_ASSERT(dalloc == 1024);
CU_ASSERT(dlen == 11);
free(source);
free(dest);
}
void test_substr_replace_alloc_and_insert_from_null_dest(void) {
char *source = "hello world";
char *dest = NULL;
int dlen = 0, dalloc = 0;
substr_replace(&dest, &dlen, &dalloc, 0, 0, source, -1);
CU_ASSERT(!strcmp(dest, source));
CU_ASSERT(dlen == strlen(source));
CU_ASSERT(dalloc > dlen);
free(dest);
}
void test_substr_replace_multiple(void) {
char *dest = NULL;
int dlen = 0, dalloc = 0;
//int x = 0;
//printf("%d: %.*s\n", x++, dlen, dest);
substr_replace(&dest, &dlen, &dalloc, 0, 0, "hello", -1);
substr_replace(&dest, &dlen, &dalloc, dlen, 0, " ", -1);
substr_replace(&dest, &dlen, &dalloc, dlen, 0, "world", -1);
const char *expect = "hello world";
CU_ASSERT(!strcmp(dest, expect));
CU_ASSERT(dlen == strlen(expect));
CU_ASSERT(dalloc > dlen);
free(dest);
}
void test_substr_replace_remove(void) {
char *source = strdup("hello world");
int len = strlen(source);
int size = len + 1;
//printf("\n--\n%s\n", source);
substr_replace(&source, &len, &size, 5, len, "", 0);
//printf("\n--\n%s\n", source);
CU_ASSERT(!strcmp(source, "hello"));
}
void test_substr_replace_lenchange(void) {
char *source = strdup("hello world test");
int len = strlen(source);
int size = len + 1;
CU_ASSERT(len == 16);
substr_replace(&source, &len, &size, 5, len, "", 0);
CU_ASSERT(len == 5);
}
void test_string_escape_c(void) {
struct {
char *input;
char *output;
} data[] = {
{ "no change", "no change" },
{ "quoty \" ?", "quoty \\\" ?" },
{ "test \n", "test \\n" },
{ "test \r", "test \\r" },
{ "test \f", "test \\f" },
{ "test \a", "test \\a" },
{ "test \b", "test \\b" },
{ "test \\", "test \\\\" },
{ NULL, NULL },
};
int i = 0;
for (i = 0; data[i].input != NULL ; i++) {
int len, size;
char *s = strdup(data[i].input);
len = strlen(s);
size = len + 1;
string_escape(&s, &len, &size, "\\\"", 2, ESCAPE_LIKE_C);
string_escape(&s, &len, &size, "", 0, ESCAPE_LIKE_C | ESCAPE_NONPRINTABLE);
//printf("\n");
//printf("'%s' vs '%s' ('%s')\n", data[i].input, s, data[i].output);
//printf("\n");
if (strcmp(s, data[i].output)) {
printf("\n");
printf("'%s' vs '%s' ('%s')\n", data[i].input, s, data[i].output);
printf("\n");
}
CU_ASSERT(!strcmp(s, data[i].output));
free(s);
}
}
void test_string_ndup(void) {
char data[] = "hello there";
char *p;
p = string_ndup(data, 5);
CU_ASSERT(!strcmp(p, "hello"));
}
|