Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers file_test.cpp Source File

file_test.cpp

00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2018 ARM Limited
00004  *
00005  * SPDX-License-Identifier: Apache-2.0
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 /*
00021  * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
00022 */
00023 
00024 #include "mbed.h"
00025 #include "unity/unity.h"
00026 
00027 void file_test_write(const char* file, size_t offset, const unsigned char* data, size_t data_length, size_t block_size) {
00028     char filename[255] = { 0 };
00029     snprintf(filename, 255, "/sd/%s", file);
00030 
00031     FILE* output = fopen(filename, "w+");
00032     TEST_ASSERT_NOT_NULL_MESSAGE(output, "could not open file");
00033 
00034     int result = fseek(output, offset, SEEK_SET);
00035     TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not seek to location");
00036 
00037     Timer timer;
00038     timer.start();
00039 
00040     size_t index = 0;
00041     while (index < data_length) {
00042         size_t write_length = data_length - index;
00043 
00044         if (write_length > block_size) {
00045             write_length = block_size;
00046         }
00047 
00048         size_t written = fwrite(&data[index], sizeof(unsigned char), write_length, output);
00049         TEST_ASSERT_EQUAL_UINT_MESSAGE(write_length, written, "failed to write");
00050 
00051         index += write_length;
00052     }
00053     TEST_ASSERT_EQUAL_UINT_MESSAGE(index, data_length, "wrong length");
00054 
00055     result = fclose(output);
00056     TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not close file");
00057 
00058     timer.stop();
00059     printf("[FS] Wrote: \"%s\" %.2fKB (%.2fKB/s, %.2f secs)\r\n", file,
00060         float(data_length) / 1024, float(data_length) / timer.read() / 1024, timer.read());
00061 }
00062 
00063 void file_test_read(const char* file, size_t offset, const unsigned char* data, size_t data_length, size_t block_size) {
00064     char filename[255] = { 0 };
00065     snprintf(filename, 255, "/sd/%s", file);
00066 
00067     FILE* output = fopen(filename, "r");
00068     TEST_ASSERT_NOT_NULL_MESSAGE(output, "could not open file");
00069 
00070     int result = fseek(output, offset, SEEK_SET);
00071     TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not seek to location");
00072 
00073     char* buffer = (char*) malloc(block_size);
00074     TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "could not allocate buffer");
00075 
00076     Timer timer;
00077     timer.start();
00078 
00079     size_t index = 0;
00080     while (index < data_length) {
00081         uint32_t read_length = data_length - index;
00082 
00083         if (read_length > block_size) {
00084             read_length = block_size;
00085         }
00086 
00087         size_t read = fread(buffer, sizeof(char), read_length, output);
00088         TEST_ASSERT_EQUAL_MESSAGE(read, read_length, "failed to read");
00089         TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(buffer, &data[index], read_length, "character mismatch");
00090 
00091         index += read_length;
00092     }
00093     TEST_ASSERT_EQUAL_UINT_MESSAGE(index, data_length, "wrong length");
00094 
00095     result = fclose(output);
00096     TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not close file");
00097 
00098     free(buffer);
00099 
00100     printf("[FS] Read : \"%s\" %.2fKB (%.2fKB/s, %.2f secs)\r\n", file,
00101         float(data_length) / 1024, float(data_length) / timer.read() / 1024, timer.read());
00102 }
00103