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