• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 Google LLC
2 //
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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
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_
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "internal/filesystem.h"
24 
25 namespace cpu_features {
26 
27 class FakeFile {
28  public:
29   explicit FakeFile(int file_descriptor, const char* content);
30   ~FakeFile();
31 
32   void Open();
33   void Close();
34   int Read(int fd, void* buf, size_t count);
35 
GetFileDescriptor()36   int GetFileDescriptor() const { return file_descriptor_; }
37 
38  private:
39   const int file_descriptor_;
40   const std::string content_;
41   bool opened_ = false;
42   size_t head_index_ = 0;
43 };
44 
45 class FakeFilesystem {
46  public:
47   void Reset();
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;
51 
52  private:
53   int next_file_descriptor_ = 0;
54   std::unordered_map<std::string, std::unique_ptr<FakeFile>> files_;
55 };
56 
57 FakeFilesystem& GetEmptyFilesystem();
58 
59 }  // namespace cpu_features
60 
61 #endif  // CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_
62