Re: [PATCH 0/2] nolibc: Add fread() and fseek()

From: Thomas Weißschuh
Date: Sun Jan 04 2026 - 09:36:21 EST


On 2026-01-04 20:12:11+0900, Daniel Palmer wrote:
> Hi Thomas,
>
> On Sun, 4 Jan 2026 at 18:11, Thomas Weißschuh <linux@xxxxxxxxxxxxxx> wrote:
> > Please also do add some tests.
>
> Would a single function test that exercises the new functions be
> enough? Something like this:

That example looks good. But please use O_TMPFILE for the test file, to
make the test more robust and also test fdopen().
You can also return different error numbers depending on the failure to
make debugging easier.

> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -877,6 +877,45 @@ int test_file_stream(void)
> return 0;
> }
>
> +int test_file_stream_wsr(void)
> +{
> + const char dataout[] = "foo";
> + const size_t datasz = sizeof(dataout);
> + char datain[datasz];
> + FILE *f;
> + int r;
> +
> + f = fopen("/tmp/file_stream_test", "w+");
> + if (!f)
> + return -1;
> +
> + r = fwrite(dataout, 1, datasz, f);
> + if (r != datasz)
> + goto fail;
> +
> + r = fseek(f, 0, SEEK_SET);
> + if (r)
> + goto fail;
> +
> + r = fread(datain, 1, datasz, f);
> + if (r != datasz)
> + goto fail;
> +
> + if (memcmp(datain, dataout, datasz) != 0)
> + goto fail;
> +
> + r = fclose(f);
> + if (r == EOF)
> + return -1;
> +
> + return 0;
> +
> +fail:
> + fclose(f);
> + return -1;
> +}
> +
>
> Cheers,
>
> Daniel