From 004b73ed7ef33cb407897f7eccbec5f3861f99d7 Mon Sep 17 00:00:00 2001
From: Toni Uhlig <matzeton@googlemail.com>
Date: Fri, 7 Jul 2023 12:37:21 +0200
Subject: Squashed 'qhexedit2/' content from commit 7f22526

git-subtree-dir: qhexedit2
git-subtree-split: 7f22526a86685aec1c5722154b8a7422d5037b77
---
 test/chunks.pro      |  18 ++++++++
 test/main.cpp        |  63 ++++++++++++++++++++++++++
 test/testchunks.cpp  | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/testchunks.h    |  32 ++++++++++++++
 test/testdata/1.bin  |   1 +
 test/testdata/16.bin | Bin 0 -> 16 bytes
 test/testdata/32.bin | Bin 0 -> 32 bytes
 test/testdata/33.bin | Bin 0 -> 33 bytes
 test/testdata/4.bin  | Bin 0 -> 4 bytes
 9 files changed, 237 insertions(+)
 create mode 100644 test/chunks.pro
 create mode 100644 test/main.cpp
 create mode 100644 test/testchunks.cpp
 create mode 100644 test/testchunks.h
 create mode 100644 test/testdata/1.bin
 create mode 100644 test/testdata/16.bin
 create mode 100644 test/testdata/32.bin
 create mode 100644 test/testdata/33.bin
 create mode 100644 test/testdata/4.bin

(limited to 'test')

diff --git a/test/chunks.pro b/test/chunks.pro
new file mode 100644
index 0000000..52843fe
--- /dev/null
+++ b/test/chunks.pro
@@ -0,0 +1,18 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2015-04-20T13:35:52
+#
+#-------------------------------------------------
+
+#greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+DEFINES += MODUL_TEST
+
+SOURCES += \
+    main.cpp \
+    ../src/chunks.cpp \
+    testchunks.cpp
+
+HEADERS += \
+    ../src/chunks.h \
+    testchunks.h
diff --git a/test/main.cpp b/test/main.cpp
new file mode 100644
index 0000000..bf03695
--- /dev/null
+++ b/test/main.cpp
@@ -0,0 +1,63 @@
+#include <QCoreApplication>
+#include <QtCore>
+#include <QDir>
+
+#include "testchunks.h"
+
+
+int main()
+{
+    QDir dir("logs");
+    dir.setNameFilters(QStringList() << "*.*");
+    dir.setFilter(QDir::Files);
+    foreach(QString dirFile, dir.entryList())
+        dir.remove(dirFile);
+
+    QString logFilename = "logs/Summary.log";
+    QFile outFile(logFilename);
+    outFile.open(QIODevice::WriteOnly | QIODevice::Text);
+    QTextStream sumLog(&outFile);
+
+    TestChunks tc(sumLog, "overwrite", 0x4000, true);
+    tc.overwrite(4379, 25);
+    tc.overwrite(0, '.');
+    tc.overwrite(0x50, '.');
+    tc.overwrite(0x100, '.');
+    tc.overwrite(0xfff, '.');
+    tc.overwrite(0x1000, '.');
+    tc.overwrite(0x1fff, '.');
+    tc.overwrite(0x3000, '.');
+    tc.overwrite(0x3fff, '.');
+    tc.overwrite(0x2000, '.');
+    tc.overwrite(0x2fff, '.');
+
+    TestChunks tc2(sumLog, "insert", 0x4000, true);
+    tc2.insert(0, 'E');        // 0
+    tc2.insert(0x50, 'x');     // 1
+    tc2.insert(0x100, 'x');    // 2
+    tc2.insert(0x1002, 'L');   // 3
+    tc2.insert(0x1004, 'E');   // 4
+    tc2.insert(0x2004, 'L');   // 5
+    tc2.insert(0x4005, 'L');   // 6
+    tc2.insert(0x3007, 'E');   // 7
+    tc2.insert(0x2008, 'E');   // 8
+    tc2.insert(0x3008, 'L');   // 9
+
+    TestChunks tc3(sumLog, "remove", 0x4000, true);
+    tc3.removeAt(0);           // 0
+    tc3.removeAt(0x50);        // f
+    tc3.removeAt(0x100);       // e
+    tc3.removeAt(0xffc);       // d
+    tc3.removeAt(0xffc);       // c
+    tc3.removeAt(0x1ffa);      // b
+    tc3.removeAt(0x3ff9);      // a
+    tc3.removeAt(0x2ffa);      // 9
+    tc3.removeAt(0x2ff7);      // 8
+    tc3.removeAt(0x1ff7);      // 7
+
+    TestChunks tc4(sumLog, "random", 0x40000, true);
+    tc4.random(1000);
+
+    outFile.close();
+    return 0;
+}
diff --git a/test/testchunks.cpp b/test/testchunks.cpp
new file mode 100644
index 0000000..24fcded
--- /dev/null
+++ b/test/testchunks.cpp
@@ -0,0 +1,123 @@
+#include "testchunks.h"
+#include <cstdlib>
+
+
+TestChunks::TestChunks(QTextStream &log, QString tName, int size, bool random, int saveFile) : _chunks(nullptr)
+{
+    char hex[] = "0123456789abcdef";
+    srand(0);
+    for (int idx=0; idx < size; idx++)
+    {
+        if (random)
+            _data += char(rand() % 0x100);
+        else
+            _data += hex[idx & 0x0f];
+        _highlighted += char(0);
+    }
+    _copy = _data;
+    _cData.setData(_copy);
+    _chunks.setIODevice(_cData);
+    _tCnt = 0;
+    _log = &log;
+    _tName = tName;
+    _saveFile = saveFile;
+    compare();
+}
+
+void TestChunks::random(int count)
+{
+    for (int idx=1; idx < count; idx++)
+    {
+        int action = rand() % 3;
+        int pos = rand() % _data.size();
+        char b = char(rand() % 0x100);
+        switch (action)
+        {
+        case 0:
+            removeAt(pos);
+            break;
+        case 1:
+            insert(pos, b);
+            break;
+        case 2:
+            overwrite(pos, b);
+            break;
+        }
+    }
+}
+
+void TestChunks::insert(qint64 pos, char b)
+{
+    _data.insert((int)pos, b);
+    _copy.insert((int)pos, char(0));
+    _highlighted.insert((int)pos, 1);
+    _chunks.insert(pos, b);
+    compare();
+}
+
+void TestChunks::overwrite(qint64 pos, char b)
+{
+    _data[(int)pos] = b;
+    _highlighted[(int)pos] = 1;
+    _chunks.overwrite(pos, b);
+    compare();
+}
+
+void TestChunks::removeAt(qint64 pos)
+{
+    _data.remove((int)pos, 1);
+    _highlighted.remove((int)pos, 1);
+    _chunks.removeAt(pos);
+    compare();
+}
+
+void TestChunks::compare()
+{
+    QByteArray rHighLighted;
+    QByteArray rData = _chunks.data(0, -1, &rHighLighted);
+    bool error = false;
+
+    if (rData != _data)
+        error = true;
+    if (rHighLighted != _highlighted)
+        error = true;
+
+    _tCnt += 1;
+
+    int chunkSize = _chunks.chunkSize();
+    QString tName = QString("logs/%1_%2_%3").arg(_tName).arg(_tCnt).arg(chunkSize);
+    if (error || (_tCnt >= _saveFile))
+    {
+        QFile file1(tName + "_data.txt");
+        file1.open(QIODevice::WriteOnly);
+        file1.write(_data);
+        file1.close();
+
+        QFile file2(tName + "_highlighted.txt");
+        file2.open(QIODevice::WriteOnly);
+        file2.write(_highlighted);
+        file2.close();
+
+        QFile file3(tName + "_rData.txt");
+        file3.open(QIODevice::WriteOnly);
+        file3.write(rData);
+        file3.close();
+
+        QFile file4(tName + "_rHighlighted.txt");
+        file4.open(QIODevice::WriteOnly);
+        file4.write(rHighLighted);
+        file4.close();
+    }
+
+    if (error)
+    {
+        qDebug() << "NOK " << tName;
+        *_log << "NOK " << tName << "\n";
+    }
+    else
+    {
+        qDebug() << "OK " << tName;
+        *_log << "OK " << tName << "\n";
+    }
+
+}
diff --git a/test/testchunks.h b/test/testchunks.h
new file mode 100644
index 0000000..1304125
--- /dev/null
+++ b/test/testchunks.h
@@ -0,0 +1,32 @@
+#ifndef TESTASBYTEARRAY_H
+#define TESTASBYTEARRAY_H
+
+#include <QBuffer>
+#include <QByteArray>
+#include <QString>
+#include <QTextStream>
+
+#include "../src/chunks.h"
+
+class TestChunks
+{
+public:
+    TestChunks(QTextStream &log, QString tName, int size, bool random=true, int saveFile=0x7fffffff);
+    void insert(qint64 pos, char b);
+    void overwrite(qint64 pos, char b);
+    void removeAt(qint64 pos);
+    void random(int count);
+    void compare();
+
+
+private:
+    QByteArray _data, _highlighted, _copy;
+    QBuffer _cData;
+    Chunks _chunks;
+    int _tCnt;
+    QString _tName;
+    int _saveFile;
+    QTextStream *_log;
+};
+
+#endif // TESTASBYTEARRAY_H
diff --git a/test/testdata/1.bin b/test/testdata/1.bin
new file mode 100644
index 0000000..f1b3829
--- /dev/null
+++ b/test/testdata/1.bin
@@ -0,0 +1 @@
+�
\ No newline at end of file
diff --git a/test/testdata/16.bin b/test/testdata/16.bin
new file mode 100644
index 0000000..b66efb8
Binary files /dev/null and b/test/testdata/16.bin differ
diff --git a/test/testdata/32.bin b/test/testdata/32.bin
new file mode 100644
index 0000000..fefa1cc
Binary files /dev/null and b/test/testdata/32.bin differ
diff --git a/test/testdata/33.bin b/test/testdata/33.bin
new file mode 100644
index 0000000..3ef70ac
Binary files /dev/null and b/test/testdata/33.bin differ
diff --git a/test/testdata/4.bin b/test/testdata/4.bin
new file mode 100644
index 0000000..10b156c
Binary files /dev/null and b/test/testdata/4.bin differ
-- 
cgit v1.2.3