Example
Dependencies: FXAS21002 FXOS8700Q
simple-mbed-cloud-client/TESTS/COMMON/file_test.cpp@0:11cc2b7889af, 2019-11-19 (annotated)
- Committer:
- maygup01
- Date:
- Tue Nov 19 09:49:38 2019 +0000
- Revision:
- 0:11cc2b7889af
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maygup01 | 0:11cc2b7889af | 1 | /* |
maygup01 | 0:11cc2b7889af | 2 | * mbed Microcontroller Library |
maygup01 | 0:11cc2b7889af | 3 | * Copyright (c) 2006-2018 ARM Limited |
maygup01 | 0:11cc2b7889af | 4 | * |
maygup01 | 0:11cc2b7889af | 5 | * SPDX-License-Identifier: Apache-2.0 |
maygup01 | 0:11cc2b7889af | 6 | * |
maygup01 | 0:11cc2b7889af | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
maygup01 | 0:11cc2b7889af | 8 | * you may not use this file except in compliance with the License. |
maygup01 | 0:11cc2b7889af | 9 | * You may obtain a copy of the License at |
maygup01 | 0:11cc2b7889af | 10 | * |
maygup01 | 0:11cc2b7889af | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
maygup01 | 0:11cc2b7889af | 12 | * |
maygup01 | 0:11cc2b7889af | 13 | * Unless required by applicable law or agreed to in writing, software |
maygup01 | 0:11cc2b7889af | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
maygup01 | 0:11cc2b7889af | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maygup01 | 0:11cc2b7889af | 16 | * See the License for the specific language governing permissions and |
maygup01 | 0:11cc2b7889af | 17 | * limitations under the License. |
maygup01 | 0:11cc2b7889af | 18 | */ |
maygup01 | 0:11cc2b7889af | 19 | |
maygup01 | 0:11cc2b7889af | 20 | /* |
maygup01 | 0:11cc2b7889af | 21 | * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test |
maygup01 | 0:11cc2b7889af | 22 | */ |
maygup01 | 0:11cc2b7889af | 23 | |
maygup01 | 0:11cc2b7889af | 24 | #include "mbed.h" |
maygup01 | 0:11cc2b7889af | 25 | #include "unity/unity.h" |
maygup01 | 0:11cc2b7889af | 26 | |
maygup01 | 0:11cc2b7889af | 27 | void file_test_write(const char* file, size_t offset, const unsigned char* data, size_t data_length, size_t block_size) { |
maygup01 | 0:11cc2b7889af | 28 | char filename[255] = { 0 }; |
maygup01 | 0:11cc2b7889af | 29 | snprintf(filename, 255, "/sd/%s", file); |
maygup01 | 0:11cc2b7889af | 30 | |
maygup01 | 0:11cc2b7889af | 31 | FILE* output = fopen(filename, "w+"); |
maygup01 | 0:11cc2b7889af | 32 | TEST_ASSERT_NOT_NULL_MESSAGE(output, "could not open file"); |
maygup01 | 0:11cc2b7889af | 33 | |
maygup01 | 0:11cc2b7889af | 34 | int result = fseek(output, offset, SEEK_SET); |
maygup01 | 0:11cc2b7889af | 35 | TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not seek to location"); |
maygup01 | 0:11cc2b7889af | 36 | |
maygup01 | 0:11cc2b7889af | 37 | Timer timer; |
maygup01 | 0:11cc2b7889af | 38 | timer.start(); |
maygup01 | 0:11cc2b7889af | 39 | |
maygup01 | 0:11cc2b7889af | 40 | size_t index = 0; |
maygup01 | 0:11cc2b7889af | 41 | while (index < data_length) { |
maygup01 | 0:11cc2b7889af | 42 | size_t write_length = data_length - index; |
maygup01 | 0:11cc2b7889af | 43 | |
maygup01 | 0:11cc2b7889af | 44 | if (write_length > block_size) { |
maygup01 | 0:11cc2b7889af | 45 | write_length = block_size; |
maygup01 | 0:11cc2b7889af | 46 | } |
maygup01 | 0:11cc2b7889af | 47 | |
maygup01 | 0:11cc2b7889af | 48 | size_t written = fwrite(&data[index], sizeof(unsigned char), write_length, output); |
maygup01 | 0:11cc2b7889af | 49 | TEST_ASSERT_EQUAL_UINT_MESSAGE(write_length, written, "failed to write"); |
maygup01 | 0:11cc2b7889af | 50 | |
maygup01 | 0:11cc2b7889af | 51 | index += write_length; |
maygup01 | 0:11cc2b7889af | 52 | } |
maygup01 | 0:11cc2b7889af | 53 | TEST_ASSERT_EQUAL_UINT_MESSAGE(index, data_length, "wrong length"); |
maygup01 | 0:11cc2b7889af | 54 | |
maygup01 | 0:11cc2b7889af | 55 | result = fclose(output); |
maygup01 | 0:11cc2b7889af | 56 | TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not close file"); |
maygup01 | 0:11cc2b7889af | 57 | |
maygup01 | 0:11cc2b7889af | 58 | timer.stop(); |
maygup01 | 0:11cc2b7889af | 59 | printf("[FS] Wrote: \"%s\" %.2fKB (%.2fKB/s, %.2f secs)\r\n", file, |
maygup01 | 0:11cc2b7889af | 60 | float(data_length) / 1024, float(data_length) / timer.read() / 1024, timer.read()); |
maygup01 | 0:11cc2b7889af | 61 | } |
maygup01 | 0:11cc2b7889af | 62 | |
maygup01 | 0:11cc2b7889af | 63 | void file_test_read(const char* file, size_t offset, const unsigned char* data, size_t data_length, size_t block_size) { |
maygup01 | 0:11cc2b7889af | 64 | char filename[255] = { 0 }; |
maygup01 | 0:11cc2b7889af | 65 | snprintf(filename, 255, "/sd/%s", file); |
maygup01 | 0:11cc2b7889af | 66 | |
maygup01 | 0:11cc2b7889af | 67 | FILE* output = fopen(filename, "r"); |
maygup01 | 0:11cc2b7889af | 68 | TEST_ASSERT_NOT_NULL_MESSAGE(output, "could not open file"); |
maygup01 | 0:11cc2b7889af | 69 | |
maygup01 | 0:11cc2b7889af | 70 | int result = fseek(output, offset, SEEK_SET); |
maygup01 | 0:11cc2b7889af | 71 | TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not seek to location"); |
maygup01 | 0:11cc2b7889af | 72 | |
maygup01 | 0:11cc2b7889af | 73 | char* buffer = (char*) malloc(block_size); |
maygup01 | 0:11cc2b7889af | 74 | TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "could not allocate buffer"); |
maygup01 | 0:11cc2b7889af | 75 | |
maygup01 | 0:11cc2b7889af | 76 | Timer timer; |
maygup01 | 0:11cc2b7889af | 77 | timer.start(); |
maygup01 | 0:11cc2b7889af | 78 | |
maygup01 | 0:11cc2b7889af | 79 | size_t index = 0; |
maygup01 | 0:11cc2b7889af | 80 | while (index < data_length) { |
maygup01 | 0:11cc2b7889af | 81 | uint32_t read_length = data_length - index; |
maygup01 | 0:11cc2b7889af | 82 | |
maygup01 | 0:11cc2b7889af | 83 | if (read_length > block_size) { |
maygup01 | 0:11cc2b7889af | 84 | read_length = block_size; |
maygup01 | 0:11cc2b7889af | 85 | } |
maygup01 | 0:11cc2b7889af | 86 | |
maygup01 | 0:11cc2b7889af | 87 | size_t read = fread(buffer, sizeof(char), read_length, output); |
maygup01 | 0:11cc2b7889af | 88 | TEST_ASSERT_EQUAL_MESSAGE(read, read_length, "failed to read"); |
maygup01 | 0:11cc2b7889af | 89 | TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(buffer, &data[index], read_length, "character mismatch"); |
maygup01 | 0:11cc2b7889af | 90 | |
maygup01 | 0:11cc2b7889af | 91 | index += read_length; |
maygup01 | 0:11cc2b7889af | 92 | } |
maygup01 | 0:11cc2b7889af | 93 | TEST_ASSERT_EQUAL_UINT_MESSAGE(index, data_length, "wrong length"); |
maygup01 | 0:11cc2b7889af | 94 | |
maygup01 | 0:11cc2b7889af | 95 | result = fclose(output); |
maygup01 | 0:11cc2b7889af | 96 | TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "could not close file"); |
maygup01 | 0:11cc2b7889af | 97 | |
maygup01 | 0:11cc2b7889af | 98 | free(buffer); |
maygup01 | 0:11cc2b7889af | 99 | |
maygup01 | 0:11cc2b7889af | 100 | printf("[FS] Read : \"%s\" %.2fKB (%.2fKB/s, %.2f secs)\r\n", file, |
maygup01 | 0:11cc2b7889af | 101 | float(data_length) / 1024, float(data_length) / timer.read() / 1024, timer.read()); |
maygup01 | 0:11cc2b7889af | 102 | } |
maygup01 | 0:11cc2b7889af | 103 |