mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
newlib: add test for st_blksize and FILE buffer size
Inspired by https://github.com/joltwallet/esp_littlefs/pull/91/files#r935872478
This commit is contained in:
parent
f332790af5
commit
cd70631f13
@ -2,5 +2,6 @@ idf_component_register(SRCS
|
||||
"test_newlib_main.c"
|
||||
"test_stdatomic.c"
|
||||
"test_misc.c"
|
||||
"test_file.c"
|
||||
REQUIRES test_utils
|
||||
PRIV_REQUIRES unity)
|
||||
PRIV_REQUIRES unity vfs)
|
||||
|
94
components/newlib/test_apps/main/test_file.c
Normal file
94
components/newlib/test_apps/main/test_file.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdio_ext.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include "esp_vfs.h"
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
TEST_GROUP(file);
|
||||
|
||||
TEST_SETUP(file)
|
||||
{
|
||||
}
|
||||
|
||||
TEST_TEAR_DOWN(file)
|
||||
{
|
||||
}
|
||||
|
||||
/* This test checks that st_blksize value set in struct stat correctly affects the
|
||||
* FILE structure:
|
||||
* - _blksize field should be equal to st_blksize
|
||||
* - buffer size should be equal to st_blksize if it is nonzero, and __BUFSIZ__ otherwise.
|
||||
* This is more of an integration test since some of the functions responsible for this are
|
||||
* in ROM, and have been built without HAVE_BLKSIZE feature for the ESP32 chip.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned blksize;
|
||||
} blksize_test_ctx_t;
|
||||
|
||||
static int blksize_test_open(void* ctx, const char * path, int flags, int mode)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int blksize_test_fstat(void* ctx, int fd, struct stat * st)
|
||||
{
|
||||
blksize_test_ctx_t* test_ctx = (blksize_test_ctx_t*) ctx;
|
||||
memset(st, 0, sizeof(*st));
|
||||
st->st_mode = S_IFREG;
|
||||
st->st_blksize = test_ctx->blksize;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t blksize_test_write(void* ctx, int fd, const void * data, size_t size)
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
TEST(file, blksize)
|
||||
{
|
||||
FILE* f;
|
||||
blksize_test_ctx_t ctx = {};
|
||||
const char c = 42;
|
||||
const esp_vfs_t desc = {
|
||||
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
|
||||
.open_p = blksize_test_open,
|
||||
.fstat_p = blksize_test_fstat,
|
||||
.write_p = blksize_test_write,
|
||||
};
|
||||
|
||||
TEST_ESP_OK(esp_vfs_register("/test", &desc, &ctx));
|
||||
|
||||
/* test with zero st_blksize (=not set) */
|
||||
ctx.blksize = 0;
|
||||
f = fopen("/test/path", "w");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
fwrite(&c, 1, 1, f);
|
||||
TEST_ASSERT_EQUAL(0, f->_blksize);
|
||||
TEST_ASSERT_EQUAL(__BUFSIZ__, __fbufsize(f));
|
||||
fclose(f);
|
||||
|
||||
/* test with non-zero st_blksize */
|
||||
ctx.blksize = 4096;
|
||||
f = fopen("/test/path", "w");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
fwrite(&c, 1, 1, f);
|
||||
TEST_ASSERT_EQUAL(ctx.blksize, f->_blksize);
|
||||
TEST_ASSERT_EQUAL(ctx.blksize, __fbufsize(f));
|
||||
fclose(f);
|
||||
|
||||
TEST_ESP_OK(esp_vfs_unregister("/test"));
|
||||
}
|
||||
|
||||
TEST_GROUP_RUNNER(file)
|
||||
{
|
||||
RUN_TEST_CASE(file, blksize)
|
||||
}
|
@ -10,6 +10,7 @@ static void run_all_tests(void)
|
||||
{
|
||||
RUN_TEST_GROUP(stdatomic);
|
||||
RUN_TEST_GROUP(misc);
|
||||
RUN_TEST_GROUP(file);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
|
Loading…
x
Reference in New Issue
Block a user