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.
TestFile.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_TESTFILEHANDLE_H 00017 #define MBED_TESTFILEHANDLE_H 00018 00019 #include "platform/FileHandle.h" 00020 00021 00022 #define POS_IS_VALID(pos) (pos >= 0 && pos < _end) 00023 #define NEW_POS_IS_VALID(pos) (pos >= 0 && pos < (int32_t)FILE_SIZE) 00024 #define SEEK_POS_IS_VALID(pos) (pos >= 0 && pos <= _end) 00025 #define INVALID_POS (-1) 00026 00027 template<uint32_t FILE_SIZE> 00028 class TestFile : public FileHandle { 00029 public: 00030 TestFile(): _pos(0), _end(0) {} 00031 ~TestFile() {} 00032 00033 enum FunctionName { 00034 fnNone, fnRead, fnWrite, fnSeek, fnClose, fnIsatty 00035 }; 00036 00037 virtual ssize_t read(void *buffer, size_t size) 00038 { 00039 ssize_t read; 00040 _fnCalled = fnRead; 00041 00042 for (read = 0; (size_t)read < size; read++) { 00043 if (POS_IS_VALID(_pos)) { 00044 ((uint8_t *)buffer)[read] = _data[_pos++]; 00045 } else { 00046 break; 00047 } 00048 } // for 00049 return read; 00050 } 00051 00052 virtual ssize_t write(const void *buffer, size_t size) 00053 { 00054 ssize_t written; 00055 _fnCalled = fnWrite; 00056 00057 for (written = 0; (size_t)written < size; written++) { 00058 if (NEW_POS_IS_VALID(_pos)) { 00059 _data[_pos++] = ((uint8_t *)buffer)[written]; 00060 } else { 00061 if (0 == written) { 00062 return -ENOSPC; 00063 } 00064 break; 00065 } 00066 if (_end < _pos) { 00067 _end++; 00068 } 00069 } // for 00070 return written; 00071 } 00072 00073 virtual off_t seek(off_t offset, int whence) 00074 { 00075 _fnCalled = fnSeek; 00076 int32_t new_pos = INVALID_POS; 00077 00078 switch (whence) { 00079 case SEEK_SET: 00080 new_pos = offset; 00081 break; 00082 00083 case SEEK_CUR: 00084 new_pos = _pos + offset; 00085 break; 00086 00087 case SEEK_END: 00088 new_pos = _end - offset; 00089 break; 00090 00091 default: 00092 // nothing todo 00093 break; 00094 } 00095 00096 if (SEEK_POS_IS_VALID(new_pos)) { 00097 _pos = new_pos; 00098 } else { 00099 return -EINVAL; 00100 } 00101 00102 return _pos; 00103 } 00104 00105 virtual int close() 00106 { 00107 _fnCalled = fnClose; 00108 return 0; 00109 } 00110 00111 00112 static void resetFunctionCallHistory() 00113 { 00114 _fnCalled = fnNone; 00115 } 00116 00117 static bool functionCalled(FunctionName name) 00118 { 00119 return (name == _fnCalled); 00120 } 00121 00122 static FunctionName getFunctionCalled() 00123 { 00124 return _fnCalled; 00125 } 00126 00127 private: 00128 00129 // stores last function call name 00130 static FunctionName _fnCalled; 00131 00132 // file storage 00133 uint8_t _data[FILE_SIZE]; 00134 00135 int32_t _pos, _end; 00136 }; 00137 00138 template<uint32_t FILE_SIZE> 00139 typename TestFile<FILE_SIZE>::FunctionName TestFile<FILE_SIZE>::_fnCalled; 00140 00141 00142 #endif // MBED_TESTFILEHANDLE_H
Generated on Tue Aug 9 2022 00:37:22 by
1.7.2