Fix issues with stdio needed for Lua

See #61
This commit is contained in:
Justine Tunney 2021-03-06 16:06:15 -08:00
parent c3ed8d6c7f
commit d769df3482
17 changed files with 102 additions and 155 deletions

View file

@ -1,7 +1,7 @@
/*-*- 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
Copyright 2021 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
@ -16,29 +16,38 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/bits/popcnt.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
unsigned naive(unsigned beg, unsigned end, unsigned size) {
assert(end < size);
assert(beg < size);
assert(popcnt(size) == 1);
if (beg == end) return size;
if (end > beg) return end - beg;
return (size - beg) + end;
}
char testlib_enable_tmp_setup_teardown;
unsigned fancy(unsigned beg, unsigned end, unsigned size) {
return ((end - beg - 1) & (size - 1)) + 1;
}
TEST(fwrite, test) {
FILE *f;
char buf[512];
TEST(favail, test) {
unsigned i, j, n = 4;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
ASSERT_EQ(naive(i, j, n), fancy(i, j, n), "%u %u %u", i, j, n);
}
ASSERT_NE(NULL, (f = fopen("hog", "wb")));
EXPECT_EQ(-1, fgetc(f));
EXPECT_EQ(5, fwrite("hello", 1, 5, f));
EXPECT_EQ(5, ftell(f));
EXPECT_NE(-1, fclose(f));
ASSERT_NE(NULL, (f = fopen("hog", "r")));
EXPECT_EQ(-1, fwrite("hello", 1, 5, f));
EXPECT_EQ(EBADF, ferror(f));
EXPECT_NE(-1, fclose(f));
ASSERT_NE(NULL, (f = fopen("hog", "a+b")));
EXPECT_EQ(5, fwrite("hello", 1, 5, f));
EXPECT_NE(-1, fclose(f));
/* TODO(jart): O_APPEND on Windows */
if (!IsWindows()) {
ASSERT_NE(NULL, (f = fopen("hog", "r")));
EXPECT_EQ(10, fread(buf, 1, 10, f));
EXPECT_TRUE(!memcmp(buf, "hellohello", 10));
EXPECT_NE(-1, fclose(f));
}
}