mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 21:54:33 +02:00
Merge branch 'test/newlib_blksize' into 'master'
newlib: add test for st_blksize and FILE buffer size See merge request espressif/esp-idf!19366
This commit is contained in:
@@ -2,5 +2,6 @@ idf_component_register(SRCS
|
|||||||
"test_newlib_main.c"
|
"test_newlib_main.c"
|
||||||
"test_stdatomic.c"
|
"test_stdatomic.c"
|
||||||
"test_misc.c"
|
"test_misc.c"
|
||||||
|
"test_file.c"
|
||||||
REQUIRES test_utils
|
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(stdatomic);
|
||||||
RUN_TEST_GROUP(misc);
|
RUN_TEST_GROUP(misc);
|
||||||
|
RUN_TEST_GROUP(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
|
Reference in New Issue
Block a user