Skip to content

Commit

Permalink
libcore|Tests: Fixed bugs, added missing operators
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 98a92ed commit 726a0c0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 38 deletions.
5 changes: 4 additions & 1 deletion doomsday/libs/core/include/de/data/block.h
Expand Up @@ -105,13 +105,16 @@ class DE_PUBLIC Block
operator const iBlock *() const { return &_block; }
inline explicit operator bool() const { return !isEmpty_Block(&_block); }

inline Byte &operator[](size_t pos) { return data()[pos]; }
Byte &operator[](size_t pos);
inline Byte operator[](size_t pos) const { return at(pos); }
inline Byte at(size_t pos) const { return at_Block(&_block, pos); }

Block mid(size_t pos, size_t len = iInvalidSize) const;
Block left(size_t len) const;

bool operator==(const Block &other) const;
inline bool operator!=(const Block &other) const { return !(*this == other); }

Block operator+(const Block &other) const;

Block &operator+=(const char *nullTerminatedCStr);
Expand Down
13 changes: 13 additions & 0 deletions doomsday/libs/core/include/de/data/set.h
Expand Up @@ -20,6 +20,7 @@
#define LIBCORE_SET_H

#include <unordered_set>
#include <ostream>

namespace de {

Expand Down Expand Up @@ -55,6 +56,18 @@ class Set : public std::unordered_set<Value>
Set & operator<<(const Value &value) { Base::insert(value); return *this; }
};

template <typename T>
inline std::ostream &operator<<(std::ostream &os, const Set<T> &set)
{
os << "Set{";
for (const auto &v : set)
{
os << " " << v;
}
os << " }";
return os;
}

} // namespace de

#endif // LIBCORE_SET_H
12 changes: 6 additions & 6 deletions doomsday/libs/core/src/core/monospacelogsinkformatter.cpp
Expand Up @@ -331,19 +331,19 @@ StringList MonospaceLogSinkFormatter::logEntryToTextLines(LogEntry const &entry)
}
}

// Crop this line's text out of the entire message.
wstring lineText = message.substr(pos, lineLen);
wstring lineText;

//qDebug() << "[formatting]" << wrapIndent << lineText;

// For lines other than the first one, print an indentation.
if (pos > 0)
if (pos > 0 && wrapIndent != String::npos)
{
wstring indented(wrapIndent, L' ');
indented += lineText;
lineText = indented;
lineText += wstring(wrapIndent, L' ');
}

// Crop this line's text out of the entire message.
lineText += message.substr(pos, lineLen);

// The wrap indent for this paragraph depends on the first line's content.
const bool lineStartsWithSpace = lineText.empty() || iswspace(lineText.front());
dsize firstNonSpace = wstring::npos;
Expand Down
13 changes: 12 additions & 1 deletion doomsday/libs/core/src/data/block.cpp
Expand Up @@ -158,7 +158,13 @@ void Block::remove(size_t pos, size_t len)
remove_Block(&_block, pos, len);
}

Block &Block::operator += (const char *nullTerminatedCStr)
Block::Byte &Block::operator[](size_t pos)
{
if (pos >= size()) resize(pos + 1);
return data()[pos];
}

Block &Block::operator+=(const char *nullTerminatedCStr)
{
appendCStr_Block(&_block, nullTerminatedCStr);
return *this;
Expand All @@ -179,6 +185,11 @@ Block Block::left(size_t len) const
return trunc;
}

bool Block::operator==(const Block &other) const
{
return size() == other.size() && cmp_Block(*this, other) == 0;
}

Block Block::operator+(const Block &other) const
{
Block cat(*this);
Expand Down
56 changes: 27 additions & 29 deletions doomsday/tests/test_bitfield/main.cpp
Expand Up @@ -18,78 +18,76 @@
*/

#include <de/BitField>
#include <QTextStream>
#include <QDebug>
#include <iostream>

using namespace de;

int main(int, char **)
{
using namespace std;
try
{
BitField::Elements elems;
elems.add(1, 1);

BitField pack(elems);
pack.set(1, duint(1));
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;

elems.clear();
elems.add(1, 1);
elems.add(2, 1);

pack.setElements(elems);
pack.set(2, true);
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;
pack.set(1, true);
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;

elems.add(3, 3);
pack.set(1, false);
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;
pack.set(3, 6u);
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;

elems.add(10, 8);
pack.set(10, 149u);
qDebug() << pack.asText().toLatin1().constData();
cout << pack.asText() << endl;

qDebug() << "Field 1:" << pack[1];
qDebug() << "Field 2:" << pack[2];
qDebug() << "Field 3:" << pack[3];
qDebug() << "Field 10:" << pack[10];
cout << "Field 1: " << pack[1] << " Field 2: " << pack[2] << " Field 3: " << pack[3]
<< " Field 10: " << pack[10] << endl;

DE_ASSERT(pack[10] == 149);

BitField pack2 = pack;
qDebug() << "Copied:" << pack2.asText().toLatin1().constData();
cout << "Copied: " << pack2.asText() << endl;

qDebug() << "Equal:" << (pack2 == pack? "true" : "false");
qDebug() << "Delta:" << pack.delta(pack2);
cout << "Equal: " << (pack2 == pack? "true" : "false") << endl;
cout << "Delta: " << pack.delta(pack2) << endl;

pack2.set(3, 3u);
qDebug() << "Modified:" << pack2.asText().toLatin1().constData();
qDebug() << "Equal:" << (pack2 == pack? "true" : "false");
qDebug() << "Delta:" << pack.delta(pack2);
cout << "Modified: " << pack2.asText() << endl;
cout << "Equal: " << (pack2 == pack? "true" : "false") << endl;
cout << "Delta: " << pack.delta(pack2) << endl;

pack2.set(3, 6u);
pack2.set(10, 128u);
qDebug() << "Modified:" << pack2.asText().toLatin1().constData();
qDebug() << "Field 10:" << pack2[10];
qDebug() << "Equal:" << (pack2 == pack? "true" : "false");
qDebug() << "Delta:" << pack.delta(pack2);
cout << "Modified: " << pack2.asText() << endl;
cout << "Field 10: " << pack2[10] << endl;
cout << "Equal: " << (pack2 == pack? "true" : "false") << endl;
cout << "Delta: " << pack.delta(pack2) << endl;

pack2.set(1, true);
qDebug() << "Modified:" << pack2.asText().toLatin1().constData();
qDebug() << "Equal:" << (pack2 == pack? "true" : "false");
qDebug() << "Delta:" << pack.delta(pack2);
qDebug() << "Delta (reverse):" << pack2.delta(pack);
cout << "Modified: " << pack2.asText() << endl;
cout << "Equal: " << (pack2 == pack? "true" : "false") << endl;
cout << "Delta: " << pack.delta(pack2) << endl;
cout << "Delta (reverse): " << pack2.delta(pack) << endl;
}
catch (Error const &err)
{
qWarning() << err.asText() << "\n";
warning("%s", err.asText().c_str());
}

qDebug() << "Exiting main()...\n";
return 0;
cout << "Exiting main()..." << endl;
return 0;
}
2 changes: 1 addition & 1 deletion doomsday/tests/test_commandline/main.cpp
Expand Up @@ -41,7 +41,7 @@ int main(int argc, char **argv)
String output;
if (cmd.executeAndWait(&output))
{
LOG_MSG("Output from %s:\n") << cmd.at(0) << output;
LOG_MSG("Output from %s:\n%s") << cmd.at(0) << output;
}
else
{
Expand Down

0 comments on commit 726a0c0

Please sign in to comment.