From 0617291d3404651c5aab98ce235d9afa1aeb05e4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 5 Mar 2011 14:24:23 +0000 Subject: Remove moved files. --- Makefile | 15 - dynstr.c | 547 ------------------------- dynstr.h | 187 --------- unittests.c | 1287 ----------------------------------------------------------- 4 files changed, 2036 deletions(-) delete mode 100644 Makefile delete mode 100644 dynstr.c delete mode 100644 dynstr.h delete mode 100644 unittests.c diff --git a/Makefile b/Makefile deleted file mode 100644 index ecfdb87..0000000 --- a/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -CFLAGS = --std=c99 -Wall -Werror --coverage -g -LDFLAGS = --coverage -g - -all: unittests - -dynstr.o unittests.o: dynstr.h - -unittests: unittests.o dynstr.o - -check: all - valgrind --leak-check=full --quiet ./unittests - -clean: - rm -f *.o *.a *.so *.gcov *.gcda *.gcno core unittests unittest.tmp.?????? - diff --git a/dynstr.c b/dynstr.c deleted file mode 100644 index 44c4b56..0000000 --- a/dynstr.c +++ /dev/null @@ -1,547 +0,0 @@ -/* - * dynstr.c - implement dynamic byte strings - * Copyright 2010 Lars Wirzenius - * - * 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 . - */ - - -/* We shamelessly use GNU extensions to the standard C library. - * Primarily this is the memmem(3) function. Patches are welcome to - * get rid of this, but I did not feel like re-inventing the wheel. - */ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include - -#include "dynstr.h" - - -/* This is how the dynamic strings are represented. */ -struct Dynstr { - unsigned char *mem; - size_t size; -}; - - -/* Return value for readline_helper's callback functions. */ -#define DYNSTR_ERROR ((size_t) -1) - - -/* The current malloc and malloc error handler functions. */ -static dynstr_error_handler *error_handler = dynstr_malloc_error_indicate; -static void *(*current_malloc)(size_t) = malloc; - - -void dynstr_init(void) -{ - error_handler = dynstr_malloc_error_indicate; - current_malloc = malloc; -} - - -void *(*dynstr_get_malloc(void))(size_t) -{ - return current_malloc; -} - - -void dynstr_set_malloc(void *(*new_malloc)(size_t)) -{ - current_malloc = new_malloc; -} - - -dynstr_error_handler *dynstr_get_malloc_error_handler(void) -{ - return error_handler; -} - - -void dynstr_set_malloc_error_handler(dynstr_error_handler *handler) -{ - error_handler = handler; -} - - -void dynstr_malloc_error_indicate(int error, size_t size, void *oldptr) -{ -} - -void dynstr_malloc_error_abort(int error, size_t size, void *oldptr) -{ - abort(); -} - - -/* Dynamically allocate a number of bytes. Call error handler if necessary. */ -static void *alloc(size_t size) -{ - void *p; - - p = current_malloc(size); - if (p == NULL) - error_handler(errno, size, NULL); - return p; -} - - -static Dynstr *new_dynstr(const void *mem, size_t size, bool dynamic) -{ - Dynstr *dynstr; - - if (dynamic) { - dynstr = alloc(sizeof(Dynstr) + size); - if (dynstr == NULL) - return NULL; - dynstr->mem = ((unsigned char *) dynstr) + sizeof(Dynstr); - } else { - dynstr = alloc(sizeof(Dynstr)); - if (dynstr == NULL) - return NULL; - dynstr->mem = (unsigned char *) mem; - } - - dynstr->size = size; - return dynstr; -} - - -static void init_dynstr(Dynstr *dynstr, const void *mem, bool dynamic) -{ - if (dynamic) - memcpy(dynstr->mem, mem, dynstr->size); -} - - -/* Allocate a new dynamic string. If dynamic is true, the contents of the - * new string is copied from mem. Otherwise the new string just uses mem - * directly. */ -static Dynstr *newstr(const void *mem, size_t size, bool dynamic) -{ - Dynstr *dynstr; - - dynstr = new_dynstr(mem, size, dynamic); - if (dynstr != NULL) - init_dynstr(dynstr, mem, dynamic); - return dynstr; -} - - -Dynstr *dynstr_new_empty(void) -{ - return new_dynstr(NULL, 0, false); -} - - -Dynstr *dynstr_new_from_cstring(const char *cstring) -{ - return newstr(cstring, strlen(cstring), true); -} - - -Dynstr *dynstr_new_from_memory(const void *mem, size_t size) -{ - return newstr(mem, size, true); -} - - -Dynstr *dynstr_new_from_constant_cstring(const char *cstring) -{ - return newstr(cstring, strlen(cstring), false); -} - - -Dynstr *dynstr_new_from_constant_memory(const void *mem, size_t size) -{ - return newstr(mem, size, false); -} - - -void dynstr_free(Dynstr *dynstr) -{ - free(dynstr); -} - - -size_t dynstr_len(Dynstr *dynstr) -{ - return dynstr->size; -} - - -bool dynstr_is_empty(Dynstr *dynstr) -{ - return dynstr_len(dynstr) == 0; -} - - -size_t dynstr_memcpy(void *mem, Dynstr *dynstr, size_t offset, size_t size) -{ - size_t len; - - len = dynstr_len(dynstr); - if (offset >= len) - return 0; - if (size > len - offset) - size = len - offset; - memcpy(mem, dynstr->mem + offset, size); - return size; -} - - -char *dynstr_strdup(Dynstr *dynstr) -{ - char *mem; - - mem = alloc(dynstr->size + 1); - if (mem == NULL) - return NULL; - memcpy(mem, dynstr->mem, dynstr->size); - mem[dynstr->size] = '\0'; - return mem; -} - - -Dynstr *dynstr_substr(Dynstr *dynstr, size_t offset, size_t size) -{ - if (offset >= dynstr->size) { - offset = 0; - size = 0; - } else if (size > dynstr->size - offset) { - size = dynstr->size - offset; - } - return newstr(dynstr->mem + offset, size, true); -} - - -Dynstr *dynstr_cat(Dynstr *dynstr1, Dynstr *dynstr2) -{ - return dynstr_cat_many(dynstr1, dynstr2, (Dynstr *) NULL); -} - - -Dynstr *dynstr_cat_many(Dynstr *dynstr, ...) -{ - va_list args; - Dynstr *result; - size_t size; - size_t offset; - - va_start(args, dynstr); - size = 0; - for (Dynstr *p = dynstr; p != NULL; p = va_arg(args, Dynstr *)) - size += p->size; - va_end(args); - - result = new_dynstr(NULL, size, true); - if (result == NULL) - return NULL; - - va_start(args, dynstr); - offset = 0; - for (Dynstr *p = dynstr; p != NULL; p = va_arg(args, Dynstr *)) { - memcpy(result->mem + offset, p->mem, p->size); - offset += p->size; - } - va_end(args); - - return result; -} - - -int dynstr_byte_at(Dynstr *dynstr, size_t offset) -{ - if (offset >= dynstr->size) - return -1; - return dynstr->mem[offset]; -} - - -size_t dynstr_first_byte(Dynstr *dynstr, size_t offset, int byte) -{ - unsigned char *p; - - if (offset >= dynstr->size) - return DYNSTR_NOT_FOUND; - p = memchr(dynstr->mem + offset, byte, dynstr->size - offset); - if (p == NULL) - return DYNSTR_NOT_FOUND; - return (size_t) (p - dynstr->mem); -} - - -size_t dynstr_last_byte(Dynstr *dynstr, size_t offset, int byte) -{ - unsigned char *p; - - if (offset >= dynstr->size) - return DYNSTR_NOT_FOUND; - p = memrchr(dynstr->mem + offset, byte, dynstr->size - offset); - if (p == NULL) - return DYNSTR_NOT_FOUND; - return (size_t) (p - dynstr->mem); -} - - -size_t dynstr_first_string(Dynstr *dynstr, size_t offset, Dynstr *pattern) -{ - const unsigned char *p; - - if (offset >= dynstr->size) - return DYNSTR_NOT_FOUND; - if (pattern->size == 0) - return DYNSTR_NOT_FOUND; - p = memmem(dynstr->mem + offset, dynstr->size - offset, - pattern->mem, pattern->size); - if (p == NULL) - return DYNSTR_NOT_FOUND; - return (size_t) (p - dynstr->mem); -} - - -size_t dynstr_last_string(Dynstr *dynstr, size_t offset, Dynstr *pattern) -{ - size_t result; - size_t new_result; - - /* GNU libc lacks a memrmem function, so we loop to the last - * dynstr_first_string match. */ - result = DYNSTR_NOT_FOUND; - for (;;) { - new_result = dynstr_first_string(dynstr, offset, pattern); - if (new_result == DYNSTR_NOT_FOUND) - break; - result = new_result; - offset = result + 1; - } - return result; -} - - -int dynstr_cmp(Dynstr *dynstr1, Dynstr *dynstr2) -{ - int result; - size_t min_size; - - if (dynstr1->size < dynstr2->size) - min_size = dynstr1->size; - else - min_size = dynstr2->size; - result = memcmp(dynstr1->mem, dynstr2->mem, min_size); - if (result == 0) { - if (dynstr1->size < dynstr2->size) - return -1; - else if (dynstr1->size == dynstr2->size) - return 0; - else - return 1; - } - return result; -} - - -size_t dynstr_fwrite(FILE *file, Dynstr *dynstr) -{ - return fwrite(dynstr->mem, 1, dynstr->size, file); -} - - -ssize_t dynstr_write(int fd, Dynstr *dynstr) -{ - return write(fd, dynstr->mem, dynstr->size); -} - - -/* A helper function for dynstr_read and dynstr_fread. The callback does - * the actual reading. */ -static Dynstr *read_helper(size_t (*callback)(FILE *f, int fd, unsigned char *buf, - size_t size), - FILE *f, int fd, size_t size) -{ - Dynstr *dynstr; - Dynstr *dynstr2; - size_t n; - - dynstr = new_dynstr(NULL, size, true); - if (dynstr == NULL) - return NULL; - - n = callback(f, fd, dynstr->mem, size); - if (n == DYNSTR_ERROR) { - dynstr_free(dynstr); - return NULL; - } - - if (n < size) { - dynstr2 = dynstr_substr(dynstr, 0, n); - if (dynstr2 == NULL) { - dynstr->size = n; - } else { - dynstr_free(dynstr); - dynstr = dynstr2; - } - } - - return dynstr; -} - - -static size_t fread_callback(FILE *f, int fd, unsigned char *buf, size_t size) -{ - size_t n; - - n = fread(buf, 1, size, f); - if (n == 0 && ferror(f)) - return DYNSTR_ERROR; - return n; -} - - -static size_t read_callback(FILE *f, int fd, unsigned char *buf, size_t size) -{ - ssize_t n; - - do { - n = read(fd, buf, size); - } while (n == -1 && errno == EINTR); - if (n == -1) - return DYNSTR_ERROR; - return n; -} - - -Dynstr *dynstr_fread(FILE *file, size_t size) -{ - return read_helper(fread_callback, file, -1, size); -} - - -Dynstr *dynstr_read(int file, size_t size) -{ - return read_helper(read_callback, NULL, file, size); -} - - -/* Helper function for dynstr_readline and dynstr_freadline. The callback - * does the actual reading. Reading happens one byte at a time, since we - * must not read past the newline that terminates a line. */ -static Dynstr *readline_helper(int (*callback)(FILE *, int), FILE *f, int fd) -{ - Dynstr *line; - Dynstr *temp1; - Dynstr *temp2; - int c; - unsigned char buf[1024]; - size_t buflen; - - line = dynstr_new_empty(); - buflen = 0; - - for (;;) { - temp1 = NULL; - temp2 = NULL; - - c = callback(f, fd); - if (c == -1) - goto error; - else if (c == -2) - break; - - buf[buflen++] = c; - if (buflen == sizeof(buf)) { - temp1 = dynstr_new_from_constant_memory(buf, buflen); - if (temp1 == NULL) - goto error; - temp2 = dynstr_cat(line, temp1); - if (temp2 == NULL) - goto error; - dynstr_free(temp1); - dynstr_free(line); - line = temp2; - buflen = 0; - } - - if (c == '\n') - break; - } - - if (buflen > 0) { - temp1 = dynstr_new_from_constant_memory(buf, buflen); - if (temp1 == NULL) - goto error; - temp2 = dynstr_cat(line, temp1); - if (temp2 == NULL) - goto error; - dynstr_free(temp1); - dynstr_free(line); - line = temp2; - } - - return line; - -error: - dynstr_free(line); - dynstr_free(temp1); - dynstr_free(temp2); - return NULL; -} - - -static int freadline_callback(FILE *f, int fd) -{ - int c; - - c = getc(f); - if (c == EOF && ferror(f)) - return -1; - else if (c == EOF) - return -2; - else - return c; -} - - -Dynstr *dynstr_freadline(FILE *file) -{ - return readline_helper(freadline_callback, file, -1); -} - - -static int readline_callback(FILE *f, int fd) -{ - unsigned char c; - int n; - - do { - n = read(fd, &c, 1); - } while (n == -1 && errno == EINTR); - if (n == 0) - return -2; - else if (n == -1) - return -1; - else - return c; -} - - -Dynstr *dynstr_readline(int file) -{ - return readline_helper(readline_callback, NULL, file); -} - diff --git a/dynstr.h b/dynstr.h deleted file mode 100644 index 302902d..0000000 --- a/dynstr.h +++ /dev/null @@ -1,187 +0,0 @@ -/* - * dynstr.h - dynamic C string library - * - * This is a library for managing dynamic C strings of bytes (not - * multibyte characters). - * - * Each dynamic string is immutable: once created, it cannot be modified. - * To achieve changes, create new strings. This allows an implementation - * of the API to make use of copy-on-write techniques to save memory, - * and avoid unnecessary copying. - * - * Dynamic allocation can fail. By default, the routines in this library - * return NULL for failures. The caller may use the - * dynstr_set_malloc_error_handler function to set a function that gets - * called before NULL is returned. This function can, for example, abort - * the program (see dynstr_malloc_error_abort), or use longjmp to jump - * some error handling routine. - * - * 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 . - */ - -#ifndef DYNSTR_H -#define DYNSTR_H - -#include -#include -#include -#include - - -/* Version number of the shared library. */ -#define DYNSTR_VERSION_MAJOR 0 -#define DYNSTR_VERSION_MINOR 0 -#define DYNSTR_VERSION_PATCH 0 - - -/* Magic value returned by functions that search for things within - * dynamic strings, to indicate 'not found'. */ -#define DYNSTR_NOT_FOUND (~(size_t)0) - - -/* The abstract type for dynamic strings. The type is opaque: the caller - * may only operate on it using the functions declared in this header. */ -typedef struct Dynstr Dynstr; - - -/* Initialize or re-initialize the library. This sets all global variables - * used internally to their defaults. This is mainly useful for unit - * tests. */ -void dynstr_init(void); - -/* Set the memory allocator to use, instead of malloc. This is mainly - * useful for unit tests. */ -void dynstr_set_malloc(void *(*allocator)(size_t)); - -/* Get the memory allocator being used. This is mainly useful for unit - * tests. */ -void *(*dynstr_get_malloc(void))(size_t); - -/* Type of callback functions for handling malloc failures. */ -typedef void dynstr_error_handler(int error, size_t size, void *oldptr); - -/* Get and set malloc error handler. */ -dynstr_error_handler *dynstr_get_malloc_error_handler(void); -void dynstr_set_malloc_error_handler(dynstr_error_handler *handler); - -/* Malloc error handler that aborts the program (using abort, not exit). */ -void dynstr_malloc_error_abort(int error, size_t size, void *oldptr); - -/* Malloc error handler that does nothing. This is the default handler. - * It causes NULL to be returned. */ -void dynstr_malloc_error_indicate(int error, size_t size, void *oldptr); - - -/* Create a new, empty string. */ -Dynstr *dynstr_new_empty(void); - -/* Create a new string, copy contents from a NUL-terminated C string. */ -Dynstr *dynstr_new_from_cstring(const char *cstring); - -/* Create a new string, copy contents from an arbitrary memory area. */ -Dynstr *dynstr_new_from_memory(const void *mem, size_t size); - -/* Create a new string, use contents from a constant C string. The caller - * MUST make sure the contents of the C string do not change while the - * dynamic string exists, since the dynamic string will not copy the - * contents, but use it directly. */ -Dynstr *dynstr_new_from_constant_cstring(const char *cstring); - -/* Create a new string, use contents from an arbitrary memory area. The caller - * MUST make sure the contents of the memory area do not change while the - * dynamic string exists, since the dynamic string will not copy the - * contents, but use it directly. */ -Dynstr *dynstr_new_from_constant_memory(const void *mem, size_t size); - -/* Free a dynamic string. */ -void dynstr_free(Dynstr *dynstr); - -/* Return length of dynamic string, in bytes. */ -size_t dynstr_len(Dynstr *dynstr); - -/* Is the dynamic string empty? In other words, is its length 0? */ -bool dynstr_is_empty(Dynstr *dynstr); - -/* Create a new string, copying its contents from an existing one. */ -Dynstr *dynstr_substr(Dynstr *dynstr, size_t offset, size_t size); - -/* Create a new string by catenating two existing ones. Return NULL on - * failure. */ -Dynstr *dynstr_cat(Dynstr *dynstr1, Dynstr *dynstr2); - -/* Create a new string by catenating zero or more existing ones. The - * argument list has zero or more pointers to dynamic strings, and - * then a NULL pointer. */ -Dynstr *dynstr_cat_many(Dynstr *dynstr, ...); - -/* Return value of byte at a given offset. The value is non-negative. If - * the offset is past the end of the string, -1 is returned. */ -int dynstr_byte_at(Dynstr *dynstr, size_t offset); - -/* Copy contents of a dynamic string into a memory area. If the offset or - * size extends past the end of the string, only as much as actually exists - * in the string is actually copied. The rest of the output memory area will - * be filled with garbage. Return number of bytes actually copied, which may - * be less than requested, and may be zero. */ -size_t dynstr_memcpy(void *mem, Dynstr *dynstr, size_t offset, size_t size); - -/* Create a dynamically allocated C string copy of the dynamic string. - * This is like strdup. If the dynamic string contains NUL bytes, then - * that is the caller's problem. Return pointer to the C string. The - * caller is responsible for freeing it. Return NULL on error. */ -char *dynstr_strdup(Dynstr *dynstr); - -/* Search for first or last byte with a given value in a string, - * starting at a given offset and until the end of the string. Return offset - * of matching byte, or DYNSTR_NOT_FOUND if not found. Offset is from - * beginning of string. */ -size_t dynstr_first_byte(Dynstr *dynstr, size_t offset, int byte); -size_t dynstr_last_byte(Dynstr *dynstr, size_t offset, int byte); - -/* Search for first or last occurrence of a substring in a string, - * starting at a given offset and until the end of the string. Return - * offset of match, or DYNSTR_NOT_FOUND if not found. Offset is from - * beginning of string. */ -size_t dynstr_first_string(Dynstr *dynstr, size_t offset, Dynstr *pattern); -size_t dynstr_last_string(Dynstr *dynstr, size_t offset, Dynstr *pattern); - -/* Compare two strings, return 0 if they are equal, negative if the - * first comes before the second, and positive if second comes first. - * Comparisons are done byte-by-byte using unsigned values. */ -int dynstr_cmp(Dynstr *dynstr1, Dynstr *dynstr2); - -/* Write a dynamic string into an open file (using stdio). Return value is - * number of bytes written, just like for fwrite(3). - * If an error occurs, the size is less than the length of the string, - * and errno has been set by fwrite(3). */ -size_t dynstr_fwrite(FILE *file, Dynstr *dynstr); - -/* Write a dynamic string into an open Unix file handle. Return value is - * number of bytes written, or -1. */ -ssize_t dynstr_write(int file, Dynstr *dynstr); - -/* Read a number of bytes from an open file, either FILE or a Unix file - * handle. Return value is the string that was read. The string is empty - * to indicate EOF, or NULL for error. */ -Dynstr *dynstr_fread(FILE *file, size_t size); -Dynstr *dynstr_read(int file, size_t size); - -/* Read a line from an open file, either a FILE or a Unix file handle. - * The line ends with the first newline byte, or at the end of the file. - * If there was an error, NULL is returned, and errno is set, and the - * partially read line is discarded. */ -Dynstr *dynstr_freadline(FILE *file); -Dynstr *dynstr_readline(int file); - -#endif diff --git a/unittests.c b/unittests.c deleted file mode 100644 index 0ba5b33..0000000 --- a/unittests.c +++ /dev/null @@ -1,1287 +0,0 @@ -/* - * unittests.c - unit tests for dynamic byte strings - * Copyright 2010 Lars Wirzenius - * - * 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 . - */ - - -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "dynstr.h" - - -#define FAIL(msg) \ - do { \ - printf("FAIL: %s\n", msg); \ - return false; \ - } while (0) - - -#define FAIL_UNLESS(cond) \ - do { \ - if (!(cond)) { \ - printf("FAIL: %s:%d: %s\n", \ - __FUNCTION__, __LINE__, #cond); \ - return false;\ - } \ - } while (0) - - -#define FAIL_UNLESS_EQUAL(a,b) \ - do { \ - if ((a) != (b)) { \ - printf("FAIL: %s:%d: not equal: '%s' and '%s'\n", \ - __FUNCTION__, __LINE__, #a, #b); \ - return false;\ - } \ - } while (0) - - -#define MAX_STRINGS 128 -static Dynstr *strings[MAX_STRINGS]; -static int num_strings; - -static void add(Dynstr *dynstr) -{ - if (num_strings < MAX_STRINGS) { - strings[num_strings] = dynstr; - num_strings++; - } -} - -static Dynstr *new_from_cstring(const char *cstring) -{ - Dynstr *dynstr; - - dynstr = dynstr_new_from_cstring(cstring); - add(dynstr); - return dynstr; -} - -static Dynstr *substr(Dynstr *dynstr, size_t offset, size_t size) -{ - Dynstr *sub; - - sub = dynstr_substr(dynstr, offset, size); - add(sub); - return sub; -} - -static void free_all_strings(void) -{ - while (num_strings > 0) { - num_strings--; - dynstr_free(strings[num_strings]); - } -} - - -static int test_default_error_handler_is_indicate(void) -{ - FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), - dynstr_malloc_error_indicate); - return true; -} - - -static int test_sets_error_handler(void) -{ - dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); - FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), - dynstr_malloc_error_abort); - return true; -} - - -static int test_init_resets_error_handler(void) -{ - dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); - dynstr_init(); - FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), - dynstr_malloc_error_indicate); - return true; -} - - -static int test_indicate_handler_returns(void) -{ - dynstr_malloc_error_indicate(0, 0, NULL); - return true; -} - - -static jmp_buf env; -static void abort_handler(int signo) -{ - longjmp(env, 1); -} - - -static int test_abort_handler_calls_abort(void) -{ - struct sigaction act; - struct sigaction oldact; - - if (setjmp(env) == 0) { - act.sa_handler = abort_handler; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - sigaction(SIGABRT, &act, &oldact); - dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); - dynstr_malloc_error_abort(0, 0, 0); - FAIL("dynstr_malloc_error_abort returned"); - } else { - sigaction(SIGABRT, &oldact, NULL); - } - return true; -} - - -static bool error_handler_called; - -static void error_handler(int error, size_t size, void *oldptr) -{ - error_handler_called = true; -} - -static int fail_malloc_after; - -static void *fail_malloc(size_t size) -{ - if (fail_malloc_after <= 0) - return NULL; - --fail_malloc_after; - return malloc(size); -} - -static int test_alloc_calls_error_handler(void) -{ - Dynstr *dynstr; - - error_handler_called = false; - dynstr_set_malloc_error_handler(error_handler); - dynstr_set_malloc(fail_malloc); - dynstr = dynstr_new_empty(); - FAIL_UNLESS_EQUAL(dynstr, NULL); - FAIL_UNLESS_EQUAL(error_handler_called, true); - return true; -} - -static int test_alloc_does_not_call_error_handler(void) -{ - Dynstr *dynstr; - - error_handler_called = false; - dynstr_set_malloc_error_handler(error_handler); - dynstr_set_malloc(malloc); - dynstr = dynstr_new_empty(); - FAIL_UNLESS_EQUAL(error_handler_called, false); - dynstr_free(dynstr); - return true; -} - - -static int test_uses_malloc_by_default(void) -{ - dynstr_init(); - FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc); - return true; -} - - -static int test_sets_malloc(void) -{ - dynstr_set_malloc(fail_malloc); - FAIL_UNLESS_EQUAL(dynstr_get_malloc(), fail_malloc); - return true; -} - - -static int test_init_resets_malloc(void) -{ - dynstr_set_malloc(fail_malloc); - dynstr_init(); - FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc); - return true; -} - - -static int test_empty_string_is_empty(void) -{ - Dynstr *dynstr; - - dynstr = dynstr_new_empty(); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), 0); - FAIL_UNLESS_EQUAL(dynstr_is_empty(dynstr), true); - dynstr_free(dynstr); - return true; -} - - -static int test_new_returns_NULL_upon_first_allocation_failure(void) -{ - Dynstr *dynstr; - - dynstr_init(); - dynstr_set_malloc(fail_malloc); - dynstr = dynstr_new_empty(); - FAIL_UNLESS_EQUAL(dynstr, NULL); - return true; -} - - -static int test_creates_from_cstring(void) -{ - const char bytes[] = "asdfasdfafdasdfasdfqw4tb"; - char newbytes[sizeof(bytes)]; - Dynstr *dynstr; - size_t size; - - dynstr = new_from_cstring(bytes); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), strlen(bytes)); - - size = dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); - FAIL_UNLESS_EQUAL(size, strlen(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); - - return true; -} - - -static int test_creates_from_memory(void) -{ - const char bytes[] = "asdfasdfafdasdfasdfqw4tb"; - char newbytes[sizeof(bytes)]; - Dynstr *dynstr; - size_t size; - - dynstr = dynstr_new_from_memory(bytes, sizeof(bytes)); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(bytes)); - - size = dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); - FAIL_UNLESS_EQUAL(size, sizeof(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); - - dynstr_free(dynstr); - return true; -} - - -static int test_creates_from_constant_cstring(void) -{ - char bytes[] = "asdfasdfafdasdfasdfqw4tb"; - char newbytes[sizeof(bytes)]; - Dynstr *dynstr; - size_t size; - - dynstr = dynstr_new_from_constant_cstring(bytes); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), strlen(bytes)); - - size = dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); - FAIL_UNLESS_EQUAL(size, strlen(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); - - bytes[0] = 'x'; - dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); - - dynstr_free(dynstr); - return true; -} - - -static int test_creates_from_constant_memory(void) -{ - char bytes[] = "asdfasdfafdasdfasdfqw4tb"; - char newbytes[sizeof(bytes)]; - Dynstr *dynstr; - size_t size; - - dynstr = dynstr_new_from_constant_memory(bytes, sizeof(bytes)); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(bytes)); - - size = dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); - FAIL_UNLESS_EQUAL(size, sizeof(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); - - bytes[0] = 'x'; - dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); - FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); - - dynstr_free(dynstr); - return true; -} - - -static int test_memcpy_returns_zero_if_offset_is_too_large(void) -{ - Dynstr *dynstr; - char mem[1024]; - - dynstr = new_from_cstring("hello"); - FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, dynstr_len(dynstr), 1), 0); - FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, dynstr_len(dynstr) + 1, 1), 0); - return true; -} - - -static int test_memcpy_truncates_if_copying_too_much(void) -{ - Dynstr *dynstr; - char mem[1024]; - size_t len; - - dynstr = new_from_cstring("hello"); - len = dynstr_len(dynstr); - FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, 0, len), len); - FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, 0, len + 1), len); - return true; -} - - -static int test_memcpy_copies_whole_string_ok(void) -{ - Dynstr *dynstr; - const char data[] = "hello"; - char mem[1024]; - size_t len; - - dynstr = new_from_cstring(data); - len = dynstr_len(dynstr); - dynstr_memcpy(mem, dynstr, 0, len); - FAIL_UNLESS_EQUAL(memcmp(data, mem, len), 0); - return true; -} - - -static int test_memcpy_copies_substring_ok(void) -{ - Dynstr *dynstr; - const char data[] = "hello"; - char mem[1024]; - size_t len; - - dynstr = new_from_cstring(data); - len = dynstr_len(dynstr); - dynstr_memcpy(mem, dynstr, 1, len-2); - FAIL_UNLESS_EQUAL(memcmp(data + 1, mem, len-2), 0); - return true; -} - - -static int test_copies_empty_substring_ok(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 1, 0); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 0); - return true; -} - - -static int test_copies_single_byte_substring_ok(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 1, 1); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 1); - FAIL_UNLESS_EQUAL(memcmp(buf, "b", 1), 0); - return true; -} - - -static int test_copies_middle_substring_ok(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 1, 4); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 4); - FAIL_UNLESS_EQUAL(memcmp(buf, "bcde", 4), 0); - return true; -} - - -static int test_copies_initial_substring_ok(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 0, 5); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 5); - FAIL_UNLESS_EQUAL(memcmp(buf, "abcde", 5), 0); - return true; -} - - -static int test_copies_final_substring_ok(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 1, 5); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 5); - FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); - return true; -} - - -static int test_copies_empty_substring_when_offset_too_big(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 100, 1); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 0); - return true; -} - - -static int test_copies_partial_substring_when_length_too_big(void) -{ - Dynstr *dynstr; - Dynstr *sub; - size_t size; - char buf[1024]; - - dynstr = new_from_cstring("abcdef"); - sub = substr(dynstr, 1, 6); - size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); - FAIL_UNLESS_EQUAL(size, 5); - FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); - return true; -} - - -static int test_strdup_duplicates_string(void) -{ - Dynstr *dynstr; - char *cstring; - char data[] = "hello, world"; - - dynstr = dynstr_new_from_constant_cstring(data); - cstring = dynstr_strdup(dynstr); - FAIL_UNLESS_EQUAL(memcmp(data, cstring, sizeof(data)), 0); - free(cstring); - dynstr_free(dynstr); - return true; -} - - -static int test_cats_ok(void) -{ - Dynstr *a; - Dynstr *b; - Dynstr *c; - char buf[1024]; - size_t size; - - a = new_from_cstring("abc"); - b = new_from_cstring("def"); - c = dynstr_cat(a, b); - FAIL_UNLESS_EQUAL(dynstr_len(c), 6); - size = dynstr_memcpy(buf, c, 0, dynstr_len(c)); - FAIL_UNLESS_EQUAL(size, 6); - FAIL_UNLESS_EQUAL(memcmp(buf, "abcdef", 6), 0); - dynstr_free(c); - return true; -} - - -static int test_cat_returns_NULL_for_first_malloc_failure(void) -{ - Dynstr *a; - Dynstr *b; - Dynstr *c; - - a = new_from_cstring("abc"); - b = new_from_cstring("def"); - dynstr_set_malloc(fail_malloc); - c = dynstr_cat(a, b); - FAIL_UNLESS_EQUAL(c, NULL); - return true; -} - - -static int test_byteat_reports_correct_character(void) -{ - Dynstr *dynstr; - - dynstr = new_from_cstring("abc"); - FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1), 'b'); - return true; -} - - -static int test_byteat_reports_error_for_too_large_offset(void) -{ - Dynstr *dynstr; - - dynstr = new_from_cstring("abc"); - FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1024), -1); - return true; -} - - -static int test_byteat_reports_0xff_correctly(void) -{ - Dynstr *dynstr; - - dynstr = new_from_cstring("\xff"); - FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 0), 255); - return true; -} - - -static int test_first_byte_finds_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_first_byte(dynstr, 0, '3'); - FAIL_UNLESS_EQUAL(offset, 2); - return true; -} - - -static int test_first_byte_only_finds_first_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_first_byte(dynstr, 0, '3'); - FAIL_UNLESS_EQUAL(offset, 2); - return true; -} - - -static int test_first_byte_does_not_find_nonexistent_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_first_byte(dynstr, 0, 'x'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_first_byte_does_not_find_byte_outside_range(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_first_byte(dynstr, 4, '3'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_first_byte_only_finds_byte_inside_range(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_first_byte(dynstr, 3, '3'); - FAIL_UNLESS_EQUAL(offset, 5); - return true; -} - - -static int test_first_byte_does_not_find_in_range_outside_string(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_first_byte(dynstr, 128, '3'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_byte_finds_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_last_byte(dynstr, 0, '3'); - FAIL_UNLESS_EQUAL(offset, 2); - return true; -} - - -static int test_last_byte_only_finds_last_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_last_byte(dynstr, 0, '3'); - FAIL_UNLESS_EQUAL(offset, 5); - return true; -} - - -static int test_last_byte_does_not_find_nonexistent_byte(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_last_byte(dynstr, 0, 'x'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_byte_does_not_find_byte_outside_range(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123456"); - offset = dynstr_last_byte(dynstr, 4, '3'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_byte_only_finds_byte_inside_range(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_last_byte(dynstr, 3, '3'); - FAIL_UNLESS_EQUAL(offset, 5); - return true; -} - - -static int test_last_byte_does_not_find_in_range_outside_string(void) -{ - Dynstr *dynstr; - size_t offset; - - dynstr = new_from_cstring("123123"); - offset = dynstr_last_byte(dynstr, 128, '3'); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_first_string_finds_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("345"); - offset = dynstr_first_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, 2); - return true; -} - - -static int test_first_string_only_finds_first_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring("123"); - offset = dynstr_first_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, 0); - return true; -} - - -static int test_first_string_does_not_find_nonexistent_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("x"); - offset = dynstr_first_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_first_string_does_not_find_pattern_outside_range(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("123"); - offset = dynstr_first_string(dynstr, 3, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_first_string_only_finds_pattern_inside_range(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring("3"); - offset = dynstr_first_string(dynstr, 3, pattern); - FAIL_UNLESS_EQUAL(offset, 5); - return true; -} - - -static int test_first_string_does_not_find_empty_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring(""); - offset = dynstr_first_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_string_finds_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("345"); - offset = dynstr_last_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, 2); - return true; -} - - -static int test_last_string_only_finds_last_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring("123"); - offset = dynstr_last_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, 3); - return true; -} - - -static int test_last_string_does_not_find_nonexistent_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("x"); - offset = dynstr_last_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_string_does_not_find_pattern_outside_range(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123456"); - pattern = new_from_cstring("123"); - offset = dynstr_last_string(dynstr, 3, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - - -static int test_last_string_only_finds_pattern_inside_range(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring("3"); - offset = dynstr_last_string(dynstr, 3, pattern); - FAIL_UNLESS_EQUAL(offset, 5); - return true; -} - - -static int test_last_string_does_not_find_empty_pattern(void) -{ - Dynstr *dynstr; - Dynstr *pattern; - size_t offset; - - dynstr = new_from_cstring("123123"); - pattern = new_from_cstring(""); - offset = dynstr_last_string(dynstr, 0, pattern); - FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); - return true; -} - -static int test_cmp_returns_0_for_equal_strings(void) -{ - Dynstr *a; - Dynstr *b; - - a = new_from_cstring("foo"); - b = new_from_cstring("foo"); - FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); - return true; -} - - -static int test_cmp_returns_0_for_empty_strings(void) -{ - Dynstr *a; - Dynstr *b; - - a = dynstr_new_empty(); - b = dynstr_new_empty(); - FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); - dynstr_free(a); - dynstr_free(b); - return true; -} - - -static int test_cmp_returns_negative_if_only_first_string_is_empty(void) -{ - Dynstr *a; - Dynstr *b; - - a = new_from_cstring(""); - b = new_from_cstring("foo"); - FAIL_UNLESS(dynstr_cmp(a, b) < 0); - return true; -} - - -static int test_cmp_returns_positive_if_only_second_string_is_empty(void) -{ - Dynstr *a; - Dynstr *b; - - a = new_from_cstring("foo"); - b = new_from_cstring(""); - FAIL_UNLESS(dynstr_cmp(a, b) > 0); - return true; -} - - -static int test_cmp_returns_negative_if_first_string_comes_first(void) -{ - Dynstr *a; - Dynstr *b; - - a = new_from_cstring("x"); - b = new_from_cstring("\xff"); /* Check for unsigned comparision, too. */ - FAIL_UNLESS(dynstr_cmp(a, b) < 0); - return true; -} - - -static int test_cmp_returns_positive_if_second_string_comes_first(void) -{ - Dynstr *a; - Dynstr *b; - - a = new_from_cstring("\xff"); - b = new_from_cstring("x"); - FAIL_UNLESS(dynstr_cmp(a, b) > 0); - return true; -} - - -static int write_test(long (*callback)(char *filename, Dynstr *dynstr)) -{ - char tempname[] = "unittest.tmp.XXXXXX"; - int fd; - size_t num_bytes; - Dynstr *dynstr; - Dynstr *dynstr2; - char buf[1024]; - int read_bytes; - - dynstr = new_from_cstring("life is too short for str* and mem* in apps"); - - fd = mkstemp(tempname); - if (fd == -1) - abort(); - - num_bytes = callback(tempname, dynstr); - - FAIL_UNLESS_EQUAL(num_bytes, dynstr_len(dynstr)); - - read_bytes = read(fd, buf, sizeof(buf)); - if (read_bytes == -1) - abort(); - close(fd); - remove(tempname); - - dynstr2 = dynstr_new_from_memory(buf, read_bytes); - FAIL_UNLESS_EQUAL(read_bytes, dynstr_len(dynstr)); - FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); - dynstr_free(dynstr2); - - return true; -} - - -static long fwrite_callback(char *filename, Dynstr *dynstr) -{ - FILE *f; - long num_bytes; - - f = fopen(filename, "w"); - num_bytes = dynstr_fwrite(f, dynstr); - fclose(f); - return num_bytes; -} - - -static int test_fwrite_writes_string(void) -{ - return write_test(fwrite_callback); -} - - -static long write_callback(char *filename, Dynstr *dynstr) -{ - int fd; - long num_bytes; - - fd = open(filename, O_WRONLY, 0); - num_bytes = dynstr_write(fd, dynstr); - close(fd); - return num_bytes; -} - - -static int test_write_writes_string(void) -{ - return write_test(write_callback); -} - - -static int read_test(Dynstr *(*callback)(char *filename, size_t size)) -{ - char tempname[] = "unittest.tmp.XXXXXX"; - int fd; - Dynstr *dynstr; - Dynstr *dynstr2; - const char canonical[] = "life is too short for str* and mem* in apps"; - - fd = mkstemp(tempname); - if (fd == -1) - abort(); - if (write(fd, canonical, sizeof(canonical)) == -1) - abort(); - close(fd); - - dynstr = callback(tempname, sizeof(canonical) * 2); - remove(tempname); - - FAIL_UNLESS(dynstr != NULL); - FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(canonical)); - - dynstr2 = dynstr_new_from_memory(canonical, sizeof(canonical)); - FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); - - dynstr_free(dynstr); - dynstr_free(dynstr2); - - return true; -} - - -static Dynstr *fread_callback(char *filename, size_t size) -{ - FILE *f; - Dynstr *dynstr; - - f = fopen(filename, "r"); - dynstr = dynstr_fread(f, size); - fclose(f); - return dynstr; -} - - -static int test_fread_reads_string(void) -{ - return read_test(fread_callback); -} - - -static Dynstr *read_callback(char *filename, size_t size) -{ - int fd; - Dynstr *dynstr; - - fd = open(filename, O_RDONLY, 0); - dynstr = dynstr_read(fd, size); - close(fd); - return dynstr; -} - - -static int test_read_reads_string(void) -{ - return read_test(read_callback); -} - - -static int readline_test(Dynstr *(*callback)(char *filename)) -{ - char tempname[] = "unittest.tmp.XXXXXX"; - int fd; - const char cline1[] = "life is too short for str* and mem* in apps\n"; - const char cline2[] = "use dynstr instead!\n"; - Dynstr *lines; - Dynstr *line1; - Dynstr *line2; - Dynstr *result; - - fd = mkstemp(tempname); - if (fd == -1) - abort(); - - line1 = dynstr_new_from_constant_cstring(cline1); - line2 = dynstr_new_from_constant_cstring(cline2); - lines = dynstr_cat(line1, line2); - if (dynstr_write(fd, lines) != dynstr_len(lines)) - abort(); - close(fd); - - result = callback(tempname); - remove(tempname); - - FAIL_UNLESS_EQUAL(dynstr_cmp(line1, result), 0); - - dynstr_free(line1); - dynstr_free(line2); - dynstr_free(lines); - dynstr_free(result); - - return true; -} - - -static Dynstr *freadline_callback(char *filename) -{ - FILE *f; - Dynstr *line1; - - f = fopen(filename, "r"); - line1 = dynstr_freadline(f); - fclose(f); - return line1; -} - - -static int test_freadline_reads_first_line(void) -{ - return readline_test(freadline_callback); -} - - -static Dynstr *readline_callback(char *filename) -{ - int fd; - Dynstr *line1; - - fd = open(filename, O_RDONLY, 0); - line1 = dynstr_readline(fd); - close(fd); - return line1; -} - - -static int test_readline_reads_first_line(void) -{ - return readline_test(readline_callback); -} - - -static void setup(void) -{ - dynstr_init(); - fail_malloc_after = 0; -} - - -static void teardown(void) -{ - free_all_strings(); -} - - -struct test { - const char *name; - int (*test)(void); -}; - - -#define TEST(func) { #func, func } - -static const struct test tests[] = { - TEST(test_default_error_handler_is_indicate), - TEST(test_sets_error_handler), - TEST(test_init_resets_error_handler), - TEST(test_uses_malloc_by_default), - TEST(test_sets_malloc), - TEST(test_init_resets_malloc), - TEST(test_alloc_calls_error_handler), - TEST(test_alloc_does_not_call_error_handler), - TEST(test_indicate_handler_returns), - TEST(test_abort_handler_calls_abort), - TEST(test_empty_string_is_empty), - TEST(test_new_returns_NULL_upon_first_allocation_failure), - TEST(test_creates_from_cstring), - TEST(test_creates_from_memory), - TEST(test_creates_from_constant_cstring), - TEST(test_creates_from_constant_memory), - TEST(test_memcpy_returns_zero_if_offset_is_too_large), - TEST(test_memcpy_truncates_if_copying_too_much), - TEST(test_memcpy_copies_whole_string_ok), - TEST(test_memcpy_copies_substring_ok), - TEST(test_strdup_duplicates_string), - TEST(test_copies_empty_substring_ok), - TEST(test_copies_single_byte_substring_ok), - TEST(test_copies_middle_substring_ok), - TEST(test_copies_initial_substring_ok), - TEST(test_copies_final_substring_ok), - TEST(test_copies_empty_substring_when_offset_too_big), - TEST(test_copies_partial_substring_when_length_too_big), - TEST(test_cats_ok), - TEST(test_cat_returns_NULL_for_first_malloc_failure), - TEST(test_byteat_reports_correct_character), - TEST(test_byteat_reports_error_for_too_large_offset), - TEST(test_byteat_reports_0xff_correctly), - TEST(test_first_byte_finds_byte), - TEST(test_first_byte_only_finds_first_byte), - TEST(test_first_byte_does_not_find_nonexistent_byte), - TEST(test_first_byte_does_not_find_byte_outside_range), - TEST(test_first_byte_only_finds_byte_inside_range), - TEST(test_first_byte_does_not_find_in_range_outside_string), - TEST(test_last_byte_finds_byte), - TEST(test_last_byte_only_finds_last_byte), - TEST(test_last_byte_does_not_find_nonexistent_byte), - TEST(test_last_byte_does_not_find_byte_outside_range), - TEST(test_last_byte_only_finds_byte_inside_range), - TEST(test_last_byte_does_not_find_in_range_outside_string), - TEST(test_first_string_finds_pattern), - TEST(test_first_string_only_finds_first_pattern), - TEST(test_first_string_does_not_find_nonexistent_pattern), - TEST(test_first_string_does_not_find_pattern_outside_range), - TEST(test_first_string_only_finds_pattern_inside_range), - TEST(test_first_string_does_not_find_empty_pattern), - TEST(test_last_string_finds_pattern), - TEST(test_last_string_only_finds_last_pattern), - TEST(test_last_string_does_not_find_nonexistent_pattern), - TEST(test_last_string_does_not_find_pattern_outside_range), - TEST(test_last_string_only_finds_pattern_inside_range), - TEST(test_last_string_does_not_find_empty_pattern), - TEST(test_cmp_returns_0_for_equal_strings), - TEST(test_cmp_returns_0_for_empty_strings), - TEST(test_cmp_returns_negative_if_only_first_string_is_empty), - TEST(test_cmp_returns_positive_if_only_second_string_is_empty), - TEST(test_cmp_returns_negative_if_first_string_comes_first), - TEST(test_cmp_returns_positive_if_second_string_comes_first), - TEST(test_fwrite_writes_string), - TEST(test_write_writes_string), - TEST(test_fread_reads_string), - TEST(test_read_reads_string), - TEST(test_freadline_reads_first_line), - TEST(test_readline_reads_first_line), -}; -static const int num_tests = sizeof(tests) / sizeof(tests[0]); - -int main(void) -{ - int exit; - - exit = EXIT_SUCCESS; - for (int i = 0; i < num_tests; ++i) { - setup(); - if (!tests[i].test()) { - exit = EXIT_FAILURE; - } - teardown(); - } - return exit; -} -- cgit v1.2.1