mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 02:39:57 +02:00
Add working unit tests
This commit is contained in:
parent
f609d54a14
commit
995cc7784c
106 changed files with 589 additions and 582 deletions
91
test/emul/WString.h
Normal file
91
test/emul/WString.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
#ifndef WSTRING_H
|
||||
#define WSTRING_H
|
||||
|
||||
/*#include <string>
|
||||
|
||||
class String : public std::string {
|
||||
public:
|
||||
String() : std::string() {}
|
||||
String(uint32_t val) : std::string(std::to_string(val)) {}
|
||||
String(double value, unsigned int decimalPlaces) : std::string(std::to_string(value)) {}
|
||||
String(const char* str) : std::string(str) {}
|
||||
};
|
||||
*/
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
class String {
|
||||
private:
|
||||
std::string data;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
String() = default;
|
||||
String(const char* s) : data(s) {}
|
||||
String(const std::string& s) : data(s) {}
|
||||
String(const String& other) = default;
|
||||
String(String&& other) = default;
|
||||
|
||||
// Numeric constructors (Arduino-style)
|
||||
String(uint64_t value) { data = std::to_string(value); }
|
||||
String(int value) { data = std::to_string(value); }
|
||||
String(unsigned int value) { data = std::to_string(value); }
|
||||
String(long value) { data = std::to_string(value); }
|
||||
String(unsigned long value) { data = std::to_string(value); }
|
||||
String(float value) { data = std::to_string(value); }
|
||||
String(double value) { data = std::to_string(value); }
|
||||
|
||||
String(float value, unsigned int decimalPlaces) {
|
||||
std::ostringstream oss;
|
||||
oss << std::fixed << std::setprecision(decimalPlaces) << value;
|
||||
data = oss.str();
|
||||
}
|
||||
|
||||
// Assignment operators
|
||||
String& operator=(const String& other) = default;
|
||||
String& operator=(String&& other) = default;
|
||||
|
||||
// Conversion operator to std::string
|
||||
operator std::string() const { return data; }
|
||||
|
||||
// Accessor
|
||||
const std::string& str() const { return data; }
|
||||
|
||||
// Concatenation
|
||||
String operator+(const String& rhs) const { return String(data + rhs.data); }
|
||||
|
||||
String operator+(const std::string& rhs) const { return String(data + rhs); }
|
||||
|
||||
String operator+(const char* rhs) const { return String(data + std::string(rhs)); }
|
||||
|
||||
// Append
|
||||
String& operator+=(const String& rhs) {
|
||||
data += rhs.data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& operator+=(const std::string& rhs) {
|
||||
data += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& operator+=(const char* rhs) {
|
||||
data += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Arduino-like methods (example)
|
||||
int length() const { return static_cast<int>(data.length()); }
|
||||
const char* c_str() const { return data.c_str(); }
|
||||
|
||||
// Friend functions to allow std::string + String
|
||||
friend String operator+(const std::string& lhs, const String& rhs) { return String(lhs + rhs.data); }
|
||||
|
||||
friend String operator+(const char* lhs, const String& rhs) { return String(std::string(lhs) + rhs.data); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const String& s) { return os << s.data; }
|
||||
};
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue