mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 23:08:31 +00:00
Fix bugs and make code tinier
- Fixed bug where stdio eof wasn't being sticky - Fixed bug where fseeko() wasn't clearing eof state - Removed assert() usage from libc favoring _unassert() / _npassert()
This commit is contained in:
parent
9b7c8db846
commit
d5910e2673
115 changed files with 510 additions and 290 deletions
|
@ -27,6 +27,22 @@
|
|||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
TEST(read, eof) {
|
||||
char b[8] = "hello";
|
||||
ASSERT_SYS(0, 3, creat("foo", 0644));
|
||||
ASSERT_SYS(0, 4, open("foo", O_RDONLY));
|
||||
ASSERT_SYS(0, 0, read(4, b, 8));
|
||||
ASSERT_SYS(0, 8, write(3, b, 8));
|
||||
ASSERT_SYS(0, 8, read(4, b, 8));
|
||||
ASSERT_SYS(0, 0, read(4, b, 8));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static long Read(long fd, void *buf, unsigned long size) {
|
||||
long ax, di, si, dx;
|
||||
asm volatile("syscall"
|
||||
|
|
|
@ -120,6 +120,9 @@ o/$(MODE)/test/libc/calls/lock_test.com.runs: \
|
|||
o/$(MODE)/test/libc/calls/openbsd_test.com.runs: \
|
||||
private .PLEDGE = stdio rpath wpath cpath fattr proc unveil
|
||||
|
||||
o/$(MODE)/test/libc/calls/read_test.com.runs: \
|
||||
private .UNVEIL += /dev/zero
|
||||
|
||||
# TODO(jart): Update nointernet() to allow AF_INET6
|
||||
o/$(MODE)/test/libc/calls/pledge_test.com.runs: \
|
||||
private .INTERNET = 1
|
||||
|
|
62
test/libc/runtime/exit_test.c
Normal file
62
test/libc/runtime/exit_test.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*-*- 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 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
int i, *p;
|
||||
|
||||
void SetUp(void) {
|
||||
p = _mapshared(FRAMESIZE);
|
||||
}
|
||||
|
||||
void TearDown(void) {
|
||||
munmap(p, FRAMESIZE);
|
||||
}
|
||||
|
||||
void AtExit3(void) {
|
||||
p[i++] = 3;
|
||||
}
|
||||
|
||||
void AtExit2(void) {
|
||||
p[i++] = 2;
|
||||
exit(2);
|
||||
}
|
||||
|
||||
void AtExit1(void) {
|
||||
p[i++] = 1;
|
||||
atexit(AtExit2);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// consistent with glibc, musl, freebsd, openbsd & netbsd
|
||||
// please note posix says recursion is undefined behavior
|
||||
// however, fifo ordering of atexit handlers is specified
|
||||
TEST(exit, test) {
|
||||
SPAWN(fork);
|
||||
atexit(AtExit3);
|
||||
atexit(AtExit3);
|
||||
atexit(AtExit1);
|
||||
exit(0);
|
||||
EXITS(2);
|
||||
ASSERT_EQ(1, p[0]);
|
||||
ASSERT_EQ(2, p[1]);
|
||||
ASSERT_EQ(3, p[2]);
|
||||
ASSERT_EQ(3, p[3]);
|
||||
}
|
60
test/libc/stdio/fread_test.c
Normal file
60
test/libc/stdio/fread_test.c
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*-*- 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 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
TEST(fread, eofIsSticky) {
|
||||
FILE *fo, *fi;
|
||||
char b[10] = "hello";
|
||||
ASSERT_NE(NULL, (fo = fopen("foo", "w")));
|
||||
ASSERT_NE(NULL, (fi = fopen("foo", "r")));
|
||||
ASSERT_EQ(0, fread(b, 1, 8, fi));
|
||||
ASSERT_TRUE(feof(fi));
|
||||
ASSERT_EQ(8, fwrite(b, 1, 8, fo));
|
||||
ASSERT_EQ(0, fflush(fo));
|
||||
ASSERT_EQ(0, fread(b, 1, 8, fi));
|
||||
ASSERT_TRUE(feof(fi));
|
||||
clearerr(fi);
|
||||
ASSERT_EQ(8, fread(b, 1, 10, fi));
|
||||
ASSERT_TRUE(feof(fi));
|
||||
ASSERT_EQ(0, fseek(fi, 0, SEEK_SET));
|
||||
ASSERT_FALSE(feof(fi));
|
||||
ASSERT_EQ(0, fclose(fi));
|
||||
ASSERT_EQ(0, fclose(fo));
|
||||
}
|
||||
|
||||
TEST(fread, seekWithBuffer) {
|
||||
FILE *f;
|
||||
char b[8] = "hellosup";
|
||||
char c[8] = {0};
|
||||
char d[8] = {0};
|
||||
ASSERT_NE(NULL, (f = fopen("foo", "w")));
|
||||
ASSERT_EQ(8, fwrite(b, 1, 8, f));
|
||||
ASSERT_EQ(0, fclose(f));
|
||||
ASSERT_NE(NULL, (f = fopen("foo", "r")));
|
||||
ASSERT_EQ(5, fread(c, 1, 5, f));
|
||||
ASSERT_STREQ("hello", c);
|
||||
ASSERT_EQ(0, fseek(f, 1, SEEK_SET));
|
||||
ASSERT_EQ(5, fread(d, 1, 5, f));
|
||||
ASSERT_STREQ("ellos", d);
|
||||
ASSERT_EQ(0, fclose(f));
|
||||
}
|
|
@ -20,10 +20,10 @@
|
|||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
@ -39,6 +39,10 @@ char testlib_enable_tmp_setup_teardown;
|
|||
TEST(fwrite, test) {
|
||||
ASSERT_NE(NULL, (f = fopen(PATH, "wb")));
|
||||
EXPECT_EQ(-1, fgetc(f));
|
||||
ASSERT_FALSE(feof(f));
|
||||
ASSERT_EQ(EBADF, errno);
|
||||
ASSERT_EQ(EBADF, ferror(f));
|
||||
clearerr(f);
|
||||
EXPECT_EQ(5, fwrite("hello", 1, 5, f));
|
||||
EXPECT_EQ(5, ftell(f));
|
||||
EXPECT_NE(-1, fclose(f));
|
||||
|
@ -60,6 +64,7 @@ TEST(fwrite, testSmallBuffer) {
|
|||
ASSERT_NE(NULL, (f = fopen(PATH, "wb")));
|
||||
setbuffer(f, gc(malloc(1)), 1);
|
||||
EXPECT_EQ(-1, fgetc(f));
|
||||
clearerr(f);
|
||||
EXPECT_EQ(5, fwrite("hello", 1, 5, f));
|
||||
EXPECT_EQ(5, ftell(f));
|
||||
EXPECT_NE(-1, fclose(f));
|
||||
|
@ -84,6 +89,7 @@ TEST(fwrite, testLineBuffer) {
|
|||
ASSERT_NE(NULL, (f = fopen(PATH, "wb")));
|
||||
setvbuf(f, NULL, _IOLBF, 64);
|
||||
EXPECT_EQ(-1, fgetc(f));
|
||||
clearerr(f);
|
||||
EXPECT_EQ(5, fwrite("heyy\n", 1, 5, f));
|
||||
EXPECT_EQ(0, fread(buf, 0, 0, f));
|
||||
EXPECT_FALSE(feof(f));
|
||||
|
@ -112,6 +118,7 @@ TEST(fwrite, testNoBuffer) {
|
|||
ASSERT_NE(NULL, (f = fopen(PATH, "wb")));
|
||||
setvbuf(f, NULL, _IONBF, 64);
|
||||
EXPECT_EQ(-1, fgetc(f));
|
||||
clearerr(f);
|
||||
EXPECT_EQ(5, fwrite("heyy\n", 1, 5, f));
|
||||
EXPECT_EQ(5, ftell(f));
|
||||
EXPECT_NE(-1, fclose(f));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue