Files
arcs/arcs-sdk/test/posix/stdio_func/test_stdio.c
2026-08-13 16:50:52 +08:00

230 lines
6.7 KiB
C

/* AUTOGENERATED FILE. DO NOT EDIT. */
/*=======Test Runner Used To Run Each Test Below=====*/
#define RUN_TEST(TestFunc, TestLineNum) \
{ \
Unity.CurrentTestName = #TestFunc; \
Unity.CurrentTestLineNumber = TestLineNum; \
Unity.NumberOfTests++; \
if (TEST_PROTECT()) { \
setUp(); \
TestFunc(); \
} \
if (TEST_PROTECT()) { \
tearDown(); \
} \
UnityConcludeTest(); \
}
/*=======Automagically Detected Files To Include=====*/
#include "unity.h"
#include <setjmp.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "user_fs.h"
#include "lisa_sdmmc.h"
#include "IOMuxManager.h"
void setUp(void)
{
}
void tearDown(void)
{
}
/*=======Test Reset Option=====*/
void resetTest(void);
void resetTest(void)
{
tearDown();
setUp();
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
void test_rename_func(void)
{
const char *old_name = "/SD:/testfile_rename.txt";
const char *new_name = "/SD:/testfile_renamed.txt";
FILE *fp = fopen(old_name, "w");
TEST_ASSERT_NOT_NULL(fp);
fputs("hello", fp);
fclose(fp);
// rename
int rc = rename(old_name, new_name);
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "rename failed");
// old file should not exist
fp = fopen(old_name, "r");
TEST_ASSERT_NULL(fp);
// new file should exist
fp = fopen(new_name, "r");
TEST_ASSERT_NOT_NULL(fp);
char buf[16] = {0};
fgets(buf, sizeof(buf), fp);
TEST_ASSERT_EQUAL_STRING("hello", buf);
fclose(fp);
// cleanup
remove(new_name);
}
void test_remove_func(void)
{
const char *fname = "/SD:/testfile_remove.txt";
FILE *fp = fopen(fname, "w");
TEST_ASSERT_NOT_NULL(fp);
fputs("bye", fp);
fclose(fp);
// remove
int rc = remove(fname);
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "remove failed");
// file should not exist
fp = fopen(fname, "r");
TEST_ASSERT_NULL(fp);
}
void test_open_with_trunc_flag(void)
{
const char *fname = "/SD:/testfile_trunc.txt";
remove(fname);
FILE *fp = fopen(fname, "w");
const char *initial_content = "初始内容应该被截断";
size_t written = fputs(initial_content, fp);
fclose(fp);
fp = fopen(fname, "r");
char buf[50] = {0};
fgets(buf, sizeof(buf), fp);
fclose(fp);
int fd = open(fname, O_WRONLY | O_TRUNC);
TEST_ASSERT_MESSAGE(fd >= 0, "Failed to open file with O_TRUNC flag");
close(fd);
fp = fopen(fname, "r");
TEST_ASSERT_NOT_NULL_MESSAGE(fp, "Failed to open truncated file");
TEST_ASSERT_EQUAL_INT_MESSAGE(EOF, fgetc(fp), "File was not truncated");
fclose(fp);
remove(fname);
}
void test_ftell(void)
{
const char *fname = "/SD:/testfile_ftell.txt";
const char *test_content = "This is a ftell test";
long position;
remove(fname);
FILE *fp = fopen(fname, "w");
fputs(test_content, fp);
position = ftell(fp);
TEST_ASSERT_EQUAL(position, strlen(test_content));
fseek(fp, -2, SEEK_CUR);
position = ftell(fp);
TEST_ASSERT_EQUAL(position, strlen(test_content) - 2);
fseek(fp, 4, SEEK_SET);
position = ftell(fp);
TEST_ASSERT_EQUAL(position, 4);
fclose(fp);
remove(fname);
}
void test_ftell_then_fread(void)
{
const char *fname = "/SD:/testfile_ftell.txt";
const char *test_content = "This is a ftell test";
const int seek_offset = 4;
const char *expect_test_content = " is a ftell test";
long position;
remove(fname);
FILE *fp = fopen(fname, "w");
fputs(test_content, fp);
fclose(fp);
fp = fopen(fname, "r");
fseek(fp, seek_offset, SEEK_SET);
ftell(fp);
char buffer[128] = {0};
fread(buffer, 1, strlen(expect_test_content), fp);
TEST_ASSERT_EQUAL_MEMORY(expect_test_content, buffer, strlen(expect_test_content));
fclose(fp);
remove(fname);
}
void test_chdir(void)
{
int rc;
const char *new_dir = "/SD:/test_chdir";
remove(new_dir);
mkdir(new_dir, 0777);
rc = chdir(new_dir);
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "chdir failed");
remove(new_dir);
}
void test_getcwd(void)
{
int rc;
char *cwd_path;
char buf[256] = {0};
const char *new_dir = "/SD:/test_getcwd";
remove(new_dir);
mkdir(new_dir, 0777);
rc = chdir(new_dir);
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "chdir failed");
cwd_path = getcwd(buf, sizeof(buf));
TEST_ASSERT_EQUAL_STRING(new_dir, buf);
TEST_ASSERT_EQUAL_STRING(new_dir, cwd_path);
remove(new_dir);
}
/*=======MAIN=====*/
int main(void)
{
/* Configure SDMMC pins (same as lisa_disk test) */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 6, CSK_IOMUX_FUNC_ALTER15); /* CLK */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 7, CSK_IOMUX_FUNC_ALTER15); /* CMD */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 5, CSK_IOMUX_FUNC_ALTER15); /* DAT0 */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 4, CSK_IOMUX_FUNC_ALTER15); /* DAT1 */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 9, CSK_IOMUX_FUNC_ALTER15); /* DAT2 */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 8, CSK_IOMUX_FUNC_ALTER15); /* DAT3 */
user_fs_init();
printf("user fs init complete\n");
UnityBegin("test/stdio_func/test_stdio.c");
RUN_TEST(test_rename_func, __LINE__);
RUN_TEST(test_remove_func, __LINE__);
RUN_TEST(test_open_with_trunc_flag, __LINE__);
RUN_TEST(test_ftell, __LINE__);
RUN_TEST(test_ftell_then_fread, __LINE__);
RUN_TEST(test_chdir, __LINE__);
RUN_TEST(test_getcwd, __LINE__);
return (UnityEnd());
}