1 // Copyright 2017 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Implements a fake filesystem, useful for tests.
16 #ifndef CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_
17 #define CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_
21 #include <unordered_map>
23 #include "internal/filesystem.h"
25 namespace cpu_features {
29 explicit FakeFile(int file_descriptor, const char* content);
34 int Read(int fd, void* buf, size_t count);
36 int GetFileDescriptor() const { return file_descriptor_; }
39 const int file_descriptor_;
40 const std::string content_;
42 size_t head_index_ = 0;
45 class FakeFilesystem {
48 FakeFile* CreateFile(const std::string& filename, const char* content);
49 FakeFile* FindFileOrDie(const int file_descriptor) const;
50 FakeFile* FindFileOrNull(const std::string& filename) const;
53 size_t next_file_descriptor_ = 0;
54 std::unordered_map<std::string, std::unique_ptr<FakeFile>> files_;
57 FakeFilesystem& GetEmptyFilesystem();
59 } // namespace cpu_features
61 #endif // CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_