/* * 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); /* Creating a dynstr from a supposedly constant C string should make the dynstr re-use the C string's memory area. Thus, if we modify the C string (even though we should not), the dynstr should also change. */ 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; }