Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

View file

@ -0,0 +1,70 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
int pipefd[2];
FILE *f, *reader, *writer;
TEST(fgetc, testEnd) {
f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ(EOF, fgetc(f));
EXPECT_TRUE(feof(f));
EXPECT_FALSE(ferror(f));
EXPECT_EQ(0, fclose(f));
}
TEST(fgetwc, testEnd) {
f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ(WEOF, fgetwc(f));
EXPECT_TRUE(feof(f));
EXPECT_FALSE(ferror(f));
EXPECT_EQ(0, fclose(f));
}
TEST(fgetwc, testMultibyte) {
f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ(L'𝑥', fputwc(L'𝑥', f));
EXPECT_EQ(L'𝑦', fputwc(L'𝑦', f));
EXPECT_EQ(L'𝑧', fputwc(L'𝑧', f));
EXPECT_EQ(L'𝑥', fgetwc(f));
EXPECT_EQ(L'𝑦', fgetwc(f));
EXPECT_EQ(L'𝑧', fgetwc(f));
EXPECT_EQ(WEOF, fgetwc(f));
EXPECT_TRUE(feof(f));
fclose(f);
}
TEST(fgetc, testPipe) {
ASSERT_NE(-1, pipe(pipefd));
writer = fdopen(pipefd[1], "w");
reader = fdopen(pipefd[0], "r");
EXPECT_EQ('a', fputc('a', writer));
EXPECT_EQ('b', fputc('b', writer));
EXPECT_EQ('c', fputc('c', writer));
EXPECT_EQ(3, fflush(writer));
EXPECT_EQ('a', fgetc(reader));
EXPECT_EQ('b', fgetc(reader));
EXPECT_EQ('c', fgetc(reader));
EXPECT_EQ(0, fclose(reader));
EXPECT_EQ(0, fclose(writer));
}

View file

@ -0,0 +1,56 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
TEST(fgetwc, testAscii_oneChar) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ('A', fputc('A', f));
EXPECT_EQ('A', fgetc(f));
fclose(f);
}
TEST(fgetwc, testAscii_twoChar) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ('A', fputc('A', f));
EXPECT_EQ('B', fputc('B', f));
EXPECT_EQ('A', fgetc(f));
EXPECT_EQ('B', fgetc(f));
fclose(f);
}
TEST(fgetwc, testUnicode_oneChar) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ(L'𐌰', fputwc(L'𐌰', f));
EXPECT_EQ(L'𐌰', fgetwc(f));
fclose(f);
}
TEST(fgetwc, testUnicode_oneChar_writtenAsRawUtf8) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
EXPECT_EQ(0xF0, fputc(0xF0, f));
EXPECT_EQ(0x90, fputc(0x90, f));
EXPECT_EQ(0x8C, fputc(0x8C, f));
EXPECT_EQ(0xB0, fputc(0xB0, f));
EXPECT_EQ(L'𐌰', fgetwc(f));
EXPECT_EQ(-1u, fgetwc(f));
fclose(f);
}

View file

@ -0,0 +1,75 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
TEST(getline, testEmpty) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
char *line = NULL;
size_t linesize = 0;
EXPECT_EQ(-1, getline(&line, &linesize, f));
EXPECT_TRUE(feof(f));
EXPECT_FALSE(ferror(f));
EXPECT_EQ(0, fclose(f));
free(line);
}
TEST(getline, testOneWithoutLineFeed) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
ASSERT_EQ(5, fwrite("hello", 1, 5, f));
char *line = NULL;
size_t linesize = 0;
ASSERT_EQ(5, getline(&line, &linesize, f));
EXPECT_STREQ("hello", line);
EXPECT_TRUE(feof(f));
EXPECT_FALSE(ferror(f));
ASSERT_EQ(-1, getline(&line, &linesize, f));
EXPECT_EQ(0, fclose(f));
free(line);
}
TEST(getline, testTwoLines) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
ASSERT_EQ(12, fwrite("hello\nthere\n", 1, 12, f));
char *line = NULL;
size_t linesize = 0;
ASSERT_EQ(6, getline(&line, &linesize, f));
EXPECT_STREQ("hello\n", line);
EXPECT_FALSE(feof(f) | ferror(f));
ASSERT_EQ(6, getline(&line, &linesize, f));
EXPECT_STREQ("there\n", line);
ASSERT_EQ(-1, getline(&line, &linesize, f));
EXPECT_STREQ("", line);
EXPECT_EQ(0, fclose(f));
free(line);
}
TEST(getline, testBinaryLine_countExcludesOnlyTheBonusNul) {
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
fwrite("he\0\3o\n", 1, 6, f);
char *line = NULL;
size_t linesize = 0;
ASSERT_EQ(6, getline(&line, &linesize, f));
EXPECT_BINEQ(u"he ♥o◙ ", line);
fclose(f);
free(line);
}

View file

@ -0,0 +1,81 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/errno.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/temp.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/testlib.h"
#define MODE() \
({ \
va_list va; \
unsigned Mode; \
va_start(va, flags); \
Mode = va_arg(va, unsigned); \
va_end(va); \
Mode; \
})
static int MockOpen1(const char *file, int flags, ...) {
static bool once;
ASSERT_FALSE(once);
once = true;
EXPECT_STREQ("/tmp/mkostemps.ctre5m", file);
EXPECT_EQ(O_RDWR | O_CREAT | O_EXCL, flags);
EXPECT_EQ(0600, MODE());
return 123;
}
TEST(mkostempsm, test1) {
uint64_t rando = 1;
char path[PATH_MAX] = "/tmp/mkostemps.XXXXXX";
EXPECT_EQ(123L, mkostempsmi(path, 0, 0, &rando, 0600, MockOpen1));
EXPECT_STREQ("/tmp/mkostemps.ctre5m", path);
}
static int MockOpen2(const char *file, int flags, ...) {
static int state;
switch (state) {
case 0:
state = 1;
EXPECT_STREQ("/tmp/mkostemps.ctre5m", file);
EXPECT_EQ((unsigned)(O_RDWR | O_CREAT | O_EXCL), flags);
EXPECT_EQ(0600, MODE());
errno = EEXIST;
return -1;
case 1:
state = 1;
EXPECT_STREQ("/tmp/mkostemps.jl1h61", file);
EXPECT_EQ((unsigned)(O_RDWR | O_CREAT | O_EXCL), flags);
EXPECT_EQ(0600, MODE());
return 123;
default:
abort();
}
}
TEST(mkostempsm, test2) {
uint64_t rando = 1;
char path[PATH_MAX] = "/tmp/mkostemps.XXXXXX";
EXPECT_EQ(123, mkostempsmi(path, 0, 0, &rando, 0600, MockOpen2));
EXPECT_STREQ("/tmp/mkostemps.jl1h61", path);
}

View file

@ -0,0 +1,25 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/stdio/stdio.h"
#include "libc/calls/calls.h"
#include "libc/testlib/testlib.h"
TEST(system, nullParam_testsIfSystemHasShell) { ASSERT_EQ(true, system(NULL)); }
TEST(system, test) { ASSERT_EQ(42, WEXITSTATUS(system("exit 42"))); }

60
test/libc/stdio/test.mk Normal file
View file

@ -0,0 +1,60 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += TEST_LIBC_STDIO
TEST_LIBC_STDIO_SRCS := $(wildcard test/libc/stdio/*.c)
TEST_LIBC_STDIO_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_STDIO_SRCS))
TEST_LIBC_STDIO_COMS = $(TEST_LIBC_STDIO_OBJS:%.o=%.com)
TEST_LIBC_STDIO_OBJS = \
$(TEST_LIBC_STDIO_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_STDIO_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_STDIO_BINS = \
$(TEST_LIBC_STDIO_COMS) \
$(TEST_LIBC_STDIO_COMS:%=%.dbg)
TEST_LIBC_STDIO_TESTS = \
$(TEST_LIBC_STDIO_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_STDIO_CHECKS = \
$(TEST_LIBC_STDIO_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_STDIO_DIRECTDEPS = \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_X
TEST_LIBC_STDIO_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_STDIO_DIRECTDEPS),$($(x))))
o/$(MODE)/test/libc/stdio/stdio.pkg: \
$(TEST_LIBC_STDIO_OBJS) \
$(foreach x,$(TEST_LIBC_STDIO_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/libc/stdio/%.com.dbg: \
$(TEST_LIBC_STDIO_DEPS) \
o/$(MODE)/test/libc/stdio/%.o \
o/$(MODE)/test/libc/stdio/stdio.pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
$(TEST_LIBC_STDIO_OBJS): \
DEFAULT_CCFLAGS += \
-fno-builtin
.PHONY: o/$(MODE)/test/libc/stdio
o/$(MODE)/test/libc/stdio: \
$(TEST_LIBC_STDIO_BINS) \
$(TEST_LIBC_STDIO_CHECKS)