mirror of https://github.com/ARMmbed/mbed-os.git
STORAGE: fixes of basic.cpp test case to adopt new version of filesystem direct API.
parent
7e223bdaeb
commit
b9d9685b43
|
@ -599,21 +599,22 @@ static control_t fsfat_basic_test_07()
|
|||
static bool fsfat_basic_test_file_write_fhandle(const char *filename, const int kib_rw)
|
||||
{
|
||||
int ret = -1;
|
||||
FileHandle* file = fs.open(filename, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
File file;
|
||||
|
||||
ret = file.open(&fs, filename, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
|
||||
TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
|
||||
|
||||
int byte_write = 0;
|
||||
fsfat_basic_timer.start();
|
||||
for (int i = 0; i < kib_rw; i++) {
|
||||
ret = file->write(fsfat_basic_buffer, sizeof(fsfat_basic_buffer));
|
||||
ret = file.write(fsfat_basic_buffer, sizeof(fsfat_basic_buffer));
|
||||
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__);
|
||||
TEST_ASSERT_MESSAGE(ret == sizeof(fsfat_basic_buffer), fsfat_basic_msg_g);
|
||||
byte_write++;
|
||||
}
|
||||
fsfat_basic_timer.stop();
|
||||
file->close();
|
||||
file.close();
|
||||
double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
|
||||
double speed = kib_rw / test_time_sec;
|
||||
FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed);
|
||||
|
@ -624,18 +625,20 @@ static bool fsfat_basic_test_file_write_fhandle(const char *filename, const int
|
|||
|
||||
static bool fsfat_basic_test_file_read_fhandle(const char *filename, const int kib_rw)
|
||||
{
|
||||
FileHandle* file = fs.open(filename, O_RDONLY);
|
||||
int ret = -1;
|
||||
File file;
|
||||
ret = file.open(&fs, filename, O_RDONLY);
|
||||
|
||||
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
|
||||
TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
|
||||
|
||||
fsfat_basic_timer.start();
|
||||
int byte_read = 0;
|
||||
while (file->read(fsfat_basic_buffer, sizeof(fsfat_basic_buffer)) == sizeof(fsfat_basic_buffer)) {
|
||||
while (file.read(fsfat_basic_buffer, sizeof(fsfat_basic_buffer)) == sizeof(fsfat_basic_buffer)) {
|
||||
byte_read++;
|
||||
}
|
||||
fsfat_basic_timer.stop();
|
||||
file->close();
|
||||
file.close();
|
||||
double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
|
||||
double speed = kib_rw / test_time_sec;
|
||||
FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed);
|
||||
|
|
|
@ -1,151 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "mbed.h"
|
||||
#include "greentea-client/test_env.h"
|
||||
#include "unity.h"
|
||||
#include "utest.h"
|
||||
|
||||
#include "HeapBlockDevice.h"
|
||||
#include "FATFileSystem.h"
|
||||
#include <stdlib.h>
|
||||
#include "retarget.h"
|
||||
|
||||
using namespace utest::v1;
|
||||
|
||||
// Test block device
|
||||
#define BLOCK_SIZE 512
|
||||
HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE);
|
||||
|
||||
|
||||
void test_format() {
|
||||
int err = FATFileSystem::format(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
|
||||
// Simple test for reading/writing files
|
||||
template <ssize_t TEST_SIZE>
|
||||
void test_read_write() {
|
||||
FATFileSystem fs("fat");
|
||||
|
||||
int err = fs.mount(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
|
||||
TEST_ASSERT(buffer);
|
||||
|
||||
// Fill with random sequence
|
||||
srand(1);
|
||||
for (int i = 0; i < TEST_SIZE; i++) {
|
||||
buffer[i] = 0xff & rand();
|
||||
}
|
||||
|
||||
// write and read file
|
||||
FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT);
|
||||
TEST_ASSERT(file);
|
||||
ssize_t size = file->write(buffer, TEST_SIZE);
|
||||
TEST_ASSERT_EQUAL(TEST_SIZE, size);
|
||||
err = file->close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
file = fs.open("test_read_write.dat", O_RDONLY);
|
||||
TEST_ASSERT(file);
|
||||
size = file->read(buffer, TEST_SIZE);
|
||||
TEST_ASSERT_EQUAL(TEST_SIZE, size);
|
||||
err = file->close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Check that the data was unmodified
|
||||
srand(1);
|
||||
for (int i = 0; i < TEST_SIZE; i++) {
|
||||
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
|
||||
}
|
||||
|
||||
err = fs.unmount();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
// Simple test for iterating dir entries
|
||||
void test_read_dir() {
|
||||
FATFileSystem fs("fat");
|
||||
|
||||
int err = fs.mount(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.mkdir("test_read_dir/test_dir", S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
FileHandle *file = fs.open("test_read_dir/test_file", O_WRONLY | O_CREAT);
|
||||
TEST_ASSERT(file);
|
||||
err = file->close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Iterate over dir checking for known files
|
||||
DirHandle *dir = fs.opendir("test_read_dir");
|
||||
TEST_ASSERT(dir);
|
||||
|
||||
struct dirent *de;
|
||||
bool test_dir_found = false;
|
||||
bool test_file_found = true;
|
||||
|
||||
while ((de = readdir(dir))) {
|
||||
printf("d_name: %.32s, d_type: %x\n", de->d_name, de->d_type);
|
||||
|
||||
if (strcmp(de->d_name, "test_dir") == 0) {
|
||||
test_dir_found = true;
|
||||
TEST_ASSERT_EQUAL(DT_DIR, de->d_type);
|
||||
} else if (strcmp(de->d_name, "test_file") == 0) {
|
||||
test_file_found = true;
|
||||
TEST_ASSERT_EQUAL(DT_REG, de->d_type);
|
||||
} else {
|
||||
char *buf = new char[NAME_MAX];
|
||||
snprintf(buf, NAME_MAX, "Unexpected file \"%s\"", de->d_name);
|
||||
TEST_ASSERT_MESSAGE(false, buf);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_ASSERT_MESSAGE(test_dir_found, "Could not find \"test_dir\"");
|
||||
TEST_ASSERT_MESSAGE(test_file_found, "Could not find \"test_file\"");
|
||||
|
||||
err = dir->closedir();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.unmount();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
|
||||
// Test setup
|
||||
utest::v1::status_t test_setup(const size_t number_of_cases) {
|
||||
GREENTEA_SETUP(10, "default_auto");
|
||||
return verbose_test_setup_handler(number_of_cases);
|
||||
}
|
||||
|
||||
Case cases[] = {
|
||||
Case("Testing formating", test_format),
|
||||
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
|
||||
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
|
||||
Case("Testing dir iteration", test_read_dir),
|
||||
};
|
||||
|
||||
Specification specification(test_setup, cases);
|
||||
|
||||
int main() {
|
||||
return !Harness::run(specification);
|
||||
}
|
Loading…
Reference in New Issue