From dc73d0d7a1346bc6fecec2bc91f27c3e473834ff Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 25 Feb 2014 18:12:55 +0100 Subject: [PATCH] Add test-fmemopen. --- regress/Makefile | 1 + regress/test-fmemopen.c++ | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 regress/test-fmemopen.c++ diff --git a/regress/Makefile b/regress/Makefile index f24ab82f..3748854c 100644 --- a/regress/Makefile +++ b/regress/Makefile @@ -14,6 +14,7 @@ BINARIES:=\ regress \ TESTS:=\ +test-fmemopen \ test-pthread-argv \ test-pthread-basic \ test-pthread-main-join \ diff --git a/regress/test-fmemopen.c++ b/regress/test-fmemopen.c++ new file mode 100644 index 00000000..3bba4cbc --- /dev/null +++ b/regress/test-fmemopen.c++ @@ -0,0 +1,61 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + 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 . + + test-fmemopen.c++ + Tests basic fmemopen() usage. + +*******************************************************************************/ + +#include +#include + +#include "test.h" + +int main(void) +{ + const char* string = "VYl/plYoFGAg2"; + size_t string_length = strlen(string); + + FILE* fp; + int c; + + unsigned char* buffer = (unsigned char*) string; + size_t buffer_size = string_length; + + if ( !(fp = fmemopen(buffer, buffer_size, "r")) ) + test_error(errno, "fmemopen"); + + for ( size_t i = 0; i < buffer_size; i++ ) + { + c = fgetc(fp); + test_assert(!(c == EOF && feof(fp))); + test_assert(!(c == EOF && ferror(fp))); + test_assert(c != EOF); + test_assert((unsigned char) c == buffer[i]); + test_assert(!feof(fp)); + test_assert(!ferror(fp)); + } + + c = fgetc(fp); + test_assert(c == EOF); + test_assert(!ferror(fp)); + test_assert(feof(fp)); + + fclose(fp); + + return 0; +}