init
Embed:
(wiki syntax)
Show/hide line numbers
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 { 00044 if(POS_IS_VALID(_pos)) { 00045 ((uint8_t*)buffer)[read] = _data[_pos++]; 00046 } else { 00047 break; 00048 } 00049 } // for 00050 return read; 00051 } 00052 00053 virtual ssize_t write(const void *buffer, size_t size) 00054 { 00055 ssize_t written; 00056 _fnCalled = fnWrite; 00057 00058 for(written = 0; (size_t)written < size; written++) 00059 { 00060 if(NEW_POS_IS_VALID(_pos)) { 00061 _data[_pos++] = ((uint8_t*)buffer)[written]; 00062 } else { 00063 if(0 == written) { 00064 return -ENOSPC; 00065 } 00066 break; 00067 } 00068 if(_end < _pos) { 00069 _end++; 00070 } 00071 } // for 00072 return written; 00073 } 00074 00075 virtual off_t seek(off_t offset, int whence) 00076 { 00077 _fnCalled = fnSeek; 00078 int32_t new_pos = INVALID_POS; 00079 00080 switch(whence) 00081 { 00082 case SEEK_SET: 00083 new_pos = offset; 00084 break; 00085 00086 case SEEK_CUR: 00087 new_pos = _pos + offset; 00088 break; 00089 00090 case SEEK_END: 00091 new_pos = _end - offset; 00092 break; 00093 00094 default: 00095 // nothing todo 00096 break; 00097 } 00098 00099 if(SEEK_POS_IS_VALID(new_pos)) { 00100 _pos = new_pos; 00101 } else { 00102 return -EINVAL; 00103 } 00104 00105 return _pos; 00106 } 00107 00108 virtual int close() 00109 { 00110 _fnCalled = fnClose; 00111 return 0; 00112 } 00113 00114 00115 static void resetFunctionCallHistory() 00116 { 00117 _fnCalled = fnNone; 00118 } 00119 00120 static bool functionCalled(FunctionName name) 00121 { 00122 return (name == _fnCalled); 00123 } 00124 00125 static FunctionName getFunctionCalled() 00126 { 00127 return _fnCalled; 00128 } 00129 00130 private: 00131 00132 // stores last function call name 00133 static FunctionName _fnCalled; 00134 00135 // file storage 00136 uint8_t _data[FILE_SIZE]; 00137 00138 int32_t _pos, _end; 00139 }; 00140 00141 template<uint32_t FILE_SIZE> 00142 typename TestFile<FILE_SIZE>::FunctionName TestFile<FILE_SIZE>::_fnCalled; 00143 00144 00145 #endif // MBED_TESTFILEHANDLE_H
Generated on Tue Jul 12 2022 13:25:15 by
1.7.2