Made native tests compile again

This commit is contained in:
Martin Bauer
2023-08-28 15:06:29 +02:00
parent 900d3c8262
commit 2efa985a05
12 changed files with 172 additions and 28 deletions

View File

@@ -0,0 +1,13 @@
#pragma once
#ifdef PLATFORM_NATIVE
#include <stdlib.h>
static constexpr uint32_t MALLOC_CAP_SPIRAM = -1;
inline void *heap_caps_malloc(size_t size, uint32_t /*caps*/) {
return std::malloc(size);
}
#endif

View File

@@ -8,6 +8,12 @@
namespace portablefs
{
using File = ::File;
using string = String;
template<typename T>
String to_string(const T & value) {
return String(value);
}
class Dir
{
@@ -85,20 +91,11 @@ namespace portablefs
return SPIFFS.totalBytes();
}
} // namespace portablefs
#endif
#ifdef PLATFORM_ESP8266
#include <FS.h>
namespace portablefs
{
using Dir;
} // namespace portablefs
#endif
#ifdef PLATFORM_NATIVE
@@ -108,18 +105,27 @@ namespace portablefs
namespace fs = std::filesystem;
enum SeekMode {
SeekSet = 0,
SeekCur = 1,
SeekEnd = 2
};
namespace portablefs
{
const std::string basePath = "./base";
using string = std::string;
using std::to_string;
class Dir
{
public:
Dir() {}
Dir(const String &path)
: it_(fs::directory_iterator(path).begin()),
end_(fs::directory_iterator(path).end()),
: it_(fs::directory_iterator(path)),
end_(fs::directory_iterator()),
firstIncremented_(false)
{
}
@@ -136,22 +142,22 @@ namespace portablefs
bool isFile()
{
return file.is_regular_file();
return it_->is_regular_file();
}
bool isDirectory()
{
return it_.is_directory();
return it_->is_directory();
}
String fileName() const
{
return it_.path().filename().string();
return it_->path().filename().string();
}
size_t fileSize() const
{
return it_.file_size();
return it_->file_size();
}
private:
@@ -165,28 +171,73 @@ namespace portablefs
return Dir(path);
}
class File
{
public:
File(std::FILE * file) : file_(file) {}
bool seek(uint32_t pos, SeekMode mode)
{
if(mode == SeekSet)
return std::fseek(file_, pos, SEEK_SET) == 0;
else if (mode == SeekCur)
return std::fseek(file_, pos, SEEK_CUR) == 0;
else
return std::fseek(file_, pos, SEEK_END) == 0;
}
bool seek(uint32_t pos) { return std::fseek(file_, pos, SEEK_SET); }
size_t write(const uint8_t *buf, size_t size) {
return std::fwrite(buf, size, 1UL, file_);
}
size_t size() {
auto current_position = ftell(file_);
seek(0, SeekEnd);
auto end = ftell(file_);
seek(0, SeekSet);
auto begin = ftell(file_);
seek(current_position, SeekSet);
return end - begin;
}
private:
std::FILE * file_;
};
inline File open(const char *name, const char *mode)
{
if(mode == "r")
return fopen()
return SPIFFS.open(name, mode);
std::string modeStr;
if(modeStr.find("b") == std::string::npos)
modeStr += "b";
return File(std::fopen(name, modeStr.c_str()));
}
inline bool exists(const char *name)
{
return SPIFFS.exists(name);
return fs::exists(fs::path(name));
}
inline bool remove(const char *name)
{
return SPIFFS.remove(name);
return fs::remove(fs::path(name));
}
inline bool mkdir(const char *name)
{
return SPIFFS.mkdir(name);
return fs::create_directory(fs::path(name));
}
//inline size_t totalBytes() {
// return 1024 * 1024 * 4; // some dummy value
//}
//inline size_t usedBytes() {
// // TODO: makes more sense to report total file size of all files in folder
// return 1024; // some dummy value
//}
} // namespace portablefs
#endif

View File

@@ -2,6 +2,7 @@
#include <iostream>
#ifdef PLATFORM_NATIVE
class SerialMock {
public:
@@ -15,4 +16,7 @@ public:
}
};
static SerialMock Serial;
static SerialMock Serial;
#endif

View File

@@ -0,0 +1,36 @@
#pragma once
#ifdef PLATFORM_ESP32
#include "Preferences.h"
#endif
#ifdef PLATFORM_NATIVE
#include <map>
class Preferences
{
public:
std::string getString(const char * key, const std::string & defaultValue) const {
std::string strKey(key);
const auto it = strings_.find(strKey);
return (it == string_.end()) ? defaultValue : *it;
}
size_t putString(const char * key, const char * value) {
strings_[std::string(key)] = std::string(value);
return std::strlen(value);
}
bool remove(const char * key) {
return strings_.remove(key);
}
private:
std::map<std::string, std::string> strings_;
};
#endif // PLATFORM_NATIVE

View File

@@ -0,0 +1,16 @@
#pragma once
#ifdef PLATFORM_ESP32
#include <Arduino.h>
#endif
#ifdef PLATFORM_NATIVE
#include <chrono>
inline unsigned long millis() {
static auto timeOfFirstCall = std::chrono::steady_clock::now();
const auto timePoint = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(timePoint - timeOfFirstCall).count();
}
#endif