Testing
This commit is contained in:
parent
d362d8c3b3
commit
c75cb9fb48
7 changed files with 145 additions and 155 deletions
|
|
@ -14,6 +14,7 @@
|
||||||
* 2) updating list entry to that block.
|
* 2) updating list entry to that block.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "HeapAllocatorGlobal.hpp"
|
||||||
#include "Environment.hpp"
|
#include "Environment.hpp"
|
||||||
#include "PrivateConfig.hpp"
|
#include "PrivateConfig.hpp"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
#include "Buffer.hpp"
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,34 +2,27 @@
|
||||||
|
|
||||||
#include "PoolAllocator.hpp"
|
#include "PoolAllocator.hpp"
|
||||||
#include "StringLogic.hpp"
|
#include "StringLogic.hpp"
|
||||||
#include "Buffer.hpp"
|
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
// Static data
|
// Static data
|
||||||
extern ModuleManifest gModuleStrings;
|
extern ModuleManifest gModuleStrings;
|
||||||
|
|
||||||
template<typename tChar>
|
template<typename tChar> class StringTemplate;
|
||||||
class StringTemplate;
|
|
||||||
using String = StringTemplate<int1>;
|
|
||||||
|
|
||||||
|
// Hidden slave class
|
||||||
template<typename tChar = int1>
|
template<typename tChar = int1>
|
||||||
class StringTemplate {
|
class StringData {
|
||||||
public:
|
friend StringTemplate<tChar>;
|
||||||
typedef StringLogic<tChar> Logic;
|
typedef StringLogic<tChar> Logic;
|
||||||
typedef Logic::Index Index;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Hidden slave class
|
|
||||||
class StringData {
|
|
||||||
public:
|
|
||||||
uint2 mIsConst; // source is non-modifiable
|
uint2 mIsConst; // source is non-modifiable
|
||||||
uint2 mEditedIdx; // editor id
|
uint2 mEditedIdx; // editor id
|
||||||
uint4 mReferenceCount; // number of users of this
|
uint4 mReferenceCount; // number of users of this
|
||||||
tChar* mBuff; // actual string data
|
tChar* mBuff; // actual string data
|
||||||
|
|
||||||
StringData() {
|
StringData() {
|
||||||
MODULE_SANITY_CHECK(gModuleStrings)
|
|
||||||
mBuff = (tChar*) "*";
|
mBuff = (tChar*) "*";
|
||||||
mReferenceCount = 0;
|
mReferenceCount = 0;
|
||||||
mIsConst = true;
|
mIsConst = true;
|
||||||
|
|
@ -72,92 +65,97 @@ namespace tp {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
template<typename tChar = int1>
|
||||||
class StringData* mStringData;
|
class StringTemplate {
|
||||||
|
|
||||||
private:
|
|
||||||
enum { STRINGS_POOL_SIZE = 1024, };
|
|
||||||
static PoolAlloc<StringData, STRINGS_POOL_SIZE>* sStringPool;
|
|
||||||
static StringData sNullString;
|
|
||||||
|
|
||||||
// --------------------------------- Main Interface ---------------------------------//
|
|
||||||
public:
|
public:
|
||||||
|
typedef StringLogic<tChar> Logic;
|
||||||
|
typedef StringData<tChar> Data;
|
||||||
|
typedef typename Logic::Index Index;
|
||||||
|
|
||||||
|
enum { STRINGS_POOL_SIZE = 1024, };
|
||||||
|
static PoolAlloc<StringData<tChar>, STRINGS_POOL_SIZE>* sStringPool;
|
||||||
|
static Data sNullString;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Data* mData;
|
||||||
|
|
||||||
|
public: // Main Interface
|
||||||
// Creates null string reference
|
// Creates null string reference
|
||||||
StringTemplate() {
|
StringTemplate() {
|
||||||
mStringData = &sNullString;
|
mData = &sNullString;
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// References existing string data
|
// References existing string data
|
||||||
StringTemplate(const StringTemplate& in) {
|
StringTemplate(const StringTemplate& in) {
|
||||||
mStringData = in.mStringData;
|
mData = in.mData;
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates new string data from raw data pointer.
|
// Creates new string data from raw data pointer.
|
||||||
// Does not own string buffer - string buffer will not be freed. Initializes as const memory.
|
// Does not own string buffer - string buffer will not be freed. Initializes as const memory.
|
||||||
explicit StringTemplate(const tChar* raw) {
|
explicit StringTemplate(const tChar* raw) {
|
||||||
mStringData = new (sStringPool->allocate(0)) StringData(raw, true);
|
mData = new (sStringPool->allocate(0)) StringData(raw, true);
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies raw data and claims ownership over it
|
// Copies raw data and claims ownership over it
|
||||||
explicit StringTemplate(tChar* raw) {
|
explicit StringTemplate(tChar* raw) {
|
||||||
mStringData = new (sStringPool->allocate(0)) StringData(raw);
|
mData = new (sStringPool->allocate(0)) StringData(raw);
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
}
|
}
|
||||||
|
|
||||||
~StringTemplate() {
|
~StringTemplate() {
|
||||||
decReference(mStringData);
|
decReference(mData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void incReference(StringData* dp) {
|
static void incReference(Data* dp) {
|
||||||
dp->mReferenceCount++;
|
dp->mReferenceCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void decReference(StringData* dp) {
|
void decReference(Data* dp) {
|
||||||
dp->mReferenceCount--;
|
dp->mReferenceCount--;
|
||||||
if (!dp->mReferenceCount) {
|
if (!dp->mReferenceCount) {
|
||||||
sStringPool->deallocate(mStringData);
|
sStringPool->deallocate(mData);
|
||||||
mStringData->~StringData();
|
mData->~StringData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public: // Access
|
public: // Access
|
||||||
// used to read data
|
// used to read data
|
||||||
[[nodiscard]] const tChar* read() const {
|
[[nodiscard]] const tChar* read() const {
|
||||||
return mStringData->mBuff;
|
return mData->mBuff;
|
||||||
}
|
}
|
||||||
|
|
||||||
// used to write data
|
// used to write data
|
||||||
// output will be null if in TextEditing mode
|
// output will be null if in TextEditing mode
|
||||||
[[nodiscard]] tChar* write() {
|
[[nodiscard]] tChar* write() {
|
||||||
makeModifiable();
|
makeModifiable();
|
||||||
return mStringData->getBuffer();
|
return mData->getBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
// output will be null if in TextEditing mode
|
// output will be null if in TextEditing mode
|
||||||
[[nodiscard]] tChar* resize() {
|
[[nodiscard]] tChar* resize() {
|
||||||
if (!mStringData->getEditor()) mStringData->resize();
|
if (!mData->getEditor()) mData->resize();
|
||||||
return mStringData->getBuffer();
|
return mData->getBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void makeModifiable() {
|
void makeModifiable() {
|
||||||
// We have no rights to modify if someone references that memory too
|
// We have no rights to modify if someone references that memory too
|
||||||
// So we need to create new copy of StringData
|
// So we need to create new copy of StringData
|
||||||
if (mStringData->mReferenceCount > 1) {
|
if (mData->mReferenceCount > 1) {
|
||||||
mStringData = new (sStringPool->allocate(0)) StringData(mStringData->mBuff);
|
mData = new (sStringPool->allocate(0)) StringData(mData->mBuff);
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also if initial raw data was marked as const - create copy of raw data
|
// Also if initial raw data was marked as const - create copy of raw data
|
||||||
// Note - raw data will be lost at this point if no other record of such is stored
|
// Note - raw data will be lost at this point if no other record of such is stored
|
||||||
if (mStringData->mIsConst) {
|
if (mData->mIsConst) {
|
||||||
mStringData = new (sStringPool->allocate(0)) StringData(mStringData->mBuff);
|
mData = new (sStringPool->allocate(0)) StringData(mData->mBuff);
|
||||||
incReference(mStringData);
|
incReference(mData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -183,4 +181,12 @@ namespace tp {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename tChar>
|
||||||
|
PoolAlloc<StringData<tChar>, StringTemplate<tChar>::STRINGS_POOL_SIZE>* StringTemplate<tChar>::sStringPool;
|
||||||
|
|
||||||
|
template<typename tChar>
|
||||||
|
StringData<tChar> StringTemplate<tChar>::sNullString;
|
||||||
|
|
||||||
|
using String = StringTemplate<int1>;
|
||||||
}
|
}
|
||||||
21
Strings/tests/Tests.cpp
Normal file
21
Strings/tests/Tests.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
|
||||||
|
#include "Testing.hpp"
|
||||||
|
#include "Strings.hpp"
|
||||||
|
|
||||||
|
void testStrings();
|
||||||
|
void testLogging();
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
tp::ModuleManifest* deps[] = { &tp::gModuleStrings, &tp::gModuleUtils, nullptr };
|
||||||
|
tp::ModuleManifest testModule("StringsTest", nullptr, nullptr, deps);
|
||||||
|
|
||||||
|
if (!testModule.initialize()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
testStrings();
|
||||||
|
testLogging();
|
||||||
|
|
||||||
|
testModule.deinitialize();
|
||||||
|
}
|
||||||
11
Strings/tests/TestsLogging.cpp
Normal file
11
Strings/tests/TestsLogging.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
#include "Testing.hpp"
|
||||||
|
#include "Logging.hpp"
|
||||||
|
|
||||||
|
TEST_DEF_STATIC(Simple) {
|
||||||
|
TEST(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_DEF(Logging) {
|
||||||
|
testSimple();
|
||||||
|
}
|
||||||
16
Strings/tests/TestsStrings.cpp
Normal file
16
Strings/tests/TestsStrings.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
#include "Testing.hpp"
|
||||||
|
#include "Strings.hpp"
|
||||||
|
|
||||||
|
TEST_DEF_STATIC(StringLogic) {
|
||||||
|
TEST(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_DEF_STATIC(Simple) {
|
||||||
|
TEST(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_DEF(Strings) {
|
||||||
|
testStringLogic();
|
||||||
|
testSimple();
|
||||||
|
}
|
||||||
|
|
@ -1,66 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
|
|
||||||
#include "allocators.h"
|
|
||||||
|
|
||||||
#include "strings.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "TextEditor.hpp"
|
|
||||||
|
|
||||||
#include "Logging.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
||||||
void test_conversions() {
|
|
||||||
|
|
||||||
for (auto i : tp::Range(1000)) {
|
|
||||||
|
|
||||||
auto sign = bool(std::rand() % 2) ? 1 : -1;
|
|
||||||
|
|
||||||
auto floating = std::rand() / (tp::alnf)std::rand() * sign;
|
|
||||||
auto tmp = std::to_string(floating);
|
|
||||||
auto left = tp::string(tmp.c_str());
|
|
||||||
auto right = tp::string(floating);
|
|
||||||
DBG_BREAK(left != right);
|
|
||||||
auto back = (tp::alnf)right;
|
|
||||||
//DBG_BREAK(back != floating);
|
|
||||||
|
|
||||||
auto integer = std::rand() * sign;
|
|
||||||
tmp = std::to_string(integer);
|
|
||||||
left = tp::string(tmp.c_str());
|
|
||||||
right = tp::string(integer);
|
|
||||||
DBG_BREAK(left != right);
|
|
||||||
back = (tp::alni)right;
|
|
||||||
DBG_BREAK(back != integer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_editor() {
|
|
||||||
tp::string str = " initial ";
|
|
||||||
str.createEdited();
|
|
||||||
auto edited = str.getEdited();
|
|
||||||
edited->setCursor({0, 4});
|
|
||||||
edited->insert({"aaa", 3});
|
|
||||||
str.saveEdited();
|
|
||||||
str.clearEdited();
|
|
||||||
|
|
||||||
GLog->write(str, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
tp::alloc_init();
|
|
||||||
string::Initialize();
|
|
||||||
Logger::init();
|
|
||||||
//test_conversions();
|
|
||||||
test_editor();
|
|
||||||
Logger::deinit();
|
|
||||||
string::UnInitialize();
|
|
||||||
tp::alloc_uninit();
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue