Add readline tests

This commit is contained in:
Drew DeVault 2016-06-19 12:48:20 -04:00
parent 8758a2bd04
commit 689935ed39
6 changed files with 86 additions and 14 deletions

View file

@ -10,7 +10,7 @@ char *read_line(FILE *file) {
return NULL;
}
while (1) {
int c = getc(file);
int c = fgetc(file);
if (c == '\n' && lastChar == '\\'){
--length; // Ignore last character.
lastChar = '\0';
@ -51,7 +51,7 @@ char *read_line_buffer(FILE *file, char *string, size_t string_len) {
return NULL;
}
while (1) {
int c = getc(file);
int c = fgetc(file);
if (c == EOF || c == '\n' || c == '\0') {
break;
}

View file

@ -1 +1,2 @@
add_subdirectory(list)
add_subdirectory(readline)

View file

@ -1,6 +1,4 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "tests.h"
#include "list.h"

View file

@ -0,0 +1,9 @@
configure_test(
SUBPROJECT common
NAME readline
SOURCES
${PROJECT_SOURCE_DIR}/common/readline.c
readline.c
WRAPPERS
fgetc
)

View file

@ -0,0 +1,61 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include "tests.h"
#include "readline.h"
int __wrap_fgetc(FILE *stream) {
return mock_type(int);
}
static void prep_string(const char *str) {
while (*str) {
will_return(__wrap_fgetc, *str++);
}
}
static void test_eof_line_ending(void **state) {
prep_string("hello");
will_return(__wrap_fgetc, EOF);
char *line = read_line(NULL);
assert_string_equal(line, "hello");
free(line);
}
static void test_newline(void **state) {
prep_string("hello\n");
char *line = read_line(NULL);
assert_string_equal(line, "hello");
free(line);
}
static void test_continuation(void **state) {
prep_string("hello \\\nworld");
will_return(__wrap_fgetc, EOF);
char *line = read_line(NULL);
assert_string_equal(line, "hello world");
free(line);
}
static void test_expand_buffer(void **state) {
const char *test = "This is a very very long string. "
"This string is so long that it may in fact be greater "
"than 128 bytes (or octets) in length, which is suitable "
"for triggering a realloc";
prep_string(test);
will_return(__wrap_fgetc, EOF);
char *line = read_line(NULL);
assert_string_equal(line, test);
free(line);
assert_int_equal(realloc_calls(), 1);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_eof_line_ending),
cmocka_unit_test(test_newline),
cmocka_unit_test(test_continuation),
cmocka_unit_test(test_expand_buffer),
};
return cmocka_run_group_tests(tests, reset_mem_wrappers, NULL);
}

View file

@ -8,6 +8,8 @@ do
ret+=$?
done
if (( $ret == 0 ))
then
if grep 'enable-coverage:BOOL=YES' "$1/CMakeCache.txt"
then
echo "Generating coverage reports"
@ -20,5 +22,6 @@ then
--output-file "$1/coverage/lcov.info.clean"
genhtml -o "$1/coverage/" "$1/coverage/lcov.info.clean"
fi
fi
exit $ret